Update: Postfix + Spamassassin + SPF + Greylist

ผม upgrade postfix ใหม่ ทีแรกนึกว่าจะยุ่งยาก แต่ก็แค่
#make
#make upgrade

ทีนี้พอลงใหม่ SPF ที่เคยติดตั้งไว้ก็อันตระธารหายไปด้วย พอดีไปเจอวิธีการติดตั้ง postfix + SpamAssassin + Procmail ที่นี่

Postfix + site-wide SpamAssassin + Procmail for individual ‘spam’ mailboxes.
(http://www.geekly.com/entries/archives/00000155.htm)
วิธีข้างล่างนี้เอามาจากเว็บข้างบนครับ ผมใส่รายละเอียดเพิ่มเติมลงไป

1. Install postfix and get it working for your domain. See http://www.postfix.org for details.

2. Install SpamAssassin. The easiest way to do this is with the command “perl -MCPAN -e ‘install Mail::SpamAssassin'”
Note:
- ต้องสั่ง #export LANG=en_US ก่อน
- ถ้ายังไม่มี CPAN ต้องสั่ง #up2date perl-CPAN

3. As root, create a file at /usr/bin/postfixfilter with the following content:

#!/bin/bash
/usr/bin/spamc | /usr/sbin/sendmail -i "$@"
exit $?

4. Run “chmod 755 /usr/bin/postfixfilter”

5. Create a user called ‘spamfilter’. Make it a complete user, with home directory and shell.

6. Run “chown spamfilter /usr/bin/postfixfilter”

7. In /etc/postfix/master.cf in the “Services” section, alter the ‘smtp’ line as follows (the ” -o con…” SHOULD be on the next line:
Note: ข้อควรระวังก็คือ ให้ระวังว่า spamfilter จะซ้ำกับ filter ที่มีแล้วหรือไม่ ถ้ามีก็ต้องเปลี่ยนเป็นชื่ออื่น เช่น spamfilter_smtp

smtp inet n - n - - smtpd
-o content_filter=spamfilter:

8. In /etc/postfix/master.cf in the “Interfaces to non-Postfix software” section add:
Note: ข้อควรระวังก็คือ ให้ระวังว่า spamfilter จะซ้ำกับ filter ที่มีแล้วหรือไม่ ถ้ามีก็ต้องเปลี่ยนเป็นชื่ออื่น เช่น spamfilter_smtp

spamfilter unix - n n - - pipe
flags=Rq user=spamfilter argv=/usr/bin/postfixfilter -f ${sender} -- ${recipient}

9. Restart postfix and make sure you can still get email. Send a fake spam through the system (from an outside address) to verify that spam is marked as spam. For information on configuring SpamAssassin, go to http://www.spamassassin.org.

10. Install ‘procmail’ if it is not already installed. To discover this try ‘which procmail’.

11. For each user who needs a ‘spam’ mailbox, follow steps 12 – 13. You can safely stop right here if you do not want any users to have their spam filtered into another mailbox.

12. Create a file in their home directory, owned by them, called ‘.procmailrc’. Enter the following into it, making sure that the locations listed are what you wish to use for mailbox location (here we are using /home/$username/mail/ as the location for mail folders:

MAILDIR=$HOME/mail
DEFAULT=$HOME/mail/Inbox
LOGFILE=$HOME/mail/proc.log

# Catch SPAM
:0
* ^X-Spam-Flag: YES
* ^X-Spam-Level: ******
spam

Explanation of the above –

MAILDIR is the location of mailbox folders
DEFAULT is the default mailbox.
“* ^X-Spam-Flag: YES” makes sure the email is marked as spam before doing anything with it.
“* ^X-Spam-Level: ******” make sure the spam received a ‘spam score’ of at least 6 before doing anything with it. This ensures that ‘borderline’ cases that are more likely to be ‘false positives’ (meaning they have been falsely marked as spam) do not go into the spam mailbox.
‘spam’ is the name of the mailbox they are going into.

13. Create a file in their home directory, owned by them, called ‘.forward’. Enter the following into it, changing ‘username’ to their username. Include the quotes and the pound (“#”) symbol!

"|IFS=' ' && exec /usr/bin/procmail || exit 75 #username"

14. As with any change made to user’s mailboxes, test!

15. Keep SpamAssassin updated with the same procedure you used to install it in #2.

หลังจากนั้นก็ติดตั้ง SPF (Sender Policy Framework) โดยอ้างอิงจากที่นี่
Filtering spam with Postfix
(http://www.freesoftwaremagazine.com/free_issues/issue_02/focus_spam_postfix/)

1. แก้ไข master.cf และเพิ่มเข้าไปดังนี้

spfpolicy unix - n n - - spawn user=nobody argv=/usr/bin/perl /usr/local/libexec/postfix/smtpd-policy.pl

2. ไฟล์ smtpd-policy.pl เอามาจากที่นี่ http://spf.pobox.com/postfix-policyd.txt

3. ลองสั่ง perl smtpd-policy.pl อาจจะได้รับข้อความ error ถ้ายังไม่ติดตั้ง SPF::Query
Note: วิธีการติดตั้ง SPF::Query
export LANG=en_US
perl -MCPAN -e 'install Mail::SPF::Query'

4. เพิ่มคำสั่งต่อไปนี้ใน smtpd_recipient_restrictions
check_policy_service unix:private/spfpolicy

5. restart postfix แล้วก็ทดสอบ spoof email address ดูว่า SPF ทำงานหรือไม่

เท่านี้ก็เป็นอันเสร็จ 🙂

ทีนี้ผมลองมาใช้ Greylist จาก Greylisting.org ช่วยสะกัด spam mail อีกชั้นหนึ่ง (http://greylisting.org/implementations/postfix.shtml)
(http://www.postfix.org/SMTPD_POLICY_README.html#greylist)

วิธีติดตั้งก็ตามข้างล่าง

1. ก๊อปปี้ไฟล์ graylist.pl ซึ่งจะมีมาพร้อมกับ source code ของ postfix อยู่แล้ว ไปไว้ที่ /usr/libexec/postfix/
cp postfix-2.2.X/examples/smtpd-policy/greylist.pl /usr/libexec/postfix/

2. แก้ไขไฟล์ greylist.pl
$database_name="/etc/postfix/mta/greylist.db";
$greylist_delay=60;

3. สร้างไดเรกทอรี /etc/postfix/mta แล้ว chown ให้เป็นของ nobody
#mkdir /etc/postfix/mta
#chown nobody.nobody /etc/postfix/mta

4. ทดสอบดูว่า greylist.pl ทำงานได้รึเปล่า โดยใช้คำสั่ง
perl greylist.pl
Note: ถ้าไม่มี DB_File.pm ก็จะต้องทำการติดตั้งก่อน
perl -MCPAN -e 'install DB_File'

5. แก้ไขไฟล์ master.cf เพิ่มคำสั่งต่อไปนี้
greypolicy unix - n n - - spawn user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl

6. แก้ไขไฟล์ main.cf เพิ่มคำสั่งต่อไปนี้ใน smtpd_recipient_restrictions
check_policy_service unix:private/greypolicy

7.restart postfix แล้วก็ทดสอบ ดูว่า greylist ทำงานหรือไม่

*ปัญหา greylist กับ gmail.com
หลังจากทดสอบ greylist ไปได้พักหนึ่งผมก็พบว่า gmail.com ใช้ rproxy.gmail.com ซึ่งมีทั้งหมดกว่า 16 IP Address และก็จะสุ่มเปลี่ยนไปเรื่อย ทำให้มีปัญหากับ greylist ที่คอยตรวจสอบ IP Address ในฐานข้อมูลผู้ส่ง ดังนั้นก็เลยต้องให้สิทธิพิเศษ gmail สักหน่อย โดยการเพิ่มคำสั่ง check_sender_access เอาไว้ก่อน greypolicy แต่ให้อยู่หลัง spfpolicy เพื่อให้ผ่าน spfpolicy ก่อน

check_policy_service unix:private/spfpolicy,
check_sender_access hash:/etc/postfix/maps/access_sender,
check_policy_service unix:private/greypolicy

ทีนี้ในไฟล์ access_sender ก็ใส่บรรทัดนี้ลงไป
gmail.com OK

เท่านี้ก็เรียบร้อย

Leave a Reply

Your email address will not be published. Required fields are marked *