Example #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
Example #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
Example #3
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
Example #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"
Example #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"
Example #6
0
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
Example #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
Example #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]
Example #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
Example #10
0
File: core.py Project: 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
Example #11
0
def get_if_index(iff):
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
Example #12
0
def get_if_index(iff):
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
Example #13
0
def get_if_index(iff):
    # type: (Union[NetworkInterface, str]) -> int
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
Example #14
0
def get_if_raw_addr(iff):
    try:
        return get_if(iff, SIOCGIFADDR)[20:24]
    except IOError:
        return "\0\0\0\0"
Example #15
0
def get_if_raw_hwaddr(iff):
    return struct.unpack("16xh6s8x", get_if(iff, SIOCGIFHWADDR))
Example #16
0
def get_if_raw_hwaddr(iff):
    return struct.unpack("16xh6s8x", get_if(iff, SIOCGIFHWADDR))
Example #17
0
def get_if_raw_addr(iff):
    try:
        return get_if(iff, SIOCGIFADDR)[20:24]
    except IOError:
        return b"\0\0\0\0"