Skip to content Skip to sidebar Skip to footer

Scapy: How To Manipulate Host In Http Header?

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 th

Solution 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

Post a Comment for "Scapy: How To Manipulate Host In Http Header?"