.. _lua-output: Lua输出 ========== Suricata提供了通过可插拔的lua脚本获取特定类型网络流量更详细输出的功能。您可以自行编写这些脚本,只需定义四个钩子函数即可。 对于lua输出脚本,Suricata提供了丰富的lua函数集。这些函数都能返回关于引擎内部特定细节和网络流量各方面的信息。下文将按事件/流量类型分组进行描述。但让我们先从一个示例开始,解释四个钩子函数以及如何让Suricata加载lua输出脚本。 脚本结构 ---------------- 一个lua输出脚本需要定义4个钩子函数:init()、setup()、log()、deinit() * init() -- 注册脚本挂载到输出引擎的位置 * setup() -- 执行每个输出线程的初始化设置 * log() -- 日志记录函数 * deinit() -- 清理函数 示例: :: local config = require("suricata.config") local logger = require("suricata.log") function init (args) local needs = {} needs["protocol"] = "http" return needs end function setup (args) filename = config.log_path() .. "/" .. name file = assert(io.open(filename, "a")) logger.info("HTTP日志文件名 " .. filename) http = 0 end function log(args) http_uri = HttpGetRequestUriRaw() if http_uri == nil then http_uri = "" end http_uri = string.gsub(http_uri, "%c", ".") http_host = HttpGetRequestHost() if http_host == nil then http_host = "" end http_host = string.gsub(http_host, "%c", ".") http_ua = HttpGetRequestHeader("User-Agent") if http_ua == nil then http_ua = "" 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) logger.info ("记录的HTTP事务数: " .. http); file:close(file) end .. _lua-output-yaml: YAML配置 ---- 要启用lua输出,需添加'lua'输出模块并配置一个或多个脚本如下: :: outputs: - lua: enabled: yes scripts-dir: /etc/suricata/lua-output/ # 默认情况下Lua模块搜索路径为空。如果计划使用外部模块, # 则需要设置这些路径。以下示例适用于64位Linux系统中 # 通过包管理器安装的模块查找,但可能需要调整。 #path: "/usr/share/lua/5.4/?.lua;/usr/share/lua/5.4/?/init.lua;/usr/lib64/lua/5.4/?.lua;/usr/lib64/lua/5.4/?/init.lua;./?.lua;./?/init.lua" #cpath: "/usr/lib64/lua/5.4/?.so;/usr/lib64/lua/5.4/loadall.so;./?.so" scripts: - tcp-data.lua - flow.lua scripts-dir选项是可选的。它指定Suricata从该目录加载脚本。若不设置则从当前工作目录加载。 开发lua输出脚本 ----------------------------- 您可以使用 :ref:`Lua函数 ` 中描述的函数进行开发