mirror of https://github.com/OISF/suricata
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
540 lines
9.5 KiB
ReStructuredText
540 lines
9.5 KiB
ReStructuredText
.. _lua-functions:
|
|
|
|
Lua functions
|
|
=============
|
|
|
|
Differences between `output` and `detect`:
|
|
------------------------------------------
|
|
|
|
Currently, the ``needs`` key initialization varies, depending on what is the goal of the script: output or detection.
|
|
|
|
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):
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["packet"] = tostring(true)
|
|
return needs
|
|
end
|
|
|
|
For output logs, follow the pattern below. (The complete script structure can be seen at :ref:`lua-output`:)
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["protocol"] = "http"
|
|
return needs
|
|
end
|
|
|
|
|
|
Do notice that the functions and protocols available for ``log`` and ``match`` may also vary. DNP3, for instance, is not
|
|
available for logging.
|
|
|
|
packet
|
|
------
|
|
|
|
Initialize with:
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["type"] = "packet"
|
|
return needs
|
|
end
|
|
|
|
|
|
flow
|
|
----
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["type"] = "flow"
|
|
return needs
|
|
end
|
|
|
|
http
|
|
----
|
|
|
|
For output, init with:
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["protocol"] = "http"
|
|
return needs
|
|
end
|
|
|
|
For detection, use the specific buffer (cf :ref:`lua-detection` for a complete list), as with:
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["http.uri"] = tostring(true)
|
|
return needs
|
|
end
|
|
|
|
HttpGetRequestBody and HttpGetResponseBody.
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Make normalized body data available to the script through
|
|
HttpGetRequestBody and HttpGetResponseBody.
|
|
|
|
There no guarantees that all of the body will be available.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log(args)
|
|
a, o, e = HttpGetResponseBody();
|
|
--print("offset " .. o .. " end " .. e)
|
|
for n, v in ipairs(a) do
|
|
print(v)
|
|
end
|
|
end
|
|
|
|
HttpGetRequestHost
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
Get the host from libhtp's htp_tx_request_hostname(tx), which can either be
|
|
the host portion of the url or the host portion of the Host header.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
http_host = HttpGetRequestHost()
|
|
if http_host == nil then
|
|
http_host = "<hostname unknown>"
|
|
end
|
|
|
|
HttpGetRequestHeader
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
http_ua = HttpGetRequestHeader("User-Agent")
|
|
if http_ua == nil then
|
|
http_ua = "<useragent unknown>"
|
|
end
|
|
|
|
HttpGetResponseHeader
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
server = HttpGetResponseHeader("Server");
|
|
print ("Server: " .. server);
|
|
|
|
HttpGetRequestLine
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
rl = HttpGetRequestLine();
|
|
print ("Request Line: " .. rl);
|
|
|
|
HttpGetResponseLine
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
rsl = HttpGetResponseLine();
|
|
print ("Response Line: " .. rsl);
|
|
|
|
HttpGetRawRequestHeaders
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
rh = HttpGetRawRequestHeaders();
|
|
print ("Raw Request Headers: " .. rh);
|
|
|
|
HttpGetRawResponseHeaders
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
rh = HttpGetRawResponseHeaders();
|
|
print ("Raw Response Headers: " .. rh);
|
|
|
|
HttpGetRequestUriRaw
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
http_uri = HttpGetRequestUriRaw()
|
|
if http_uri == nil then
|
|
http_uri = "<unknown>"
|
|
end
|
|
|
|
HttpGetRequestUriNormalized
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
http_uri = HttpGetRequestUriNormalized()
|
|
if http_uri == nil then
|
|
http_uri = "<unknown>"
|
|
end
|
|
|
|
HttpGetRequestHeaders
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
a = HttpGetRequestHeaders();
|
|
for n, v in pairs(a) do
|
|
print(n,v)
|
|
end
|
|
|
|
HttpGetResponseHeaders
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
a = HttpGetResponseHeaders();
|
|
for n, v in pairs(a) do
|
|
print(n,v)
|
|
end
|
|
|
|
TLS
|
|
---
|
|
|
|
For log output, initialize with:
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["protocol"] = "tls"
|
|
return needs
|
|
end
|
|
|
|
For detection, initialization is as follows:
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["tls"] = tostring(true)
|
|
return needs
|
|
end
|
|
|
|
TlsGetVersion
|
|
~~~~~~~~~~~~~
|
|
|
|
Get the negotiated version in a TLS session as a string through TlsGetVersion.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log (args)
|
|
version = TlsGetVersion()
|
|
if version then
|
|
-- do something
|
|
end
|
|
end
|
|
|
|
TlsGetCertInfo
|
|
~~~~~~~~~~~~~~
|
|
|
|
Make certificate information available to the script through TlsGetCertInfo.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log (args)
|
|
version, subject, issuer, fingerprint = TlsGetCertInfo()
|
|
if version == nil then
|
|
return 0
|
|
end
|
|
end
|
|
|
|
TlsGetCertChain
|
|
~~~~~~~~~~~~~~~
|
|
|
|
Make certificate chain available to the script through TlsGetCertChain.
|
|
|
|
The output is an array of certificate with each certificate being an hash
|
|
with `data` and `length` keys.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
-- Use debian lua-luaossl coming from https://github.com/wahern/luaossl
|
|
local x509 = require"openssl.x509"
|
|
|
|
chain = TlsGetCertChain()
|
|
for k, v in pairs(chain) do
|
|
-- v.length is length of data
|
|
-- v.data is raw binary data of certificate
|
|
cert = x509.new(v["data"], "DER")
|
|
print(cert:text() .. "\n")
|
|
end
|
|
|
|
|
|
TlsGetCertNotAfter
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
Get the Unix timestamp of end of validity of certificate.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log (args)
|
|
notafter = TlsGetCertNotAfter()
|
|
if notafter < os.time() then
|
|
-- expired certificate
|
|
end
|
|
end
|
|
|
|
TlsGetCertNotBefore
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
Get the Unix timestamp of beginning of validity of certificate.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log (args)
|
|
notbefore = TlsGetCertNotBefore()
|
|
if notbefore > os.time() then
|
|
-- not yet valid certificate
|
|
end
|
|
end
|
|
|
|
TlsGetCertSerial
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
Get TLS certificate serial number through TlsGetCertSerial.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log (args)
|
|
serial = TlsGetCertSerial()
|
|
if serial then
|
|
-- do something
|
|
end
|
|
end
|
|
|
|
TlsGetSNI
|
|
~~~~~~~~~
|
|
|
|
Get the Server name Indication from a TLS connection.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
function log (args)
|
|
asked_domain = TlsGetSNI()
|
|
if string.find(asked_domain, "badguys") then
|
|
-- ok connection to bad guys let's do something
|
|
end
|
|
end
|
|
|
|
Files
|
|
-----
|
|
|
|
To use the file logging API, the script's init() function needs to look like:
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs['type'] = 'file'
|
|
return needs
|
|
end
|
|
|
|
SCFileInfo
|
|
~~~~~~~~~~
|
|
|
|
::
|
|
|
|
|
|
fileid, txid, name, size, magic, md5, sha1, sha256 = SCFileInfo()
|
|
|
|
returns fileid (number), txid (number), name (string), size (number),
|
|
magic (string), md5 in hex (string), sha1 (string), sha256 (string)
|
|
|
|
SCFileState
|
|
~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
state, stored = SCFileState()
|
|
|
|
returns state (string), stored (bool)
|
|
|
|
Streaming Data
|
|
--------------
|
|
|
|
Streaming data can currently log out reassembled TCP data and
|
|
normalized HTTP data. The script will be invoked for each consecutive
|
|
data chunk.
|
|
|
|
In case of TCP reassembled data, all possible overlaps are removed
|
|
according to the host OS settings.
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["type"] = "streaming"
|
|
needs["filter"] = "tcp"
|
|
return needs
|
|
end
|
|
|
|
In case of HTTP body data, the bodies are unzipped and dechunked if applicable.
|
|
|
|
::
|
|
|
|
function init (args)
|
|
local needs = {}
|
|
needs["type"] = "streaming"
|
|
needs["protocol"] = "http"
|
|
return needs
|
|
end
|
|
|
|
SCStreamingBuffer
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
function log(args)
|
|
-- sb_ts and sb_tc are bools indicating the direction of the data
|
|
data, sb_open, sb_close, sb_ts, sb_tc = SCStreamingBuffer()
|
|
if sb_ts then
|
|
print("->")
|
|
else
|
|
print("<-")
|
|
end
|
|
hex_dump(data)
|
|
end
|
|
|
|
Flow variables
|
|
--------------
|
|
|
|
It is possible to access, define and modify Flow variables from Lua. To do so,
|
|
you must use the functions described in this section and declare the counter in
|
|
init function:
|
|
|
|
::
|
|
|
|
function init(args)
|
|
local needs = {}
|
|
needs["tls"] tostring(true)
|
|
needs["flowint"] = {"tls-cnt"}
|
|
return needs
|
|
end
|
|
|
|
Here we define a `tls-cnt` Flowint that can now be used in output or in a
|
|
signature via dedicated functions. The access to the Flow variable is done by
|
|
index so in our case we need to use 0.
|
|
|
|
::
|
|
|
|
function match(args)
|
|
a = SCFlowintGet(0);
|
|
if a then
|
|
SCFlowintSet(0, a + 1)
|
|
else
|
|
SCFlowintSet(0, 1)
|
|
end
|
|
|
|
SCFlowintGet
|
|
~~~~~~~~~~~~
|
|
|
|
Get the Flowint at index given by the parameter.
|
|
|
|
SCFlowintSet
|
|
~~~~~~~~~~~~
|
|
|
|
Set the Flowint at index given by the first parameter. The second parameter is the value.
|
|
|
|
SCFlowintIncr
|
|
~~~~~~~~~~~~~
|
|
|
|
Increment Flowint at index given by the first parameter.
|
|
|
|
SCFlowintDecr
|
|
~~~~~~~~~~~~~
|
|
|
|
Decrement Flowint at index given by the first parameter.
|
|
|
|
Misc
|
|
----
|
|
|
|
SCThreadInfo
|
|
~~~~~~~~~~~~
|
|
|
|
::
|
|
|
|
tid, tname, tgroup = SCThreadInfo()
|
|
|
|
It gives: tid (integer), tname (string), tgroup (string)
|
|
|
|
SCLogError, SCLogWarning, SCLogNotice, SCLogInfo, SCLogDebug
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Print a message. It will go into the outputs defined in the
|
|
yaml. Whether it will be printed depends on the log level.
|
|
|
|
Example:
|
|
|
|
::
|
|
|
|
SCLogError("some error message")
|
|
|
|
SCLogPath
|
|
~~~~~~~~~
|
|
|
|
Expose the log path.
|
|
|
|
::
|
|
|
|
|
|
name = "fast_lua.log"
|
|
function setup (args)
|
|
filename = SCLogPath() .. "/" .. name
|
|
file = assert(io.open(filename, "a"))
|
|
end
|
|
|
|
SCByteVarGet
|
|
~~~~~~~~~~~~
|
|
|
|
Get the ByteVar at index given by the parameter. These variables are defined by
|
|
`byte_extract` or `byte_math` in Suricata rules. Only callable from match scripts.
|
|
|
|
::
|
|
|
|
function init(args)
|
|
local needs = {}
|
|
needs["bytevar"] = {"var1", "var2"}
|
|
return needs
|
|
end
|
|
|
|
Here we define a register that we will be using variables `var1` and `var2`.
|
|
The access to the Byte variables is done by index.
|
|
|
|
::
|
|
|
|
function match(args)
|
|
var1 = SCByteVarGet(0)
|
|
var2 = SCByteVarGet(1)
|