20.3.18. HTTP

HTTP事务的详细信息通过 suricata.http 库暴露给Lua脚本。例如:

local http = require("suricata.http")

20.3.18.1. 初始化设置

如果目的是创建日志脚本,按如下方式初始化缓冲区:

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

如果脚本将用于规则匹配,请选择 lua-detection 中列出的可用HTTP缓冲区之一,并按以下模式操作:

function init (args)
   local needs = {}
   needs["http.request_line"] = tostring(true)
   return needs
end

20.3.18.1.1. 事务处理

HTTP基于事务,使用前必须获取当前事务:

local tx, err = http.get_tx()
if tx == err then
    print(err)
end

其他所有函数都是事务表的方法。

20.3.18.1.2. 事务方法

20.3.18.2. request_header()

通过键名获取HTTP请求头值。

示例:

local tx = http.get_tx()
local ua = tx:request_header("User-Agent")
if ua ~= nil then
      print(ua)
end

20.3.18.3. response_header()

通过键名获取HTTP响应头值。

示例:

local tx = http.get_tx()
local content_type = tx:response_header("Content-Type")
if content_type ~= nil then
      print(content_type)
end

20.3.18.4. request_line

以字符串形式获取HTTP请求行。

示例:

local tx = http.get_tx()
local http_request_line = tx:request_line();
if #http_request_line > 0 then
    if http_request_line:find("^GET") then
        print(http_request_line)
    end
end

20.3.18.5. response_line

以字符串形式获取HTTP响应行。

示例:

local tx = http.get_tx()
local http_response_line = tx:response_line();
if #http_response_line > 0 then
      print(http_response_line)
end

20.3.18.6. request_headers_raw()

获取原始HTTP请求头。

示例:

http_request_headers_raw = tx:request_headers_raw()

if #http_request_headers_raw > 0 then
    if http_request_headers_raw:find("User%-Agent: curl") then
        print(http_request_headers_raw)
    end
end

20.3.18.7. response_headers_raw()

获取原始HTTP响应头。

示例:

http_response_headers_raw = tx:response_headers_raw()

if #http_response_headers_raw > 0 then
      print(http_response_headers_raw)
end

20.3.18.8. request_uri_raw()

获取原始HTTP请求URI。

示例:

local tx = http.get_tx()
http_request_uri_raw = tx:request_uri_raw()
print(http_request_uri_raw)

20.3.18.9. request_uri_normalized()

获取标准化后的HTTP请求URI。

示例:

local tx = http.get_tx()
http_request_uri_normalized = tx:request_uri_normalized()
print(http_request_uri_normalized)

20.3.18.10. request_headers()

获取HTTP请求头。

示例:

local tx = http.get_tx()
http_request_headers = tx:request_headers()
print(http_request_headers)

20.3.18.11. response_headers()

获取HTTP响应头。

示例:

local tx = http.get_tx()
http_response_headers = tx:response_headers()
print(http_response_headers)

20.3.18.12. request_body()

获取HTTP请求体。

示例:

local tx = http.get_tx()
http_request_body = tx:request_body()
print(http_request_body)

20.3.18.13. response_body()

获取HTTP响应体。

示例:

local tx = http.get_tx()
http_response_body = tx:response_body()
print(http_response_body)

20.3.18.14. request_host()

获取HTTP请求主机名。

示例:

local tx = http.get_tx()
http_host = tx:request_host()
print(http_host)