Packet sniffing in Python (Windows)

前端 未结 6 1380
悲&欢浪女
悲&欢浪女 2020-12-01 03:02

What is the best way to sniff network packets using Python?

I\'ve heard from several places that the best module for this is a module called Scapy, unfortunately, it

6条回答
  •  不思量自难忘°
    2020-12-01 03:54

    If scapy, pleae try the following method. (It works on Windows 10)

    # -*- coding: utf-8 -*-
    
    # pip install scapy
    
    """
    [{'name': 'Intel(R) 82574L Gigabit Network Connection',
      'win_index': '4',
      'description': 'Ethernet0',
      'guid': '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}',
      'mac': '00:0C:29:5C:EE:6D',
      'netid': 'Ethernet0'}]
    """
    
    from pprint import pprint
    from scapy.arch.windows import get_windows_if_list
    from scapy.all import *
    
    
    # disable verbose mode
    conf.verb = 0
    
    
    def parse_packet(packet):
        """sniff callback function.
        """
        if packet and packet.haslayer('UDP'):
            udp = packet.getlayer('UDP')
            udp.show()
    
    
    def udp_sniffer():
        """start a sniffer.
        """
        interfaces = get_windows_if_list()
        pprint(interfaces)
    
        print('\n[*] start udp sniffer')
        sniff(
            filter="udp port 53",
            iface=r'Intel(R) 82574L Gigabit Network Connection', prn=parse_packet
        )
    
    
    if __name__ == '__main__':
        udp_sniffer()
    

提交回复
热议问题