def load_winpcapy(): """This functions calls Winpcap/Npcap pcap_findalldevs function, and extracts and parse all the data scapy will need to use it: - the Interface List - the IPv4 addresses - the IPv6 addresses This data is stored in their respective conf.cache_* subfields: conf.cache_iflist conf.cache_ipaddrs conf.cache_in6_getifaddr """ err = create_string_buffer(PCAP_ERRBUF_SIZE) devs = POINTER(pcap_if_t)() if_list = [] ip_addresses = {} ip6_addresses = [] if pcap_findalldevs(byref(devs), err) < 0: return try: p = devs # Iterate through the different interfaces while p: if_list.append(plain_str(p.contents.name)) a = p.contents.addresses while a: # IPv4 address if a.contents.addr.contents.sa_family == socket.AF_INET: # noqa: E501 ap = a.contents.addr val = cast(ap, POINTER(sockaddr_in)) if_raw_addr = b"".join( chb(x) for x in val.contents.sin_addr[:4]) # noqa: E501 if if_raw_addr != b'\x00\x00\x00\x00': ip_addresses[plain_str( p.contents.name )] = if_raw_addr # noqa: E501 # IPv6 address if a.contents.addr.contents.sa_family == socket.AF_INET6: # noqa: E501 ap = a.contents.addr val = cast(ap, POINTER(sockaddr_in6)) addr = inet_ntop(socket.AF_INET6, b"".join( chb(x) for x in val.contents.sin6_addr[:])) # noqa: E501 scope = scapy.utils6.in6_getscope(addr) ip6_addresses.append( (addr, scope, plain_str(p.contents.name))) # noqa: E501 a = a.contents.next p = p.contents.next conf.cache_iflist = if_list conf.cache_ipaddrs = ip_addresses conf.cache_in6_getifaddr = ip6_addresses except Exception: raise finally: pcap_freealldevs(devs)
def load_winpcapy(): """This functions calls libpcap ``pcap_findalldevs`` function, and extracts and parse all the data scapy will need to build the Interface List. The date will be stored in ``conf.cache_iflist``, or accessible with ``get_if_list()`` """ err = create_string_buffer(PCAP_ERRBUF_SIZE) devs = POINTER(pcap_if_t)() if_list = {} if pcap_findalldevs(byref(devs), err) < 0: return try: p = devs # Iterate through the different interfaces while p: name = plain_str(p.contents.name) # GUID description = plain_str(p.contents.description) # NAME flags = p.contents.flags # FLAGS ips = [] a = p.contents.addresses while a: # IPv4 address family = a.contents.addr.contents.sa_family ap = a.contents.addr if family == socket.AF_INET: val = cast(ap, POINTER(sockaddr_in)) val = val.contents.sin_addr[:] elif family == socket.AF_INET6: val = cast(ap, POINTER(sockaddr_in6)) val = val.contents.sin6_addr[:] else: # Unknown address family # (AF_LINK isn't a thing on Windows) a = a.contents.next continue addr = inet_ntop(family, bytes(bytearray(val))) if addr != "0.0.0.0": ips.append(addr) a = a.contents.next if_list[name] = (description, ips, flags) p = p.contents.next conf.cache_iflist = if_list except Exception: raise finally: pcap_freealldevs(devs)
def load_winpcapy(): """This functions calls Winpcap/Npcap pcap_findalldevs function, and extracts and parse all the data scapy will need to use it: - the Interface List This data is stored in their respective conf.cache_* subfields: conf.cache_iflist """ err = create_string_buffer(PCAP_ERRBUF_SIZE) devs = POINTER(pcap_if_t)() if_list = {} if pcap_findalldevs(byref(devs), err) < 0: return try: p = devs # Iterate through the different interfaces while p: name = plain_str(p.contents.name) # GUID description = plain_str(p.contents.description) # NAME ips = [] a = p.contents.addresses while a: # IPv4 address family = a.contents.addr.contents.sa_family ap = a.contents.addr if family == socket.AF_INET: val = cast(ap, POINTER(sockaddr_in)) val = val.contents.sin_addr[:] elif family == socket.AF_INET6: val = cast(ap, POINTER(sockaddr_in6)) val = val.contents.sin6_addr[:] else: # Unknown address family # (AF_LINK isn't a thing on Windows) a = a.contents.next continue addr = inet_ntop(family, bytes(bytearray(val))) if addr != "0.0.0.0": ips.append(addr) a = a.contents.next if_list[description] = (name, ips) p = p.contents.next conf.cache_iflist = if_list except Exception: raise finally: pcap_freealldevs(devs)
def load_winpcapy(): err = create_string_buffer(PCAP_ERRBUF_SIZE) devs = POINTER(pcap_if_t)() if_list = [] ip_addresses = {} ip6_addresses = [] if pcap_findalldevs(byref(devs), err) < 0: return try: p = devs # Iterate through the different interfaces while p: if_list.append(plain_str(p.contents.name)) a = p.contents.addresses while a: # IPv4 address if a.contents.addr.contents.sa_family == socket.AF_INET: # noqa: E501 ap = a.contents.addr val = cast(ap, POINTER(sockaddr_in)) if_raw_addr = b"".join(chb(x) for x in val.contents.sin_addr[:4]) # noqa: E501 if if_raw_addr != b'\x00\x00\x00\x00': ip_addresses[plain_str(p.contents.name)] = if_raw_addr # noqa: E501 # IPv6 address if a.contents.addr.contents.sa_family == socket.AF_INET6: # noqa: E501 ap = a.contents.addr val = cast(ap, POINTER(sockaddr_in6)) addr = inet_ntop(socket.AF_INET6, b"".join(chb(x) for x in val.contents.sin6_addr[:])) # noqa: E501 scope = scapy.utils6.in6_getscope(addr) ip6_addresses.append((addr, scope, plain_str(p.contents.name))) # noqa: E501 a = a.contents.next p = p.contents.next conf.cache_iflist = if_list conf.cache_ipaddrs = ip_addresses conf.cache_in6_getifaddr = ip6_addresses except Exception: raise finally: pcap_freealldevs(devs)