What is Modbus TCP?
Modbus is one of the oldest and most common ways to talk to industrial machines. A client, such as a control room PC, asks a server, such as a PLC that runs a pump, to read or write numbered 16 bit registers and on or off coils. Modbus TCP carries those same tiny requests over Ethernet on TCP port 502.
It is simple, free to use and supported by almost every PLC, energy meter, drive and building controller. That simplicity is also its weakness: classic Modbus has no login and no encryption. Anyone who can reach port 502 can change a machine, so Modbus devices must sit on a separate network behind a firewall.
Think of it like this. Modbus is like a wall of numbered pigeonholes in a workshop. You walk up and say read box 1 and 2, or put 1200 in box 1, and the machine does it. There is no lock on the wall, so the only protection is the door to the workshop.
Step by step
Normal
5 stepsA control room PC reads a pump's speed and tank level from a PLC, then sets a new speed.
Normal
5 stepsA control room PC reads a pump's speed and tank level from a PLC, then sets a new speed.
- 1
Connect
TCP port 502
The control room software, called an HMI, opens a TCP connection to the PLC on port 502 and keeps it open.
- 2
Read
Function 3, Read Holding Registers
The PC asks for 2 registers starting at address 0. Each Modbus TCP message is a 7 byte MBAP header plus a short function and its data.
- 3
Answer
Two 16 bit numbers
The PLC answers with the same transaction ID and the values: pump speed 1500 rpm and tank level 72 percent. Modbus sends only numbers; what they mean is written in the machine's manual.
- 4
Write
Function 6, Write Single Register
The operator lowers the pump speed to 1200 rpm. The PLC echoes the request back to confirm the write was done.
- 5
Poll again
Every half second
The HMI keeps asking in a loop, a pattern called polling. The PLC never speaks first.
Bad address
3 stepsThe PC asks for a register the PLC does not have, and gets an exception response.
Bad address
3 stepsThe PC asks for a register the PLC does not have, and gets an exception response.
- 1
Wrong address
Read register 256
A new screen on the HMI was set up with the wrong register number. It asks for 2 registers at address 256, which this PLC does not have.
- 2
Exception
Function 0x83, code 2
The PLC answers with the function code plus 128 (0x80), which marks an error, and exception code 2: Illegal Data Address. Other common codes are 1 (illegal function), 3 (illegal data value) and 4 (device failure).
- 3
Fix
Check the register map
The HMI shows an error. The fix is in the PLC's documentation: the register map lists which addresses exist and what they mean.
Attack blocked
4 stepsModbus has no login, so a firewall decides who may talk to the PLC. Here it stops malware.
Attack blocked
4 stepsModbus has no login, so a firewall decides who may talk to the PLC. Here it stops malware.
- 1
No password
Anyone who can reach 502 is trusted
Classic Modbus has no usernames, passwords or encryption. If a device can open TCP port 502 on the PLC, it can read and write any register.
- 2
Malware tries
Write from the office network
Malware on an office laptop scans for port 502 and tries to change the pump speed.
- 3
Firewall drops it
Only the control room PC may connect
The plant firewall allows TCP 502 to the PLC only from the control room PC at 10.10.1.10. Everything else is dropped and logged.
- 4
Real traffic passes
The allowed PC carries on
The control room PC's polls pass as usual. Newer sites can also use Modbus Security, which wraps Modbus in TLS on port 802, but a firewall is still the first line.
Myths and mix-ups
- Modbus does not check who is asking; any device that reaches port 502 can read and write.
- Register 40001 in a manual is not address 40001 on the wire; it is address 0.
- Modbus does not say what a number means; that comes from the device's register map.
- Modbus TCP is not the same as Modbus RTU; RTU runs on serial cables with a CRC instead of the MBAP header.
Try it yourself
Run these on your own computer to see Modbus TCP at work.
Read two holding registers
mbpoll -m tcp -a 1 -r 1 -c 2 -t 4 -1 192.168.1.20Use a PLC or simulator you own. -r 1 is register 40001, -t 4 means holding registers and -1 polls once.
Find Modbus devices
nmap -p 502 --script modbus-discover 192.168.1.20Only scan networks you are allowed to. Shows whether port 502 answers and the device ID if offered.
Watch the traffic
sudo tcpdump -n -X -i any tcp port 502Every value travels in the clear. In a packet viewer, the filter modbus.func_code == 6 shows only writes.
Modbus TCP at a glance
- Layer
- Application (7)
- Default port
- 502 (TCP), 802 for Modbus over TLS
- Transport
- TCP (serial lines use Modbus RTU)
- Header
- 7 byte MBAP header plus function code and data
- Data
- 16 bit registers and 1 bit coils
- Security
- None built in, protect with network separation
- First version
- 1979 (serial), Modbus TCP 1999
Defined inModbus Application Protocol Specification V1.1b3Modbus Messaging on TCP/IP Implementation Guide V1.0bMODBUS/TCP Security Protocol Specification
Header fields
What Modbus TCP puts in its messages, with example values from the stories above.
TCP
- Source port
49822- Destination port
502 (Modbus)502 is the standard Modbus TCP port.- Flags
SYN
Modbus
- Function code
3 (Read Holding Registers)Holding registers are 16 bit values the PLC keeps, like a set point or a reading.- Starting address
0 (0x0000)Address 0 on the wire is what manuals call register 40001. This off-by-one trips up many people.- Quantity
2- Byte count
4Two registers of 2 bytes each.- Register 0
1500Pump speed in rpm, according to this PLC's register map.- Register 1
72Tank level in percent.- Register address
0 (0x0000)- Register value
1200An exact echo of the request means success.- Exception code
2 (Illegal Data Address)The address, or the address plus the quantity, is outside what the PLC has.
Firewall decision
- Rule
allow tcp 10.10.1.10 to 10.10.1.20 port 502; deny all elseBecause Modbus cannot check who is asking, the firewall has to.- Action
Drop and log
Test yourself
Which function code reads holding registers?
Questions people ask
What port does Modbus TCP use?
TCP port 502. The secure version, Modbus over TLS, uses port 802.
What is the difference between Modbus RTU and Modbus TCP?
RTU sends compact binary frames over serial cables like RS-485, with a CRC check. Modbus TCP sends the same functions over Ethernet with a 7 byte MBAP header and leaves error checking to TCP.
Why is register 40001 address 0?
Manuals add a prefix and count from 1: 4xxxx means holding register and 40001 is the first one. On the wire the address counts from 0, so 40001 is sent as 0.
Is Modbus TCP secure?
No. It has no login and no encryption. Keep Modbus devices on their own network, allow port 502 only from the systems that need it, and never expose it to the internet.
What is a holding register in Modbus?
A 16 bit value inside the device that a client can read and usually write, such as a set point, a speed or a reading.