2
votes

I'm learning how to use scapy to sniff packets. I have set up a simple echo server running on localhost on port 50420.

I then enter the following line in my terminal to sniff for packets on that port when I send traffic over it:

p = sniff(filter = 'port 50420')

However, no packets are captured, although the data flowed correctly. I have verified that sniffing works for other traffic not using localhost. How can I sniff this localhost traffic using Scapy, if at all possible?

2

2 Answers

2
votes

With that line you are sniffing the traffic over the port 50420, but you should do something more. You should add one function to jump when you sniff the packets

sniff(filter="port 50420",prn=myFunction)

And write myFunction:

def myFunction(pkt):
    print "Packet!"
0
votes

You may need to specify the interface. In Linux,

    sniff(filter="port 50420",iface="lo")

Of course to use the packets, you will need to either specify a callback function with "prn" option (as mentioned in an other response) or you can save the packets to a list for later use, e.g.

    packet_list = sniff(filter="port 50420",iface="lo")
    first_packet = packet_list[0]

To make extra sure, you can set the filter to something like "port 50420 and host 127.0.0.1"