<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "自動擋掉嘗試用ssh入侵Linux的攻擊者IP"]]></title>
		<link>http://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=&quot;ppp0&quot; # you must edit this<br /> BANNEDHOSTFILE=&quot;/tmp/bannedhosts.txt&quot; #edit this as required<br /> HISTORYHOSTSFILE=&quot;/tmp/history.txt&quot; #edit this as required<br /> IPTABLES=&quot;/sbin/iptables&quot;<br /> GREP_PARAM=&quot;^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*&quot;<br /> <br /> # 找出攻擊的主機IP<br /> grep &quot;Failed password for invalid user&quot; /var/log/secure | cut -d&quot; &quot; -f13 | sort | uniq | cut -d&quot;:&quot; -f4 &gt; /tmp/attack.log<br /> grep &quot;Failed password for invalid user&quot; /var/log/secure | cut -d&quot; &quot; -f14 | sort | uniq | cut -d&quot;:&quot; -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 &quot;Deny access to host: $i&quot;<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">http://www.andowson.com/posts/preList/33/37.page</guid>
				<link>http://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&quot;=&quot; -f2 | sort | grep &quot;\.&quot; | uniq | grep -v &quot;192.168.1&quot; &gt;&gt; /tmp/attack.log<br /> [/code]]]></description>
				<guid isPermaLink="true">http://www.andowson.com/posts/preList/33/258.page</guid>
				<link>http://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">http://www.andowson.com/posts/preList/33/432.page</guid>
				<link>http://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/read-174.html][Linux] 防 ssh 暴力攻擊 使用iptables &amp; shell @ CentOS[/url]]]></description>
				<guid isPermaLink="true">http://www.andowson.com/posts/preList/33/433.page</guid>
				<link>http://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=&quot;eth0&quot; # value can be &quot;eth0&quot; or &quot;ppp0&quot;<br /> BANNED_HOST_FILE=&quot;/tmp/bannedhosts.txt&quot;<br /> HISTORY_HOSTS_FILE=&quot;/tmp/history.txt&quot;<br /> IPTABLES=&quot;/sbin/iptables&quot;<br /> GREP_PARAM=&quot;^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*&quot;<br /> TODAY=`date +%Y-%m-%d`<br /> SECURE_LOG=&quot;/tmp/secure.$TODAY&quot;<br /> FAILURE_LOG=&quot;/tmp/failure.$TODAY&quot;<br /> FAILED_LOG=&quot;/tmp/failed.$TODAY&quot;<br /> <br /> # 將資料資料範圍縮小到今天<br /> grep $TODAY /var/log/secure &gt; $SECURE_LOG<br /> grep &quot;authentication failure&quot; $SECURE_LOG | awk '{print $12}' | cut -d&quot;=&quot; -f2 &gt; $FAILURE_LOG<br /> # 因rhost可能是domain name方式，為避免重複計算次數，另存到一個檔案去<br /> grep &quot;Failed password for invalid user&quot; $SECURE_LOG | awk '{print $11}' &gt; $FAILED_LOG<br /> grep &quot;Failed password&quot; $SECURE_LOG | grep -v &quot;invalid user&quot; | 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 &quot;Deny access to host: $ip&quot;<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">http://www.andowson.com/posts/preList/33/913.page</guid>
				<link>http://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>
	</channel>
</rss>
