Scapy: How to manipulate Host in http header?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 04:23:43

问题


I wrote this piece of code to get http header and set Host:

http_layer = packet.getlayer(http.HTTPRequest).fields
http_layer['Host'] = "newHostName"
return packet

After running the afforementioned code,the new host name has been set correctly, but the problem is that when I write the packet in pcap file, I still see the previous host in http fields, Is there an absolute way to manipulate http_layer['Host'] ? Any help would be appreciated. Regards.


回答1:


After all, found the answer. The key is that scapy firstly parses HTTP Request and shows the dict of its fields. So when we try to assign a new field like Host, it changes the Host which it has already parsed and does not change the original field value. So, this is the way to modify Host or any other respective fields:

str_headers = pkt['HTTP']['HTTP Request'].fields['Headers']
str_headers = str_headers.replace('Host: ' + pkt['HTTP']['HTTP Request'].fields['Host'], 'Host: ' + new_val)
pkt['HTTP']['HTTP Request'].fields['Headers'] = str_headers
return pkt


来源:https://stackoverflow.com/questions/39763486/scapy-how-to-manipulate-host-in-http-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!