在 Suricata 中使用 Lua 脚本检测文件变化

我刚学习 Lua 编程,一直在寻找一个能够通过 Suricata 从互联网下载文件并检测文件是否更改的 Lua 脚本。任何帮助都将不胜感激。先感谢大家。

类似于以下格式:

function init(args)
    return {http.response_body = tostring(true)}
end

local function read_file(path)
    local file = open(path, "rb") -- r读模式和b二进制模式
    if not file then return nil end
    local content = file:read "*a" -- *a 或 *all 读取整个文件
    file:close()
    return content
end

local fileContent = read_file(之前存储文件的路径);
local fileContent2 = read_file(init());

if fileContent != fileContent2:
     print("文件已更改")

并且当内容相同时阻止

drop http any any -> any any (msg:"NVISO PDF file lua"; flow:established,to_client; luajit:pdfcheckname.lua; classtype:policy-violation; sid:1000000; rev:1;)

原文链接 https://stackoverflow.com/questions/69026904

点赞
stackoverflow用户2858170
stackoverflow用户2858170

除非你有其他未在你的片段中显示的“open”函数,否则local file = open(path, "rb") 应该替换为 local file = io.open(path, "rb")

local fileContent2 = read_file(init()); 会引起问题,因为init()将返回一个表,而不是一个路径字符串。这将在调用io.open时引发错误。

if fileContent != fileContent2:
     print("File changed")

语法有误。

替换为

if fileContent != fileContent2 then
  print("File Changed")
end

而且像 file2Content这样的名称更有意义,因为它不是文件的第二个内容,而是文件2的内容。但这只是我的个人意见。

2021-09-02 09:02:39