Get package size in scapy / python

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

 >>> len(IP(dst="www.google.com")) 20 >>> len(IP(dst="www.google.com")/TCP(dport=80)) 40 
+4
source share
3 answers
 >>> len(IP(dst="www.google.com")) 20 

The minimum IP header has 20 bytes.

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

There is another 20 bytes in the minimum TCP header (20 + 20 == 40).

So it seems len returning the length of the packet.

+5
source

What I observed is that Len (the [Layer] package) will actually perform an action like LenField. It will return the number of bytes in the packet, starting from the specified level, up to the end of the packet. Therefore, while this method will work to determine the total size of the package, just be careful that it does not work to determine the length of a single layer.

+2
source

This is how I take the packet size / length when sniffing packets using 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 used scapy to sniff packages, so I'm not sure if this makes sense when using scapy for other things like creating packages.

+1
source

Source: https://habr.com/ru/post/1411986/


All Articles