title: how to add http headers to a packet sniffed using scapy
body: <p>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 the packet intact. At max any checksum recalculation should be done if needed.</p>
  
  <p>Have been through almost all questions on SO but didn't exactly get a solution.</p>
  
  <p>Following is what i have done.</p>
  
  <pre><code>def parse(pkt):
  
      if pkt.haslayer(TCP) and pkt.getlayer(TCP).dport == 80 and pkt.haslayer(Raw):
          pkt = pkt / "New Header:value\r\n\r\n"
  
          # OR i tried this
          #pkt = pkt.getlayer(Raw).load / Raw.load(load="New Header:value\r\n\r\n")
  
          #pkt.getlayer(Raw).load("New Header:value\r\n\r\n")
          pkt.show()
          #del pkt[IP].chksum
          send(pkt)
  #end parse function
  
  # start sniffing
  a=sniff(filter="tcp and ( port 80 )", prn=parse)
  </code></pre>
  
  <p>The problem is that above code inserts a new raw payload section instead of adding a plain header. There is already a double newline \r\n\r\n to indicate header termination according to HTTP standard.</p>
  
  <p>To overcome this i tried removing the last \r\n by doing as follows</p>
  
  <pre><code>   #pkt = pkt.getlayer(Raw).load[-2:] / Raw.load(load="New Header:value\r\n\r\n")
  </code></pre>
  
  <p>But this strips all of the previously existing headers and only the "New Header" remains.</p>
  
  <p>I have tried this on Linux mint.</p>
  
  <p><em>UPDATE:
  I am trying to create a new http payload which would contain previous headers and i will add some. Can some one help with how to removing an existing layer</em></p>