示例#1
0
 def test_intread(self):
     """Use intread to read from valid strings."""
     try:
         for r in range(4):
             utils.intread(b'a' * (r + 1))
     except ValueError as e:
         self.fail("intread failed calling 'int%dread: %s" % \
             (int(r)+1, e))
 def test_intread(self):
     """Use intread to read from valid strings."""
     try:
         for i in range(4):
             utils.intread(bytearray(b'a') * (i + 1))
     except ValueError as err:
         self.fail("intread failed calling 'int{0}read: {1}".format(
                  int(i) + 1, err))
示例#3
0
 def test_intread(self):
     """Use intread to read from valid strings."""
     try:
         for i in range(4):
             utils.intread(bytearray(b'a') * (i + 1))
     except ValueError as err:
         self.fail("intread failed calling 'int{0}read: {1}".format(
             int(i) + 1, err))
 def test_intread(self):
     """Use intread to read from valid strings."""
     try:
         for r in range(4):
             utils.intread(b'a'*(r+1))
     except ValueError as e:
         self.fail("intread failed calling 'int%dread: %s" % \
             (int(r)+1, e))
示例#5
0
 def _scramble_password(self, passwd, seed):
     """Scramble a password ready to send to MySQL"""
     hash4 = None
     try:
         hash1 = sha1(passwd).digest()
         hash2 = sha1(hash1).digest() # Password as found in mysql.user()
         hash3 = sha1(seed + hash2).digest()
         xored = [ utils.intread(h1) ^ utils.intread(h3)
             for (h1,h3) in zip(hash1, hash3) ]
         hash4 = struct.pack('20B', *xored)
     except Exception, err:
         raise errors.InterfaceError(
             'Failed scrambling password; %s' % err)
示例#6
0
 def _scramble_password(self, passwd, seed):
     """Scramble a password ready to send to MySQL"""
     hash4 = None
     try:
         hash1 = sha1(passwd).digest()
         hash2 = sha1(hash1).digest()  # Password as found in mysql.user()
         hash3 = sha1(seed + hash2).digest()
         xored = [
             utils.intread(h1) ^ utils.intread(h3)
             for (h1, h3) in zip(hash1, hash3)
         ]
         hash4 = struct.pack('20B', *xored)
     except Exception, err:
         raise errors.InterfaceError('Failed scrambling password; %s' % err)
示例#7
0
    def _parse_binary_values(self, fields, packet):
        """Parse values from a binary result packet"""
        null_bitmap_length = (len(fields) + 7 + 2) // 8
        null_bitmap = utils.intread(packet[0:null_bitmap_length])
        packet = packet[null_bitmap_length:]

        values = []
        for pos, field in enumerate(fields):
            if null_bitmap & 1 << (pos + 2):
                values.append(None)
                continue
            elif field[1] in (FieldType.TINY, FieldType.SHORT,
                              FieldType.INT24,
                              FieldType.LONG, FieldType.LONGLONG):
                (packet, value) = self._parse_binary_integer(packet, field)
                values.append(value)
            elif field[1] in (FieldType.DOUBLE, FieldType.FLOAT):
                (packet, value) = self._parse_binary_float(packet, field)
                values.append(value)
            elif field[1] in (FieldType.DATETIME, FieldType.DATE,
                              FieldType.TIMESTAMP):
                (packet, value) = self._parse_binary_timestamp(packet, field)
                values.append(value)
            elif field[1] == FieldType.TIME:
                (packet, value) = self._parse_binary_time(packet, field)
                values.append(value)
            else:
                (packet, value) = utils.read_lc_string(packet)
                values.append(value)

        return tuple(values)
示例#8
0
    def _parse_binary_values(self, fields, packet):
        """Parse values from a binary result packet"""
        null_bitmap_length = (len(fields) + 7 + 2) // 8
        null_bitmap = utils.intread(packet[0:null_bitmap_length])
        packet = packet[null_bitmap_length:]

        values = []
        for pos, field in enumerate(fields):
            if null_bitmap & 1 << (pos + 2):
                values.append(None)
                continue
            elif field[1] in (FieldType.TINY, FieldType.SHORT,
                              FieldType.INT24,
                              FieldType.LONG, FieldType.LONGLONG):
                (packet, value) = self._parse_binary_integer(packet, field)
                values.append(value)
            elif field[1] in (FieldType.DOUBLE, FieldType.FLOAT):
                (packet, value) = self._parse_binary_float(packet, field)
                values.append(value)
            elif field[1] in (FieldType.DATETIME, FieldType.DATE,
                              FieldType.TIMESTAMP):
                (packet, value) = self._parse_binary_timestamp(packet, field)
                values.append(value)
            elif field[1] == FieldType.TIME:
                (packet, value) = self._parse_binary_time(packet, field)
                values.append(value)
            else:
                (packet, value) = utils.read_lc_string(packet)
                values.append(value)

        return tuple(values)
