Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify if a packet in Scapy has a TCP layer

Tags:

scapy

I want to know how can I verify that a packet I received from the sr1() function in Scapy contains a TCP layer, in order to do some treatment on the TCP flags.

like image 328
farfalla Avatar asked Feb 28 '14 11:02

farfalla


People also ask

How do I create a TCP packet in Scapy?

Creating a packet In scapy, packets are constructed by defining packet headers for each protocol at different layers of TCP/IP and then stacking these layers in order. To create a DNS query, you need to build Ether(sometimes optional), IP,UDP headers and stack them using / operator.

How do you read Scapy packets?

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 sr1 in Scapy?

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. The packets must be layer 3 packets (IP, ARP, etc.).

What is Sprintf in Scapy?

sprintf is a function to format a packet's data in a human readable form.


1 Answers

You have two options, the in operator is one.

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

Packet objects in Scapy also have a function called 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.'
like image 85
RyPeck Avatar answered Oct 30 '22 05:10

RyPeck