Exemplo n.º 1
0
    def parse_handshake_response(packet):
        """Parse the handshake response packet and returns the information.

        :param packet: Packet received as received from socket without header.
        :return: Dictionary containing the handshake information; empty if
                 there was an error.
        :rtype: dict
        """
        try:
            (capabilities, max_allowed_packet,
             charset) = struct.unpack_from("<IIB23x", buffer(packet[0:32]))
            buf = packet[32:]
            (buf, username) = utils.read_string(buf, end='\x00')
            (buf, auth_data) = utils.read_lc_string(buf)
            if auth_data and auth_data[-1] == '\x00':
                auth_data = auth_data[:-1]
            if capabilities & ClientFlag.CONNECT_WITH_DB and buf:
                (buf, database) = utils.read_string(buf, end='\x00')
        except Exception as exc:
            _LOGGER.debug("Failed parsing handshake: %s", str(exc))
            return {}

        return {
            'capabilities': capabilities,
            'max_allowed_packet': max_allowed_packet,
            'charset': charset,
            'username': str(username),
            'auth_data': auth_data,
        }
Exemplo n.º 2
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
Exemplo n.º 3
0
    def parse_handshake_response(packet):
        """Parse the handshake response packet and returns the information.

        :param packet: Packet received as received from socket without header.
        :return: Dictionary containing the handshake information; empty if
                 there was an error.
        :rtype: dict
        """
        try:
            (capabilities, max_allowed_packet, charset) = struct.unpack_from(
                "<IIB23x", buffer(packet[0:32]))
            buf = packet[32:]
            (buf, username) = utils.read_string(buf, end='\x00')
            (buf, auth_data) = utils.read_lc_string(buf)
            if auth_data and auth_data[-1] == '\x00':
                    auth_data = auth_data[:-1]
            if capabilities & ClientFlag.CONNECT_WITH_DB and buf:
                (buf, database) = utils.read_string(buf, end='\x00')
        except Exception as exc:
            _LOGGER.debug("Failed parsing handshake: %s", str(exc))
            return {}

        return {
            'capabilities': capabilities,
            'max_allowed_packet': max_allowed_packet,
            'charset': charset,
            'username': str(username),
            'auth_data': auth_data,
        }
Exemplo n.º 4
0
    def test_read_string_1(self):
        """Read a string from a buffer up until a certain character."""
        buf = 'abcdef\x00ghijklm'
        exp = 'abcdef'
        exprest = 'ghijklm'
        end = '\x00'

        (rest, result) = utils.read_string(buf, end=end)
        self.assertEqual(exp, result)
        self.assertEqual(exprest, rest)
Exemplo n.º 5
0
    def test_read_string_1(self):
        """Read a string from a buffer up until a certain character."""
        buf = 'abcdef\x00ghijklm'
        exp = 'abcdef'
        exprest = 'ghijklm'
        end = '\x00'

        (rest, result) = utils.read_string(buf, end=end)
        self.assertEqual(exp, result)
        self.assertEqual(exprest, rest)
Exemplo n.º 6
0
    def test_read_string_1(self):
        """Read a string from a buffer up until a certain character."""
        buf = bytearray(b'abcdef\x00ghijklm')
        exp = bytearray(b'abcdef')
        exprest = bytearray(b'ghijklm')
        end = bytearray(b'\x00')

        (rest, result) = utils.read_string(buf, end=end)
        self.assertEqual(exp, result)
        self.assertEqual(exprest, rest)
Exemplo n.º 7
0
    def test_read_string_1(self):
        """Read a string from a buffer up until a certain character."""
        buf = bytearray(b'abcdef\x00ghijklm')
        exp = bytearray(b'abcdef')
        exprest = bytearray(b'ghijklm')
        end = bytearray(b'\x00')

        (rest, result) = utils.read_string(buf, end=end)
        self.assertEqual(exp, result)
        self.assertEqual(exprest, rest)
Exemplo n.º 8
0
    def test_read_string_2(self):
        """Read a string from a buffer up until a certain size."""
        buf = bytearray(b'abcdefghijklm')
        exp = bytearray(b'abcdef')
        exprest = bytearray(b'ghijklm')
        size = 6

        (rest, result) = utils.read_string(buf, size=size)
        self.assertEqual(exp, result)
        self.assertEqual(exprest, rest)
Exemplo n.º 9
0
    def test_read_string_2(self):
        """Read a string from a buffer up until a certain size."""
        buf = bytearray(b'abcdefghijklm')
        exp = bytearray(b'abcdef')
        exprest = bytearray(b'ghijklm')
        size = 6

        (rest, result) = utils.read_string(buf, size=size)
        self.assertEqual(exp, result)
        self.assertEqual(exprest, rest)
Exemplo n.º 10
0
    def parse_auth_switch_request(self, packet):
        """Parse a MySQL AuthSwitchRequest-packet"""
        if not packet[4] == 254:
            raise errors.InterfaceError("Failed parsing AuthSwitchRequest packet")

        (packet, plugin_name) = utils.read_string(packet[5:], end=b"\x00")
        if packet[-1] == 0:
            packet = packet[:-1]

        return plugin_name.decode("utf8"), packet
Exemplo n.º 11
0
    def parse_auth_switch_request(self, packet):
        """Parse a MySQL AuthSwitchRequest-packet"""
        if not packet[4] == '\xfe':
            raise errors.InterfaceError(
                "Failed parsing AuthSwitchRequest packet")

        (packet, plugin_name) = utils.read_string(packet[5:], end='\x00')
        if packet[-1] == '\x00':
            packet = packet[:-1]

        return plugin_name, packet
