问题
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