Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get packet size in scapy / python

In Scapy (or even just Python, for that sake), how do I get the size in bytes of a given packet? I'm tempted to use the function len but I'm not sure what exactly it returns in the case of packets.

>>> len(IP(dst="www.google.com"))
20

>>> len(IP(dst="www.google.com")/TCP(dport=80))
40
like image 336
Ricky Robinson Avatar asked May 11 '12 13:05

Ricky Robinson


People also ask

How do I read pcap in Scapy?

Reading a pcap file with Scapy, is commonly done by using rdpcap() . This function reads the whole file and load it up in memory, depending on the size of the file you're trying to read can take quite some memory. Two versions of a Script to do the same thing.

How do you sniff packets with Scapy in Python?

Sniffing packets using scapy: To sniff the packets use the sniff() function. The sniff() function returns information about all the packets that has been sniffed. To see the summary of packet responses, use summary(). The sniff() function listens for an infinite period of time until the user interrupts.

What is SRP in Scapy?

sr(): send and receive packets at level 3. srp(): send and receive packets at level 2. sr1(): send and receive only the first packet at level 3. srp1(): sends and receives only the first packet to level 2. sniff(): packet sniffing.

What does sr1 return?

The sr() function is for sending packets and receiving answers. The function returns a couple of packet and answers, and the unanswered packets. The function sr1() is a variant that only return one packet that answered the packet (or the packet set) sent.


3 Answers

>>> len(IP(dst="www.google.com"))
20

There are 20 bytes in a minimal IP header.

>>> len(IP(dst="www.google.com")/TCP(dport=80))
40

There are another 20 bytes in a minimal TCP header (20+20==40).

So it seems that len is returning the packet length.

like image 144
MattH Avatar answered Sep 19 '22 15:09

MattH


What I have been observing is that Len(packet[Layer]) will actually perform the action of the LenField type. It will return the number of bytes in the packet, starting with the specified layer, all the way to the end of the packet. So while this method will work for determining the overall packet size, just beware that it will not work to determine the length of an individual layer.

like image 25
Stanley Switalski Avatar answered Sep 17 '22 15:09

Stanley Switalski


Here is how I grab the packet size/length when sniffing packets with scapy.

pkt.sprintf("%IP.len%")

Full example:

from scapy.all import *

# callback function - called for every packet
def traffic_monitor_callbak(pkt):
    if IP in pkt:
        print pkt.sprintf("%IP.len%")

# capture traffic for 10 seconds
sniff(iface="eth1", prn=traffic_monitor_callbak, store=0, timeout=10)

I've only used scapy for sniffing packets, so I'm not sure if the above makes sense when using scapy for other things like creating packets.

like image 36
Banjer Avatar answered Sep 19 '22 15:09

Banjer