Modbus function codes explained: which ones read, write or reprogram?
AdminKit's short answer: every Modbus request carries a one-byte function code that says what the device should do. Codes 1 to 4 read data, 5, 6, 15, 16, 22 and 23 write it, and a few, such as 8 (Diagnostics) and vendor programming codes, change how the device behaves. Knowing them lets you monitor and restrict Modbus traffic.
By AdminKit team, Netbase JSC · Updated
Key takeaways
- A Modbus request is a one-byte function code plus data. The code alone tells you whether a request reads, writes, diagnoses or reprograms.
- Reads: 1, 2, 3, 4 (plus 20 and 24). Writes: 5, 6, 15, 16, 21, 22, 23. Treat 8 (Diagnostics) and vendor programming or firmware codes such as 90 and 125 as state-changing too.
- Modbus has no authentication, so the function code is the most useful thing a monitor or industrial firewall can check after the source address.
- Start by logging which codes each host actually sends; then alert on, or block, everything outside that list.
What is a Modbus function code?
Modbus is a request-and-response protocol: a client, often an HMI, SCADA server or historian, asks a device such as a PLC, drive or energy meter to do one thing, and the device answers. The “one thing” is encoded in a single byte at the start of the request, the function code. Everything after it, such as a start address, a quantity of items or the values to write, is interpreted according to that code.
On Modbus TCP, each request starts with a seven-byte MBAP header. Wireshark’s Modbus/TCP display filter reference names its four fields: transaction identifier (mbtcp.trans_id), protocol identifier (mbtcp.prot_id), length (mbtcp.len) and unit identifier (mbtcp.unit_id). The function code is the next byte, the eighth of the TCP payload, followed by the function’s own data.
| Bytes | Field | Example (Read Holding Registers) | Notes |
|---|---|---|---|
| 1–2 | Transaction ID | 00 01 | Lets the client match responses to requests |
| 3–4 | Protocol ID | 00 00 | Always 0 for Modbus |
| 5–6 | Length | 00 06 | Bytes that follow, including the unit ID |
| 7 | Unit ID | 01 | Selects a device behind a gateway |
| 8 | Function code | 03 | What the device should do |
| 9–12 | Function data | 00 6B 00 03 | Here: start address 107, read 3 registers |
None of these bytes carry a user, a password or a signature. A device that receives a well-formed request on port 502 has no protocol-level way to know who sent it, which is why the function code matters so much for defenders: after the source address, it is the most meaningful thing you can inspect.
Which data does each function code touch?
Modbus models a device as four tables. The Suricata Modbus keyword documentation refers to the same four as “Discretes Input, Coils, Input Registers and Holding Registers”, and only allows write matching on coils and holding registers, because those are the two tables a client can change.
| Table | Size of one item | Client access | Typical contents |
|---|---|---|---|
| Discrete inputs | 1 bit | Read only | Limit switches, alarms, digital sensor states |
| Coils | 1 bit | Read and write | Relay outputs, start/stop, enable flags |
| Input registers | 16 bits | Read only | Measurements such as temperature, flow or voltage |
| Holding registers | 16 bits | Read and write | Setpoints, configuration, counters, mode selections |
The last row is the dangerous one. A holding register might hold a pump’s target pressure or a heating controller’s setpoint, and a single write changes it. MITRE ATT&CK for ICS lists this behaviour as T0836 Modify Parameter, and cites FrostyGoop, malware that “allows for the modification of system settings by reading and writing to registers via Modbus commands”, as a documented example.
Which function codes read and which write?
The table below uses the function names from Zeek’s Modbus constants, which is also how they appear in Zeek’s modbus.log. It covers the public codes you are most likely to see on a real network.
| Code | Hex | Zeek name | Action | Table |
|---|---|---|---|---|
| 1 | 0x01 | READ_COILS | Read | Coils |
| 2 | 0x02 | READ_DISCRETE_INPUTS | Read | Discrete inputs |
| 3 | 0x03 | READ_HOLDING_REGISTERS | Read | Holding registers |
| 4 | 0x04 | READ_INPUT_REGISTERS | Read | Input registers |
| 5 | 0x05 | WRITE_SINGLE_COIL | Write | Coils |
| 6 | 0x06 | WRITE_SINGLE_REGISTER | Write | Holding registers |
| 15 | 0x0F | WRITE_MULTIPLE_COILS | Write | Coils |
| 16 | 0x10 | WRITE_MULTIPLE_REGISTERS | Write | Holding registers |
| 20 | 0x14 | READ_FILE_RECORD | Read | File records |
| 21 | 0x15 | WRITE_FILE_RECORD | Write | File records |
| 22 | 0x16 | MASK_WRITE_REGISTER | Write (AND/OR mask) | Holding registers |
| 23 | 0x17 | READ_WRITE_MULTIPLE_REGISTERS | Write, then read | Holding registers |
| 24 | 0x18 | READ_FIFO_QUEUE | Read | Holding registers |
Two codes in that list are easy to misclassify. Mask Write Register (22) sounds like a read-modify operation but changes the register. Read/Write Multiple Registers (23) performs a write and a read in one request, so an allowlist that permits “reads” must not let 23 through by accident.
In most plants the everyday traffic is a historian or HMI polling with 3 and 4, and perhaps 1 and 2. Writes normally come from a short list of hosts, such as the HMI when an operator changes a setpoint and the engineering workstation during maintenance. That pattern is what makes function-code monitoring practical.
Which codes diagnose, identify or reprogram a device?
Beyond reads and writes, the specification and vendors define codes that report on, reset or reprogram the device. They are rare in normal operation, so they are exactly the codes worth noticing.
| Code | Hex | Zeek name | What it does | Why it matters |
|---|---|---|---|---|
| 7 | 0x07 | READ_EXCEPTION_STATUS | Reads a status byte (serial devices) | Low risk, but unusual on TCP |
| 8 | 0x08 | DIAGNOSTICS | Sub-functions for counters, loopback and communication control | Some sub-functions, such as Force Listen Only Mode, take a device off the network |
| 11 | 0x0B | GET_COMM_EVENT_COUNTER | Reads a communication event counter | Reconnaissance |
| 12 | 0x0C | GET_COMM_EVENT_LOG | Reads the communication event log | Reconnaissance |
| 17 | 0x11 | REPORT_SLAVE_ID | Returns a device-specific identifier | Fingerprinting |
| 43 | 0x2B | ENCAP_INTERFACE_TRANSPORT | Encapsulated interface; MEI type 14 is Read Device Identification | Vendor, product and revision without authentication |
| 90 | 0x5A | PROGRAM_UNITY | Vendor programming function | Programming traffic can change the logic a device runs |
| 125 | 0x7D | FIRMWARE_REPLACEMENT | Vendor function named for firmware replacement | Should never appear outside a maintenance window |
Wireshark’s Modbus display filter reference has dedicated fields for these, such as modbus.diagnostic_code for the Diagnostics sub-function and modbus.mei plus modbus.read_device_id for Read Device Identification, so you can filter captures on them precisely.
The Modbus specification also reserves ranges of codes for user-defined functions (65 to 72 and 100 to 110) that vendors may use however they like. Suricata’s modbus keyword can match whole categories, such as function user or function reserved, which is useful when you do not know every vendor code in your plant but want to see any of them.
What does an exception response look like?
When a device cannot carry out a request, it answers with the original function code plus 128 (hex 0x80) and a one-byte exception code. A rejected Read Holding Registers request (3) comes back as function 131 (0x83); a rejected Write Single Register (6) comes back as 134 (0x86).
Request: 00 07 00 00 00 06 01 06 00 64 01 F4 unit 1, function 6, write 500 to register 100
Response: 00 07 00 00 00 03 01 86 02 unit 1, function 0x86 = 6 + 0x80, exception 2
Zeek’s constants list the standard exception codes by name. The three you will see most:
- 1, ILLEGAL_FUNCTION: the device does not support that function code.
- 2, ILLEGAL_DATA_ADDRESS: the address or address range does not exist on the device.
- 3, ILLEGAL_DATA_VALUE: a value in the request is not acceptable.
Codes 10 and 11 (GATEWAY_PATH_UNAVAILABLE and GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND) come from gateways that forward Modbus TCP to serial devices. A burst of exception 1 or 2 responses from one device usually means someone is probing which functions and addresses it supports.
How do you see which function codes your network uses?
Before you restrict anything, find out what normal looks like. Zeek’s Modbus analyzer writes one line per request and response to modbus.log, and its main script documentation lists the fields: tid, unit, func, pdu_type (REQ or RESP) and exception, alongside the connection details. Feed it from a mirror port on the OT switch, then count function names per client:
$ zeek-cut id.orig_h func pdu_type < modbus.log | grep REQ | sort | uniq -c | sort -rn
48211 10.20.0.15 READ_HOLDING_REGISTERS REQ
9120 10.20.0.15 READ_INPUT_REGISTERS REQ
37 10.20.0.10 WRITE_SINGLE_REGISTER REQ
2 10.10.4.88 WRITE_MULTIPLE_REGISTERS REQ
The output above is example data. The first three lines are a historian polling and an HMI making occasional setpoint changes; the last is a write from an office-side address that should never send one. In a Wireshark capture, the filter modbus.func_code in {5 6 15 16 21 22 23} shows only write requests.
How do you alert on or restrict write function codes?
Once you know which hosts legitimately write, you can act on everything else. A plain port filter cannot tell reads from writes, because both travel on TCP 502, so this needs a device or sensor that parses Modbus. With Suricata, whose Modbus parser has to be enabled in the app-layer section of suricata.yaml, rules like these follow the keyword syntax in its documentation (example addresses):
alert modbus !10.20.0.10 any -> 10.30.0.0/24 502 (msg:"Modbus write from a host not on the writer list"; modbus: access write; sid:1000101; rev:1;)
alert modbus any any -> 10.30.0.0/24 502 (msg:"Modbus Diagnostics function"; modbus: function 8; sid:1000102; rev:1;)
alert modbus any any -> 10.30.0.0/24 502 (msg:"Modbus user-defined or reserved function"; modbus: function user; sid:1000103; rev:1;)
Suricata’s documentation notes that addresses in the access keyword start at 1, so add 1 if your device documentation numbers registers from 0. Run rules in alert mode first and review the hits for a while; blocking an unexpected but legitimate write can stop a process as surely as an attack.
Function-code filtering sits on top of network controls, not instead of them. NIST SP 800-82 treats segmentation and controlled access paths as the core defence for OT protocols that cannot authenticate, and the Modbus TCP remediation playbook walks through that work step by step, from removing internet exposure to monitoring.
What does AdminKit’s Modbus check send?
AdminKit’s Modbus exposure check tests whether a Modbus TCP device on the public IP you are connecting from accepts a connection on port 502, by default. It sends exactly one Modbus request: function 43 with MEI type 14, Read Device Identification, which asks for vendor name, product code and revision. It never sends a read or write for any of the four data tables in this guide.
If the device answers with an exception 1 instead, it does not support identification, but the result is the same in practice: anyone on the internet could send it any function code on this page, including the writes. The fix is to remove the path to port 502, as described in is Modbus TCP (port 502) exposed?, not to rely on the device refusing particular codes.
Where does this fit with other machine protocols?
Modbus is one of several protocols in AdminKit’s machine protocols cluster that carry commands with no built-in authentication, and the Modbus TCP glossary entry summarises its history. The same approach of learning what normal traffic looks like, then flagging the rare commands that change state, applies to the other industrial protocols covered there.
Defensive use only
Inspect and test Modbus traffic only on equipment you own or are explicitly authorised to work on. Never send function codes to industrial devices on networks you do not control. AdminKit’s active checks only ever run against the IP address making the request. See our Disclaimer and Acceptable Use Policy for the full terms.
This guide is general information, not a security audit of your specific network — see our disclaimer for what AdminKit's guidance does and does not commit to.
Frequently asked questions
What is a Modbus function code?
It is the first byte of the Modbus protocol data unit, right after the seven-byte MBAP header in Modbus TCP. It tells the device which operation to perform, for example 3 for Read Holding Registers or 6 for Write Single Register. The bytes that follow depend on the function: usually a start address, a quantity and, for writes, the values.
Which Modbus function codes write to a device?
The common write codes are 5 (Write Single Coil), 6 (Write Single Register), 15 (Write Multiple Coils), 16 (Write Multiple Registers), 22 (Mask Write Register) and 23 (Read/Write Multiple Registers). Code 21 writes file records. Diagnostics (8) and vendor programming codes can also change device state, so treat them as writes when you build an allowlist.
How do I recognise a Modbus exception response?
The device answers with the request's function code plus 128 (hex 0x80) and one exception code byte. A rejected Read Holding Registers (3) therefore comes back as function 131 (0x83). Exception 1 means the function is not supported, 2 an illegal data address and 3 an illegal data value, as listed in Zeek's Modbus constants.
Can a firewall block Modbus writes but allow reads?
A plain port filter cannot, because reads and writes both use TCP 502. You need something that parses Modbus: an industrial firewall or gateway with function-code filtering, or an IDS such as Suricata, whose modbus keyword matches read or write access and specific function codes. Restricting which hosts can reach port 502 at all comes first.
Which function code does AdminKit's Modbus check send?
Only function 43 with MEI type 14, Read Device Identification, which asks the device for its vendor name, product code and revision. It never sends a read or write for coils, discrete inputs, input registers or holding registers, and it only tests the public IP address you are connecting from.
Sources
- Zeek documentation — base/protocols/modbus/consts.zeek (function and exception code names)
- Zeek documentation — base/protocols/modbus/main.zeek (modbus.log fields)
- Suricata documentation — Modbus keyword
- Wireshark — Display Filter Reference: Modbus
- Wireshark — Display Filter Reference: Modbus/TCP (MBAP header fields)
- MITRE ATT&CK for ICS — T0836 Modify Parameter
- NIST SP 800-82 Rev. 3 — Guide to Operational Technology (OT) Security
Want AdminKit to watch this for you?
Free checks tell you the state today. Protective DNS and monitoring tell you when it changes.
Talk to an expert
Huy Nguyen (David), Founder & CEO, Netbase JSC
Ask whether this fix applies to your setup, or get a second pair of eyes on your DNS and mail records.