20.3.23. SMTP¶
SMTP事务细节通过``suricata.smtp``库暴露给Lua脚本,例如:
local smtp = require("suricata.smtp")
20.3.23.1. 初始化设置¶
若需创建日志脚本,请按以下方式初始化缓冲区:
function init (args)
local needs = {}
needs["protocol"] = "smtp"
return needs
end
若为检测脚本则使用:
function init (args)
return {}
end
20.3.23.1.1. 事务处理¶
SMTP基于事务机制,使用前必须获取当前事务:
local tx, err = smtp.get_tx()
if tx == nil then
print(err)
end
其余所有函数均为事务表的方法。
20.3.23.1.2. 事务方法¶
20.3.23.1.2.1. get_mime_field(name)
¶
从SMTP事务中获取指定名称的MIME头字段。
示例:
local tx = smtp.get_tx()
local encoding = tx:get_mime_field("Content-Transfer-Encoding")
if encoding ~= nil then
print("Encoding: " .. subject)
end
20.3.23.1.2.2. get_mime_list()
¶
以表形式获取SMTP事务中的所有MIME头字段名称。
示例:
local tx = smtp.get_tx()
local mime_fields = tx:get_mime_list()
if mime_fields ~= nil then
for i, name in pairs(mime_fields) do
local value = tx:get_mime_field(name)
print(name .. ": " .. value)
end
end
20.3.23.1.2.3. get_mail_from()
¶
从MAIL FROM命令获取发件人邮箱地址。
示例:
local tx = smtp.get_tx()
local mail_from = tx:get_mail_from()
if mail_from ~= nil then
print("Sender: " .. mail_from)
end
20.3.23.1.2.4. get_rcpt_list()
¶
以表形式获取RCPT TO命令中的所有收件人邮箱地址。
示例:
local tx = smtp.get_tx()
local recipients = tx:get_rcpt_list()
if recipients ~= nil then
for i, recipient in pairs(recipients) do
print("Recipient " .. i .. ": " .. recipient)
end
end