示例#1
0
 def load(self):
     from scapy.fields import FlagValue
     data = {}
     ips = in6_getifaddr()
     for i in _get_if_list():
         ifflags = struct.unpack("16xH14x", get_if(i, SIOCGIFFLAGS))[0]
         index = get_if_index(i)
         mac = scapy.utils.str2mac(
             get_if_raw_hwaddr(i, siocgifhwaddr=SIOCGIFHWADDR)[1]
         )
         ip = inet_ntop(socket.AF_INET, get_if_raw_addr(i))
         if ip == "0.0.0.0":
             ip = None
         ifflags = FlagValue(ifflags, _iff_flags)
         if_data = {
             "name": i,
             "network_name": i,
             "description": i,
             "flags": ifflags,
             "index": index,
             "ip": ip,
             "ips": [x[0] for x in ips if x[2] == i] + [ip] if ip else [],
             "mac": mac
         }
         data[i] = NetworkInterface(self, if_data)
     return data
示例#2
0
        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 data will be stored in ``conf.cache_pcapiflist``
            """
            from scapy.fields import FlagValue

            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 or ""
                    )  # DESC
                    flags = p.contents.flags  # FLAGS
                    ips = []
                    mac = ""
                    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[:]
                        elif family == socket.AF_LINK:
                            # Special case: MAC
                            # (AF_LINK is mostly BSD specific)
                            val = ap.contents.sa_data
                            val = val[:6]
                            mac = str2mac(bytes(bytearray(val)))
                            a = a.contents.next
                            continue
                        else:
                            # Unknown AF
                            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
                    flags = FlagValue(flags, _pcap_if_flags)
                    if_list[name] = (description, ips, flags, mac)
                    p = p.contents.next
                conf.cache_pcapiflist = if_list
            except Exception:
                raise
            finally:
                pcap_freealldevs(devs)
示例#3
0
 def load(self):
     from scapy.fields import FlagValue
     data = {}
     ips = in6_getifaddr()
     for ifname, index in _get_ifindex_list():
         try:
             ifflags = _get_if_flags(ifname)
             mac = scapy.utils.str2mac(get_if_raw_hwaddr(ifname)[1])
             ip = inet_ntop(socket.AF_INET, get_if_raw_addr(ifname))
         except Scapy_Exception:
             continue
         ifflags = FlagValue(ifflags, _iff_flags)
         if_data = {
             "name": ifname,
             "network_name": ifname,
             "description": ifname,
             "flags": ifflags,
             "index": index,
             "ip": ip,
             "ips": [x[0] for x in ips if x[2] == ifname] + [ip],
             "mac": mac
         }
         data[ifname] = NetworkInterface(self, if_data)
     return data