repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
pentesting-modbus.mdMarkdown196 lines7.6 KiBsource raw download

title: "502/tcp - Pentesting Modbus Protocol" section: "Network Services" sectionSlug: "network-services-pentesting" sourcePath: "src/network-services-pentesting/pentesting-modbus.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/network-services-pentesting/pentesting-modbus.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

502/tcp - Pentesting Modbus Protocol#

Basic Information#

Modicon introduced Modbus in 1979 for communication among industrial devices. Current specifications use client/server terminology, although many tools and deployed serial systems still say master/slave.[3]

Default Modbus/TCP port: 502. The separate Modbus Security profile wraps Modbus in TLS with X.509 authentication and uses port 802; do not assume every Modbus deployment is necessarily plaintext.[3][4]

PORT    STATE SERVICE
502/tcp open  modbus

Modbus TCP carries a seven-byte MBAP header followed by the Modbus PDU:[3]

The Unit ID is frequently ignored by native Modbus/TCP devices, but it becomes critical when the target is a TCP-to-RTU gateway. In that case, you usually need to enumerate multiple unit/slave IDs to reach the device behind the bridge.

Legacy Modbus/TCP traffic on port 502 is typically plaintext and unauthenticated, so passive captures often reveal:

Enumeration#

Automatic#

nmap -sV --script modbus-discover -p 502 <IP>
nmap --script modbus-discover --script-args='modbus-discover.aggressive=true' -p 502 <IP>
msf> use auxiliary/scanner/scada/modbusdetect
msf> use auxiliary/scanner/scada/modbus_findunitid

modbus-discover is useful because it tries to enumerate legal slave IDs and extract device identification data such as vendor and firmware strings.[1]

Passive Recon#

If you can sniff traffic, extract the parameters you need before sending anything intrusive:

tshark -r modbus.pcap -Y modbus \
  -T fields -e ip.src -e ip.dst -e modbus.unit_id \
  -e modbus.func_code -e modbus.reference_num -e modbus.word_cnt

Useful Wireshark display filters:

modbus
modbus.func_code == 3
modbus.func_code == 16
modbus.exception_code

Manual Enumeration with PyModbus#

python3 -m pip install pymodbus
from pymodbus.client import ModbusTcpClient

host = "10.10.10.10"
unit = 1
client = ModbusTcpClient(host, port=502)
client.connect()

print(client.read_coils(address=0, count=16, device_id=unit))
print(client.read_holding_registers(address=0, count=10, device_id=unit))
print(client.read_device_information(device_id=unit))

client.close()

The current PyModbus client uses zero-based protocol addresses and names the unit argument device_id; older releases used slave or unit, so match the example to the installed version.[5]

Notes:

Offensive Tooling#

git clone https://github.com/TacticalGator/modbuster
cd modbuster && pipx install .
modbuster getfunctions <IP>
modbuster read -s 1 <IP> 400001 10
modbuster diag -s 1 <IP>

Other useful frameworks/tools:[6]

Interesting Function Codes#

The following standardized function codes and the 0x2B/0x0E device-identification MEI are defined in the Modbus application protocol specification.[3]

Read / Recon#

Write / Impact#

These are the first function codes to validate carefully during authorized testing because they can directly alter process state:

Less Common but Worth Testing#

These are not universally implemented, but when present they are valuable because defenders often monitor them less:

A practical workflow is:

  1. Find the valid Unit ID.
  2. Verify read-only access with 0x01-0x04.
  3. Pull device identification with 0x2B/0x0E.
  4. Enumerate supported function codes.
  5. Only then validate whether write functions are accepted.

Scapy Packet Crafting#

Scapy ships Modbus packet definitions for common read/write primitives and Read Device Identification.[6]

from scapy.contrib.modbus import ModbusADURequest, ModbusPDU2B0EReadDeviceIdentificationRequest
from scapy.all import sr1

pkt = ModbusADURequest(transId=1, unitId=1) / ModbusPDU2B0EReadDeviceIdentificationRequest()
resp = sr1(pkt, timeout=2)
if resp:
    resp.show()

This is useful when you need to:

What Usually Leads to Impact#

In real assessments, attackers rarely start with memory corruption. The usual path is:

Recent Modbus research and datasets keep emphasizing the same attacker behaviors: query flooding, false data injection, brute-force writes, replay, and malformed length/frame handling. Those are usually more realistic test cases than hunting for a single vendor-specific CVE.[2]

References#