Esempio n. 1
0
	def _load_from_file(self,source):
		self.filename = source
		self.wavFile  = wavFile = wave_open(self.filename, "rb")

		channels     = wavFile.getnchannels()
		sampleWidth  = wavFile.getsampwidth()
		samplingRate = wavFile.getframerate()
		numSamples   = wavFile.getnframes()
		compName     = wavFile.getcompname()

		if (channels not in [1,2]):
			msg = "for \"%s\", channels=%d is not supported" \
			    % (self.filename,channels)
			raise UGenError(msg)
		if (sampleWidth not in [1,2]):
			msg = "for \"%s\", sampleWidth=%d is not supported" \
			    % (self.filename,sampleWidth)
			raise UGenError(msg)
		if (samplingRate != UGen.samplingRate):
			msg = "for \"%s\", samplingRate=%s does not match %s" \
			   % (self.filename,samplingRate)
			raise UGenError(msg)
		if (compName != "not compressed"):
			msg = "for \"%s\", compName=\"%s\" is not supported" \
			    % (self.filename,compName)
			raise UGenError(msg)

		self.inChannels  = 0
		self.outChannels = channels
		sampleScale = (1<<(8*sampleWidth-1)) - 1
		if (sampleWidth == 1): packFormat = "b"
		else:                  packFormat = "h"
		if (channels == 2):    packFormat = "2%s" % packFormat

		self._allocate(numSamples)

		if ("Clip" in UGen.debug):
			print >>stderr, "Clip.load(%s)" % self
			print >>stderr, "  channels    = %s" % channels
			print >>stderr, "  numSamples  = %s" % numSamples
			print >>stderr, "  sampleWidth = %s" % sampleWidth
			print >>stderr, "  sampleScale = %s" % sampleScale
			print >>stderr, "  packFormat  = %s" % packFormat

		if (channels == 1):
			for ix in xrange(numSamples):
				sample = wavFile.readframes(1)
				sample = struct_unpack(packFormat,sample)[0]
				if (sample < -sampleScale): sample = -sampleScale
				self._buffer[ix] = sample / float(sampleScale)
		else: # (channels == 2):
			for ix in xrange(numSamples):
				sample = wavFile.readframes(1)
				(sample,sample2) = struct_unpack(packFormat,sample)
				if (sample  < -sampleScale): sample  = -sampleScale
				if (sample2 < -sampleScale): sample2 = -sampleScale
				self._buffer [ix] = sample  / float(sampleScale)
				self._buffer2[ix] = sample2 / float(sampleScale)

		wavFile.close()
Esempio n. 2
0
    def _read_snappy_java_header(self):
        magic = self.fileobj.read(len(SNAPPY_JAVA_MAGIC))
        if magic != SNAPPY_JAVA_MAGIC:
            raise IOError, 'Not a snappy-java file'

        version = struct_unpack("<H", self._read_exact(2))
        compatible_version = struct_unpack("<H", self._read_exact(2))
Esempio n. 3
0
def read_le_bytes(f, n):
  s = f.read(n)
  if len(s) < n:
    raise ValueError("data malformed")
  if n == 1:
    return ord(s)
  elif n == 2:
    return struct_unpack("<H", s)[0]
  elif n == 3:
    return ord(s[0]) | (ord(s[1]) << 8) | (ord(s[2]) << 16)
  elif n == 4:
    return struct_unpack("<I", s)[0]
  else:
    raise ValueError("number of bytes must be in range [1..4]")
Esempio n. 4
0
def intread(buf):
    """Unpacks the given buffer to an integer."""
    try:
        if isinstance(buf, int):
            return buf
        length = len(buf)
        if length == 1:
            return buf[0]
        elif length <= 4:
            tmp = buf + b"\x00" * (4 - length)
            return struct_unpack("<I", tmp)[0]
        tmp = buf + b"\x00" * (8 - length)
        return struct_unpack("<Q", tmp)[0]
    except:
        raise
Esempio n. 5
0
def decode_date(encvalue):
    val, = struct_unpack('I', encvalue + b'\x00')
    return date(
        day=val & 0x1F,
        month=(val >> 5) & 0xF,
        year=(val >> 9)
    )
Esempio n. 6
0
 def _binparse_fixed(self, read):
     s = struct_unpack(self._struct_format, read(self.arraysize * 2))[0]
     s = s.decode('utf_16_be')
     end = s.find('\0')
     if end != -1:
         return s[:end], False
     return s, False
Esempio n. 7
0
 def getAdata(self):
     if DNS.LABEL_UTF8:
         enc = 'utf8'
     else:
         enc = DNS.LABEL_ENCODING
     x = socket.inet_aton(self.getaddr().decode(enc))
     return struct_unpack("!I", x)[0]
Esempio n. 8
0
def addr2bin(addr):
    # Updated from pyspf
    """Convert a string IPv4 address into an unsigned integer.

    Examples::
    >>> addr2bin('127.0.0.1')
    2130706433

    >>> addr2bin('127.0.0.1') == socket.INADDR_LOOPBACK
    1

    >>> addr2bin('255.255.255.254')
    4294967294L

    >>> addr2bin('192.168.0.1')
    3232235521L

    Unlike old DNS.addr2bin, the n, n.n, and n.n.n forms for IP addresses
    are handled as well::
    >>> addr2bin('10.65536')
    167837696
    >>> 10 * (2 ** 24) + 65536
    167837696

    >>> addr2bin('10.93.512')
    173867520
    >>> 10 * (2 ** 24) + 93 * (2 ** 16) + 512
    173867520
    """
    return struct_unpack("!L", inet_aton(addr))[0]
Esempio n. 9
0
def decode_time(encvalue):
    if len(encvalue) == 6:
        val, = struct_unpack('Q', encvalue + b'\x00\x00')
        return time(
            microsecond=val & 0xFFFFF,
            second=(val >> 20) & 0x3F,
            minute=(val >> 26) & 0x3F,
            hour=(val >> 32)
        )
    else:  # must be 3
        val, = struct_unpack('I', encvalue + b'\x00')
        return time(
            microsecond=0,
            second=(val) & 0x3F,
            minute=(val >> 6) & 0x3F,
            hour=(val >> 12)
        )
Esempio n. 10
0
 def ioctl_GWINSZ(fd):
     try:
         from fcntl import ioctl as fcntl_ioctl
         import termios
         cr = struct_unpack('hh',
                            fcntl_ioctl(fd, termios.TIOCGWINSZ, '1234'))
         return cr
     except:
         pass
Esempio n. 11
0
 def parse(self, data):
     """ unpacks iwparam"""
     
     size = struct_calcsize(self.fmt)
     m, e, i, pad = struct_unpack(self.fmt, data[:size])
     # XXX well, its not *the* frequency - we need a better name
     if e == 0:
         return m
     else:
         return float(m)*10**e
Esempio n. 12
0
 def __init__(self, data, range):
     """Initialize the scan result with the access point data"""
     self.iwstruct = Iwstruct()
     self.range = range
     self.bssid = "%02X:%02X:%02X:%02X:%02X:%02X" % struct_unpack('BBBBBB', data[2:8])
     self.essid = None
     self.mode = None
     self.rate = []
     self.quality = Iwquality() 
     self.frequency = None
     self.encode = None
     self.custom = []
     self.protocol = None
