Hacks

MySQL Client / Server Protocol Using Python & Wireshark: P1
Developers Corner Hacks Languages, frameworks, tools, and trends

Understanding MySQL Client/Server Protocol Using Python Wireshark

This blog explains what the MySQL Client / Server protocol is all about, according to the official MySQL documentation.

MySQL Client / Server protocol is used in many areas. For example:

  • MySQL Connectors like ConnectorC, ConnectorJ and etc.
  • MySQL proxy
  • Between master and slave

What is MySQL Client / Server protocol?

MySQL Client / Server protocol is accepted conventions (rules). Through these rules client and server “talks” and understand each other. Client connects to server through TCP connection with special socket, sends to server special packets and accepts them from server. There are two phases of this connection:

  • Connection phase
  • Command phase

Next illustration describes phases:

STRUCTURE OF PACKETS

Each packet consists of valuable data types. Maximum length of each packet can be 16MB. If the length of packet is more than 16MB, then it is separated into several chunks (16MB). First of all let’s see the protocol data types. MySQL Client / Server protocol has two data types:

  • Integer types
  • String types

(See the official documentation: https://dev.mysql.com/doc/internals/en/basic-types.html)

INTEGER TYPES

Integer types also separates into two section:

  • Fixed length integer types
  • Length-encoded integer types

Fixed length integer type consumes 1, 2, 3, 4, 6 or 8 bytes. For example if we want to describe number 2 in int<3> data type then we can write it like this in hex format: 02 00 00. Or if we want to describe number 2 in int<2> then we can write it like this in hex format: 02 00

Length-encoded integer types consumes 1, 3, 4 or 9 bytes. Before length-encoded integer types comes 1 byte. To detect the length of integer we have to check that first byte.

  • If the first byte is less than 0xfb ( < 251 ) then next one byte is valuable (it is stored as a 1-byte integer)
  • If the first byte is equal to 0xfc ( == 252 ) then it is stored as a 2-byte integer
  • If the first byte is equal to 0xfd ( == 253 ) then it is stored as a 3-byte integer
  • If the first byte is equal to 0xfe ( == 254 ) then it is stored as a 8-byte integer

But if the first byte is equal to 0xfb there is no need to read next bytes, it is equal to the NULL value of MySQL, and if equal to 0xff it means that it is undefined.

For example to convert fd 03 00 00 … into normal integer we have to read first byte and it is 0xfd. According to the above rules we have to read next 3 bytes and convert it into normal integer, and its value is 2 in decimal number system. So value of length-encoded integer data type is 2.

STRING TYPES

String types also separates into several sections.

  • String – Fixed-length string types. They have a known, hardcoded length
  • String – Null terminated string types. These strings end with 0x00 byte
  • String – Variable length string types. Before such strings comes fixed-length integer type. According to that integer we can calculate actual length of string
  • String – Length-encoded string types. Before such strings comes length-encoded integer type. According to that integer we can calculate actual length of string
  • String – If a string is the last component of a packet, its length can be calculated from the overall packet length minus the current position

SNIFF WITH WIRESHARK

Let’s start wireshark to sniff the network, filter MySQL packets by ip (in my case server ip is 54.235.111.67). Then let’s try to connect to MySQL server by MySQL native client on our local machine.

>> mysql -u[username] -p[password] -h[host ip] -P3306

As you can see after TCP connection to the server we several MySQL packets from the server. First of them is greeting packet.

picture1

Let’s dig into this packet and describe each field.

First 3 bytes are packet length:

picture2

Next 1 byte is packet number:

picture3

Rest of bytes are payload of Greeting packet of MySQL Client / Server protocol

picture4

Let’s describe each field of greeting packet.

  • Protocol number – Int<1>
  • Server version – String
  • Thread id – Int<4>
  • Salt1 – String
  • Server capabilities – Int<2>
  • Server language – Int<1>
  • Server Status – Int<2>
  • Extended Server Capabilities – Int<2>
  • Authentication plugin length – Int<1>
  • Reserved bytes – 10 bytes
  • Salt2 – String
  • Authentication plugin string – String

Server language is integer, next table will help us to pick appropriate language by integer value:

In my case server language is 0x08 (in decimal number system it is 8 also). From above table we can see that equivalent of 8 is latin1_swedish_ci. Now we know that default language of server is latin1_swedish_ci.

Server capabilities and server status are also integers. But reading each BIT of these integers we can know about server capabilities and status. Next illustration describes server capability and status bits:

Using greeting packet client prepares Login Request Packet to send to the server for authentication. Now let’s research login request packet.

picture5

  • First 3 bytes describes payload length
  • Next 1 byte is packet number
  • Client capabilities – Int<2> / Same as Server capabilities
  • Extended client capabilities – Int<2> / Same as Server extended capabilities
  • Max packet – Int<4> / describes the maximum length of packet
  • Charset – Int<1> / in my case it is 0x21 (in decimal number system is 33), from the table we can see that it is utf8_general_ci. We set server’s default charset from latin1_swedish_ci to utf8_general_ci
  • Username – String
  • Password – String
  • Client Auth Plugin string – String

As you can see the password is encrypted. To encrypt a password we will use sha1, md5 algorithms, also salt1 and salt2 strings from previous Greeting Packet sent from server.

Then we get OK packet from the server if we are authenticated successfully. Otherwise we would get ERR packet.

picture6.png

  • 3 bytes are packet length
  • 1 byte is packet number
  • Affected rows – Int<1>
  • Server status – Int<2>
  • Warnings – Int<2>

That’s all. We have finished the theory. Now it’s time to start the practical part. In the second part of this article we will write our own MySQL native client from scratch using no external module or library.

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.

Apply for Jobs

By Oct 2, 2020
Smart Slack hacks ultimate guide
COVID-19 For Employers Hacks Pro Tips Remote First How-to Remote Now RemoteFocusedTools Suddenly Remote

The Ultimate Guide to Slack Hacks

Slack is one of the most popular communication tools for remote teams. With the ability to coordinate conversations by a specific theme, within a team or company-wide, there is great power in this platform. Long popular with remote teams, here are some tips and tricks that you might have missed. Use these to make the most of your time and communication efforts.

Slack is one of the most popular communication tools for remote teams. With the ability to coordinate conversations by a specific theme, within a team or company-wide, there is great power in this platform.

Long popular with remote teams, here are some tips and tricks that you might have missed.

Use these to make the most of your time and communication efforts.

We will cover:

Notification Management

Pin important messages

Keyboard shortcuts

Polls and surveys

Star items = quick list

Slackbot Reminders

Include posts

Keyword Notifications

Find mentions quickly

Search Smarter

Quick link to previous posts

Bookmark location in a channel

Formatting for Impact

Deep Work Time – DND

Customized Emojis

Integrate! Apps, bots, and workflows

Notification Management

Pin important messages

    • You might want to pin the most current update of the project you are working on or your guide to using the channel. You can pin up to 100 messages per channel, but keeping it to crucial information is better.

Keyboard shortcuts

    • You can use keyboard shortcuts to navigate around the Workspace, mark a message as read or unread, format your messages, and more. Saving time and giving flow and process to your time in Slack.
    • When all the pictures and GIFs are making it hard to follow the conversation type “/collapse” into your text box, which will leave text only within the chat and reverse this type “/expand.”
    • To see the list of shortcuts hit “ctrl” and “/.”

Notification Management

    • You may not need notifications for all activity within a channel. Adjust your notification requirements by selecting the settings icon (cog) at the top of the channel on desktop or using the drop-down menu (3 dots) on mobile, then settings, then notifications/notification preferences.

Polls and surveys

    • Want to get some quick feedback from the team? Or trying to decide on meeting times? Use the command “/poll” and follow the instructions to call up the “Polly” app. Both powerful and fun.

Star items = quick list

    • Click “Show starred items” or use the keyboard shortcut “crtl_shift+s” for a quick list of all your starred messages. This hack can be useful to quickly review essential items and help you to come back to something you might not have time to handle at the moment.

Slackbot Reminders

    • Use the “/remind” command to give you a personal reminder, or to remind someone else on your team. An example is “/remind @Jeff of meeting at 2 pm”, your reminder will be sent at 2 pm to Jeff about your upcoming meeting.

Include posts

    • Where you need to share longer-form content, you can include posts in your Slack messages. You might want to share project plans, or quickly link to your code of conduct, vision, and mission statements.
    • Create a post – (On your desktop). Select the attachment icon (paperclip) → “Create new” → “Post.” From here, you can share your post, edit it, allow others to edit it, format the article, and add images to it—further instructions via the Slack help section

Keyword Notifications

    • While you will get notifications if you are tagged or mentioned, it can be useful to get a notification. Likewise, when someone uses a specific keyword. Go to preferences, then notifications to set up your desired keywords. You will receive the notification, and the channel will display a badge.

Find mentions quickly

    • You can use the @ symbol on the top right on desktop to show, or hide, mentions if you are looking for a recent conversation (or you’re drowning in notifications). Or use the keyboard shortcut “ctrl” + ”shift” + ”m” to show and hide your mentions.

Search Smarter

    • Use modifiers that help you to search smarter. “From:user”, “in:channel”, “during:March”, or “has:emoji-code”, this one is particularly useful if you follow a specific process of emoji usage within your Workspace. You can find a complete list of search modifiers here.

Quick link to previous posts

    • Using the timestamp (time beside your name above a post), you can copy this as a link and paste into a new post, bringing old posts back into the conversation quickly.

Bookmark a location in a channel

    • Some channels require more attention from us than others, if you need to read everything in the channel then use “Alt+click” on the message where you are leaving the channel, it will mark all subsequent messages as unread. On mobile, this should be a long press/hold on the message.

Formatting for Impact

    • Need certain parts of a post to stand out? Then use formatting. E.g., using * either side of a word will embolden it i.e., *here* now becomes here. Or traditional keyboard shortcuts also work. You can also add formatting like inline code or block quotes, very useful to help faster communication of crucial information. More on formatting your messages from Slack.

Deep Work Time – DND

    • DND – You can type in “/dnd” into the text box and then set a time, or use the bell icon to the top left and select a time frame. Stopping you from receiving any sounds or notifications in the time frame you specify, and your co-workers will see a sleeping symbol beside your name.

Customized Emojis

    • Emojis can be useful to speed up communication within your team or to help strengthen branding and team identity. You can add the custom emojis from the drop-down menu on the top left and choose “Customize Slack.” Admins have the power to limit who can edit these.

Integrate! Apps, bots, and workflows

      • Slack supports integrations with 100s of apps. Here is a list of some of the most popular ones. See the Slack help center for details on each and a full list of apps available, or click here 
      • Zoom for SlackStart a meeting, join a meeting and make a call with Zoom phone. Remember, you must register your Slack and Zoom accounts with the same email address.
      • Trello for SlackManage cards on your boards from Slack, add conversations to cards, and update due dates.
      • Asana for SlackCreate new tasks and turn your conversations in Slack into to-dos in Asana
      • Salesforce for SlackSearch Salesforce records from Slack, configure Salesforce alerts for Slack. Send Slack messages to Salesforce, or send Salesforce records to Slack.
      • Twitter for SlackYou can set up mentions on Twitter to feed into a channel that the team can keep an eye on. You can also follow similar businesses or your suppliers.
      • Google Calendar for SlackView your full schedule and have your status update when you are scheduled in a meeting.
      • Donut for SlackDonut helps to improve team-building; there are prebuilt connection programs in Donut, one of the most popular functions is virtual random coffees. This helps to encourage cross-organizational bonding.
      • Loom for SlackMention @person or #channel directly in your Loom videos, and they can watch the clip without needing to leave Slack.
      • Polly for SlackThis Polls and surveys bot lets you get quick feedback. You can also use pulse surveys directly in Slack.
      • Greetbot for SlackAn onboarding assistant for Slack. You can use this to customize welcome messages and schedule follow-ups.
      • You can also create custom integrations, build your bot, create automated workflows, and much more, read more on this via the Slack help center here.

 

Do you have a workflow or trick that works well for you in Slack? Then please share it with us over on Twitter @Turingcom.

Tell us the skills you need and we'll find the best developer for you in days, not weeks.

Hire Developers

By Mar 31, 2020