20.3.16. Flowvar

suricata.flowvar 库向 Lua 脚本暴露流变量功能。

20.3.16.1. 初始化

首先需要加载 flowvar 库模块:

local flowvarlib = require("suricata.flowvar")

然后在 init 方法中注册脚本中要使用的所有流变量。此步骤是可选的,如果您确定该流变量会通过其他方式注册,则可以跳过。

示例:

local flowvarlib = require("suricata.flowvar")

function init ()
    flowvarlib.register("count")
    return {}
end

最后,在 thread_init 函数中获取流变量的句柄并存储为全局变量:

function thread_init ()
    count_flow_var = flowvarlib.get("count")
end

20.3.16.2. 流变量方法

20.3.16.2.1. value()

以字符串形式获取流变量的当前值。注意:如果流变量没有值,可能会返回 nil

20.3.16.2.2. set(value, len)

将流变量的值设置为提供的值。必须同时提供值的长度。

20.3.16.3. 示例

local flowvarlib = require("suricata.flowvar")

function init ()
    flowvarlib.register("count")
    return {}
end

function thread_init ()
    count_var = flowvarlib.get("count")
end

function match ()
    local value = count_var:value()
    if value == nil then
        -- 将初始值设为1
        value = tostring(1)
        count_var:set(value, #value)
    else
        value = tostring(tonumber(value) + 1)
        count_var:set(value, #value)
    fi

    -- 根据自定义逻辑返回1或0
    return 1
end