Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sniffing packets on localhost with Scapy

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?

like image 895
freedomflyer Avatar asked Oct 12 '25 11:10

freedomflyer


2 Answers

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!"
like image 92
sinkmanu Avatar answered Oct 15 '25 05:10

sinkmanu


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"

like image 44
the4960 Avatar answered Oct 15 '25 05:10

the4960