示例#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 get_working_if():
    for i in get_if_list():
        if i == LOOPBACK_NAME:
            continue
        ifflags = struct.unpack("16xH14x", get_if(i, SIOCGIFFLAGS))[0]
        if ifflags & IFF_UP:
            return i
    return LOOPBACK_NAME
示例#3
0
文件: linux.py 项目: plorinquer/scapy
def get_working_if():
    for i in get_if_list():
        if i == LOOPBACK_NAME:
            continue
        ifflags = struct.unpack("16xH14x", get_if(i, SIOCGIFFLAGS))[0]
        if ifflags & IFF_UP:
            return i
    return LOOPBACK_NAME
示例#4
0
def get_if_raw_addr(iff):
    r"""
    Return the raw IPv4 address of an interface.
    If unavailable, returns b"\0\0\0\0"
    """
    try:
        return get_if(iff, SIOCGIFADDR)[20:24]
    except IOError:
        return b"\0\0\0\0"
示例#5
0
def get_if_raw_addr(iff):
    # type: (Union[NetworkInterface, str]) -> bytes
    r"""
    Return the raw IPv4 address of an interface.
    If unavailable, returns b"\0\0\0\0"
    """
    try:
        return get_if(iff, SIOCGIFADDR)[20:24]
    except IOError:
        return b"\0\0\0\0"
示例#6
0
文件: linux.py 项目: xjzpguob/scapy
def get_working_if():
    """
    Return the name of the first network interfcace that is up.
    """
    for i in get_if_list():
        if i == LOOPBACK_NAME:
            continue
        ifflags = struct.unpack("16xH14x", get_if(i, SIOCGIFFLAGS))[0]
        if ifflags & IFF_UP:
            return i
    return LOOPBACK_NAME
示例#7
0
def _get_if_flags(ifname):
    """Internal function to get interface flags"""
    # Get interface flags
    try:
        result = get_if(ifname, SIOCGIFFLAGS)
    except IOError:
        warning("ioctl(SIOCGIFFLAGS) failed on %s !", ifname)
        return None

    # Convert flags
    ifflags = struct.unpack("16xH14x", result)[0]
    return ifflags
示例#8
0
def get_working_ifaces():
    """
    Returns an ordered list of interfaces that could be used with BPF.
    Note: the order mimics pcap_findalldevs() behavior
    """

    # Only root is allowed to perform the following ioctl() call
    if os.getuid() != 0:
        return []

    # Test all network interfaces
    interfaces = []
    for ifname in get_if_list():

        # Unlike pcap_findalldevs(), we do not care of loopback interfaces.
        if ifname == conf.loopback_name:
            continue

        # Get interface flags
        try:
            result = get_if(ifname, SIOCGIFFLAGS)
        except IOError:
            warning("ioctl(SIOCGIFFLAGS) failed on %s !", ifname)
            continue

        # Convert flags
        ifflags = struct.unpack("16xH14x", result)[0]
        if ifflags & 0x1:  # IFF_UP

            # Get a BPF handle
            fd = get_dev_bpf()[0]
            if fd is None:
                raise Scapy_Exception("No /dev/bpf are available !")

            # Check if the interface can be used
            try:
                fcntl.ioctl(fd, BIOCSETIF,
                            struct.pack("16s16x", ifname.encode()))
            except IOError:
                pass
            else:
                ifnum, ifab = _IFNUM.search(ifname).groups()
                interfaces.append((ifname, int(ifnum) if ifnum else -1, ifab))
            finally:
                # Close the file descriptor
                os.close(fd)

    # Sort to mimic pcap_findalldevs() order
    interfaces.sort(key=lambda elt: (elt[1], elt[2], elt[0]))

    return [iface[0] for iface in interfaces]
