shell腳本結合iptables防端口掃描的實現(xiàn)
更新時間:2014年05月27日 10:16:01 作者:
這篇文章主要介紹了shell腳本結合iptables防端口掃描的實現(xiàn),中間使用了inotify-tools工具,需要的朋友可以參考下
網(wǎng)上有現(xiàn)在的防端口工具,如psad、portsentry,但覺得配置有點麻煩,且服務器不想再裝一個額外的軟件。所以自己就寫了個shell腳本實現(xiàn)這個功能?;舅悸肥牵菏褂胕ptables的recent模塊記錄下在60秒鐘內(nèi)掃描超過10個端口的IP,并結合inotify-tools工具實時監(jiān)控iptables的日志,一旦iptables日志文件有寫入新的ip記錄,則使用iptables封鎖源ip,起到了防止端口掃描的功能。
1、iptables規(guī)則設置
新建腳本iptables.sh,執(zhí)行此腳本。
復制代碼 代碼如下:
IPT="/sbin/iptables"
$IPT --delete-chain
$IPT --flush
#Default Policy
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT DROP
#INPUT Chain
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
$IPT -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
$IPT -A INPUT -p tcp --syn -m recent --name portscan --rcheck --seconds 60 --hitcount 10 -j LOG
$IPT -A INPUT -p tcp --syn -m recent --name portscan --set -j DROP
#OUTPUT Chain
$IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
$IPT -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
$IPT -A OUTPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
#iptables save
service iptables save
service iptables restart
$IPT --delete-chain
$IPT --flush
#Default Policy
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT DROP
#INPUT Chain
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
$IPT -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
$IPT -A INPUT -p tcp --syn -m recent --name portscan --rcheck --seconds 60 --hitcount 10 -j LOG
$IPT -A INPUT -p tcp --syn -m recent --name portscan --set -j DROP
#OUTPUT Chain
$IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
$IPT -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
$IPT -A OUTPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
#iptables save
service iptables save
service iptables restart
注意:17-18行的兩條規(guī)則務必在INPUT鏈的最下面,其它規(guī)則自己可以補充。
2、iptables日志位置更改
編輯/etc/syslog.conf,添加:
復制代碼 代碼如下:
kern.warning /var/log/iptables.log
重啟syslog
復制代碼 代碼如下:
/etc/init.d/syslog restart
3、防端口掃描shell腳本
首先安裝inotify:
復制代碼 代碼如下:
yum install inotify-tools
保存以下代碼為ban-portscan.sh
復制代碼 代碼如下:
btime=600 #封ip的時間
while true;do
while inotifywait -q -q -e modify /var/log/iptables.log;do
ip=`tail -1 /var/log/iptables.log | awk -F"[ =]" '{print $13}' | grep '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}'`
if test -z "`/sbin/iptables -nL | grep $ip`";then
/sbin/iptables -I INPUT -s $ip -j DROP
{
sleep $btime && /sbin/iptables -D INPUT -s $ip -j DROP
} &
fi
done
done
while true;do
while inotifywait -q -q -e modify /var/log/iptables.log;do
ip=`tail -1 /var/log/iptables.log | awk -F"[ =]" '{print $13}' | grep '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}'`
if test -z "`/sbin/iptables -nL | grep $ip`";then
/sbin/iptables -I INPUT -s $ip -j DROP
{
sleep $btime && /sbin/iptables -D INPUT -s $ip -j DROP
} &
fi
done
done
執(zhí)行命令開始啟用端口防掃描
復制代碼 代碼如下:
nohup ./ban-portscan.sh &
相關文章
linux shell在while中用read從鍵盤輸入的實現(xiàn)
下面小編就為大家?guī)硪黄猯inux shell在while中用read從鍵盤輸入的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Linux下查找后門程序 CentOS 查后門程序的shell腳本
這篇文章主要介紹了Linux下查找后門程序 CentOS 查后門程序的shell腳本,需要的朋友可以參考下2014-09-09

