email MIME/QP decode
取名� /etc/parser_subj (mode 755)
取名� /etc/parser_subj (mode 755)
#!/usr/bin/perl
$sub=join(" ",@ARGV);
if ($sub=~ /=\?\S+\?\l(\S)\?/) {
if ($1 =~ /[Qq]/) {
$sub=decode_qp($sub);
}
elsif ($1 =~ /[Bb]/) {
$sub=decode_base64($sub);
}
}
elsif ($sub=~ /=[a-fA-F0-9][a-fA-F0-9]/) {
$sub=decode_qp($sub);
}
$subject.=$sub;
$subject=~s/\'/\\\'/g;
$subject=~s/\"/\\\"/g;
print $subject;
sub decode_qp {
my($string) = @_;
@buffer=split(/\?/,$string);
$string = $buffer[3] if ($buffer[3] ne "");
$string =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge;
$string =~ /\?=(.*)/;
$string =~ tr/_/ /;
$buffer[4]=~s/^=//;
$buffer[0]=~s/=$//;
$string="$buffer[0]$string$buffer[4]";
return($string);
}
sub decode_base64 {
my($string) = @_;
my($string2);
@buffer=split(/\?/,$string);
$string = $buffer[3] if ($buffer[3] ne "");
$string =~ s/=+$//;
$string =~ tr|A-Za-z0-9+/| -_|;
while($string =~ /(.{1,60})/gs) {
my($string3) = chr(32+length($1)*3/4);
$string2 .= unpack("u",$string3 . $1 );
}
$buffer[4]=~s/^=//;
$buffer[0]=~s/=$//;
$string2="$buffer[0]$string2$buffer[4]";
return($string2);
}
在 procmail 中加入:
LOGABSTRACT=all
LOGFILE=/tmp/procmail.log
tmp=`formail -I "" | sed -e '/Content-Type/,/^$/!d' | grep "filename=" | sed -e 's/.*filename\=\(.*\)\?.*/\1/g'`
ATTACH=`/etc/parser_subj $tmp`
:0
....��你自己去�吧...自己做�功�就�了
去看 /tmp/procmail.log 中的�西,就知道用意了
PATH=$PATH
SHELL=/bin/sh
MAILDIR=/var/spool/mail
VERBOSE=yes
LOGABSTRACT=all
SENDMAIL=/usr/sbin/sendmail
LOGFILE=$HOME/procmail/procmail.log
FILE=`echo $HOME`/procmail/mail_folder/mail-`date +%Y%m%d%H%I%S`-`echo $$`.eml
tmp=`formail >; $FILE; chmod 644 $FILE`
tmp=`formail -zxSubject:`
SUBJ=`/etc/parser_subj "$tmp"| sed -e "s/[\$,\(,\),\',\\]//g"`
FROM=`formail -zxFrom:| sed -e 's/.*<\(.*\)>;.*/\1/g'`
SPAMID=`formail -I ""|grep '/sc?id=' | sed -e 's/.*id=\(.*\)/\1/g'`
PTR=`formail -zxReceived:|grep ^from|sed -e 's/.*\[\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)\].*/\4.\3.\2.\1/g'|head -1`
MYSQL="mysql -h procmail.mydomain.net.tw -u abel -pxxxxxxxx procmail"
# ���取回�的信不再��
:0 H
* ^X-resend:
/var/spool/mail/$LOGNAME
# 白名��理
WHITE_LIST=`echo "SELECT WHITE_NAME FROM WHITE_LIST WHERE USERNAME='$LOGNAME' AND instr('$FROM', WHITE_NAME ) or instr('$SUBJ', WHITE_NAME )"| $MYSQL | tr '\n' '|' ; echo "aaaaaaaazzzzzzzzzggggggggg"`
:0 H
* $($WHITE_LIST)
$ORGMAIL
# �?#125;中巳�有 SPAM
:0 HD
* ^Subject: SPAM.*
{
:0 fw
| ( echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','SPAM','�?#125; SPAM')" | $MYSQL)
:0
/dev/null
}
# �信不�自己�位但 Message-ID �自己
:0 HD
* !^From: .*@mydomain.net.tw.*
* !^Received: .*211\.72\.21[0-1]\..*
* ^Message-Id: .*@mydomain.net.*
{
:0 fw
| echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','�信��','Message-ID � mydomain')" | $MYSQL
:0
/dev/null
}
# spamcop 的��信回函�存 mysql, 再以程式去 submit form
:0 H
* ^Subject: .*SpamCop has accepted.*
{
:0 fw
| ( echo "insert into SPAMCOP_ID(SPAM_ID) values('$SPAMID')" | $MYSQL)
:0
/dev/null
}
# 一些大的�告信商
:0 HD
* (^From:|^Received:|Return-Path:) .*(mailserver.idv.tw|mailserver.com|worldad.net|mailnews.com.tw|rayman.com.cn).*
{
:0 fw
| echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','�信��','mailserver 信商�出')" | $MYSQL
:0
/dev/null
}
# 收信人��字型�
:0 HD
* ^To: .*[0-9][0-9].txt.*
{
:0 fw
| echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','�信��','�字型收件者')" | $MYSQL
:0
/dev/null
}
# From: user@mydomain.net.tw 但不�自己人
:0 HD
* ^From: .*@mydomain.net.tw.*
* !^Received: .*211\.72\.21[0-1]\..*
{
:0 fw
| (username=${FROM%%@*};if [ -d /home/$username ] ; then formail -A "Bogus: No"; else formail -A "Bogus: Yes"; echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','寄件人不存在','$FROM ')" | $MYSQL ;fi );
:0
* ^Bogus: Yes
/dev/null
}
# 主旨��,�料存?mysql
:0 HD
* ! ^From: .*@mydomain.net.tw.*
{
patten=`echo "select concat(\"'\",PATTEN,\"'\") from SPAM_PATTEN where USERNAME='$LOGNAME' and instr('$SUBJ',PATTEN)"|$MYSQL | grep -v 'PATTEN'| tr '\n' ','`
patten2=`echo $patten | sed -e "s/'//g"`
:0 fw
* ? test -n "$patten"
| ( formail -A "Spam: $patten" ; \
echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','符合特定字串','$patten2'); update SPAM_PATTEN set CNT=CNT+1 where USERNAME='$LOGNAME' and PATTEN in ($patten ''); " | $MYSQL ;\
)
:0
* ^Spam: .*
/dev/null
}
# RBL ��, rbl doamin 存於 mysql
:0
* ! ^From: .*@(mydomain.net.tw|mydomain.net).*
* ! ^Received: .*159\.226\.*
{
rbl=`for spam in $(echo "select RBL_DOMAIN from SPAM_SOURCE order by SN"| $MYSQL | grep -v 'RBL_DOMAIN'); do rbl=$(host $PTR.$spam| grep 'has address'); test -n "$rbl" && echo $PTR.$spam && break; done`
:0 fw
|( test -n "$rbl" && (echo "insert into SPAM(MAIL_FROM,RCPT_TO,SUBJ,FILE_NAME,MAIL_TYPE,USERNAME,REASON,PATTEN ) values('$FROM ','$TO ','$SUBJ ','$FILE',1,'$LOGNAME','�自 RBL IP','$rbl.$spam')" | $MYSQL) && formail -A "Spam: From_RBL $rbl.$spam" )
:0
* ^Spam: .*
/dev/null
}
�果呈�:
http://211.72.210.251/spamcop.html
http://211.72.210.251/spamcop.html
2007-11-04
r01ustc
没有评论:
发表评论