Skip to content

Commit eaef74a

Browse files
committed
lua: document the dnp3 lib
Ticket: OISF#7631
1 parent 3b7b908 commit eaef74a

2 files changed

Lines changed: 185 additions & 0 deletions

File tree

doc/userguide/lua/libs/dnp3.rst

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
DNP3
2+
####
3+
4+
The ``suricata.dnp3`` module provides access to DNP3 (Distributed
5+
Network Protocol 3) transaction data in Suricata Lua rules.
6+
7+
It is only available in Suricata Lua rules, not output scripts.
8+
9+
Setup
10+
*****
11+
12+
::
13+
14+
local dnp3 = require("suricata.dnp3")
15+
16+
Module Functions
17+
****************
18+
19+
.. function:: dnp3.get_tx()
20+
21+
Returns the current DNP3 transaction object containing request or response data.
22+
23+
:returns: A table containing the DNP3 transaction data, or nil on error
24+
:raises error: If the protocol is not DNP3
25+
:raises error: If no transaction is available
26+
27+
Example:
28+
29+
::
30+
31+
function match(args)
32+
local tx = dnp3.get_tx()
33+
if tx and tx.is_request then
34+
-- Process DNP3 request
35+
end
36+
end
37+
38+
Transaction Object Structure
39+
****************************
40+
41+
The transaction object returned by ``get_tx()`` contains the following fields:
42+
43+
.. attribute:: tx_num
44+
45+
Transaction number (integer)
46+
47+
.. attribute:: is_request
48+
49+
Boolean indicating if this is a request (true) or response (false)
50+
51+
.. attribute:: request
52+
53+
Table containing request data (only present when ``is_request`` is true)
54+
55+
.. attribute:: response
56+
57+
Table containing response data (only present when ``is_request`` is false)
58+
59+
Request/Response Structure
60+
**************************
61+
62+
Both request and response tables contain:
63+
64+
.. attribute:: done
65+
66+
Boolean indicating if the transaction is complete
67+
68+
.. attribute:: complete
69+
70+
Boolean indicating if all data has been received
71+
72+
.. attribute:: link_header
73+
74+
Table containing DNP3 link layer header fields:
75+
76+
- ``len``: Frame length
77+
- ``control``: Control byte
78+
- ``dst``: Destination address
79+
- ``src``: Source address
80+
- ``crc``: CRC value
81+
82+
.. attribute:: transport_header
83+
84+
Transport layer header byte (integer)
85+
86+
.. attribute:: application_header
87+
88+
Table containing DNP3 application layer header fields:
89+
90+
- ``control``: Application control byte
91+
- ``function_code``: DNP3 function code
92+
93+
.. attribute:: objects
94+
95+
Array of DNP3 objects in the message
96+
97+
Additionally, response tables contain:
98+
99+
.. attribute:: indicators
100+
101+
Internal Indication (IIN) field as a 16-bit integer combining IIN1 and IIN2
102+
103+
Objects Structure
104+
*****************
105+
106+
Each object in the ``objects`` array contains:
107+
108+
.. attribute:: group
109+
110+
DNP3 object group number (integer)
111+
112+
.. attribute:: variation
113+
114+
DNP3 object variation number (integer)
115+
116+
.. attribute:: points
117+
118+
Array of data points for this object
119+
120+
Points Structure
121+
****************
122+
123+
Each point in the ``points`` array contains:
124+
125+
.. attribute:: index
126+
127+
Point index (integer)
128+
129+
Additional point fields depend on the object group and variation. Common fields include:
130+
131+
- ``state``: Binary state value
132+
- ``online``: Online status flag
133+
- ``restart``: Restart flag
134+
- ``comm_lost``: Communication lost flag
135+
- ``remote_forced``: Remote forced flag
136+
- ``local_forced``: Local forced flag
137+
- ``chatter_filter``: Chatter filter flag
138+
- ``reserved``: Reserved bits
139+
- ``value``: Analog value (for analog objects)
140+
- ``timestamp``: Timestamp value (for time-tagged objects)
141+
142+
For all available fields, see ``app-layer-dnp3-objects.h`` in the
143+
Suricata source code.
144+
145+
Example Usage
146+
*************
147+
148+
Complete example checking for specific DNP3 function codes:
149+
150+
::
151+
152+
local dnp3 = require("suricata.dnp3")
153+
154+
function init(args)
155+
return {}
156+
end
157+
158+
function match(args)
159+
local tx = dnp3.get_tx()
160+
161+
if not tx then
162+
return 0
163+
end
164+
165+
-- Check for write function code in request
166+
if tx.is_request and tx.request then
167+
local func_code = tx.request.application_header.function_code
168+
if func_code == 2 then -- WRITE function
169+
return 1
170+
end
171+
end
172+
173+
-- Check for specific object types
174+
if tx.request and tx.request.objects then
175+
for _, obj in ipairs(tx.request.objects) do
176+
if obj.group == 12 and obj.variation == 1 then
177+
-- Control Relay Output Block
178+
return 1
179+
end
180+
end
181+
end
182+
183+
return 0
184+
end

doc/userguide/lua/libs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ environment without access to additional modules.
1111
base64
1212
bytevar
1313
config
14+
dnp3
1415
dns
1516
file
1617
flowlib

0 commit comments

Comments
 (0)