postfix邮件过滤

在上一篇 RH254小结(二)postfix邮件服务器的搭建 中有提到邮件内容过滤 ,不过由于篇幅所限,写的有些零散,有些东西总有些意犹未尽。这里再写下。在此之前,先了解下过滤技术。postfix 的过滤技术非常的全而细 ,其可以在客户端smtp连接时过虑、HELO 阶段过虑、进行发件人过滤、收件人过虑,邮件头和邮件内容过滤等。前面提到的几种过虑可以参看如下几个参数:

smtpd_client_restrictions
smtpd_helo_restrictions
smtpd_sender_restrictions
smtpd_recipient_restrictions

一、header_checks 规则与分类

下面接下来了解header_checks  ,header_checks规则是一个统称,其规则不仅仅限于狭义的头检测,还包含body和附件等。通过man header_checks 可以获取如下用法:

#main.cf 文件中增加的规则及分类
header_checks = pcre:/etc/postfix/header_checks
mime_header_checks = pcre:/etc/postfix/mime_header_checks
nested_header_checks = pcre:/etc/postfix/nested_header_checks
body_checks = pcre:/etc/postfix/body_checks
milter_header_checks = pcre:/etc/postfix/milter_header_checks
smtp_header_checks = pcre:/etc/postfix/smtp_header_checks
smtp_mime_header_checks = pcre:/etc/postfix/smtp_mime_header_checks
smtp_nested_header_checks = pcre:/etc/postfix/smtp_nested_header_checks
smtp_body_checks = pcre:/etc/postfix/smtp_body_checks
# 规则生效的方法
postmap -q "string" pcre:/etc/postfix/filename
postmap -q - pcre:/etc/postfix/filename <inputfile

具体的规则分为三部分,格式如下

正则匹配部分        ACTIONS动作部分           通知信息部分

正则部分以标准的regexp正则为例(实则pcre正则更强大一些,但基本部分两者差别不大):

1、正则格式

/pattern/flags result    //匹配时执行后面定义的动作
!/pattern/flags result   //不匹配时执行后面定义的动作
if /pattern/flags        //条件匹配时的动作, 以endif结束
endif
if !/pattern/flags       //条件不匹配时的动作, 以endif结束
endif

注:if语句多用于开篇提到的smtpd规则部分。如:

if !/^owner-/
/^(.*)-outgoing@(.*)$/   550 Use ${1}@${2} instead
endif

2、正则元字符

.  :代表任意一个字符
* :代表一个或多个字符
\  :将一个特别字符取消转义
^ :匹配行首
$  :匹配行尾

3、flags部分

i   :忽略大小写,默认为on
m :多行匹配模式,默认为off
x  : 扩展正则 ,默认为on

4、actions动作

actions动作有DISCARD、DUNNO、FILTER、 HOLD、IGNORE、INFO、PREPEND、REDIRECT、REPLACE、REJECT、WARN ,这里取几个常用的作下说明:

  • DISCARD 通知Postfix直接删除邮件,在这种情况下,服务器不对发送者返回任何信息,以便让发送端感觉邮件已经正常发送。只适用于postfix2.0以后的版本,其不支持smtp header/body checks 。

  • HOLD 通知Postfix将邮件保存在邮件队列中,以便管理对之进一步处理。

  • IGNORE 通知Postfix删除满足过滤条件的那部分内容。

  • REJECT 退回邮件,并告知通信原因。

  • WARN 将邮件收下来,但将邮件的基本信息记录在日志档内。

  • REPLACE 将匹配行替换后发给邮件接收者。

二、常用过虑示例

邮件头过滤:

/filename\=\".*src\.exe/                       REJECT      Virus filename is unavailable header
/filename\=\".*\.cpl\"/                          REJECT      Virus filename is unavailable header
/name\=\".*\.bat\"/                              REJECT      Virus filename is unavailable header
/name\=\".*\.scr/                                REJECT      Virus filename is unavailable header
/^Subject:.*Hello\,.*\,how\ are\ you.*/   DISCARD    Virus drop the unavailable header
/^Subject:.*W32.*removal\ tools$/        DISCARD    Virus drop the unavailable header
/^From:.*rayman\.com\.cn/                 DISCARD     Commercial unavailable header
/^From:.*\@263\.net/                          DISCARD     Commercial unavailable header
/^To:.*\.TXT@/                                   DISCARD     Commercial unavailable header
/^To:.*apatite.*\
/^Subject: .* / REJECT Spam Header Many Spaces
/^Subject: Free Money/ REJECT Spam Free Money Mails
/^Date: 19[0-9][0-9]/ REJECT Spam past date
/name=[^>]*/.(bat|com|exe)/ REJECT Spam Executable Attachements

