<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "自動擋掉嘗試用ssh入侵Linux的攻擊者IP"]]></title>
		<link>https://www.andowson.com/posts/list/16.page</link>
		<description><![CDATA[Latest messages posted in the topic "自動擋掉嘗試用ssh入侵Linux的攻擊者IP"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>自動擋掉嘗試用ssh入侵Linux的攻擊者IP</title>
				<description><![CDATA[ 在 Linux 系統上的 /var/log/secure 裡面常會發現一些類似底下的紀錄 
<br>
[quote]Jan 21 05:32:32 www sshd[24782]: Invalid user office from ::ffff:210.202.33.129 
<br>
Jan 20 21:32:32 www sshd[24783]: input_userauth_request: invalid user office 
<br>
Jan 21 05:32:35 www sshd[24782]: Failed password for invalid user office from ::ffff:210.202.33.129 port 61321 ssh2[/quote] 
<br>
<br>
可是系統上並沒有office這個使用者，而且這個210.202.33.129還出現在其他連續的多筆記錄上，可以知道這是個嘗試入侵的行為，我們可以寫支 shell script 程式來自動擋掉這些討厭的傢伙。 
<br>
<br>
將底下的程式碼複製存檔為 /root/admin/banip.sh 
<br>
[code]#!/bin/bash 
<br>
# Name: banip.sh 
<br>
# Author: Andowson Chang (andowson [at] gmail [dot] com) 
<br>
# Version: 0.1 
<br>
# Last Modified: 2007-01-21 
<br>
<br>
# 修改這邊的參數 
<br>
EXTERNAL_INTERFACE="ppp0" # you must edit this 
<br>
BANNEDHOSTFILE="/tmp/bannedhosts.txt" #edit this as required 
<br>
HISTORYHOSTSFILE="/tmp/history.txt" #edit this as required 
<br>
IPTABLES="/sbin/iptables" 
<br>
GREP_PARAM="^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*" 
<br>
<br>
# 找出攻擊的主機IP 
<br>
grep "Failed password for invalid user" /var/log/secure | cut -d" " -f13 | sort | uniq | cut -d":" -f4 &gt; /tmp/attack.log 
<br>
grep "Failed password for invalid user" /var/log/secure | cut -d" " -f14 | sort | uniq | cut -d":" -f4 &gt;&gt; /tmp/attack.log 
<br>
# 刪除一些不是IP的字，目前發現的有from和port，也可以包含測試用的來源IP 
<br>
sed -e '/from/d' -e '/port/d' -e '/192.168.1/d' /tmp/attack.log &gt; /tmp/attack.txt 
<br>
<br>
# 加入新增的主機 
<br>
touch $HISTORYHOSTSFILE 
<br>
sort /tmp/attack.txt | uniq &gt; /tmp/ip1 
<br>
sort $HISTORYHOSTSFILE | uniq &gt; /tmp/ip2 
<br>
comm -23 /tmp/ip[1-2] &gt; $BANNEDHOSTFILE # 新增站台資料 
<br>
rm -rf /tmp/ip[1-2] 
<br>
rm -rf /tmp/attack.* 
<br>
<br>
# 將攻擊的主機IP加到iptables擋掉 
<br>
for i in $( grep $GREP_PARAM $BANNEDHOSTFILE ) 
<br>
do 
<br>
echo "Deny access to host: $i" 
<br>
$IPTABLES -A INPUT -i $EXTERNAL_INTERFACE -s $i -j DROP 
<br>
$IPTABLES -A OUTPUT -o $EXTERNAL_INTERFACE -d $i -j DROP 
<br>
done 
<br>
<br>
# 將處理過的IP清單加到歷史檔去 
<br>
cat $BANNEDHOSTFILE &gt;&gt; $HISTORYHOSTSFILE 
<br>
sort $HISTORYHOSTSFILE | uniq &gt; /tmp/history.tmp 
<br>
mv -f /tmp/history.tmp $HISTORYHOSTSFILE 
<br>
rm -rf $BANNEDHOSTFILE[/code] 
<br>
<br>
然後chmod 755 /root/admin/banip.sh 
<br>
接著掛到/etc/crontab讓它每五分鐘執行一次 
<br>
[code]# ban intruder's ip (2007.01.21) 
<br>
*/5 * * * * root /root/admin/banip.sh &gt; /var/log/banip.log[/code] 
<br>
<br>
察看目前被擋掉的IP：iptables -L -n 
<br>
如果有誤加的IP，可以利用類似底下的指令來刪除設定 
<br>
[code]iptables -D INPUT -i ppp0 -s 192.168.1.5 -j DROP 
<br>
iptables -D OUTPUT -o ppp0 -d 192.168.1.5 -j DROP[/code] 
<br>
<br>
 :!:為避免重複加入iptables，已在$HISTORYHOSTSFILE裡面的ip，會被自動過濾掉。可是重開機之後iptables的設定會回到預設值，只剩下開放的服務port的設定，沒有擋掉任何主機的設定。如此一來，重開機後將不會再擋掉先前已在歷史檔內的主機。解決的方法是每次重開機後將歷史檔更名，如改為history.txt-yyyymmdd 
<br>
<br>
vi /etc/rc.local 
<br>
[code] 
<br>
mv /tmp/history.txt /tmp/history.txt-`date +%Y%m%d` 
<br>
[/code] 
<br>
參考資料：[url=http://phi.sinica.edu.tw/aspac/reports/96/96005/]SED 手冊[/url]]]></description>
				<guid isPermaLink="true">https://www.andowson.com/posts/preList/33/37.page</guid>
				<link>https://www.andowson.com/posts/preList/33/37.page</link>
				<pubDate><![CDATA[Sun, 21 Jan 2007 23:04:30]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:自動擋掉嘗試用ssh入侵Linux的攻擊者IP</title>
				<description><![CDATA[ 在原來的程式第16行下面加上這一行, 可以增加自動擋掉嘗試用ftp入侵的攻擊者: 
<br>
[code] 
<br>
grep failure messages | awk '{print $13}' | uniq | grep -v tty | cut -d"=" -f2 | sort | grep "\." | uniq | grep -v "192.168.1" &gt;&gt; /tmp/attack.log 
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://www.andowson.com/posts/preList/33/258.page</guid>
				<link>https://www.andowson.com/posts/preList/33/258.page</link>
				<pubDate><![CDATA[Sat, 22 Sep 2007 07:59:34]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:自動擋掉嘗試用ssh入侵Linux的攻擊者IP</title>
				<description><![CDATA[ 版主你好 
<br>
想請問一下，我使用了這程式 
<br>
在擋FTP那段，想請問一下如何判斷嘗試超過多少次才擋下IP呢 
<br>
因為不這樣的話，會連本身使用者的IP都會擋掉。 
<br>
這是FTP訊息 
<br>
[code]vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=Administrator rhost=88.208.230.36 
<br>
[/code] 
<br>]]></description>
				<guid isPermaLink="true">https://www.andowson.com/posts/preList/33/432.page</guid>
				<link>https://www.andowson.com/posts/preList/33/432.page</link>
				<pubDate><![CDATA[Mon, 11 Aug 2008 12:53:30]]> GMT</pubDate>
				<author><![CDATA[ doubleaisno1]]></author>
			</item>
			<item>
				<title>回覆:自動擋掉嘗試用ssh入侵Linux的攻擊者IP</title>
				<description><![CDATA[ 您可參考這篇文章的作法[url=http://blog.phptw.idv.tw/index.html/2008/06/linux-%E9%98%B2-ssh-%E6%9A%B4%E5%8A%9B%E6%94%BB%E6%93%8A-%E4%BD%BF%E7%94%A8iptables-shell-centos.html][Linux] 防 ssh 暴力攻擊 使用iptables &amp; shell @ CentOS[/url]]]></description>
				<guid isPermaLink="true">https://www.andowson.com/posts/preList/33/433.page</guid>
				<link>https://www.andowson.com/posts/preList/33/433.page</link>
				<pubDate><![CDATA[Mon, 11 Aug 2008 21:45:37]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:自動擋掉嘗試用ssh入侵Linux的攻擊者IP</title>
				<description><![CDATA[ 改用新的判斷方式並加入了次數的判斷，以當天失敗超過三次者才加入阻擋 
<br>
<br>
[code=bash] 
<br>
#!/bin/bash 
<br>
# Name: banip.sh 
<br>
# Author: Andowson Chang (andowson [at] gmail [dot] com) 
<br>
# Version: 0.3 
<br>
# Since: 2007-01-21 
<br>
# Last Modified: 2010-10-24 
<br>
<br>
# 修改這邊的參數 
<br>
EXTERNAL_INTERFACE="eth0" # value can be "eth0" or "ppp0" 
<br>
BANNED_HOST_FILE="/tmp/bannedhosts.txt" 
<br>
HISTORY_HOSTS_FILE="/tmp/history.txt" 
<br>
IPTABLES="/sbin/iptables" 
<br>
GREP_PARAM="^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*" 
<br>
TODAY=`date +%Y-%m-%d` 
<br>
SECURE_LOG="/tmp/secure.$TODAY" 
<br>
FAILURE_LOG="/tmp/failure.$TODAY" 
<br>
FAILED_LOG="/tmp/failed.$TODAY" 
<br>
<br>
# 將資料資料範圍縮小到今天 
<br>
grep $TODAY /var/log/secure &gt; $SECURE_LOG 
<br>
grep "authentication failure" $SECURE_LOG | awk '{print $12}' | cut -d"=" -f2 &gt; $FAILURE_LOG 
<br>
# 因rhost可能是domain name方式，為避免重複計算次數，另存到一個檔案去 
<br>
grep "Failed password for invalid user" $SECURE_LOG | awk '{print $11}' &gt; $FAILED_LOG 
<br>
grep "Failed password" $SECURE_LOG | grep -v "invalid user" | awk '{print $9}' &gt;&gt; $FAILED_LOG 
<br>
<br>
# 找出攻擊的主機IP 
<br>
cat $FAILURE_LOG $FAILED_LOG &gt; /tmp/attacker.log 
<br>
<br>
# 加入新增的主機 
<br>
touch $HISTORY_HOSTS_FILE 
<br>
sort /tmp/attacker.log | uniq &gt; /tmp/attacker_ip1 
<br>
sort $HISTORY_HOSTS_FILE | uniq &gt; /tmp/attacker_ip2 
<br>
comm -23 /tmp/attacker_ip[1-2] &gt; $BANNED_HOST_FILE # 新增主機資料 
<br>
rm -rf /tmp/attacker* 
<br>
<br>
# 將攻擊的主機IP加到iptables擋掉 
<br>
for ip in $( grep $GREP_PARAM $BANNED_HOST_FILE ) 
<br>
do 
<br>
 #計算失敗次數 
<br>
 failcount=`grep $ip $FAILURE_LOG | wc -l` 
<br>
 failcount2=`grep $ip $FAILED_LOG | wc -l` 
<br>
 #超過三次失敗者加入阻擋名單 
<br>
 if [ $failcount &gt; 3 ] || [ $failcount2 &gt; 3 ]; then 
<br>
 echo "Deny access to host: $ip" 
<br>
 $IPTABLES -A INPUT -i $EXTERNAL_INTERFACE -s $ip -j DROP 
<br>
 $IPTABLES -A OUTPUT -o $EXTERNAL_INTERFACE -d $ip -j DROP 
<br>
 # 將處理過的IP清單加到歷史檔去 
<br>
 echo $ip &gt;&gt; $HISTORY_HOSTS_FILE 
<br>
 fi 
<br>
done 
<br>
<br>
rm -rf $BANNED_HOST_FILE 
<br>
rm -rf $SECURE_LOG 
<br>
rm -rf $FAILURE_LOG 
<br>
rm -rf $FAILED_LOG 
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://www.andowson.com/posts/preList/33/913.page</guid>
				<link>https://www.andowson.com/posts/preList/33/913.page</link>
				<pubDate><![CDATA[Sat, 23 Oct 2010 21:38:41]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:自動擋掉嘗試用ssh入侵Linux的攻擊者IP</title>
				<description><![CDATA[ 今天發現透過檢查BANNED_HOSTS_HISTORY檔案內容有可能會漏封(先前有封過，重開機後因已在歷史檔中)，修改為透過iptables -L OUTPUT -n指令取得目前系統已封鎖之IP，修正後之檔案如下: 
<br>
<br>
[code=bash] 
<br>
#!/bin/bash 
<br>
# Name: banip.sh 
<br>
# Author: Andowson Chang (andowson [at] gmail [dot] com) 
<br>
# Version: 0.6 
<br>
# Since: 2007-01-21 
<br>
# Last Modified: 2013-06-09 
<br>
<br>
# 修改這邊的參數 
<br>
EXTERNAL_INTERFACE="eth0" # value can be "eth0" or "ppp0" 
<br>
BANNED_HOSTS="/tmp/bannedhosts.txt" 
<br>
BANNED_HOSTS_HISTORY="/tmp/history.txt" 
<br>
IPTABLES="/sbin/iptables" 
<br>
GREP_PARAM="^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*" 
<br>
export LANG=en_US 
<br>
TODAY=`date +%Y-%m-%d` 
<br>
RANGE=`date "+%b %e"` 
<br>
SECURE_LOG="/tmp/secure.${TODAY}" 
<br>
SUSPECTED="/tmp/failed.${TODAY}" 
<br>
<br>
# 將資料資料範圍縮小到今天 
<br>
grep "$RANGE" /var/log/secure &gt; $SECURE_LOG 
<br>
grep "Failed password for invalid user" $SECURE_LOG | awk '{print $13}' &gt; $SUSPECTED 
<br>
grep "Failed password" $SECURE_LOG | grep -v "invalid user" | awk '{print $11}' &gt;&gt; $SUSPECTED 
<br>
<br>
# 找出攻擊的主機IP 
<br>
cat $SUSPECTED | sort | uniq &gt; /tmp/attacker_ip1 
<br>
<br>
# 找出已被封鎖的主機IP 
<br>
$IPTABLES -L OUTPUT -n | grep DROP | awk '{print $5}' | sort | uniq &gt; /tmp/attacker_ip2 
<br>
<br>
# 比對差異，找出新增的IP 
<br>
comm -23 /tmp/attacker_ip[1-2] &gt; $BANNED_HOSTS # 新增主機資料 
<br>
rm -rf /tmp/attacker* 
<br>
<br>
# 將攻擊的主機IP加到iptables擋掉 
<br>
for ip in $( grep $GREP_PARAM $BANNED_HOSTS ) 
<br>
do 
<br>
 echo "Check $ip" 
<br>
 #計算失敗次數 
<br>
 failcount=`grep $ip $SUSPECTED | wc -l` 
<br>
 #超過三次失敗者加入阻擋名單 
<br>
 if [ $failcount &gt; 3 ]; then 
<br>
 echo "Deny access from host: $ip" 
<br>
 $IPTABLES -A INPUT -i $EXTERNAL_INTERFACE -s $ip -j DROP 
<br>
 $IPTABLES -A OUTPUT -o $EXTERNAL_INTERFACE -d $ip -j DROP 
<br>
 # 將處理過的IP清單加到歷史檔去 
<br>
 echo $ip &gt;&gt; $BANNED_HOSTS_HISTORY 
<br>
 fi 
<br>
done 
<br>
<br>
rm -rf $BANNED_HOSTS 
<br>
rm -rf $SECURE_LOG 
<br>
rm -rf $SUSPECTED 
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://www.andowson.com/posts/preList/33/1293.page</guid>
				<link>https://www.andowson.com/posts/preList/33/1293.page</link>
				<pubDate><![CDATA[Sun, 9 Jun 2013 13:32:08]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>