Monday, January 10, 2011

block IP ต่างประเทศ ไม่ให้ FTP/SSH เข้ามาได้

#************************************************
#สั่งเคลีย Firewall ก่อน
iptables -F
#สั่งเคลีย Firewall ก่อน
iptables -X
##ส่วนนี้ใช้สำหรับlist ip ของ FTP มาจาก iplist ท่าน icez
iptables -N ftp_pass
for i in `wget -qO – http://www.icez.net/files/thaiiplist`; do
iptables -A ftp_pass -s $i -j ACCEPT
done
## ในส่วนนี้ทำไว้สำหรับ Block IP ในกรณีฉุกเฉินเช่นตอนโดนยิง
## วิธี Block ก็ iptables -A block_ip -s xx.xx.xx.xx -j DROP
iptables -N block_ip
iptables -A INPUT -j block_ip
## ในส่วนนี้ทำไว้สำหรับ Block Email ดึงข้อมูลจาก www.spamhaus.org
iptables -N block_email
iptables -A INPUT -p tcp –dport 25 -j block_email
curl -s http://www.spamhaus….drop/drop.lasso |grep ^[1-9]|cut -f 1 -d ‘ ‘ | xargs -iX -n 1 /sbin/iptables -A block_email -s X -j DROP
#ป้องกัน scan Port หรือเปล่านะ
iptables -A INPUT -p tcp ! –syn -m state –state NEW -j DROP
#ตรวจเช็ค State Full
iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
#เฉพาะ FTP เท่านั้นที่เราจะกรอง ก็สั่งให้กระโดดไป ftp_pass
iptables -A INPUT -p tcp –dport 21 -j ftp_pass
iptables -A INPUT -p tcp –dport 20 -j ACCEPT
#เปิด ให้เข้ามา access เฉพาะ Port ที่ต้องการ
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
iptables -A INPUT -p tcp –dport 25 -j ACCEPT
iptables -A INPUT -p tcp –dport 110 -j ACCEPT
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
iptables -A INPUT -p tcp –dport 443 -j ACCEPT
iptables -A INPUT -p tcp –dport 2222 -j ACCEPT
iptables -A INPUT -p udp –dport 53 -j ACCEPT
#ส่วนนี้ให้ดูจาก proftpd คือว่าเราเปิด port 21 ใช้ในการเชือมต่อ แต่ตอนรับส่งข้อมูลจะใช้ port 35000-35999
iptables -A INPUT -p tcp –dport 35000:35999 -j ACCEPT
# icmp ก็รับด้วยเดี่ยว ping ไม่เจอ
iptables -A INPUT -p icmp -j ACCEPT
#นอก เหนือจาก port ที่เราเปิด ก็ Block ซ่ะ (*** ระวังคำสั่งนี้ให้ดีครับ ถ้าทำบรรทัดนี้แล้วอาจจะ ssh ไม่ได้ ถ้าไม่มั่นใจให้เปิดเฉพาะ IP ตัวเองก่อน)
iptables -A INPUT -j DROP
#****************************************

0 comments:

Post a Comment

 
Design by GURU