連接上互聯網的linux主機不免有人探測你的ssh的密碼
最近發現我的機器被來自各個國家的黑客不停測在暴力猜測密碼,經不起這樣的折騰!
後來發現了一款軟體可以阻止這樣的攻擊,很奏效的!
下面是我的配置:
下載源碼包安裝
地址:http://www.fail2ban.org/wiki/index.php/Main_Page
上傳到伺服器上
安裝
#tar xvfj fail2ban-0.8.4.tar.bz2
#cd fail2ban-0.8.4
#python setup.py install
安裝完成後服務會自動設置成啟動的
在之前需要將系統的IPTABLES啟動
配置:
# vi /etc/fail2ban/jail.conf
# 忽悠 IP範圍 如果有二組以上用空白做為間隔
ignoreip = 127.0.0.1
# 設定 IP 被封鎖的時間(秒),如果值為 -1,代表永遠封鎖
bantime = 86400
# 設定在多少時間內達到 maxretry 的次數就封鎖
findtime = 600
# 允許嘗試的次數
maxretry = 3
[ssh-iptables]
# 開啟ssh阻止規則
enabled = true
filter = sshd
action = iptables[name=SSH, port=22, protocol=tcp]
#不發送郵件
#sendmail-whois[name=SSH, dest=you@mail.com, sender=fail2ban@mail.com]
#ssh 失敗日誌路徑
logpath = /var/log/secure
啟動fail2ban
service fail2ban start
查看iptables 的規則多出了 iptables-ssh的規則
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
fail2ban-SSH tcp -- anywhere anywhere tcp dpt:ssh
fail2ban-SSH tcp -- anywhere anywhere tcp dpt:ssh
Chain fail2ban-SSH (2 references)
target prot opt source destination
DROP all -- 122.102.64.54 anywhere
# 這有一個被阻止的IP 拒絕時間根據在你的配置文件設置時間有關 我設置的是一天
RETURN all -- anywhere anywhere
在其他的機器上ssh 192.168.1.2
連續三次輸錯密碼
使用命令fail2ban-client status ssh-iptables
查看阻止狀態
或者
fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: ssh-iptables
測試結果:
#fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
| |- File list: /var/log/secure
| |- Currently failed: 0
| `- Total failed: 3
`- action
|- Currently banned: 1
| `- IP list: 192.168.1.1
`- Total banned: 1
我們在來看看fail2ban的日誌記錄
2010-05-17 16:57:23,964 fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.1.1
2010-05-17 21:35:00,449 fail2ban.actions: WARNING [ssh-iptables] Ban 218.108.85.244
2010-05-18 03:56:34,986 fail2ban.actions: WARNING [ssh-iptables] Ban 59.39.66.30
記錄了被阻止的IP
成功阻止了ssh 密碼猜測
這只是阻止其中的一種ssh攻擊,配置文件中還有其他的服務配置,等有時間了我慢慢的貼上來!
[火星人 ] 利用fail2ban阻止ssh暴力破解密碼已經有1243次圍觀