Spamhaus เป็นเวปไซต์ที่คอยเก็บรวมรวมหมายเลขไอพีที่มีพฤติกรรมที่ไม่ปกติเช่นส่ง Spam Email , DDOS Attack ทำการรบกวนเครือข่าย Internet ซึ่งทาง Spamhaus ก็ได้มีการเปิดเผยหมายเลข IP ที่ติด blacklist แจกไว้ในเวปไซต์โดยจะมีการอัพเดทฐานข้อมูลทุกวัน สามารถดูได้ที่ https://www.spamhaus.org/drop/drop.lasso
ในบทความนี้จะแนะนำการนำเอา IP Blacklist เหล่านี้มาทำการใส่ไปใน Chain ของ IPTables ซึ่งเป็น firewall ที่ติดอยู่กับ linux ทุกเครื่องอยู่แล้ว โดยมีขั้นตอนดังนี้
สร้างไฟล์ spamhaus.sh
vi spamhaus.sh
#!/bin/bash
# based off the following two scripts
# http://www.theunsupported.com/2012/07/block-malicious-ip-addresses/
# http://www.cyberciti.biz/tips/block-spamming-scanning-with-iptables.html
# path to iptables
IPTABLES="/sbin/iptables";
# list of known spammers
URL="www.spamhaus.org/drop/drop.lasso";
# save local copy here
FILE="/tmp/drop.lasso";
# iptables custom chain
CHAIN="Spamhaus";
# check to see if the chain already exists
$IPTABLES -L $CHAIN -n
# check to see if the chain already exists
if [ $? -eq 0 ]; then
# flush the old rules
$IPTABLES -F $CHAIN
echo "Flushed old rules. Applying updated Spamhaus list...."
else
# create a new chain set
$IPTABLES -N $CHAIN
# tie chain to input rules so it runs
$IPTABLES -A INPUT -j $CHAIN
# don't allow this traffic through
$IPTABLES -A FORWARD -j $CHAIN
echo "Chain not detected. Creating new chain and adding Spamhaus list...."
fi;
# get a copy of the spam list
wget -qc $URL -O $FILE
# iterate through all known spamming hosts
for IP in $( cat $FILE | egrep -v '^;' | awk '{ print $1}' ); do
# add the ip address log rule to the chain
$IPTABLES -A $CHAIN -p 0 -s $IP -j LOG --log-prefix "[SPAMHAUS BLOCK]" -m limit --limit 3/min --limit-burst 10
# add the ip address to the chain
$IPTABLES -A $CHAIN -p 0 -s $IP -j DROP
echo $IP
done
echo "Done!"
# remove the spam list
unlink $FILE
ทำให้สามารถ execute ได้
chmod +x spamhaus.sh
ทำการรัน script เพื่อสร้าง rule ใน IPTables
sh spamhaus.sh
ทดสอบดู IPTables rule
iptables -nL
ข้อมูลจาก Spamhaus จะเปลี่ยนแปลงและอัพเดท blacklist ip ทุกวันเราจึงจำเป็นต้องมี crontab เพื่ออัพเดทฐานข้อมูลวันละครั้งด้วยครับ โดยให้ทำการเพิ่มคำสั่งด้านล่างไว้ในไฟล์ /etc/crontab
0 3 * * * /usr/bin/spamhaus
หรือติดตั้งผ่าน script ด้านล่าง
wget https://hostings.ruk-com.in.th/files/blockspam.sh chmod +x blockspam.sh sh blockspam.sh



![วิธีตั้งค่า SpamAssassin ใน DirectAdmin [Evolution Skin] directadmin-cpanel-easy-control](https://help.ruk-com.in.th/wp-content/uploads/2022/04/directadmin-cpanel-easy-control-267x133.png)

