def __init__(self, srcMAC, dstMAC): ''' Constructor ''' self.SrcMAC = srcMAC self.DstMAC = dstMAC print("List of devices:") devices = WinPcapDevices.list_devices() for device in devices: print(device, devices[device])
def __init__(self): self.devices = WinPcapDevices.list_devices() return
def start(self): global VNIC_MAC_BYTES, VNIC_IPV4_INT, NIC_INFO cfg = configparser.ConfigParser() # open config.ini cfg.read(self.cfg_path) assert cfg.has_option("VirtualNic", "MacAddress") vnic_mac_str = cfg.get("VirtualNic", "MacAddress").replace( ":", "").replace("-", "") assert len(vnic_mac_str) == 12 VNIC_MAC_BYTES = bytes.fromhex(vnic_mac_str) assert cfg.has_option("VirtualNic", "Ipv4Address") vnic_ip_str = cfg.get("VirtualNic", "Ipv4Address") VNIC_IPV4_INT = get_ip_value(vnic_ip_str) # show nic devices dev_dict = WinPcapDevices.list_devices() info_print("Some NICs available:") nic_cnt = len(dev_dict) nic_names = list(dev_dict.keys()) nic_ids = {str(i) for i in range(1, nic_cnt + 1)} nic_idx = 0 for nic_name in nic_names: nic_idx += 1 info_print( "NIC[{}] -> name='{}', description='{}'".format((nic_idx), (nic_name), (try_get_nic_device_description(nic_name, dev_dict[nic_name])))) nic_names.insert(0, "") # choose two nics sel_nic_ids = [] try: has_wrong_input = False while len(sel_nic_ids) < NIC_COUNT: sel_nic = get_striped_input_from_stdin("{}选择第{}块NIC({})=>".format((has_wrong_input and 'ID非法,请重新' or ''), (1+len(sel_nic_ids)), ('或'.join(nic_ids)))) if sel_nic in nic_ids: sel_nic_ids.append(int(sel_nic)) nic_ids.remove(sel_nic) has_wrong_input = False else: has_wrong_input = True verbose_print( "您已成功选择{}个NIC:{}".format((NIC_COUNT), (['NIC[{}]'.format((id)) for id in sel_nic_ids]))) is_success = True except: is_success = False # warning_print(SEPERATOR) if(is_success): nic_id = sel_nic_ids[0] nic_name = nic_names[nic_id] thd = Thread(target=listen, args=(nic_id, nic_name)) thd.setDaemon(True) thd.start() signal.signal(signal.SIGINT, exit) signal.signal(signal.SIGTERM, exit) while True: try: if not thd.is_alive(): break time.sleep(1e6) except KeyboardInterrupt: break thd.join() warning_print("再见!")
from winpcapy import WinPcapUtils from winpcapy import WinPcapDevices # Return a list of all the devices detected on the machine WinPcapDevices.list_devices() # Itearte over devices (in memory), with full details access with WinPcapDevices() as devices: for device in devices: print device.description
def listwinpcapyifaces(): with WinPcapDevices() as devices: for device in devices: yield device.name, device.description, device.flags, device.addresses.contents.netmask.contents.sa_family
import ipaddress from winpcapy import WinPcapUtils, WinPcapDevices from interpreter.formats.net import EthernetFormat devices = WinPcapDevices.list_devices() for name in devices.keys(): print(name, ':', devices[name]) eth = EthernetFormat() filter = None # filter = ipaddress.IPv4Address('192.168.1.40') def packet_callback(win_pcap, param, header, pkt_data): try: packet = eth.parse_bytes(pkt_data) if hasattr(packet, 'ip'): # print('%s -> %s %s' % (packet.ip.source, packet.ip.dest, packet.ip.protocol)) if hasattr(packet, 'ip') and hasattr(packet.ip, 'udp'): if filter is None or packet.ip.source == filter or packet.ip.dest == filter: print('%s:%d -> %s:%d = %s' % (packet.ip.source, packet.ip.udp.src_port, packet.ip.dest, packet.ip.udp.dst_port, packet.ip.udp.data)) except: print('PARSE ERROR') print(pkt_data) WinPcapUtils.capture_on_device_name(list(devices.keys())[0], packet_callback)
def PrintDevices(): with WinPcapDevices() as devices: for device in devices: print device.name print device.description
#!/usr/bin/env python # -*- coding: utf-8 -*- # pip install pypiwin32 winpcapy from winpcapy import WinPcapDevices # Return a list of all the devices detected on the machine print((WinPcapDevices.list_devices())) from winpcapy import WinPcapUtils print((WinPcapUtils.capture_on_and_print("*Microsoft*")))
def get_info_by_name(if_name): if_name = "*%s*" % (if_name) for name, desc in WinPcapDevices.list_devices().items(): if fnmatch.fnmatch(name, if_name): return name, desc return None, None