Skip to content

Commit 787aa54

Browse files
committed
doc: update lua-function documentation
- cleanup usage and documentation around needs - mentiond that rule hooks are used instead of "needs" keywords with link with rule hooks (which is still in the firewall-design doc)
1 parent 56b0a0d commit 787aa54

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

doc/userguide/firewall/firewall-design.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ drop
5656
.. note:: the action ``pass`` is not available in firewall rules due to ambiguity around
5757
the existing meaning for threat detection rules.
5858

59+
.. _rule-hooks:
5960

6061
Explicit rule hook (states)
6162
~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/userguide/lua/lua-functions.rst

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,41 @@ Lua functions
66
Differences between `output` and `detect`:
77
------------------------------------------
88

9-
Currently, the ``needs`` key initialization varies, depending on what is the goal of the script: output or detection.
10-
The Lua script for the ``luaxform`` transform **does not use ``needs``**.
9+
Currently the ``table`` returned from the ``init`` method varies,
10+
depending on if the script is an output script or a detection script.
1111

12-
If the script is for detection, the ``needs`` initialization should be as seen in the example below (see :ref:`lua-detection` for a complete example of a detection script):
12+
Lua scripts for ``luaxform`` do not require an ``init`` method.
1313

14-
::
14+
If the script is for detection, the ``init`` method should return a
15+
table, for example, if a packet is required:
16+
17+
.. code-block:: lua
1518
1619
function init (args)
17-
local needs = {}
18-
needs["packet"] = tostring(true)
19-
return needs
20+
local needs = {}
21+
needs["packet"] = true
22+
return needs
2023
end
2124
22-
For output logs, follow the pattern below. (The complete script structure can be seen at :ref:`lua-output`:)
25+
See :ref:`lua-detection` for more detection script examples.
2326

24-
::
27+
For output scripts, follow the pattern below. (The complete script
28+
structure can be seen at :ref:`lua-output`:)
29+
30+
.. code-block:: lua
2531
2632
function init (args)
2733
local needs = {}
2834
needs["protocol"] = "tls"
2935
return needs
3036
end
3137
38+
Do notice that the functions and protocols available for ``log`` and
39+
``match`` may also vary. DNP3, for instance, is not available for
40+
logging.
3241

33-
Do notice that the functions and protocols available for ``log`` and ``match`` may also vary. DNP3, for instance, is not
34-
available for logging.
42+
.. note:: By convention, many scripts use a variable name of ``needs``
43+
for this table, however this is not a hard requirement.
3544

3645
packet
3746
------
@@ -71,16 +80,39 @@ For output, init with:
7180
return needs
7281
end
7382

74-
For detection, use the specific buffer (cf :ref:`lua-detection` for a complete list), as with:
83+
For detection, rule hooks are used are execute the Lua script at
84+
specific protocol states, for example::
7585

76-
::
86+
alert http1:request_line any any -> any any (
87+
msg: "Test HTTP Lua request.line";
88+
lua: test-request-line.lua; sid:1;)
89+
90+
where ``test-request-line.lua`` might look like:
91+
92+
.. code-block:: lua
93+
94+
local http = require("suricata.http")
7795
7896
function init (args)
79-
local needs = {}
80-
needs["http.uri"] = tostring(true)
81-
return needs
97+
return {}
98+
end
99+
100+
function match(args)
101+
local tx, err = http.get_tx()
102+
http_request_line, err = tx:request_line()
103+
104+
if #http_request_line > 0 then
105+
--GET /base64-hello-world.txt HTTP/1.1
106+
if http_request_line:find("^GET") then
107+
return 1
108+
end
109+
end
110+
111+
return 0
82112
end
83113
114+
For more information on rule hooks, see :ref:`rule-hooks`.
115+
84116
Streaming Data
85117
--------------
86118

0 commit comments

Comments
 (0)