Search code examples
pythonpython-2.7tkinterscapysniffing

How to enter user defined values like IP Address or count for capturing packets using python


I am trying to build a GUI using python to collect udp packets.

I have succeeded in collecting packets from the machine I am working on.

Now as I am building a GUI for this, How can I give user inputs for selecting the count of packets, source and destination IP address for collecting the packets using the sniff command?

Or is there another way other than sniff?

I have succeeded in building the GUI for providing user inputs, but unable to filter them in sniff command.

I have used the following code and was able to get the udp packets.

def pkt_callback(pkt):
    self.S = pkt.summary()
    logger.info("Information of collected packets: %s",self.S)

    self.P = sniff(count = 10, filter="udp", prn=pkt_callback )
    logger.info("Collected packets are: %s",self.P)

Expected result: Display of n number of packets collected from X source IP to Y destination IP
where n: count provided by the user
           X: Source IP
           Y: destination IP

Actual Result: Displaying C number of packets from the machine on which I am coding.

C count is given in sniff command.


Solution

  • You simply need to extend your filter to handle this. Have a look at the BPF format: http://biot.com/capstats/bpf.html

    For instance:

    >>> sniff(count=10, filter="dst host 192.168.0.1 or src host 192.168.0.12 and udp")
    

    Or more generally:

    >>> src = "192.168.0.12"
    >>> dst = "192.168.0.1"
    >>> c = 10
    >>> sniff(count=c, filter="dst host %s and src host %s and udp" % (dst, src))