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秒鐘內掃描超過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中的service命令與systemctl命令有何區(qū)別
在Linux中,service?和?systemctl?是兩個至關重要且極其相似的命令,它們如此相似,以至于人們很容易想知道它們之間有何不同,是否可以互換使用,下面我們就來看看之前的區(qū)別到底有哪些吧2023-08-08Shell腳本中判斷輸入變量或者參數(shù)是否為空的方法
這篇文章主要介紹了Shell腳本中判斷輸入變量或者參數(shù)是否為空的方法,本文總結了5種方法,并分別給出了代碼實例,需要的朋友可以參考下2014-10-10