13

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?

RatDon
  • 3,403
  • 8
  • 43
  • 85

2 Answers2

17

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)
Community
  • 1
  • 1
RatDon
  • 3,403
  • 8
  • 43
  • 85
  • Please read [this answer](http://stackoverflow.com/a/28296641/3903832) regarding your claim on setting `store=0`. – Yoel Mar 04 '15 at 11:36
  • @Yoel It'll not have any impact on the execution of the program. But don't you think, appending each packet to a list (`store=1`) is an extra overhead while we are processing each packet with `prn`. Do we really need to store those packets? – RatDon Mar 05 '15 at 05:43
  • That is not what you answer originally stated. I agree that setting `store=0` would slightly improve space and time efficiency and I even argued that in a comment to [my answer](http://stackoverflow.com/a/28296641/3903832) when you have actually claimed the opposite. Now that you have edited your answer, it is correct, though I fail to see what it adds to the discussion since my answer and its comments, that were posted long ago, have already stated all of the above. – Yoel Mar 05 '15 at 09:19
  • I guess the example shows the extended use of `prn` by calling a function and few of the parameters removed which, if absent, takes the default. And I never said your answer is wrong. The above one is more suitable for my question. – RatDon Mar 05 '15 at 09:51
8

This can be done with the prn argument of the sniff function. Scapy's tutorial has a simple example here. Scapy's official API documentation specifies:

sniff(prn=None, lfilter=None, count=0, store=1, offline=None, L2socket=None, timeout=None)

...
prn: function to apply to each packet. If something is returned, it is displayed. For instance you can use prn = lambda x: x.summary().
...


EDIT:
The accepted answer claims that the store argument must be set to 0 for the prn callback to be invoked. However, setting store=0 doesn't have any such effect. Scapy's own examples don't set store=0 and the official API documentation doesn't mention any such requirement. In fact, inspecting Scapy's source code reveals no connection whatsoever between the store and prn arguments. Here is an excerpt of the relevant code block:

...
if store:
    lst.append(p)
c += 1
if prn:
    r = prn(p)
    if r is not None:
        print r
...

Executing a few simple test cases supports this finding as well.

Community
  • 1
  • 1
Yoel
  • 9,144
  • 7
  • 42
  • 57
  • Thanks. I've already found it and I've to set the `store` parameter to `0` I guess to process a live traffic. But everywhere it is mentioned that it's time consuming and hence, I'll try with some other means. Anyway thanks. – RatDon Feb 03 '15 at 10:54
  • Oh, that's surprising. I would expect setting `store=0` would be more efficient, if anything. Where have you read this? – Yoel Feb 03 '15 at 11:06
  • FTR, not setting `store=1` means that you store each packet in a giant list. Usually this is used to sniff a few packets, but in the case you never stop sniffing (here: there is no timeout nor packet count), you will end up having a giant list when you ctrl^c – Cukic0d Mar 17 '20 at 08:48
  • @Cukic0d, thanks for your input. However I think you probably meant _"setting `store=1`"_ or _"not setting `store=0`"_ rather than _"not setting `store=1`"_, right? – Yoel Mar 17 '20 at 16:15