示例#9
0
def get_working_ifaces():
    """
    Returns an ordered list of interfaces that could be used with BPF.
    Note: the order mimics pcap_findalldevs() behavior
    """

    # Only root is allowed to perform the following ioctl() call
    if os.getuid() != 0:
        return []

    # Test all network interfaces
    interfaces = []
    for ifname in get_if_list():

        # Unlike pcap_findalldevs(), we do not care of loopback interfaces.
        if ifname == LOOPBACK_NAME:
            continue

        # Get interface flags
        try:
            result = get_if(ifname, SIOCGIFFLAGS)
        except IOError as msg:
            warning("ioctl(SIOCGIFFLAGS) failed on %s !", ifname)
            continue

        # Convert flags
        ifflags = struct.unpack("16xH14x", result)[0]
        if ifflags & 0x1:  # IFF_UP

            # Get a BPF handle
            fd, _ = get_dev_bpf()
            if fd is None:
                raise Scapy_Exception("No /dev/bpf are available !")

            # Check if the interface can be used
            try:
                fcntl.ioctl(fd, BIOCSETIF,
                            struct.pack("16s16x", ifname.encode()))
                interfaces.append((ifname, int(ifname[-1])))
            except IOError as err:
                pass

            # Close the file descriptor
            os.close(fd)

    # Sort to mimic pcap_findalldevs() order
    interfaces.sort(
        lambda ifname_left_and_ifid_left, ifname_right_and_ifid_right:
        ifname_left_and_ifid_left[1] - ifname_right_and_ifid_right[1])
    return interfaces
示例#10
0
文件: core.py 项目: netkey/scapy
def get_working_ifaces():
    """
    Returns an ordered list of interfaces that could be used with BPF.
    Note: the order mimics pcap_findalldevs() behavior
    """

    # Only root is allowed to perform the following ioctl() call
    if os.getuid() != 0:
        return []

    # Test all network interfaces
    interfaces = []
    for ifname in get_if_list():

        # Unlike pcap_findalldevs(), we do not care of loopback interfaces.
        if ifname == LOOPBACK_NAME:
            continue

        # Get interface flags
        try:
            result = get_if(ifname, SIOCGIFFLAGS)
        except IOError:
            warning("ioctl(SIOCGIFFLAGS) failed on %s !", ifname)
            continue

        # Convert flags
        ifflags = struct.unpack("16xH14x", result)[0]
        if ifflags & 0x1:  # IFF_UP

            # Get a BPF handle
            fd, _ = get_dev_bpf()
            if fd is None:
                raise Scapy_Exception("No /dev/bpf are available !")

            # Check if the interface can be used
            try:
                fcntl.ioctl(fd, BIOCSETIF, struct.pack("16s16x", ifname.encode()))  # noqa: E501
                interfaces.append((ifname, int(ifname[-1])))
            except IOError:
                pass

            # Close the file descriptor
            os.close(fd)

    # Sort to mimic pcap_findalldevs() order
    interfaces.sort(key=lambda elt: elt[1])

    return interfaces
示例#11
0
文件: linux.py 项目: plorinquer/scapy
def get_if_index(iff):
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
示例#12
0
def get_if_index(iff):
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
示例#13
0
def get_if_index(iff):
    # type: (Union[NetworkInterface, str]) -> int
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
示例#14
0
def get_if_raw_addr(iff):
    try:
        return get_if(iff, SIOCGIFADDR)[20:24]
    except IOError:
        return "\0\0\0\0"
示例#15
0
def get_if_raw_hwaddr(iff):
    return struct.unpack("16xh6s8x", get_if(iff, SIOCGIFHWADDR))
示例#16
0
文件: linux.py 项目: plorinquer/scapy
def get_if_raw_hwaddr(iff):
    return struct.unpack("16xh6s8x", get_if(iff, SIOCGIFHWADDR))
示例#17
0
文件: linux.py 项目: plorinquer/scapy
def get_if_raw_addr(iff):
    try:
        return get_if(iff, SIOCGIFADDR)[20:24]
    except IOError:
        return b"\0\0\0\0"