Esempio n. 13
0
File: pcapng.py Progetto: smutt/dpkt
    def __iter__(self):
        while 1:
            buf = self.__f.read(8)
            if len(buf) < 8:
                break

            blk_type, blk_len = struct_unpack('<II' if self.__le else '>II', buf)
            buf += self.__f.read(blk_len - 8)

            if blk_type == PCAPNG_BT_EPB:
                epb = EnhancedPacketBlockLE(buf) if self.__le else EnhancedPacketBlock(buf)
                ts = self._tsoffset + (((epb.ts_high << 32) | epb.ts_low) / self._divisor)
                yield (ts, epb.pkt_data)
Esempio n. 14
0
    def _read(self, size=1024*30):
        raw_chunk_size = self.fileobj.read(4)
        if not raw_chunk_size:
            return False
        chunk_size = struct_unpack(">L", raw_chunk_size)[0]
        chunk = snappy_uncompress(self._read_exact(chunk_size))
        self.pos += len(chunk)

        if self.extrabuf:
            self.extrabuf += chunk
        else:
            self.extrabuf = chunk

        return True
Esempio n. 15
0
    def parse_data(self, fmt, data):
        """ unpacks raw C data """
        size = struct_calcsize(fmt)
        idx = self.idx

        str = data[idx:idx + size]
        self.idx = idx+size
        value = struct_unpack(fmt, str)

        # take care of a tuple like (int, )
        if len(value) == 1:
            return value[0]
        else:
            return value
Esempio n. 16
0
def _get_terminal_size_windows():
    try:
        from ctypes import windll, create_string_buffer
        # stdin handle is -10
        # stdout handle is -11
        # stderr handle is -12
        h = windll.kernel32.GetStdHandle(-12)
        csbi = create_string_buffer(22)
        res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
        if res:
            (bufx, bufy, curx, cury, wattr,
             left, top, right, bottom,
             maxx, maxy) = struct_unpack("hhhhHhhhhhh", csbi.raw)
            sizex = right - left + 1
            sizey = bottom - top + 1
            return sizex, sizey
    except:
        pass
Esempio n. 17
0
File: pcapng.py Progetto: smutt/dpkt
    def _do_unpack_options(self, buf, oo=None):
        self.opts = []
        self.data = ''
        oo = oo or self.__hdr_len__ - 4  # options offset
        ol = self.len - oo - 4  # length

        opts_buf = buf[oo:oo + ol]
        while opts_buf:
            opt = (PcapngOptionLE(opts_buf) if self.__hdr_fmt__[0] == '<'
                   else PcapngOption(opts_buf))
            self.opts.append(opt)

            opts_buf = opts_buf[len(opt):]
            if opt.code == PCAPNG_OPT_ENDOFOPT:
                break

        # duplicate total length field
        self._len = struct_unpack(self.__hdr_fmt__[0] + 'I', buf[-4:])[0]
        if self._len != self.len:
            raise dpkt.UnpackError('length fields do not match')
Esempio n. 18
0
 def _read_chunk(self):
     size, = struct_unpack(">H", self.wire.read(2))
     if size:
         return self.wire.read(size)
     else:
         return b""
Esempio n. 19
0
 def _read_u16be(self):
     q = self._offset + 2
     n, = struct_unpack(">H", self._data[self._offset:q])
     self._offset = q
     return n
Esempio n. 20
0
File: pcapng.py Progetto: smutt/dpkt
    def __init__(self, fileobj):
        self.name = getattr(fileobj, 'name', '<{0}>'.format(fileobj.__class__.__name__))
        self.__f = fileobj

        shb = SectionHeaderBlock()
        buf = self.__f.read(shb.__hdr_len__)
        if len(buf) < shb.__hdr_len__:
            raise ValueError('invalid pcapng header')

        # unpack just the header since endianness is not known
        shb.unpack_hdr(buf)
        if shb.type != PCAPNG_BT_SHB:
            raise ValueError('invalid pcapng header: not a SHB')

        # determine the correct byte order and reload full SHB
        if shb.bom == BYTE_ORDER_MAGIC_LE:
            self.__le = True
            buf += self.__f.read(_swap32b(shb.len) - shb.__hdr_len__)
            shb = SectionHeaderBlockLE(buf)
        elif shb.bom == BYTE_ORDER_MAGIC:
            self.__le = False
            buf += self.__f.read(shb.len - shb.__hdr_len__)
            shb = SectionHeaderBlock(buf)
        else:
            raise ValueError('unknown endianness')

        # check if this version is supported
        if shb.v_major != PCAPNG_VERSION_MAJOR:
            raise ValueError('unknown pcapng version {0}.{1}'.format(shb.v_major, shb.v_minor,))

        # look for a mandatory IDB
        idb = None
        while 1:
            buf = self.__f.read(8)
            if len(buf) < 8:
                break

            blk_type, blk_len = struct_unpack('<II' if self.__le else '>II', buf)
            buf += self.__f.read(blk_len - 8)

            if blk_type == PCAPNG_BT_IDB:
                idb = (InterfaceDescriptionBlockLE(buf) if self.__le
                       else InterfaceDescriptionBlock(buf))
                break
            # just skip other blocks

        if idb is None:
            raise ValueError('IDB not found')

        # set timestamp resolution and offset
        self._divisor = float(1e6)  # defaults
        self._tsoffset = 0
        for opt in idb.opts:
            if opt.code == PCAPNG_OPT_IF_TSRESOL:
                # if MSB=0, the remaining bits is a neg power of 10 (e.g. 6 means microsecs)
                # if MSB=1, the remaining bits is a neg power of 2 (e.g. 10 means 1/1024 of second)
                opt_val = struct_unpack('b', opt.data)[0]
                pow_num = 2 if opt_val & 0b10000000 else 10
                self._divisor = float(pow_num ** (opt_val & 0b01111111))

            elif opt.code == PCAPNG_OPT_IF_TSOFFSET:
                # 64-bit int that specifies an offset (in seconds) that must be added to the
                # timestamp of each packet
                self._tsoffset = struct_unpack('<q' if self.__le else '>q', opt.data)[0]

        if idb.linktype in dltoff:
            self.dloff = dltoff[idb.linktype]
        else:
            self.dloff = 0

        self.idb = idb
        self.snaplen = idb.snaplen
        self.filter = ''
        self.__iter = iter(self)
Esempio n. 21
0
def unpack16bit(s):
    return struct_unpack('!H', s)[0]
Esempio n. 22
0
File: pcapng.py Progetto: smutt/dpkt
def _swap32b(i):
    """swap endianness of an uint32"""
    return struct_unpack('<I', struct_pack('>I', i))[0]
Esempio n. 23
0
 def unpack(self, fmt, packed_data):
     """ unpacks data with given format """
     return struct_unpack(fmt, packed_data)