Exemplo n.º 12
0
    def parse_auth_switch_request(self, packet):
        """Parse a MySQL AuthSwitchRequest-packet"""
        if not packet[4] == '\xfe':
            raise errors.InterfaceError(
                "Failed parsing AuthSwitchRequest packet")

        (packet, plugin_name) = utils.read_string(packet[5:], end='\x00')
        if packet[-1] == '\x00':
            packet = packet[:-1]

        return plugin_name, packet
Exemplo n.º 13
0
 def test_read_string_2(self):
     """Read a string from a buffer up until a certain size."""
     buf = 'abcdefghijklm'
     exp = 'abcdef'
     exprest = 'ghijklm'
     size = 6
     
     (rest, result) = utils.read_string(buf, size=size)
     if result != exp:
         self.fail("Wrong result. Expected '%s', got '%s'" %\
             exp, result)
     elif rest != exprest:
         self.fail("Wrong result. Expected '%s', got '%s'" %\
             exp, result)
Exemplo n.º 14
0
 def test_read_string_1(self):
     """Read a string from a buffer up until a certain character."""
     buf = 'abcdef\x00ghijklm'
     exp = 'abcdef'
     exprest = 'ghijklm'
     end = '\x00'
     
     (rest, result) = utils.read_string(buf, end=end)
     if result != exp:
         self.fail("Wrong result. Expected '%s', got '%s'" %\
             exp, result)
     elif rest != exprest:
         self.fail("Wrong result. Expected '%s', got '%s'" %\
             exp, result)
Exemplo n.º 15
0
    def test_read_string_2(self):
        """Read a string from a buffer up until a certain size."""
        buf = b'abcdefghijklm'
        exp = b'abcdef'
        exprest = b'ghijklm'
        size = 6

        (rest, result) = utils.read_string(buf, size=size)
        if result != exp:
            self.fail("Wrong result. Expected '%s', got '%s'" %\
                exp, result)
        elif rest != exprest:
            self.fail("Wrong result. Expected '%s', got '%s'" %\
                exp, result)
Exemplo n.º 16
0
    def test_read_string_1(self):
        """Read a string from a buffer up until a certain character."""
        buf = b'abcdef\x00ghijklm'
        exp = b'abcdef'
        exprest = b'ghijklm'
        end = b'\x00'

        (rest, result) = utils.read_string(buf, end=end)
        if result != exp:
            self.fail("Wrong result. Expected '%s', got '%s'" %\
                exp, result)
        elif rest != exprest:
            self.fail("Wrong result. Expected '%s', got '%s'" %\
                exp, result)
Exemplo n.º 17
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
Exemplo n.º 18
0
 def parse_handshake(self, packet):
     """Parse a MySQL Handshake-packet"""
     res = {}
     (packet, res["protocol"]) = utils.read_int(packet[4:], 1)
     (packet, res["server_version_original"]) = utils.read_string(packet, end=b"\x00")
     (packet, res["server_threadid"]) = utils.read_int(packet, 4)
     (packet, res["scramble"]) = utils.read_bytes(packet, 8)
     packet = packet[1:]  # Filler 1 * \x00
     (packet, res["capabilities"]) = utils.read_int(packet, 2)
     (packet, res["charset"]) = utils.read_int(packet, 1)
     (packet, res["server_status"]) = utils.read_int(packet, 2)
     packet = packet[13:]  # Filler 13 * \x00
     (packet, scramble_next) = utils.read_bytes(packet, 12)
     res["scramble"] += scramble_next
     return res
Exemplo n.º 19
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
Exemplo n.º 20
0
 def parse_handshake(self, packet):
     """Parse a MySQL Handshake-packet"""
     res = {}
     (packet, res['protocol']) = utils.read_int(packet[4:], 1)
     (packet, res['server_version_original']) = utils.read_string(
         packet, end='\x00')
     (packet, res['server_threadid']) = utils.read_int(packet, 4)
     (packet, res['scramble']) = utils.read_bytes(packet, 8)
     packet = packet[1:]  # Filler 1 * \x00
     (packet, res['capabilities']) = utils.read_int(packet, 2)
     (packet, res['charset']) = utils.read_int(packet, 1)
     (packet, res['server_status']) = utils.read_int(packet, 2)
     packet = packet[13:]  # Filler 13 * \x00
     (packet, scramble_next) = utils.read_bytes(packet, 12)
     res['scramble'] += scramble_next
     return res
Exemplo n.º 21
0
 def parse_handshake(self, packet):
     """Parse a MySQL Handshake-packet"""
     res = {}
     (packet, res['protocol']) = utils.read_int(packet[4:], 1)
     (packet, res['server_version_original']) = utils.read_string(
         packet, end='\x00')
     (packet, res['server_threadid']) = utils.read_int(packet, 4)
     (packet, res['scramble']) = utils.read_bytes(packet, 8)
     packet = packet[1:]  # Filler 1 * \x00
     (packet, res['capabilities']) = utils.read_int(packet, 2)
     (packet, res['charset']) = utils.read_int(packet, 1)
     (packet, res['server_status']) = utils.read_int(packet, 2)
     packet = packet[13:]  # Filler 13 * \x00
     (packet, scramble_next) = utils.read_bytes(packet, 12)
     res['scramble'] += scramble_next
     return res