Search code examples
pythonscapysniffing

how to sniff packets, modify and then send


I want to sniff IP packets and then change ToS field to 1 and add options to IP header then send modified packet instead of original packet. I found that can do this with scapy but it seems not working. can anyone help me with this?

from scapy.all import *
from scapy.layers.inet import IP

def chgSend(x):
    x[IP].tos = 1
    send(x)
while 1:
    sniff(filter="ip src host 10.0.0.2", prn=chgSend)

Solution

  • Now I'm able to spoof IP address and change ToS field with this code:

    from scapy.all import *
    from scapy.layers.inet import IP
    
    
    def change_send(pckt):
        actual_src = pckt[IP].src
        pckt[IP].src = "192.168.1.5"
        pckt[IP].tos = 1
        sendp(pckt)
        print("We changed source from " + actual_src + " to " + pckt[IP].src)
    
    
    while 1:
        sniff(filter="ip src host 192.168.1.2", prn=change_send)
    

    If you don't want to change source IP address, be sure that you wont stuck in an infinite loop.