How to check if a packet in Scapy has a TCP level

I want to know how I can verify that the packet I received from a function sr1()in Scapy contains a TCP layer in order to handle the TCP flags a bit.

+4
source share
1 answer

You have two options: the operator inis one.

>>> TCP in pkt
True
>>> if TCP in pkt:
...     # Handle TCP Flags

Batch objects in Scapy also have a function named haslayer().

>>> pkt = IP()/TCP()
>>> pkt.haslayer(TCP)
1
>>> pkt2 = IP()/UDP()
>>> pkt2.haslayer(TCP)
0
>>> Packet.haslayer.__doc__
'true if self has a layer that is an instance of cls. Superseded by "cls in self" syntax.'
+6
source

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


All Articles