15.2. Lua输出

Suricata提供了通过可插拔的Lua脚本获得特定类型网络流量更详细输出的可能性。您可以自己编写这些脚本,只需要定义四个钩子函数。

对于lua输出脚本,suricata提供了广泛的lua函数。它们都返回关于特定引擎内部和网络流量方面的信息。它们在以下各节中进行了描述,并按事件/流量类型分组。但是,让我们从解释四个hook函数的示例开始,以及如何使suricata加载Lua输出脚本。

15.2.1. 脚本结构

一个lua输出脚本需要定义4个钩子函数:init()、setup()、log()、deinit()。

  • init()--注册脚本挂接到输出引擎的位置

  • setup()--按输出线程设置

  • log()--日志功能

  • deinit()--清除功能

例子:

function init (args)
    local needs = {}
    needs["protocol"] = "http"
    return needs
end

function setup (args)
    filename = SCLogPath() .. "/" .. name
    file = assert(io.open(filename, "a"))
    SCLogInfo("HTTP Log Filename " .. filename)
    http = 0
end

function log(args)
    http_uri = HttpGetRequestUriRaw()
    if http_uri == nil then
        http_uri = "<unknown>"
    end
    http_uri = string.gsub(http_uri, "%c", ".")

    http_host = HttpGetRequestHost()
    if http_host == nil then
        http_host = "<hostname unknown>"
    end
    http_host = string.gsub(http_host, "%c", ".")

    http_ua = HttpGetRequestHeader("User-Agent")
    if http_ua == nil then
        http_ua = "<useragent unknown>"
    end
    http_ua = string.gsub(http_ua, "%g", ".")

    timestring = SCPacketTimeString()
    ip_version, src_ip, dst_ip, protocol, src_port, dst_port = SCFlowTuple()

    file:write (timestring .. " " .. http_host .. " [**] " .. http_uri .. " [**] " ..
           http_ua .. " [**] " .. src_ip .. ":" .. src_port .. " -> " ..
           dst_ip .. ":" .. dst_port .. "\n")
    file:flush()

    http = http + 1
end

function deinit (args)
    SCLogInfo ("HTTP transactions logged: " .. http);
    file:close(file)
end

15.2.2. YAML

要启用lua输出,请添加“lua”输出,然后添加一个或多个这样的脚本:

outputs:
  - lua:
      enabled: yes
      scripts-dir: /etc/suricata/lua-output/
      scripts:
        - tcp-data.lua
        - flow.lua

scripts dir选项是可选的。它使Surica从此目录加载脚本。否则,将从当前workdir加载脚本。

15.2.3. 开发Lua输出脚本

您可以使用中描述的函数 Lua Functions