how to add http headers to a packet sniffed using scapy

后端 未结 1 637
星月不相逢
星月不相逢 2020-12-19 09:19

I am trying to sniff an out going http packet using scapy, add a few new http headers in it and send it ahead. The intention here is to only insert new headers while keeping

相关标签:
1条回答
  • 2020-12-19 09:40

    If I understand correctly, the problem you're having is that you want to update an existing HTTP request with a new header. What you want is to update a string in place, which Python can't do directly (strings are immutable).

    So what you should do is take the HTTP header:

    old_hdr = pkt[Raw] or old_hdr = pkt[TCP].payload

    and manipulate it like a string:

    new_hdr = 'New Header: value'
    hdr = old_hdr.split('\r\n') # This is a crappy hack. Parsing HTTP headers
    hdr.insert(new_hdr, 2)      # is a [solved problem][1].
    send_hdr = '\r\n'.join(hdr)
    pkt[TCP].payload = send_hdr
    

    If you find checksums are not updating, delete them before sending the packet:

    del pkt[TCP].chksum
    

    and Scapy will put them back for you, with the right values.

    Edit: I just noticed that my link is fail. Here is how to parse HTTP headers.

    0 讨论(0)
提交回复
热议问题