Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How the Modbus protocol works

After this lesson: I can read a Modbus/TCP request byte by byte.

65 min lesson3 commandsIndustrial control system security lab: a simulated PLC, Modbus, HMI and conduit

A protocol from 1979 that still runs plants

Modbus is a simple protocol for reading and writing the values inside a PLC. It was made in 1979 for serial cables, and Modbus/TCP carries it over ordinary networks today.

It is everywhere in industry because it is simple. It is also a risk, because it never asks who is sending. All of this lesson happens in your own lab copy.

By the end of this lesson

Note

I can read a Modbus/TCP request byte by byte.

Key words

WordWhat it meansExample
Coila one-bit value a client may writepump command
Registera 16-bit number inside the PLCa setpoint
Function codethe byte that says what a request does03
MBAP headerthe seven bytes in front of every Modbus/TCP request
Exceptionan error reply with a code saying what was wrong
Big-endianthe high byte of a number comes first

The cells students fill in are left blank in this preview.

The one rule

Warning

Send Modbus only to your own lab copy. A Modbus write to a real device can start a motor or open a valve. Sending one to a system that is not yours can be a crime under the Computer Misuse Act 1990.

Four tables hold everything a PLC shares

TableSizeWho may writeIn the lab
Coils1 bitClients0 pump command, 1 manual mode
Discrete inputs1 bitNobody, read only0 pump running, 1 high switch, 2 safety trip
Holding registers16 bitsClients0 low setpoint, 1 high setpoint
Input registers16 bitsNobody, read only0 level, 1 scans, 3 level at last trip

The cells students fill in are left blank in this preview.

A request is a header and a PDU

Every Modbus/TCP request starts with the seven-byte MBAP header. It holds a transaction id, a protocol id that is always 0, a length and a unit id.

Then comes the PDU: a function code and its data. For a read, the data is a start address and a count. For a write of one value, it is an address and the value. Every number is big-endian.

Build a request and read the reply

Command
docker exec work python3 steps/frame.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: the request the lab sent

BytesFieldMeaning
00 01Transaction idRequest number 1
00 00Protocol idAlways 0 for Modbus
00 06LengthSix bytes follow
01Unit idDevice 1
03Function codeRead holding registers
00 00 00 02Address and countFrom 0, two registers

The cells students fill in are left blank in this preview.

Try it: build a request

Code: python
import struct

tid, unit, fc, address, count = 7, 1, 4, 0, 5
pdu = struct.pack(">BHH", fc, address, count)
frame = struct.pack(">HHHB", tid, 0, len(pdu) + 1, unit) + pdu
print(frame.hex(" "))
print("bytes:", len(frame))
# Change fc to 6, address to 1 and count to 75. What would that frame do?

Function codes read, write or refuse

Functions 1 to 4 read the four tables. Function 5 writes one coil and function 6 writes one register. Functions 15 and 16 write many at once.

When a request is wrong, the PLC sends an exception. It sets the top bit of the function code and adds a code. Code 1 is a function it lacks, 2 an address that does not exist, and 3 a bad value.

Ask for three things the PLC refuses

Command
docker exec work python3 steps/errors.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Nothing in the frame says who is asking

Look at the table again. There is no user, no password and no signature. A PLC answers any request that reaches its port.

The next Run writes 81 into the high setpoint from the work box, then puts 80 back. The PLC accepts both. On a real plant, any machine that can reach port 502 could do the same.

Change a setpoint with no password

Command
docker exec work python3 steps/noauth.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think a PLC checks who sent a write. Plain Modbus has no login at all, so a write from any machine that can reach the port is obeyed. The defence is outside the protocol: who can reach the port, and a safety layer the network cannot change.

About this lab and its source: Industrial control system security lab: a simulated PLC, Modbus, HMI and conduit

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.