示例#9
0
    def parse_handshake(self, packet):
        """Parse a MySQL Handshake-packet"""
        res = {}
        res['protocol'] = struct.unpack('<xxxxB', packet[0:5])[0]
        (packet,
         res['server_version_original']) = utils.read_string(packet[5:],
                                                             end=b'\x00')

        (res['server_threadid'], auth_data1, capabilities1, res['charset'],
         res['server_status'], capabilities2,
         auth_data_length) = struct.unpack('<I8sx2sBH2sBxxxxxxxxxx',
                                           packet[0:31])

        packet = packet[31:]

        capabilities = utils.intread(capabilities1 + capabilities2)
        auth_data2 = b''
        if capabilities & ClientFlag.SECURE_CONNECTION:
            size = min(13, auth_data_length - 8) if auth_data_length else 13
            auth_data2 = packet[0:size]
            packet = packet[size:]
            if auth_data2[-1] == 0:
                auth_data2 = auth_data2[:-1]

        if capabilities & ClientFlag.PLUGIN_AUTH:
            (packet, res['auth_plugin']) = utils.read_string(packet,
                                                             end=b'\x00')
            res['auth_plugin'] = res['auth_plugin'].decode('utf-8')
        else:
            res['auth_plugin'] = 'mysql_native_password'

        res['auth_data'] = auth_data1 + auth_data2
        res['capabilities'] = capabilities
        return res
示例#10
0
    def parse_handshake(self, packet):
        """Parse a MySQL Handshake-packet"""
        res = {}
        res['protocol'] = struct.unpack('<xxxxB', packet[0:5])[0]
        (packet, res['server_version_original']) = utils.read_string(
            packet[5:], end=b'\x00')

        (res['server_threadid'],
         auth_data1,
         capabilities1,
         res['charset'],
         res['server_status'],
         capabilities2,
         auth_data_length
         ) = struct.unpack('<I8sx2sBH2sBxxxxxxxxxx', packet[0:31])

        packet = packet[31:]

        capabilities = utils.intread(capabilities1 + capabilities2)
        auth_data2 = b''
        if capabilities & ClientFlag.SECURE_CONNECTION:
            size = min(13, auth_data_length - 8) if auth_data_length else 13
            auth_data2 = packet[0:size]
            packet = packet[size:]
            if auth_data2[-1] == 0:
                auth_data2 = auth_data2[:-1]

        if capabilities & ClientFlag.PLUGIN_AUTH:
            (packet, res['auth_plugin']) = utils.read_string(
                packet, end=b'\x00')
            res['auth_plugin'] = res['auth_plugin'].decode('utf-8')
        else:
            res['auth_plugin'] = 'mysql_native_password'

        res['auth_data'] = auth_data1 + auth_data2
        res['capabilities'] = capabilities
        return res
示例#11
0
    def parse_handshake(self, packet):
        """Parse a MySQL Handshake-packet"""
        res = {}
        res["protocol"] = struct.unpack("<xxxxB", packet[0:5])[0]
        (packet, res["server_version_original"]) = utils.read_string(packet[5:], end=b"\x00")

        (
            res["server_threadid"],
            auth_data1,
            capabilities1,
            res["charset"],
            res["server_status"],
            capabilities2,
            auth_data_length,
        ) = struct.unpack("<I8sx2sBH2sBxxxxxxxxxx", packet[0:31])

        packet = packet[31:]

        capabilities = utils.intread(capabilities1 + capabilities2)
        auth_data2 = b""
        if capabilities & ClientFlag.SECURE_CONNECTION:
            size = min(13, auth_data_length - 8) if auth_data_length else 13
            auth_data2 = packet[0:size]
            packet = packet[size:]
            if auth_data2[-1] == 0:
                auth_data2 = auth_data2[:-1]

        if capabilities & ClientFlag.PLUGIN_AUTH:
            (packet, res["auth_plugin"]) = utils.read_string(packet, end=b"\x00")
            res["auth_plugin"] = res["auth_plugin"].decode("utf-8")
        else:
            res["auth_plugin"] = "mysql_native_password"

        res["auth_data"] = auth_data1 + auth_data2
        res["capabilities"] = capabilities
        return res