# mysql — Port 3306 Handshakes, COM_QUERY SQL & Error Frames (`mysql`)

> **FrameAssembly v3.2.2** · Protocol 71 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/mysql

- **Full Protocol Name:** MySQL & MariaDB Wire Protocol v10
- **Keyword:** `mysql`
- **OSI Layer & Transport:** Layer 7 · TCP 3306
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** MySQL Client/Server Protocol v10
- **Wireshark Display Filter:** `mysql`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 3306 -> MySQL / MariaDB`

## Overview

Need to test a database activity monitor or IDS rule against `sql "SELECT * FROM users WHERE '1'='1';"` without spinning up a live MySQL instance? `mysql` handles the 24-bit little-endian packet length and sequence byte automatically.

MySQL and MariaDB (`mysql`) communicate over **TCP port `3306`** using a binary packet framing where every message begins with a **3-byte little-endian payload length** and a **1-byte packet sequence number** (`0`, `1`, `2`...). Unlike HTTP, the MySQL server speaks first immediately after the TCP handshake by sending a Protocol v10 `greeting` banner.

FrameAssembly encodes valid MySQL v10 `greeting` handshakes (`serverversion`), `login` (`HandshakeResponse41` carrying `user` and `db`), `COM_QUERY` (`0x03`, `query` + `sql`), `ok` (`0x00`), `err` (`0xff`, e.g. `errcode 1045` Access Denied), and `eof` (`0xfe`) packets.

## Minimal Quickstart (`mysql_quickstart.frasm`)

```frameassembly
host app_srv  { ip 10.50.1.20 }
host mysql_db { ip 10.50.2.33 }

compile {
    app_srv -> mysql_db tcp syn srcport 53306 dstport 3306 wait 1ms
    app_srv <- mysql_db tcp syn ack wait 1ms
    app_srv -> mysql_db tcp ack wait 1ms
    app_srv <- mysql_db mysql greeting serverversion "8.0.36" wait 2ms
    app_srv -> mysql_db mysql login user "app_rw" db "orders" wait 2ms
    app_srv <- mysql_db mysql ok
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `3306` (`psh ack`) and computes the 3-byte little-endian MySQL packet length automatically.
- Encodes NUL-terminated server version, thread ID, auth-plugin-data salt, and capability flags inside `greeting` so Wireshark recognizes the session as MySQL.
- Prefixes `sql` strings with `0x03` (`COM_QUERY`) on `query` frames and encodes little-endian 16-bit error numbers on `err` frames.

## Practical Lab Example: Blocked `root` Login (`errcode 1045`) + Authorized Application SQL Session (`mysql_dam_audit.frasm`)

Shows a rejected `root` login returning MySQL Error `1045`, followed by an authorized `app_ro` connection running a `SELECT` query.

```frameassembly
host web_node { ip 172.16.50.15 }
host db_main  { ip 172.16.60.10 }

compile {
    // 1. Unauthorized root login attempt -> MySQL Error 1045
    web_node -> db_main tcp syn srcport 54306 dstport 3306 win 65535 mss 1460 wait 1ms
    web_node <- db_main tcp syn ack win 65535 mss 1460 wait 1ms
    web_node -> db_main tcp ack wait 1ms
    web_node <- db_main mysql greeting serverversion "8.0.37-commercial" wait 2ms
    web_node -> db_main mysql login user "root" db "mysql" wait 2ms
    web_node <- db_main mysql err errcode 1045 wait 5ms
    // 2. Authorized query on a second connection
    web_node -> db_main tcp syn srcport 54307 dstport 3306 win 65535 mss 1460 wait 1ms
    web_node <- db_main tcp syn ack win 65535 mss 1460 wait 1ms
    web_node -> db_main tcp ack wait 1ms
    web_node <- db_main mysql greeting serverversion "8.0.37-commercial" wait 1ms
    web_node -> db_main mysql login user "app_ro" db "billing" wait 1ms
    web_node <- db_main mysql ok wait 2ms
    web_node -> db_main mysql query sql "SELECT invoice_id, total_cents FROM invoices WHERE paid = 0 LIMIT 25" wait 3ms
    web_node <- db_main mysql ok
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show mysql_dam_audit.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly mysql_dam_audit.frasm mysql_dam_audit.pcap
frameassembly mysql_dam_audit.frasm mysql_dam_audit.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse mysql_dam_audit.pcap mysql_dam_audit_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r mysql_dam_audit.pcap
```

## All `mysql` Keywords & Header Options

Packet line pattern: `src_host -> dst_host mysql [greeting | login | query | ok | err | eof] [serverversion "<text>"] [user "<text>"] [db "<text>"] [sql "<text>"] [errcode <0..65535>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `greeting \| login \| query \| ok \| err \| eof` | MySQL packet type keyword | `query (COM_QUERY 0x03)` | Selects Server Greeting v10 (`greeting`), Client HandshakeResponse41 (`login`), SQL Query (`query`), `0x00` OK (`ok`), `0xff` Error (`err`), or `0xfe` EOF (`eof`). |
| `serverversion "<text>"` | Quoted version banner on `greeting` | `"8.0.36"` | Sets the NUL-terminated server version string in `greeting` packets. |
| `user "<text>" \| db "<text>"` | Quoted MySQL username and schema name on `login` | `user "root", db ""` | Populates username and initial database inside `login` packets. |
| `sql "<text>"` | Quoted SQL statement on `query` | `"SELECT 1"` | Writes the SQL query string following `COM_QUERY` (`0x03`). |
| `errcode <0..65535>` | 16-bit MySQL error code (`1045` Access denied, `1049` Unknown DB, `1064` Syntax error) | `1045 on err` | Sets the 2-byte little-endian error code in `err` (`0xff`) packets. |

## Cross-Layer Options Valid with `mysql`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 3306` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Include Server greeting First for Full Wireshark Conversation State

- **Valid v3.2.2 Syntax:** `app_srv <- mysql_db mysql greeting serverversion "8.0.36"`

Because MySQL is server-speaks-first, emitting `mysql greeting` from the server before `mysql login` helps Wireshark and Zeek classify the handshake phase accurately.
