I'm using scapy
with python
to sniff live traffic.
capture=sniff(iface="<My Interface>", filter="tcp")
But this sniffs each packet and adds it to the list capture
which can be processed later.
I want to process a packet and display few fields of the packet, as soon as it's sniffed. i.e. upon sniffing a packet, it'll trigger a function where I can analyse that packet. And this would continue for few packets.
I've the function ready which I'm using with the captured packet list. But I'm unable to use it for each live packet.
How to achieve that? Is it possible with scapy
or do I need to install any other package?
Sniffing packets using scapy:The sniff() function listens for an infinite period of time until the user interrupts. To restrict the number of packets to be captured sniff() allows a count parameter. By specifying a value for the count, the packet capturing will be restricted to the specified number.
prn: function to apply to each packet. If something is returned, it is displayed.
Packet sniffing is a technique whereby packet data flowing across the network is detected and observed. Network administrators use packet sniffing tools to monitor and validate network traffic, while hackers may use similar tools for nefarious purposes.
The send() function will send packets at layer 3. That is to say, it will handle routing and layer 2 for you. The sendp() function will work at layer 2. It's up to you to choose the right interface and the right link layer protocol.
The parameters to the sniff function should be like the below code.:
from scapy.all import *
def pkt_callback(pkt):
pkt.show() # debug statement
sniff(iface="<My Interface>", prn=pkt_callback, filter="tcp", store=0)
store=0
says not to store any packet received and prn
says send the pkt
to pkt_callback
.
Source.
As mentioned by Yoel, if only one action is required, lambda
can be used with prn
instead of a new function like in this case:
sniff(iface="<My Interface>", prn = lambda x: x.show(), filter="tcp", store=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With