示例#1
0
def _inet6_ntop(addr):
    # type: (bytes) -> str
    """Convert an IPv6 address from binary form into text representation,
used when socket.inet_pton is not available.

    """
    # IPv6 addresses have 128bits (16 bytes)
    if len(addr) != 16:
        raise ValueError("invalid length of packed IP address string")

    # Decode to hex representation
    address = ":".join(
        plain_str(bytes_hex(addr[idx:idx +
                                 2])).lstrip('0') or '0'  # noqa: E501
        for idx in range(0, 16, 2))

    try:
        # Get the longest set of zero blocks. We need to take a look
        # at group 1 regarding the length, as 0:0:1:0:0:2:3:4 would
        # have two matches: 0:0: and :0:0: where the latter is longer,
        # though the first one should be taken. Group 1 is in both
        # cases 0:0.
        match = max(_IP6_ZEROS.finditer(address),
                    key=lambda m: m.end(1) - m.start(1))
        return '{}::{}'.format(address[:match.start()], address[match.end():])
    except ValueError:
        return address
示例#2
0
def pkcs_os2ip(s):
    """
    OS2IP conversion function from RFC 3447.

    Input : s        octet string to be converted
    Output: n        corresponding nonnegative integer
    """
    return int(bytes_hex(s), 16)
示例#3
0
def pkcs_os2ip(s):
    """
    OS2IP conversion function from RFC 3447.

    :param s: octet string to be converted
    :return: n, the corresponding nonnegative integer
    """
    return int(bytes_hex(s), 16)
示例#4
0
def _guess_alarm_payload(_pkt, *args, **kargs):
    cls = AlarmItem

    btype = bytes_hex(_pkt[:2]).decode("utf8")
    if btype in PNIO_RPC_ALARM_ASSOCIATION:
        cls = PNIO_RPC_ALARM_ASSOCIATION[btype]

    return cls(_pkt, *args, **kargs)
示例#5
0
文件: pkcs1.py 项目: commial/scapy
def pkcs_os2ip(s):
    """
    OS2IP conversion function from RFC 3447.

    Input : s        octet string to be converted
    Output: n        corresponding nonnegative integer
    """
    return int(bytes_hex(s), 16)
示例#6
0
def run_test(test,
             get_interactive_session,
             theme,
             verb=3,
             ignore_globals=None,
             my_globals=None):
    """An internal UTScapy function to run a single test"""
    start_time = time.time()
    test.output, res = get_interactive_session(test.test.strip(),
                                               ignore_globals=ignore_globals,
                                               verb=verb,
                                               my_globals=my_globals)
    test.result = "failed"
    try:
        if res is None or res:
            test.result = "passed"
        if test.output.endswith('KeyboardInterrupt\n'):
            test.result = "interrupted"
            raise KeyboardInterrupt
    except Exception:
        test.output += "UTscapy: Error during result interpretation:\n"
        test.output += "".join(
            traceback.format_exception(
                sys.exc_info()[0],
                sys.exc_info()[1],
                sys.exc_info()[2],
            ))
    finally:
        test.duration = time.time() - start_time
        if test.result == "failed":
            from scapy.sendrecv import debug
            # Add optional debugging data to log
            if debug.crashed_on:
                cls, val = debug.crashed_on
                test.output += "\n\nPACKET DISSECTION FAILED ON:\n %s(hex_bytes('%s'))" % (
                    cls.__name__, plain_str(bytes_hex(val)))
                debug.crashed_on = None
        test.prepare(theme)
        if verb > 2:
            print("%(fresult)6s %(crc)s %(duration)06.2fs %(name)s" % test,
                  file=sys.stderr)
        elif verb > 1:
            print("%(fresult)6s %(crc)s %(name)s" % test, file=sys.stderr)

    return bool(test)
示例#7
0
def _guess_block_class(_pkt, *args, **kargs):
    cls = Block  # Default block type

    # Special cases
    if _pkt[:2] == b'\x00\x08':  # IODWriteReq
        if _pkt[34:36] == b'\xe0@':  # IODWriteMultipleReq
            cls = IODWriteMultipleReq
        else:
            cls = IODWriteReq

    elif _pkt[:2] == b'\x80\x08':    # IODWriteRes
        if _pkt[34:36] == b'\xe0@':  # IODWriteMultipleRes
            cls = IODWriteMultipleRes
        else:
            cls = IODWriteRes

    # Common cases
    else:
        btype = bytes_hex(_pkt[:2]).decode("utf8")
        if btype in PNIO_RPC_BLOCK_ASSOCIATION:
            cls = PNIO_RPC_BLOCK_ASSOCIATION[btype]

    return cls(_pkt, *args, **kargs)
示例#8
0
文件: pnio_rpc.py 项目: commial/scapy
def _guess_block_class(_pkt, *args, **kargs):
    cls = Block  # Default block type

    # Special cases
    if _pkt[:2] == b'\x00\x08':  # IODWriteReq
        if _pkt[34:36] == b'\xe0@':  # IODWriteMultipleReq
            cls = IODWriteMultipleReq
        else:
            cls = IODWriteReq

    elif _pkt[:2] == b'\x80\x08':    # IODWriteRes
        if _pkt[34:36] == b'\xe0@':  # IODWriteMultipleRes
            cls = IODWriteMultipleRes
        else:
            cls = IODWriteRes

    # Common cases
    else:
        btype = bytes_hex(_pkt[:2]).decode("utf8")
        if btype in PNIO_RPC_BLOCK_ASSOCIATION:
            cls = PNIO_RPC_BLOCK_ASSOCIATION[btype]

    return cls(_pkt, *args, **kargs)