最近在老板的督促下看了一点儿垃圾邮件筛选规则的知识。老板要求把看过的规则的框架提取出来一下,虽然我觉得没多大用处,毕竟都要自己定义规则的人了,还不知道去要写的规则大概长什么样子咩。(不过对于老板还是有点儿用处的,毕竟她不想看代码,就想结合我之前写的规则的解释文档了解一下儿这个东西zzzzzz)。
下面就贴出大致整理的Rspamd的Html.lua、Forwarding.lua、Subject_checks.lua 和 Compromised_hosts.lua的规则框架。
Rspamd_rule_frame
Html.lua
//定义HTML_RULE的框架如下:
rspamd_config.HTML_RULE = {
callback = function(task)
return rule_function(task ,arg) //调用规则函数并且
//得到规则的返回值
end,
score = 2.0,
group = 'html',
description = ‘Rspamd rule’s function description'
}
//回调函数的框架如下:
local function rule_function(task, arg)
local tp = task:get_text_parts() //首先Get all text (and HTML) parts
for _,p in ipairs(tp) do //用ipairs遍历取出所有的part
if p:is_html() then //如果当前部分是HTML context
local hc = p:get_html() //获取html context
//用户可以取出html中的子标签、数据等,定义自己的规
//则,取出这些数据的接口可以参考官网给出的API文档。
end
end
end
forwarding.lua
//检测转发邮箱类别的框架如下:
rspamd_config.FWD_MAIL_CATEGORY = {
callback = function (task)
if not (task:has_from(1) and
//首先检测发送方和接收方均使用的是SMTP协议。
task:has_recipients(1)) then
return false
end
local hostname = task:get_hostname()
//然后获取MTA(Mail-Transfer_Agent)提供的发送者
//的.hostname。
if hostname and hostname:lower():
find('%.MAIL_CATEGORY%.相应的regexp') then
//用正则表达式匹配hostname中是否MAIL_CATEGORY。
if task:get_header_raw('MAIL_CATEGORY') then
//如果含有MAIL_CATEGORY则表明由该
//MAIL_CATEGORY转发。
return true
end
end
return false
end,
score = 0.0,
description = "Message was forwarded by MAIL_CATEGORY",
group = "forwarding"
}
//检测其他服务转发邮件的框架如下:
rspamd_config.FWD_Service = {
callback = function (task)
if not (task:has_from(arg) and task:has_recipients(arg)) then
return false
end
local envfrom = task:get_from(arg) //获取使用相应服务发送者的用户网络地址,arg可以
// 取值:0、1、2。
// 0代表:尝试用SMTP服务发送回退到MIME失败
//1代表:只探测SMTP发送者
//2代表:只探测MIME发送者
local envrcpts = task:get_recipients(arg)//获取使用相应服务的接收者的网络地
址
local lu = task:get_xxx() //参考官方API,获取想要操作的部分,下面定义规则
// 依据这些取得的数据
//最后定义用户自己想要添加的规则:使用前面task.get_xxx()获得的数据,并
//返回相应的值。
end,
score = 0.0,
description = "通过其他服务转发的邮件",
group = "forwarding"
}
subject_checks.lua
根据邮件主题编写规则的框架如下:
- rspamd_config.SUBJ_RULE = {
callback = function(task)
return rule_function(task,arg)
end,
score = 3.0,
group = 'subject',
description = '根据邮件主题编写规则'
}
local function rule_function(task,arg)
local sbj = task:get_header(‘Subject') //获取邮件的Subject header。
// 根据subject编写相应的规则。
return false
end
compromised_hosts.lua
检测正则表达式:用于匹配消息的头部信息的框架如下:
reconf['HAS_xxx] = {
re = “构造相应的正则表达式",
description = "PHPMailer signature",
group = "compromised_hosts"
}
检测消息头部是否含有指定的协议、服务的框架如下:
reconf[‘HAS_X_xxx’] = {
re = “header_exists('X-xxx')", // 使用Rspamd的内部函数header_exists,来判读头部X-xxx是否存在
description = "Has X-xxxheader",
group = "compromised_hosts"
}