title: Python modify packet on the fly. (dpkt and pcap)
body: <p>I have a piece of code that I am using to sniff packets. It works great and I get the raw data without any problem. </p>
  
  <p>Now I would like to modify the content of the packets that are going through my interface. For example modify the dest IP or the data in the packet.
  How can I do it?</p>
  
  <p>Here is the code I'm using to sniff the traffic that is directed to the port 22 (SSH):</p>
  
  <pre><code>import dpkt, pcap, time
  
  
  pc = pcap.pcap()
  pc.setfilter('tcp')
  pc.setfilter("port 22")
  for ts, pkt in pc:
      a= dpkt.ethernet.Ethernet(pkt)
      print time.time(), repr(a)
      print "----------------------------------------------------------------"
  </code></pre>
  
  <p>And this is the response I get:</p>
  
  <pre><code>&gt; 1406580959.72 Ethernet(src='\x00\x0c)\x1d\x86\xc7', dst='\xb8\xca:\xab\xe2\xed', data=IP(src='\n\x01\x91;', off=16384,
  &gt; dst='\n\x01\x91P', sum=63108, len=52, p=6, id=3506,
  &gt; data=TCP(seq=321409681, off_x2=128, ack=3806515902L, win=247,
  &gt; sum=38669, flags=20, dport=22, sport=36999)))
  &gt; ----------------------------------------------------------------
  &gt; 1406580960.7 Ethernet(src='\x00\x0c)\x1d\x86\xc7', dst='\xb8\xca:\xab\xe2\xed', data=IP(src='\n\x01\x91;', off=16384,
  &gt; dst='\n\x01\x91P', sum=4207, len=60, p=6, id=62399,
  &gt; data=TCP(seq=4079882711L, off_x2=160, win=29200, sum=5504, dport=22,
  &gt; sport=37000)))
  &gt; ----------------------------------------------------------------
  &gt; 1406580960.7 Ethernet(src='\xb8\xca:\xab\xe2\xed', dst='\x00\x0c)\x1d\x86\xc7', data=IP(src='\n\x01\x91P', off=16384,
  &gt; dst='\n\x01\x91;', len=60, p=6, ttl=128, id=16575,
  &gt; data=TCP(seq=3804512442L, off_x2=160, ack=4079882712L, win=8192,
  &gt; sum=14012, flags=18, dport=37000, sport=22)))
  &gt; ----------------------------------------------------------------
  &gt; 1406580960.7 Ethernet(src='\x00\x0c)\x1d\x86\xc7', dst='\xb8\xca:\xab\xe2\xed', data=IP(src='\n\x01\x91;', off=16384,
  &gt; dst='\n\x01\x91P', sum=4214, len=52, p=6, id=62400,
  &gt; data=TCP(seq=4079882712L, off_x2=128, ack=3804512443L, win=229,
  &gt; sum=24436, flags=16, dport=22, sport=37000)))
  &gt; ----------------------------------------------------------------
  &gt; 1406580960.7 Ethernet(src='\xb8\xca:\xab\xe2\xed', dst='\x00\x0c)\x1d\x86\xc7', data=IP(src='\n\x01\x91P', off=16384,
  &gt; dst='\n\x01\x91;', len=152, p=6, ttl=128, id=16576,
  &gt; data=TCP(seq=3804512443L, off_x2=128, ack=4079882712L, win=260,
  &gt; sum=14104, flags=24, dport=37000, sport=22, data='SSH-2.0-5.25
  &gt; FlowSsh: Bitvise SSH Server (WinSSHD) 6.07: free only for personal
  &gt; non-commercial use\r\n')))
  &gt; ----------------------------------------------------------------
  </code></pre>
  
  <p>Just net to modify the packets on the fly.</p>
  
  <p>Thank you for your help</p>