邮件内容过滤:

/http\:\/\/168\-news\.com/                 DISCARD      Commercial drop the unavailable body
/http\:\/\/.*edms\.tv/                          DISCARD     Commercial drop the unavailable body
/mailto\:.*\@126\.com/                     DISCARD     Commercial drop the unavailable body
/mailto\:.*\@powererp\.org/               DISCARD     Commercial drop the unavailable body
/0982\-281\-125/                               DISCARD     Commercial unavailable body
/www\.email104\.com/                      DISCARD     Commercial unavailable body
/\/nomail\.gif\"/                                 DISCARD     Commercial nomail  unavailable body
/\/nomail\.htm\"/                               DISCARD     Commercial nomail  unavailable body

以下是之前一些针对老的邮件病毒过虑的规则:

############# Part I 病毒攻防 #######################
/filename/=/"1.*zip/"/ REJECT Virus 不允许该命名的附件(/filename/=/"1.*zip/"/)
/filename/=/"postcard/.zip/"/ REJECT Virus 不允许该命名的附件(/filename/=/"postcard/.zip/"/ )
# 1. KLEZ.G 病毒攻防 #####################################
/^Subject:.*Let/'s be friends/ DISCARD Virus drop the unavailable header
#/^Subject:.*A/ funny/ game/ DISCARD Virus drop the unavailable header
#/^Subject:.*A WinXP patch$/ DISCARD Virus drop the unavailable header
#/^Subject:.*A special funny website/ DISCARD Virus drop the unavailable header
/^Subject:.*Hello/,.*/,how/ are/ you.*/ DISCARD Virus drop the unavailable header
/^Subject:.*W32.*removal/ tools$/ DISCARD Virus drop the unavailable header
/^Subject:.*Worm/ Klez.*immunity.*/ DISCARD Virus drop the unavailable header
# 4. Sobig #############################################
/^Subject.*my/ details/ DISCARD Virus drop the unavailable header
#/^Subject.*your/ details/ DISCARD Virus drop the unavailable header
#/^Subject.*your/ application/ DISCARD Virus drop the unavailable header
# 5. 微軟 假好心病毒 ####################################
#/^Subject.*Last/ Internet/ Pack/ DISCARD Virus drop the unavailable header
#/^Subject.*Last/ Network/ Pack/ DISCARD Virus drop the unavailable header
/^From.*MS/ Network/ Security/ DISCARD Virus drop the unavailable header
# 6. 其他最新 #########################################
/^Subject.*our/ private/ photos/ DISCARD Virus drop the unavailable header
/^Subject.*don/'t/ be/ late/!/ DISCARD Virus drop the unavailable header
# 其他病毒 #############################################
/^Subject:.*Here/ is/ that/ sample/ REJECT Virus drop the unavailable header
/^Subject:.*Your.*password/ REJECT Virus drop the unavailable header
/charset/=/"Windows/-1252/"/ REJECT Virus 本主机不可使用 charset 為 windows-1252 (Sorry, charset=windows-1252 is unavailable header)
#/^Subject/:/ warning$/ REJECT Virus 本主机不可使用这个标题做为邮件标题 unavailable header
#/^Subject/:/ unknown$/ REJECT Virus 本主机不可使用这个标题做为邮件标题 unavailable header
#/^Subject/:/ stolen$/ REJECT Virus 本主机不可使用这个标题做为邮件标题 unavailable header
#/^Subject/:/ hi$/ REJECT Virus 本主机不可使用这个标题做为邮件标题 unavailable header
#/^Subject/:/ fake$/ REJECT Virus 本主机不可使用这个标题做为邮件标题 unavailable header
# 一些不要被允許的帳號,這些都可以抵擋! #############################################
/^From/:.linyan/@/ REJECT Virus 本主机不允许使用该来源帐号的邮件 linyan
/^From/:.*admin/@/ REJECT Virus 本主机不允许使用该来源帐号的邮件 unavailable header

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注