3/24/2011

Linux : Debain fail2ban

Install Fail2ban

#apt-get install fail2ban
หลังจากลงเสร็จแล้วให้ทำการ copy ไฟล์ใหม่ขึ้นมานะครับเพื่อทำการแก้ไข

config file
#/etc/fail2ban/jail.conf

Edit Config file
#nano /etc/fail2ban/jail.local

[DEFAULT]
ignoreip = 127.0.0.1 # เป็น ip ที่จะไม่มีการ Ban
bantime =600 # เป็นเวลาที่จะ Ban IPสามารถเพิ่มลดได้ตามต้องการครับ
maxretry = 3 # คือตัวระบุการทำผิดพลาด 3ครั้งสามารถตั้งได้ตามต้องการ

[ssh-ddos]
enabled = true # ผมเปลี่ยนจาก false เป็น true เพื่อเพิ่มการตรวจสอบ SSH DoS
port = ssh, sftp
filter = sshd-ddos

เมื่อทำการแก้ไข config เสร็จแล้วให้ทำการ restart หervice ให้ทำงานได้เลยครับ

#/etc/init.d/fail2ban restart
และคุณสามารถเช็ค log ได้ที่นี่ครับ

#tail /var/log/fail2ban.log
อยากรู้ว่าใช้งานได้ไม่ได้นั้นลองทำการ login ได้เลยครับใส่มั่วๆลองเข้าไปดูแล้วรอเวลาที่เราตั้งให้มัน unban เองครับหรือว่ามีอีกเครื่องนึงก็ลอง ssh เข้าไปดู

#tail /var/log/fail2ban.log
iptables -X fail2ban-[name]
2007-11-28 13:25:59,853 fail2ban.actions.action: INFO
Set actionStart = iptables -N fail2ban-[name]
iptables -A fail2ban-[name]. -j RETURN
iptables -I INPUT -p -m multiport –dports
[port] -j fail2ban-[name]
2007-11-28 13:25:59,854 fail2ban.actions.action: INFO
Set actionUnban = iptables -D fail2ban-[name] -s -j DROP
2007-11-28 13:25:59,855 fail2ban.actions.action: INFO
Set actionCheck = iptables -n -L INPUT | grep -q fail2ban-[name]
2007-11-28 19:01:00,896 fail2ban.actions: WARNING [ssh] Ban 124.121.118.234
2007-11-28 19:03:46,896 fail2ban.actions: WARNING [ssh] Ban 202.142.215.206

การ unban

root@bebian:/home/redcode# iptables -n -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
fail2ban-ssh tcp — 0.0.0.0/0 0.0.0.0/0 multiport dports 22,115

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain fail2ban-ssh (1 references)
target prot opt source destination
DROP 0 — 202.142.215.206 0.0.0.0/0
DROP 0 — 124.121.118.234 0.0.0.0/0
RETURN 0 — 0.0.0.0/0 0.0.0.0/0
แล้วจากนั้นก็ใส่ DROP iptables โดยระบุ IP ที่เราต้องการ DROP ไปซ่ะ

root@bebian:/home/redcode# iptables -D fail2ban-ssh -s 124.121.118.234 -j DROP
DROP แล้วทำการเช็คดู iptable ดูครับว่า IP ที่เรา DROP ไปนั้นออกไปยัง

root@bebian:/home/redcode# iptables -n -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
fail2ban-ssh tcp — 0.0.0.0/0 0.0.0.0/0 multiport dports 22,115

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain fail2ban-ssh (1 references)
target prot opt source destination
DROP 0 — 202.142.215.206 0.0.0.0/0
RETURN 0 — 0.0.0.0/0 0.0.0.0/0
สังเกตุได้ว่า IP 124.121.118.234 ออกไปจากระบบแล้วเพียงเท่านี้ก็สามารถ login ได้ปกติ