|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1730064
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2010/3/29 10:12 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15773
|
- [轉貼]防 ssh 暴力攻擊 使用iptables & shell
- 防 ssh 暴力攻擊 使用iptables & shell @ CentOS
作者: phptw 日期: 2008-06-30 22:06
原則上 ssh port 能不要全部開啟事最好的,如果一定要開啟的話,必需要作些防護措施,
port 22 一旦開啟後最容易遇到的就是會有人不斷的try密碼
設計原理: 抵擋 ssh 暴力攻擊 如果連續攻擊3次以上則抵擋 利用 TCP-Wrapper 偵測是否有登入失敗 ,有失敗才執行shell
mkdir /etc/firewall/
touch /etc/firewall/sshd.sh
chmod 755 /etc/firewall/sshd.sh
vi /etc/firewall/sshd.sh
寫入以下資料
#!/bin/bash
# 檔案存放的路徑
basedir="/etc/firewall"
# log 存放的地方
sshlog="/var/log/auth.log"
# mail for
mailfor="root"
# 登入錯誤訊息
faildMsg="sshd.*Failed password"
faildMsgInvalid="sshd.*Failed password for invalid user"
# 登入幾次失敗 就擋掉
loginCountFail=3
# 計算前 XXXX 行是否有登入失敗的狀況
failcount=`/usr/bin/tail -n 50 "$sshlog" | /bin/grep "$faildMsg" | wc -l`
#cat "$sshlog" | grep "sshd.*Failed password for" | grep -v "invalid" | cut -d " " -f11
#cat "$sshlog" | grep "sshd.*Failed password for invalid user" | cut -d " " -f13
# 如果有登入失敗的話
if [ "$failcount" -gt "0" ]; then
# 建立 登入失敗 暫存檔
#cat /var/log/auth.log | grep "sshd.*Failed password for" | grep -v "invalid"
cat "$sshlog" | grep "sshd.*Failed password for" | grep -v "invalid" > /tmp/ssh_tmp.log
#cat /var/log/auth.log | grep "sshd.*Failed password for" | grep -v "invalid"
cat "$sshlog" | grep "sshd.*Failed password for invalid user" > /tmp/ssh_tmp2.log
# 建立 登入失敗 IP 暫存檔
#cat "$sshlog" | grep "$faildMsg" | cut -d " " -f13 | sort | uniq > /tmp/ssh_ip_tmp.log
cat /tmp/ssh_tmp.log | cut -d " " -f11 > /tmp/ssh_ip_tmp.log
cat /tmp/ssh_tmp2.log | cut -d " " -f13 >> /tmp/ssh_ip_tmp.log
# 取得要封鎖的IP位址
#blockip=`cat /tmp/ssh_ip_tmp.log`
blockip=`cat /tmp/ssh_ip_tmp.log | sort | uniq`
# iptables中,找出定義的ruleexistchar中的ip。
#iptables -L INPUT -n | grep "tcp dpt:22" | grep "DROP" | awk '{print $4}' | sort | uniq
ruleexistip=`iptables -L INPUT -n | grep "tcp dpt:22" | grep "DROP" | awk '{print $4}' | sort | uniq`
blacklist=`cat "$basedir"/ssd_blacklist_d`
for ip in $blacklist
do
`iptables -D INPUT -p TCP -s $ip --dport 22 -j DROP` > /dev/null
done
rm -rf "$basedir"/ssd_blacklist_d
# 利用 for 迴圈去跑 drop IP
for ip in $blockip
do
# 計算此IP登入幾次失敗
count=`cat /tmp/ssh_ip_tmp.log | grep "$ip" | wc -l`
# 如果登入失敗次數大於 預設值 則執行 drop
if [ "$count" -gt "$loginCountFail" ] ; then
#echo $ip $count
# 如果從secure找出的ip已存在於iptables,就不需要加這條rule了。
if [ "$ip" = "$ruleexistip" ]; then
:
else
`iptables -I INPUT -p TCP -s $ip --dport 22 -j DROP`
fi
# if [ "$ip" = "$ruleexistip" ];
# 建立黑名單
echo "$ip" >> "$basedir"/ssd_blacklist_d
cat "$basedir"/ssd_blacklist_d | sort | uniq > "$basedir"/ssd_blacklist_d
fi
# if [ "$count" -gt "$loginCountFail"] ;
done
# for loop done
# 刪除暫存檔
rm -rf /tmp/ssh*.log
mail -s "ssh hacker 抵擋的IP位址" $mailfor < "$basedir"/ssd_blacklist_d
fi
# if [ "$failcount" -gt "0" ];
然後在 加入 sshd : ALL : spawn /etc/firewall/sshd.sh
參考資料: http://www.andowson.com/posts/list/33.page http://ssorc.tw/rewrite.php/read-93.html
如果在加上 root 的權限控管會更好,限制 root 的權限只有特定的管理者可以取得權限
原文出處:[Linux] 防 ssh 暴力攻擊 使用iptables & shell @ CentOS - linux - 墮落程式
|
|
討論串
-
[分享]簡易 NAT 伺服器 (冷日 (冷日), 2005/2/19 19:51)
-
[分享]PROXY.IP Masquerading.Transparent Proxy.工作原理 (冷日 (冷日), 2005/2/19 19:58)
-
[分享]iptables/chains + squid transparent proxy (冷日 (冷日), 2005/2/19 20:05)
-
[分享]使用 iptables 設定使用 NAT 分享網路頻寬 (冷日 (冷日), 2005/2/19 20:06)
-
[分享]使用 iptables 設定一些安全防護功能 (1) (冷日 (冷日), 2005/2/19 20:08)
-
[分享]使用 iptables 設定一些安全防護功能 (2) (冷日 (冷日), 2005/2/19 20:11)
-
[分享]使用 iptables 設定一些安全防護功能 (3) (冷日 (冷日), 2005/2/19 20:12)
-
[分享]IPtables與防火牆 (冷日 (冷日), 2005/2/19 20:18)
-
[轉貼]How to bind 2 WAN 1 LAN (冷日 (冷日), 2005/2/19 20:23)
-
[分享]Linux 上多條對外連線(Multi-Path)實作 (冷日 (冷日), 2005/2/19 20:26)
-
[分享]iptables ─ 功能看齊 enterprise 防火牆 (冷日 (冷日), 2005/2/25 14:52)
-
[分享]iptables經典設置 (冷日 (冷日), 2005/5/18 19:42)
-
[分享]iptables動態NAT對應測試 (冷日 (冷日), 2005/11/4 6:44)
-
[分享]一張網卡 bind 兩個不同網段的 IP (冷日 (冷日), 2005/11/4 7:00)
-
[分享]Linux 指令簡單將攻擊IP列入iptables 限制範圍 (冷日 (冷日), 2008/4/24 6:21)
- »
[轉貼]防 ssh 暴力攻擊 使用iptables & shell (冷日 (冷日), 2010/3/29 10:12)
-
[轉貼]自動擋掉嘗試用ssh入侵Linux的攻擊者IP (冷日 (冷日), 2010/3/29 10:16)
|