A rule that gives access to a PLC over TCP 502 or TCP 102 allows both reading data from that PLC and changing the PLC’s program at the same time. If the port is open, everything is open. Deep packet inspection (DPI) on industrial firewalls makes this distinction by looking inside the protocol. Below, we describe the logic of function-based rules for Modbus TCP and Siemens S7 communication, an example rule table, the frequently mistyped address conversion, and the order for moving rules into enforcement mode in a phased, planned way.
The limits of port-based rules for industrial protocols
On an enterprise network, there are other controls at the application layer behind an “allow HTTPS” rule. Industrial protocols usually have no authentication. In Modbus TCP, the client that establishes the TCP connection can write registers; in classic S7comm too (if no protection level is set on the PLC), the side that establishes the connection can send a command to stop the CPU. That is why, in industrial network and OT infrastructure, the real question is not “who can connect” but “what can whoever connects do”.
The typical weakness of a port-based rule is this: a 502 rule opened so that the SCADA server can read also lets an attacker write setpoints if that server is compromised. With DPI, the rule leaves read function codes open and ties writes to specific register ranges and specific sources. Where the DPI rule is placed (in front of the cell, at the zone boundary) is a separate decision; it is covered in the article firewall placement at cell level.
Function code and register filtering in Modbus TCP
A Modbus TCP frame consists of a 7-byte MBAP header (transaction ID, protocol ID = 0, length, unit ID) followed by the PDU. The first byte of the PDU is the function code; this code distinguishes reading from writing:
| Function code | Function | Class |
|---|---|---|
| 01 / 02 | Read Coils / Read Discrete Inputs | Read |
| 03 / 04 | Read Holding / Input Registers | Read |
| 05 / 06 | Write Single Coil / Register | Write |
| 15 (0x0F) / 16 (0x10) | Write Multiple Coils / Registers | Write |
| 22 (0x16) | Mask Write Register | Write |
| 23 (0x17) | Read/Write Multiple Registers | Read + write |
| 43 (0x2B) / MEI 14 | Read Device Identification | Diagnostics |
A useful Modbus DPI policy filters on three dimensions: function code, address range (start address + quantity) and unit ID. The unit ID distinguishes serial devices behind a Modbus TCP–RTU gateway; if you skip this field in rules in front of a gateway, a single rule opens all the slaves behind it. The topic is covered in detail in the article migrating from Modbus RTU to Modbus TCP.
Where people get stuck in the field when writing rules:
- FC 23 both reads and writes in a single request. A “allow reads” policy must not cover 23; if needed, the address range on the write side is also checked.
- Address range checking is not satisfied by the start address; it also takes the quantity field into account. A request starting at register 100 and writing 50 registers touches the entire 100–149 range.
- User-defined and undefined codes (the 65–72 and 100–110 ranges are user-defined) are denied by default. Some products use these ranges for vendor-specific functions; if you have not seen them in the inventory, do not allow them.
- Deny behaviour: When the firewall drops a packet, the client times out; some products instead generate a Modbus exception response (function code + 0x80). The HMI’s reconnect behaviour on timeout directly determines alarm and log noise. A single denied write can turn into a stream of “communication error” alarms on the HMI.
Function-based filtering in S7comm
Classic S7comm is carried over ISO-on-TCP (RFC 1006) on TCP 102; above the COTP layer comes the S7 header with protocol ID 0x32. The ROSCTR field in the header determines the message type (Job, Ack_Data, Userdata), and the function code in the parameter section determines the operation:
| S7 function code | Action | Risk |
|---|---|---|
| 0xF0 | Setup Communication | Connection setup, required |
| 0x04 | Read Var | Low |
| 0x05 | Write Var | Medium–high (variable write) |
| 0x1A–0x1C | Request Download / Download Block / Download Ended | High (program download) |
| 0x1D–0x1F | Start Upload / Upload / End Upload | Medium (program read) |
| 0x28 | PI Service (e.g. CPU start) | High |
| 0x29 | PLC Stop | High |
Userdata messages (ROSCTR 7) carry block listing, SZL reading and diagnostic functions; engineering software and diagnostic tools use them heavily. If you block them blindly, online diagnostic screens come up empty. The typical shape of the policy is: for SCADA and data collection servers, only Setup Communication, Read Var and, if needed, limited Write Var; download, PI Service and PLC Stop only for the engineering workstation and preferably only during the maintenance window.
Which access method is chosen for data collection (PUT/GET, OPC UA, others) directly determines the DPI rule; the options are compared in the article methods for reading data from S7 PLCs.
Example rule table: reads open, writes restricted
The table below is an example policy draft for a packaging line. Addresses and ranges are examples; derive them from your own register map.
| Source | Target | Permitted function | Restriction |
|---|---|---|---|
| SCADA server | Line PLCs (Modbus) | FC 01–04 | All addresses |
| Operator HMI | Line PLCs (Modbus) | FC 06, 16 | Holding 40100–40199 (setpoint block; protocol address 99–198) |
| Data collection gateway | S7-300/400 PLC | 0xF0, 0x04 | Read only |
| Engineering workstation | S7-300/400 PLC | All functions | During the maintenance window, logged |
| Any | Any PLC | All other functions | Deny and log |
Most commercial industrial firewalls define this policy through a graphical interface. On the open-source side, Suricata lets you write rules by function and access type when the Modbus parser is enabled (app-layer.protocols.modbus.enabled: yes in suricata.yaml). It is suitable for monitoring purposes during the learning phase:
alert modbus !$SCADA_HMI any -> $LINE_PLC 502 (msg:"Modbus write - unauthorised source"; modbus: access write; sid:1000101; rev:1;)
alert modbus any any -> $LINE_PLC 502 (msg:"Modbus Diagnostics FC08"; modbus: function 8; sid:1000102; rev:1;)
alert modbus $SCADA_HMI any -> $LINE_PLC 502 (msg:"Write below setpoint block"; modbus: access write holding, address <99; sid:1000103; rev:1;)
alert modbus $SCADA_HMI any -> $LINE_PLC 502 (msg:"Write above setpoint block"; modbus: access write holding, address >198; sid:1000104; rev:1;)
The variable names ($SCADA_HMI, $LINE_PLC) are defined in the address groups in suricata.yaml. The address in the last two rules is the zero-based register address in the protocol: in 4xxxx notation, 40001 corresponds to protocol address 0, so the 40100–40199 block is the range 99–198. This off-by-one is the most common cause of wrong rules in the field. Also verify with test traffic on your own version whether the rule evaluates only the start address or the entire range formed by start + quantity; multiple writes (FC 16) that cross the block boundary are only caught in the second case. Don’t trust it without testing.
Moving from learning mode to enforcement mode
Putting DPI rules straight into blocking mode means a legitimate function that was overlooked will stop the line. The order is as follows:
- Passive observation: Run the firewall (or a sensor via SPAN) in logging-only mode. The period should cover at least one full production cycle: shift change, recipe changeover, weekly cleaning, month-end reporting.
- Building the inventory: For each source–destination pair, put the observed function codes and address ranges into a table. Discuss every unexpected write with the process engineer one by one.
- Policy draft: Sort the observed traffic into “needed”, “unneeded” and “unexplained”. Only the “needed” items go into the allow list.
- Alert mode: Apply the policy but only log violations. Stay in this mode for several weeks until there are no unexpected violations left.
- Gradual enforcement: First block the riskiest functions (PLC Stop, download, user-defined codes); bring write restrictions into force cell by cell.
- Rollback: At each step, write down in advance how to switch the policy back to alert mode in a single operation and who is authorised to do it. Know whom to call on the night shift.
The security operations side of passive monitoring and anomaly detection is covered in the guide OT attack detection and protocol anomaly monitoring.
Latency, false blocks and maintenance effects
Latency: DPI requires packets to be reassembled and inspected at the application layer. For Modbus/S7 request–response traffic, the added latency is usually tolerated; but do not read latency under load from the manufacturer’s datasheet, measure it with your own traffic profile. Passing Layer 2 cyclic traffic such as PROFINET RT through a DPI firewall is a design error anyway. That traffic stays in the cell.
False blocks: There are three common causes: the HMI starting to use a different function code after a firmware update (e.g. FC 16 instead of individual writes), a recipe download that runs only once a month never being seen during observation, and requests split across TCP segments that cannot be parsed. At the moment of a fault, the first place to look is the firewall’s deny log, not the PLC. Every firmware or HMI project change should generate a change request for the DPI policy.
Maintenance: Leaving the rule that grants full rights to the engineering workstation permanently open cancels out the most valuable part of DPI. Make this rule time-based, or open it with an approved change before maintenance. Keep the rule base under version control together with the PLC program and register map.
The primary sources are the Modbus Organization specifications page for Modbus’s official definitions and the Suricata Modbus keyword documentation for Suricata rule syntax. You can find how we work on register mapping and policy design on our OT cybersecurity and segmentation and PLC communication and integration pages.
Frequently asked questions
Can a regular NGFW be used instead of an industrial DPI firewall?
It can, but the first thing to check is this: does the NGFW merely recognise Modbus and S7, or does it let you write rules at the level of function code, address range and unit ID? Some enterprise NGFWs offer this detail with industrial signature packs; others only perform application identification.
What happens if I block writes completely in Modbus?
Setpoints, recipes and command buttons entered from the HMI stop working; they are all carried by Modbus write functions. The right approach is not to block writes, but to allow them only from authorised sources such as HMI and SCADA, and only to defined register ranges.
How effective is DPI on S7-1500 PLCs?
Because the S7comm-plus used with TIA Portal is integrity-protected, many firewalls cannot filter functions in this traffic in as much detail as classic S7comm. On these series, in addition to DPI, the PLC’s own access protection levels, keeping PUT/GET disabled and engineering access restricted to a single authorised workstation are applied together.
How long should learning mode last?
There is no fixed period. The observation period should cover all periodic jobs in production: shift changes, recipe changeovers, weekly maintenance and month-end operations. If a function that runs once a month is not seen during observation, it will show up as a false block in enforcement mode.