In the previous article we researched MySQL Client / Server Protocol using WireShark. Now lets start to write our code in python to simulate MySQL native client. Final codes are here: Github repo
First of all we have to create MYSQL_PACKAGE class. MYSQL_PACKAGE class is the parent of all other package classes (HANDSHAKE_PACKAGE, LOGIN_PACKAGE, OK_PACKAGE and etc.)
It accepts resp parameter on initialization. Resp is the binary response received from the server in bytesarray type. One of the important and interesting method of this class is next method.
bytes1 = sha1(password.encode(“utf-8”)).digest()
concat1 = salt.encode(‘utf-8’)
concat2 = sha1(sha1(password.encode(“utf-8”)).digest()).digest()
bytes2 = bytearray()bytes2.extend(concat1)bytes2.extend(concat2)
bytes2 = sha1(bytes2).digest()
hash=bytearray(x ^ y for x, y in zip(bytes1, bytes2))return hash
CLASSES FOR DATATYPES
In the previous article we’ve learned about Int and String data types of MySQL Client / Server protocol. Now we need some classes to be able to read fields from received packets.
HANDSHAKE_PACKAGE class is used for parsing the Greeting Packet received from server. It is inherited from MYSQL_PACKAGE class and accepts resp parameter on initialization. Parameter resp is the Greeting Packet response in bytes type recieved from the server.
This class is used for create Login Request packet.
OK package and ERR package are the response package of server after authentication or after sending query to server on command phase.
MYSQL class is the wrapper class which creates TCP connection with server, sends and receives packages from server using above classes.
resp =)return HANDSHAKE_PACKAGE(resp)
If you’re a brilliant developer looking for remote software jobs, Turing may be able to help you very quickly. Head over to the Jobs page to know more!
Join a network of the world's best developers and get long-term remote software jobs with better compensation and career growth.