Esempio n. 24
0
def connect(address, ssl_context=None, error_handler=None, **config):
    """ Connect and perform a handshake and return a valid Connection object, assuming
    a protocol version can be agreed.
    """

    # Establish a connection to the host and port specified
    # Catches refused connections see:
    # https://docs.python.org/2/library/errno.html
    log_info("~~ [CONNECT] %s", address)
    s = None
    try:
        if len(address) == 2:
            s = socket(AF_INET)
        elif len(address) == 4:
            s = socket(AF_INET6)
        else:
            raise ValueError("Unsupported address {!r}".format(address))
        t = s.gettimeout()
        s.settimeout(
            config.get("connection_timeout",
                       default_config["connection_timeout"]))
        s.connect(address)
        s.settimeout(t)
        s.setsockopt(
            SOL_SOCKET, SO_KEEPALIVE,
            1 if config.get("keep_alive", default_config["keep_alive"]) else 0)
    except SocketTimeout:
        if s:
            try:
                s.close()
            except:
                pass
        raise ServiceUnavailable(
            "Timed out trying to establish connection to {!r}".format(address))
    except SocketError as error:
        if s:
            try:
                s.close()
            except:
                pass
        if error.errno in (61, 111, 10061):
            raise ServiceUnavailable(
                "Failed to establish connection to {!r}".format(address))
        else:
            raise
    except ConnectionResetError:
        raise ServiceUnavailable(
            "Failed to establish connection to {!r}".format(address))

    # Secure the connection if an SSL context has been provided
    if ssl_context and SSL_AVAILABLE:
        host = address[0]
        log_info("~~ [SECURE] %s", host)
        try:
            s = ssl_context.wrap_socket(
                s, server_hostname=host if HAS_SNI else None)
        except SSLError as cause:
            s.close()
            error = SecurityError(
                "Failed to establish secure connection to {!r}".format(
                    cause.args[1]))
            error.__cause__ = cause
            raise error
        else:
            # Check that the server provides a certificate
            der_encoded_server_certificate = s.getpeercert(binary_form=True)
            if der_encoded_server_certificate is None:
                s.close()
                raise ProtocolError(
                    "When using a secure socket, the server should always "
                    "provide a certificate")
            trust = config.get("trust", default_config["trust"])
            if trust == TRUST_ON_FIRST_USE:
                from neo4j.bolt.cert import PersonalCertificateStore
                store = PersonalCertificateStore()
                if not store.match_or_trust(host,
                                            der_encoded_server_certificate):
                    s.close()
                    raise ProtocolError(
                        "Server certificate does not match known certificate "
                        "for %r; check details in file %r" %
                        (host, KNOWN_HOSTS))
    else:
        der_encoded_server_certificate = None

    # Send details of the protocol versions supported
    supported_versions = [1, 0, 0, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    log_info("C: [HANDSHAKE] 0x%X %r", MAGIC_PREAMBLE, supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    s.sendall(data)

    # Handle the handshake response
    ready_to_read, _, _ = select((s, ), (), (), 0)
    while not ready_to_read:
        ready_to_read, _, _ = select((s, ), (), (), 0)
    try:
        data = s.recv(4)
    except ConnectionResetError:
        raise ServiceUnavailable(
            "Failed to read any data from server {!r} after connected".format(
                address))
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_error("S: [CLOSE]")
        s.close()
        raise ProtocolError(
            "Connection to %r closed without handshake response" % (address, ))
    if data_size != 4:
        # Some garbled data has been received
        log_error("S: @*#!")
        s.close()
        raise ProtocolError(
            "Expected four byte handshake response, received %r instead" %
            data)
    agreed_version, = struct_unpack(">I", data)
    log_info("S: [HANDSHAKE] %d", agreed_version)
    if agreed_version == 0:
        log_info("~~ [CLOSE]")
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version == 1:
        connection = Connection(
            address,
            s,
            der_encoded_server_certificate=der_encoded_server_certificate,
            error_handler=error_handler,
            **config)
        connection.init()
        return connection
    elif agreed_version == 0x48545450:
        log_error("S: [CLOSE]")
        s.close()
        raise ServiceUnavailable("Cannot to connect to Bolt service on {!r} "
                                 "(looks like HTTP)".format(address))
    else:
        log_error("S: [CLOSE]")
        s.close()
        raise ProtocolError(
            "Unknown Bolt protocol version: {}".format(agreed_version))
Esempio n. 25
0
    def _from_bytes_and_header(cls, data: bytes,
                               header: Header) -> CloseResponse:
        body_data: bytes = data[len(header):]

        try:
            cls.check_structure_size(
                structure_size_to_test=struct_unpack('<H', body_data[:2])[0])
        except IncorrectStructureSizeError as e:
            raise MalformedCloseResponseError(str(e)) from e

        try:
            flags = CloseFlag.from_int(struct_unpack('<H', body_data[2:4])[0])
        except ValueError as e:
            raise InvalidCloseResponseFlagValueError(str(e)) from e

        reserved: bytes = body_data[4:8]
        if reserved != cls._RESERVED:
            raise NonEmptyCloseResponseReservedValueError(
                observed_reserved_value=reserved)

        creation_time: int = struct_unpack('<Q', body_data[8:16])[0]
        last_access_time: int = struct_unpack('<Q', body_data[16:24])[0]
        last_write_time: int = struct_unpack('<Q', body_data[24:32])[0]
        change_time: int = struct_unpack('<Q', body_data[32:40])[0]
        allocation_size: int = struct_unpack('<Q', body_data[40:48])[0]
        endof_file: int = struct_unpack('<Q', body_data[48:56])[0]
        file_attributes_int_value: int = struct_unpack('<I',
                                                       body_data[56:60])[0]

        try:
            file_attributes = FileAttributes.from_int(
                file_attributes_int_value)
        except ValueError as e:
            raise InvalidCloseResponseFileAttributesValueError(str(e)) from e

        if flags.postquery_attrib:
            return cls(header=header,
                       flags=flags,
                       file_information=FileInformation(
                           _creation_time=creation_time,
                           _last_access_time=last_access_time,
                           _last_write_time=last_write_time,
                           _change_time=change_time,
                           allocation_size=allocation_size,
                           endof_file=endof_file,
                           file_attributes=file_attributes))
        else:
            if creation_time != 0:
                raise NonEmptyCloseResponseCreationTimeValueError

            if last_access_time != 0:
                raise NonEmptyCloseResponseLastAccessTimeValueError

            if last_write_time != 0:
                raise NonEmptyCloseResponseLastWriteTimeValueError

            if change_time != 0:
                raise NonEmptyCloseResponseChangeTimeValueError

            if allocation_size != 0:
                raise NonEmptyCloseResponseAllocationSizeValueError

            if endof_file != 0:
                raise NonEmptyCloseResponseEndofFileValueError

            if file_attributes_int_value != 0:
                raise NonEmptyCloseResponseFileAttributesValueError

            return cls(header=header, flags=flags, file_information=None)
Esempio n. 26
0
def addr2bin(addr):
    return struct_unpack('!l', inet_aton(addr))[0]
Esempio n. 27
0
def connect(host_port, ssl_context=None, **config):
    """ Connect and perform a handshake and return a valid Connection object, assuming
    a protocol version can be agreed.
    """

    # Establish a connection to the host and port specified
    # Catches refused connections see:
    # https://docs.python.org/2/library/errno.html
    if __debug__: log_info("~~ [CONNECT] %s", host_port)
    try:
        s = create_connection(host_port)
    except SocketError as error:
        if error.errno == 111 or error.errno == 61:
            raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % host_port)
        else:
            raise

    # Secure the connection if an SSL context has been provided
    if ssl_context and SSL_AVAILABLE:
        host, port = host_port
        if __debug__: log_info("~~ [SECURE] %s", host)
        try:
            s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI else None)
        except SSLError as cause:
            error = ProtocolError("Cannot establish secure connection; %s" % cause.args[1])
            error.__cause__ = cause
            raise error
        else:
            # Check that the server provides a certificate
            der_encoded_server_certificate = s.getpeercert(binary_form=True)
            if der_encoded_server_certificate is None:
                raise ProtocolError("When using a secure socket, the server should always provide a certificate")
            trust = config.get("trust", TRUST_DEFAULT)
            if trust == TRUST_ON_FIRST_USE:
                store = PersonalCertificateStore()
                if not store.match_or_trust(host, der_encoded_server_certificate):
                    raise ProtocolError("Server certificate does not match known certificate for %r; check "
                                        "details in file %r" % (host, KNOWN_HOSTS))
    else:
        der_encoded_server_certificate = None

    # Send details of the protocol versions supported
    supported_versions = [1, 0, 0, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    if __debug__: log_info("C: [HANDSHAKE] 0x%X %r", MAGIC_PREAMBLE, supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    if __debug__: log_debug("C: %s", ":".join(map(hex2, data)))
    s.sendall(data)

    # Handle the handshake response
    ready_to_read, _, _ = select((s,), (), (), 0)
    while not ready_to_read:
        ready_to_read, _, _ = select((s,), (), (), 0)
    data = s.recv(4)
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_error("S: [CLOSE]")
        raise ProtocolError("Server closed connection without responding to handshake")
    if data_size == 4:
        if __debug__: log_debug("S: %s", ":".join(map(hex2, data)))
    else:
        # Some other garbled data has been received
        log_error("S: @*#!")
        raise ProtocolError("Expected four byte handshake response, received %r instead" % data)
    agreed_version, = struct_unpack(">I", data)
    if __debug__: log_info("S: [HANDSHAKE] %d", agreed_version)
    if agreed_version == 0:
        if __debug__: log_info("~~ [CLOSE]")
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version == 1:
        return Connection(s, der_encoded_server_certificate=der_encoded_server_certificate, **config)
    elif agreed_version == 1213486160:
        log_error("S: [CLOSE]")
        raise ProtocolError("Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
                            "(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)")
    else:
        log_error("S: [CLOSE]")
        raise ProtocolError("Unknown Bolt protocol version: %d", agreed_version)
Esempio n. 28
0
def connect(address, ssl_context=None, **config):
    """ Connect and perform a handshake and return a valid Connection object, assuming
    a protocol version can be agreed.
    """

    # Establish a connection to the host and port specified
    # Catches refused connections see:
    # https://docs.python.org/2/library/errno.html
    log_info("~~ [CONNECT] %s", address)
    try:
        s = create_connection(address)
    except SocketError as error:
        if error.errno in (61, 111, 10061):
            raise ServiceUnavailable("Failed to establish connection to %r" %
                                     (address, ))
        else:
            raise

    # Secure the connection if an SSL context has been provided
    if ssl_context and SSL_AVAILABLE:
        host, port = address
        log_info("~~ [SECURE] %s", host)
        try:
            s = ssl_context.wrap_socket(
                s, server_hostname=host if HAS_SNI else None)
        except SSLError as cause:
            error = ServiceUnavailable("Failed to establish secure "
                                       "connection to %r" % cause.args[1])
            error.__cause__ = cause
            raise error
        else:
            from neo4j.v1 import TRUST_DEFAULT, TRUST_ON_FIRST_USE
            # Check that the server provides a certificate
            der_encoded_server_certificate = s.getpeercert(binary_form=True)
            if der_encoded_server_certificate is None:
                raise ProtocolError(
                    "When using a secure socket, the server should always "
                    "provide a certificate")
            trust = config.get("trust", TRUST_DEFAULT)
            if trust == TRUST_ON_FIRST_USE:
                store = PersonalCertificateStore()
                if not store.match_or_trust(host,
                                            der_encoded_server_certificate):
                    raise ProtocolError(
                        "Server certificate does not match known certificate "
                        "for %r; check details in file %r" %
                        (host, KNOWN_HOSTS))
    else:
        der_encoded_server_certificate = None

    # Send details of the protocol versions supported
    supported_versions = [1, 0, 0, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    log_info("C: [HANDSHAKE] 0x%X %r", MAGIC_PREAMBLE, supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    log_debug("C: b%r", data)
    s.sendall(data)

    # Handle the handshake response
    ready_to_read, _, _ = select((s, ), (), (), 0)
    while not ready_to_read:
        ready_to_read, _, _ = select((s, ), (), (), 0)
    data = s.recv(4)
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_error("S: [CLOSE]")
        raise ProtocolError(
            "Connection to %r closed without handshake response" % (address, ))
    if data_size == 4:
        log_debug("S: b%r", data)
    else:
        # Some other garbled data has been received
        log_error("S: @*#!")
        raise ProtocolError(
            "Expected four byte handshake response, received %r instead" %
            data)
    agreed_version, = struct_unpack(">I", data)
    log_info("S: [HANDSHAKE] %d", agreed_version)
    if agreed_version == 0:
        log_info("~~ [CLOSE]")
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version == 1:
        return Connection(
            s,
            der_encoded_server_certificate=der_encoded_server_certificate,
            **config)
    elif agreed_version == 0x48545450:
        log_error("S: [CLOSE]")
        raise ServiceUnavailable("Cannot to connect to Bolt service on %r "
                                 "(looks like HTTP)" % (address, ))
    else:
        log_error("S: [CLOSE]")
        raise ProtocolError("Unknown Bolt protocol version: %d",
                            agreed_version)
Esempio n. 29
0
def _handshake(s, resolved_address, der_encoded_server_certificate, **config):
    """

    :param s:
    :return:
    """
    local_port = s.getsockname()[1]

    # Send details of the protocol versions supported
    supported_versions = [3, 2, 1, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    log_debug("[#%04X]  C: <MAGIC> 0x%08X", local_port, MAGIC_PREAMBLE)
    log_debug("[#%04X]  C: <HANDSHAKE> 0x%08X 0x%08X 0x%08X 0x%08X",
              local_port, *supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    s.sendall(data)

    # Handle the handshake response
    ready_to_read = False
    while not ready_to_read:
        ready_to_read, _, _ = select((s, ), (), (), 1)
    try:
        data = s.recv(4)
    except (IOError, OSError):  # TODO 2.0: remove IOError alias
        raise ServiceUnavailable(
            "Failed to read any data from server {!r} after connected".format(
                resolved_address))
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_debug("[#%04X]  S: <CLOSE>", local_port)
        s.close()
        raise ServiceUnavailable(
            "Connection to %r closed without handshake response" %
            (resolved_address, ))
    if data_size != 4:
        # Some garbled data has been received
        log_debug("[#%04X]  S: @*#!", local_port)
        s.close()
        raise ProtocolError(
            "Expected four byte Bolt handshake response from %r, received %r instead; "
            "check for incorrect port number" % (resolved_address, data))
    agreed_version, = struct_unpack(">I", data)
    log_debug("[#%04X]  S: <HANDSHAKE> 0x%08X", local_port, agreed_version)
    if agreed_version == 0:
        log_debug("[#%04X]  C: <CLOSE>", local_port)
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version in (1, 2):
        connection = Connection(
            agreed_version,
            resolved_address,
            s,
            der_encoded_server_certificate=der_encoded_server_certificate,
            **config)
        connection.init()
        return connection
    elif agreed_version in (3, ):
        connection = Connection(
            agreed_version,
            resolved_address,
            s,
            der_encoded_server_certificate=der_encoded_server_certificate,
            **config)
        connection.hello()
        return connection
    elif agreed_version == 0x48545450:
        log_debug("[#%04X]  S: <CLOSE>", local_port)
        s.close()
        raise ServiceUnavailable("Cannot to connect to Bolt service on {!r} "
                                 "(looks like HTTP)".format(resolved_address))
    else:
        log_debug("[#%04X]  S: <CLOSE>", local_port)
        s.close()
        raise ProtocolError(
            "Unknown Bolt protocol version: {}".format(agreed_version))
Esempio n. 30
0
 def get16bit(self):
     return struct_unpack('!H', self.getbytes(2))[0]
Esempio n. 31
0
 def pop_chunk(self):
     chunk_size, = struct_unpack(">H", self.recv_buffer[:2])
     print("CHUNK SIZE %r" % chunk_size)
     end = 2 + chunk_size
     chunk_data, self.recv_buffer = self.recv_buffer[2:end], self.recv_buffer[end:]
     return chunk_data
Esempio n. 32
0
def connect(host, port=None, ssl_context=None, **config):
    """ Connect and perform a handshake and return a valid Connection object, assuming
    a protocol version can be agreed.
    """

    # Establish a connection to the host and port specified
    # Catches refused connections see:
    # https://docs.python.org/2/library/errno.html
    port = port or DEFAULT_PORT
    if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
    try:
        s = create_connection((host, port))
    except SocketError as error:
        if error.errno == 111 or error.errno == 61 or error.errno == 10061:
            raise ProtocolError(
                "Unable to connect to %s on port %d - is the server running?" %
                (host, port))
        else:
            raise

    # Secure the connection if an SSL context has been provided
    if ssl_context and SSL_AVAILABLE:
        if __debug__: log_info("~~ [SECURE] %s", host)
        try:
            s = ssl_context.wrap_socket(
                s, server_hostname=host if HAS_SNI else None)
        except SSLError as cause:
            error = ProtocolError("Cannot establish secure connection; %s" %
                                  cause.args[1])
            error.__cause__ = cause
            raise error
        else:
            # Check that the server provides a certificate
            der_encoded_server_certificate = s.getpeercert(binary_form=True)
            if der_encoded_server_certificate is None:
                raise ProtocolError(
                    "When using a secure socket, the server should always provide a certificate"
                )
            trust = config.get("trust", TRUST_DEFAULT)
            if trust == TRUST_ON_FIRST_USE:
                store = PersonalCertificateStore()
                if not store.match_or_trust(host,
                                            der_encoded_server_certificate):
                    raise ProtocolError(
                        "Server certificate does not match known certificate for %r; check "
                        "details in file %r" % (host, KNOWN_HOSTS))
    else:
        der_encoded_server_certificate = None

    # Send details of the protocol versions supported
    supported_versions = [1, 0, 0, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    if __debug__:
        log_info("C: [HANDSHAKE] 0x%X %r", MAGIC_PREAMBLE, supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    if __debug__: log_debug("C: %s", ":".join(map(hex2, data)))
    s.sendall(data)

    # Handle the handshake response
    ready_to_read, _, _ = select((s, ), (), (), 0)
    while not ready_to_read:
        ready_to_read, _, _ = select((s, ), (), (), 0)
    data = s.recv(4)
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_error("S: [CLOSE]")
        raise ProtocolError(
            "Server closed connection without responding to handshake")
    if data_size == 4:
        if __debug__: log_debug("S: %s", ":".join(map(hex2, data)))
    else:
        # Some other garbled data has been received
        log_error("S: @*#!")
        raise ProtocolError(
            "Expected four byte handshake response, received %r instead" %
            data)
    agreed_version, = struct_unpack(">I", data)
    if __debug__: log_info("S: [HANDSHAKE] %d", agreed_version)
    if agreed_version == 0:
        if __debug__: log_info("~~ [CLOSE]")
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version == 1:
        return Connection(
            s,
            der_encoded_server_certificate=der_encoded_server_certificate,
            **config)
    elif agreed_version == 1213486160:
        log_error("S: [CLOSE]")
        raise ProtocolError(
            "Server responded HTTP. Make sure you are not trying to connect to the http endpoint "
            +
            "(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)")
    else:
        log_error("S: [CLOSE]")
        raise ProtocolError("Unknown Bolt protocol version: %d",
                            agreed_version)
Esempio n. 33
0
 def _read_f64be(self):
     q = self._offset + 8
     r, = struct_unpack(">d", self._data[self._offset:q])
     self._offset = q
     return r
Esempio n. 34
0
def ip_address_to_int(address):
    return struct_unpack("!I", inet_aton(address))[0]
Esempio n. 35
0
       []
    """
    iwstruct = Iwstruct()
    ifnames = []
    buff = array('c', '\0'*1024)
    caddr_t, length = buff.buffer_info()
    s = iwstruct.pack('iP', length, caddr_t)
    try:
        result = iwstruct._fcntl(SIOCGIFCONF, s)
    except IOError, (i, e):
        return i, e
   
    # get the interface names out of the buffer
    for i in range(0, 1024, 32):
        ifname = buff.tostring()[i:i+32]
        ifname = struct_unpack('32s', ifname)[0]
        ifname = ifname.split('\0', 1)[0]
        if ifname:
            # verify if ifnames are really wifi devices
            wifi = Wireless(ifname)
            result = wifi.getAPaddr()
            if result[0] == 0:
                ifnames.append(ifname)

    return ifnames  

def makedict(**kwargs):
    return kwargs


class Wireless(object):
def get_domain_cached_credentials(
    security_registry: Registry,
    cached_credentials_decryption_key: bytes,
    use_new_style: bool = True
) -> List[DomainCachedCredentials2]:
    iteration_count = 10240
    with suppress(RegistryValueNotFoundException):
        reg_iteration_count = struct_unpack('<L', security_registry.open(r'Cache').value('NL$IterationCount'))[0]
        iteration_count = reg_iteration_count & 0xfffffc00 if reg_iteration_count > 10240 else reg_iteration_count * 1024

    registry_cache_entries = (
        RegistryCacheEntry.from_bytes(cache_key.raw_data())
        for cache_key in security_registry.open('Cache').values()
        if cache_key.name() not in {'NL$Control', 'NL$IterationCount'}
    )

    domain_cached_credentials: List[Union[DomainCachedCredentials, DomainCachedCredentials2]] = list()

    for registry_cache_entry in registry_cache_entries:
        if registry_cache_entry.initialization_vector == b'\x00' * 16:
            continue

        # TODO: Make nicer? (and parse flags in class)?
        if registry_cache_entry.flags & 1 == 1:
            if use_new_style:
                decrypted_cached_entry_data = decrypt_aes(
                    key=cached_credentials_decryption_key,
                    value=registry_cache_entry.encrypted_data,
                    initialization_vector=registry_cache_entry.initialization_vector
                )
            else:
                raise NotImplementedError
                # TODO: For some reason impacket uses `encrypt()`. Confirm that `decrypt()` is correct.
                # decrypted_cached_entry_data = ARC4.new(
                #     key=HMAC.new(
                #         key=nlkm_key,
                #         msg=registry_cache_entry.initialization_vector,
                #     ).digest()
                # ).decrypt(ciphertext=registry_cache_entry.encrypted_data)
        else:
            # "Plain! Until we figure out what this is, we skip it"
            raise NotImplementedError

        def calc_padded_length(length: int) -> int:
            return length + (length & 0x3) if (length & 0x3) > 0 else length

        padded_user_length = calc_padded_length(registry_cache_entry.user_length)
        padded_domain_length = calc_padded_length(registry_cache_entry.domain_name_length)

        encrypted_hash = decrypted_cached_entry_data[:16]
        username = decrypted_cached_entry_data[72:72+registry_cache_entry.user_length].decode(encoding='utf-16le')
        dns_domain_name = decrypted_cached_entry_data[
            72+padded_user_length+padded_domain_length
            :
            72+padded_user_length+padded_domain_length+registry_cache_entry.dns_domain_name_length
        ].decode(encoding='utf-16-le')

        if use_new_style:
            domain_cached_credentials.append(
                DomainCachedCredentials2(
                    iteration_count=iteration_count,
                    dns_domain_name=dns_domain_name,
                    username=username,
                    encrypted_hash=encrypted_hash
                )
            )
        else:
            domain_cached_credentials.append(
                DomainCachedCredentials(
                    dns_domain_name=dns_domain_name,
                    username=username,
                    encrypted_hash=encrypted_hash
                )
            )

    return domain_cached_credentials
Esempio n. 37
0
    def _from_bytes_and_header(cls, data: memoryview,
                               header: Header) -> ReadRequest:
        super()._from_bytes_and_header(data=data, header=header)

        body_bytes: memoryview = data[len(header):]

        read_request_base_args: Dict[str, Any] = dict(
            padding=unpack_from('<B', buffer=body_bytes, offset=2)[0],
            length=unpack_from('<I', buffer=body_bytes, offset=4)[0],
            offset=unpack_from('<Q', buffer=body_bytes, offset=8)[0],
            file_id=FileId.from_bytes(data=body_bytes, base_offset=16),
            minimum_count=unpack_from('<I', buffer=body_bytes, offset=32)[0],
            remaining_bytes=unpack_from('<I', buffer=body_bytes, offset=40)[0])

        flags_raw = bytes(body_bytes[3:4])
        channel_raw = bytes(body_bytes[36:40])
        read_channel_info_offset_raw = bytes(body_bytes[44:46])
        read_channel_info_length_raw = bytes(body_bytes[46:48])

        if header.DIALECT in {Dialect.SMB_2_0_2, Dialect.SMB_2_1}:
            if flags_raw != cls._RESERVED_FLAGS_VALUE:
                raise InvalidReadRequestFlagError

            if channel_raw != cls._RESERVED_CHANNEL_VALUE:
                raise InvalidReadRequestChannelError

            if read_channel_info_offset_raw != cls._RESERVED_READ_CHANNEL_OFFSET:
                raise InvalidReadRequestReadChannelInfoOffsetError

            if read_channel_info_length_raw != cls._RESERVED_READ_CHANNEL_LENGTH:
                raise InvalidReadRequestReadChannelLengthError

            return cls._DIALECT_TO_CLASS[header.DIALECT](
                header=header, **read_request_base_args)

        read_channel_info_offset: int = struct_unpack(
            '<H', read_channel_info_offset_raw)[0]
        read_channel_info_length: int = struct_unpack(
            '<H', read_channel_info_length_raw)[0]
        read_channel_buffer: bytes = bytes(
            data[read_channel_info_offset:read_channel_info_offset +
                 read_channel_info_length])
        channel = ReadRequestChannel(struct_unpack('<I', channel_raw)[0])

        if header.DIALECT is Dialect.SMB_3_0:
            if flags_raw != cls._RESERVED_FLAGS_VALUE:
                raise InvalidReadRequestFlagError
            return ReadRequest300(header=header,
                                  **read_request_base_args,
                                  read_channel_buffer=read_channel_buffer,
                                  channel=channel)

        try:
            flags = ReadRequestFlag.from_int(
                unpack_from('<B', buffer=body_bytes, offset=3)[0])
        except ValueError as e:
            raise InvalidReadRequestFlagError from e

        if header.DIALECT in {Dialect.SMB_3_0_2, Dialect.SMB_3_1_1}:
            return cls._DIALECT_TO_CLASS[header.DIALECT](
                header=header,
                **read_request_base_args,
                read_channel_buffer=read_channel_buffer,
                flags=flags,
                channel=channel)

        # TODO: Raise proper exception.
        raise ValueError
Esempio n. 38
0
 def _parse_length(read):
     return struct_unpack(">I", read(4))[0]
Esempio n. 39
0
 def getMAC(self, packed_data):
     """ extracts mac addr from packed data and returns it as str """
     mac_addr = struct_unpack('xxBBBBBB', packed_data[:8])
     return "%02X:%02X:%02X:%02X:%02X:%02X" % mac_addr
Esempio n. 40
0
def unpack32bit(s):
    return struct_unpack('!L', s)[0]
Esempio n. 41
0
def bin2long6(str):
    # Also from pyspf
    h, l = struct_unpack("!QQ", str)
    return h << 64 | l
Esempio n. 42
0
 def _binparse_fixed(self, read):
     s = struct_unpack(self._struct_format, read(self.arraysize))[0]
     end = s.find(_zero_byte)
     if end != -1:
         return s[:end], False
     return s, False
Esempio n. 43
0
def unpack32bit(s):
    return struct_unpack('!L', s)[0]
Esempio n. 44
0
 def _read_i8(self):
     q = self._offset + 1
     z, = struct_unpack(">b", self._data[self._offset:q])
     self._offset = q
     return z
Esempio n. 45
0
 def _read_u8(self):
     q = self._offset + 1
     n, = struct_unpack(">B", self._data[self._offset:q])
     self._offset = q
     return n
Esempio n. 46
0
 def _read_i32be(self):
     q = self._offset + 4
     z, = struct_unpack(">i", self._data[self._offset:q])
     self._offset = q
     return z
Esempio n. 47
0
 def _read_u32be(self):
     q = self._offset + 4
     n, = struct_unpack(">I", self._data[self._offset:q])
     self._offset = q
     return n
Esempio n. 48
0
def bin2long6(str):
    # Also from pyspf
    h, l = struct_unpack("!QQ", str)
    return h << 64 | l
Esempio n. 49
0
 def _read_i16be(self):
     q = self._offset + 2
     z, = struct_unpack(">h", self._data[self._offset:q])
     self._offset = q
     return z
Esempio n. 50
0
def _handshake(s, resolved_address, der_encoded_server_certificate,
               error_handler, **config):
    """

    :param s:
    :return:
    """

    # Send details of the protocol versions supported
    supported_versions = [2, 1, 0, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    log_debug("C: [HANDSHAKE] 0x%X %r", MAGIC_PREAMBLE, supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    s.sendall(data)

    # Handle the handshake response
    ready_to_read, _, _ = select((s, ), (), (), 0)
    while not ready_to_read:
        ready_to_read, _, _ = select((s, ), (), (), 0)
    try:
        data = s.recv(4)
    except ConnectionResetError:
        raise ServiceUnavailable(
            "Failed to read any data from server {!r} after connected".format(
                resolved_address))
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_error("S: [CLOSE]")
        s.close()
        raise ProtocolError(
            "Connection to %r closed without handshake response" %
            (resolved_address, ))
    if data_size != 4:
        # Some garbled data has been received
        log_error("S: @*#!")
        s.close()
        raise ProtocolError(
            "Expected four byte handshake response, received %r instead" %
            data)
    agreed_version, = struct_unpack(">I", data)
    log_debug("S: [HANDSHAKE] %d", agreed_version)
    if agreed_version == 0:
        log_debug("~~ [CLOSE]")
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version in (1, 2):
        connection = Connection(
            resolved_address,
            s,
            agreed_version,
            der_encoded_server_certificate=der_encoded_server_certificate,
            error_handler=error_handler,
            **config)
        connection.init()
        return connection
    elif agreed_version == 0x48545450:
        log_error("S: [CLOSE]")
        s.close()
        raise ServiceUnavailable("Cannot to connect to Bolt service on {!r} "
                                 "(looks like HTTP)".format(resolved_address))
    else:
        log_error("S: [CLOSE]")
        s.close()
        raise ProtocolError(
            "Unknown Bolt protocol version: {}".format(agreed_version))
Esempio n. 51
0
 def _read_i64be(self):
     q = self._offset + 8
     z, = struct_unpack(">q", self._data[self._offset:q])
     self._offset = q
     return z
Esempio n. 52
0
 def from_bytes(cls, data: bytes) -> PortAny:
     length: int = struct_unpack('<H', data[:2])[0]
     return cls(port_spec=data[2:2 + length - 1].decode(encoding='ascii'))
Esempio n. 53
0
    def _unpack(self):
        marker = self.read_int()

        if marker == -1:
            raise RuntimeError("Nothing to unpack")

        # Tiny Integer
        if 0x00 <= marker <= 0x7F:
            return marker
        elif 0xF0 <= marker <= 0xFF:
            return marker - 0x100

        # Null
        elif marker == 0xC0:
            return None

        # Float
        elif marker == 0xC1:
            value, = struct_unpack(">d", self.read(8))
            return value

        # Boolean
        elif marker == 0xC2:
            return False
        elif marker == 0xC3:
            return True

        # Integer
        elif marker == 0xC8:
            return struct_unpack(">b", self.read(1))[0]
        elif marker == 0xC9:
            return struct_unpack(">h", self.read(2))[0]
        elif marker == 0xCA:
            return struct_unpack(">i", self.read(4))[0]
        elif marker == 0xCB:
            return struct_unpack(">q", self.read(8))[0]

        # Bytes
        elif marker == 0xCC:
            size, = struct_unpack(">B", self.read(1))
            return self.read(size).tobytes()
        elif marker == 0xCD:
            size, = struct_unpack(">H", self.read(2))
            return self.read(size).tobytes()
        elif marker == 0xCE:
            size, = struct_unpack(">I", self.read(4))
            return self.read(size).tobytes()

        else:
            marker_high = marker & 0xF0
            # String
            if marker_high == 0x80:  # TINY_STRING
                return decode(self.read(marker & 0x0F), "utf-8")
            elif marker == 0xD0:  # STRING_8:
                size, = struct_unpack(">B", self.read(1))
                return decode(self.read(size), "utf-8")
            elif marker == 0xD1:  # STRING_16:
                size, = struct_unpack(">H", self.read(2))
                return decode(self.read(size), "utf-8")
            elif marker == 0xD2:  # STRING_32:
                size, = struct_unpack(">I", self.read(4))
                return decode(self.read(size), "utf-8")

            # List
            elif 0x90 <= marker <= 0x9F or 0xD4 <= marker <= 0xD7:
                return self._unpack_list(marker)

            # Map
            elif 0xA0 <= marker <= 0xAF or 0xD8 <= marker <= 0xDB:
                return self._unpack_map(marker)

            # Structure
            elif 0xB0 <= marker <= 0xBF or 0xDC <= marker <= 0xDD:
                size, tag = self._unpack_structure_header(marker)
                value = Structure(tag, *([None] * size))
                for i in range(len(value)):
                    value[i] = self._unpack()
                return value

            elif marker == 0xDF:  # END_OF_STREAM:
                return EndOfStream

            else:
                raise RuntimeError("Unknown PackStream marker %02X" % marker)
Esempio n. 54
0
    def unpack(self):
        current_collection = List(INFINITY)
        current_capacity = current_collection.capacity
        current_size = len(current_collection)
        push_item = current_collection.append

        collection_stack = []
        push_collection = collection_stack.append
        pop_collection = collection_stack.pop

        stream_read = self.stream.read
        while True:
            marker_byte = stream_read(1)

            if not marker_byte:
                break

            is_collection = False

            try:
                value = UNPACKED_MARKERS[
                    marker_byte]  # NULL, TRUE, FALSE and TINY_INT

            except KeyError:
                marker = UNPACKED_UINT_8[marker_byte]
                marker_high = marker & 0xF0

                # Float
                if marker_byte == FLOAT_64:
                    value = struct_unpack(DOUBLE_STRUCT, stream_read(8))[0]

                # Integer
                elif marker_byte == INT_8:
                    value = UNPACKED_INT_8[stream_read(1)]
                elif marker_byte == INT_16:
                    value = UNPACKED_INT_16[stream_read(2)]
                elif marker_byte == INT_32:
                    value = struct_unpack(INT_32_STRUCT, stream_read(4))[0]
                elif marker_byte == INT_64:
                    value = struct_unpack(INT_64_STRUCT, stream_read(8))[0]

                # Bytes
                elif marker_byte == BYTES_8:
                    byte_size = UNPACKED_UINT_8[stream_read(1)]
                    value = stream_read(byte_size)
                elif marker_byte == BYTES_16:
                    byte_size = UNPACKED_UINT_16[stream_read(2)]
                    value = stream_read(byte_size)
                elif marker_byte == BYTES_32:
                    byte_size = struct_unpack(UINT_32_STRUCT,
                                              stream_read(4))[0]
                    value = stream_read(byte_size)

                # String
                elif marker_high == 0x80:
                    value = stream_read(marker & 0x0F).decode(ENCODING)
                elif marker_byte == STRING_8:
                    byte_size = UNPACKED_UINT_8[stream_read(1)]
                    value = stream_read(byte_size).decode(ENCODING)
                elif marker_byte == STRING_16:
                    byte_size = UNPACKED_UINT_16[stream_read(2)]
                    value = stream_read(byte_size).decode(ENCODING)
                elif marker_byte == STRING_32:
                    byte_size = struct_unpack(UINT_32_STRUCT,
                                              stream_read(4))[0]
                    value = stream_read(byte_size).decode(ENCODING)

                # List
                elif marker_high == 0x90:
                    value = List(marker & 0x0F)
                    is_collection = True
                elif marker_byte == LIST_8:
                    size = UNPACKED_UINT_8[stream_read(1)]
                    value = List(size)
                    is_collection = True
                elif marker_byte == LIST_16:
                    size = UNPACKED_UINT_16[stream_read(2)]
                    value = List(size)
                    is_collection = True
                elif marker_byte == LIST_32:
                    size = struct_unpack(UINT_32_STRUCT, stream_read(4))[0]
                    value = List(size)
                    is_collection = True
                elif marker_byte == LIST_STREAM:
                    size = INFINITY
                    value = List(size)
                    is_collection = True

                # Map
                elif marker_high == 0xA0:
                    value = Map(marker & 0x0F)
                    is_collection = True
                elif marker_byte == MAP_8:
                    size = UNPACKED_UINT_8[stream_read(1)]
                    value = Map(size)
                    is_collection = True
                elif marker_byte == MAP_16:
                    size = UNPACKED_UINT_16[stream_read(2)]
                    value = Map(size)
                    is_collection = True
                elif marker_byte == MAP_32:
                    size = struct_unpack(UINT_32_STRUCT, stream_read(4))[0]
                    value = Map(size)
                    is_collection = True
                elif marker_byte == MAP_STREAM:
                    size = INFINITY
                    value = Map(size)
                    is_collection = True

                # Structure
                elif marker_high == 0xB0:
                    signature = stream_read(1)
                    value = Structure(marker & 0x0F, signature)
                    is_collection = True
                elif marker_byte == STRUCT_8:
                    size, signature = stream_read(2)
                    value = Structure(UNPACKED_UINT_8[size], signature)
                    is_collection = True
                elif marker_byte == STRUCT_16:
                    data = stream_read(3)
                    value = Structure(UNPACKED_UINT_16[data[0:2]], data[2])
                    is_collection = True

                elif marker_byte == END_OF_STREAM:
                    value = END_OF_STREAM

            appended = False
            while not appended:
                if current_size >= current_capacity:
                    current_collection = pop_collection()
                    current_capacity = current_collection.capacity
                    current_size = len(current_collection)
                    push_item = current_collection.append
                else:
                    if push_item(value) is not NotImplemented:
                        current_size += 1
                    if is_collection:
                        push_collection(current_collection)
                        current_collection = value
                        current_capacity = current_collection.capacity
                        current_size = len(current_collection)
                        push_item = current_collection.append
                    appended = True

        if collection_stack:
            return iter(collection_stack[0])
        else:
            return iter(current_collection)
Esempio n. 55
0
 def from_bytes(cls, data: bytes) -> RDeleteServiceResponse:
     return cls(
         return_code=Win32ErrorCode(struct_unpack('<I', data[:4])[0]))
Esempio n. 56
0
def _swap32b(i):
    """Swap endianness of an uint32"""
    return struct_unpack('<I', struct_pack('>I', i))[0]
Esempio n. 57
0
def unpack16bit(s):
    return struct_unpack('!H', s)[0]
Esempio n. 58
0
def normalize_ip_subnet(ip, fmt=to_str("!I")):
    ip, mask = ip.split("/")
    ip = struct_unpack(fmt, inet_aton(ip))[0] & _IP_MASK_CACHES[int(mask)]
    return "{}/{}".format(inet_ntoa(struct_pack(fmt, ip)), mask)
Esempio n. 59
0
def addr2bin(addr):
    return struct_unpack('!l', inet_aton(addr))[0]
Esempio n. 60
0
    def __init__(self, fileobj):
        self.name = getattr(fileobj, 'name', '<{0}>'.format(fileobj.__class__.__name__))
        self.__f = fileobj

        shb = SectionHeaderBlock()
        buf = self.__f.read(shb.__hdr_len__)
        if len(buf) < shb.__hdr_len__:
            raise ValueError('invalid pcapng header')

        # unpack just the header since endianness is not known
        shb.unpack_hdr(buf)
        if shb.type != PCAPNG_BT_SHB:
            raise ValueError('invalid pcapng header: not a SHB')

        # determine the correct byte order and reload full SHB
        if shb.bom == BYTE_ORDER_MAGIC_LE:
            self.__le = True
            buf += self.__f.read(_swap32b(shb.len) - shb.__hdr_len__)
            shb = SectionHeaderBlockLE(buf)
        elif shb.bom == BYTE_ORDER_MAGIC:
            self.__le = False
            buf += self.__f.read(shb.len - shb.__hdr_len__)
            shb = SectionHeaderBlock(buf)
        else:
            raise ValueError('unknown endianness')

        # check if this version is supported
        if shb.v_major != PCAPNG_VERSION_MAJOR:
            raise ValueError('unknown pcapng version {0}.{1}'.format(shb.v_major, shb.v_minor,))

        # look for a mandatory IDB
        idb = None
        while 1:
            buf = self.__f.read(8)
            if len(buf) < 8:
                break

            blk_type, blk_len = struct_unpack('<II' if self.__le else '>II', buf)
            buf += self.__f.read(blk_len - 8)

            if blk_type == PCAPNG_BT_IDB:
                idb = (InterfaceDescriptionBlockLE(buf) if self.__le
                       else InterfaceDescriptionBlock(buf))
                break
            # just skip other blocks

        if idb is None:
            raise ValueError('IDB not found')

        # set timestamp resolution and offset
        self._divisor = float(1e6)  # defaults
        self._tsoffset = 0
        for opt in idb.opts:
            if opt.code == PCAPNG_OPT_IF_TSRESOL:
                # if MSB=0, the remaining bits is a neg power of 10 (e.g. 6 means microsecs)
                # if MSB=1, the remaining bits is a neg power of 2 (e.g. 10 means 1/1024 of second)
                opt_val = struct_unpack('b', opt.data)[0]
                pow_num = 2 if opt_val & 0b10000000 else 10
                self._divisor = float(pow_num ** (opt_val & 0b01111111))

            elif opt.code == PCAPNG_OPT_IF_TSOFFSET:
                # 64-bit int that specifies an offset (in seconds) that must be added to the
                # timestamp of each packet
                self._tsoffset = struct_unpack('<q' if self.__le else '>q', opt.data)[0]

        if idb.linktype in dltoff:
            self.dloff = dltoff[idb.linktype]
        else:
            self.dloff = 0

        self.idb = idb
        self.snaplen = idb.snaplen
        self.filter = ''
        self.__iter = iter(self)