iptables防攻击自动黑白名单脚本
2011-12-28 09:51
295 查看
在网上也看到过iptables黑白名单的脚本,拿下来都不符合,现在写了个自动黑白名单的脚本,已经在线上跑的了,有兴趣的朋友可以拿去试下。
脚本是相对简单的
1、因为crontab任务是按分来执行任务的,所以写一个每秒都执行任务的脚本,脚本如下,脚本名为“ipt.sh”
#!/bin/bash
SLEEPTIME=5
CONN=150
iptables_drop()
{
netstat -an| grep :80 | grep -v 127.0.0.1 |grep ESTABLISHED |awk '{ print $5 }' | sort|awk -F: '{print $1,$4}' | uniq -c | awk '$1>150 {print $1,$2}' > /root/black.txt
[ -z /root/black.txt ] && exit 0
for i in `awk '{print $2}' /root/black.txt`
do
for lm in `grep "\<$i\>" /root/black.txt | awk '{print $1}'`
do
two=`grep "\<$i\>" /root/bak.txt`
if [ "$i" != "$two" ]; then
[ $lm -gt $CONN ] && grep "\<$i\>" /root/work.txt > /dev/null
if [ $? -ne 0 ]; then
echo $i >> /root/bak.txt;
/sbin/iptables -I INPUT -p tcp -s $i --dport 80 -j DROP;
echo "/sbin/iptables -D INPUT -p tcp -s $i --dport 80 -j DROP" | at `date -d '+20 minutes' +%H:%M` && \
echo "sed -i \"/\<$i\>/d\" /root/bak.txt" | at `date -d '+20 minutes' +%H:%M`
fi
fi
done
sleep 1
done
}
while true; do
iptables_drop
sleep $SLEEPTIME
done
详细:具体操作方法在/home/script下建立ipt.sh脚本,在/root目录下建个work.txt文件,里面写着白名单ip,这样这个白名单ip访问量再大也不会被X掉了,一般正常的访问量,单1秒的时间里同一个ip 过来的连接不会有超过150个的,所以判断1秒时间里web的连接数超过150个,直接用iptables拉黑,然后再用at任务在一定时间里将其还原,我这里设置为20分钟,基本上就是这样了......如那位朋友还有好的方案,可以拿出来一起分享,技术因为分享而快乐,呵呵
脚本是相对简单的
1、因为crontab任务是按分来执行任务的,所以写一个每秒都执行任务的脚本,脚本如下,脚本名为“ipt.sh”
#!/bin/bash
SLEEPTIME=5
CONN=150
iptables_drop()
{
netstat -an| grep :80 | grep -v 127.0.0.1 |grep ESTABLISHED |awk '{ print $5 }' | sort|awk -F: '{print $1,$4}' | uniq -c | awk '$1>150 {print $1,$2}' > /root/black.txt
[ -z /root/black.txt ] && exit 0
for i in `awk '{print $2}' /root/black.txt`
do
for lm in `grep "\<$i\>" /root/black.txt | awk '{print $1}'`
do
two=`grep "\<$i\>" /root/bak.txt`
if [ "$i" != "$two" ]; then
[ $lm -gt $CONN ] && grep "\<$i\>" /root/work.txt > /dev/null
if [ $? -ne 0 ]; then
echo $i >> /root/bak.txt;
/sbin/iptables -I INPUT -p tcp -s $i --dport 80 -j DROP;
echo "/sbin/iptables -D INPUT -p tcp -s $i --dport 80 -j DROP" | at `date -d '+20 minutes' +%H:%M` && \
echo "sed -i \"/\<$i\>/d\" /root/bak.txt" | at `date -d '+20 minutes' +%H:%M`
fi
fi
done
sleep 1
done
}
while true; do
iptables_drop
sleep $SLEEPTIME
done
详细:具体操作方法在/home/script下建立ipt.sh脚本,在/root目录下建个work.txt文件,里面写着白名单ip,这样这个白名单ip访问量再大也不会被X掉了,一般正常的访问量,单1秒的时间里同一个ip 过来的连接不会有超过150个的,所以判断1秒时间里web的连接数超过150个,直接用iptables拉黑,然后再用at任务在一定时间里将其还原,我这里设置为20分钟,基本上就是这样了......如那位朋友还有好的方案,可以拿出来一起分享,技术因为分享而快乐,呵呵
相关文章推荐
- 自动甄别黑白名单的iptables安全脚本 推荐
- Linux iptables简单防攻击脚本(自动过滤白名单)
- iptables的黑白名单机制
- 自动分析黑名单及白名单的iptables脚本
- shell脚本实现批量自动redis反弹shell攻击
- Iptables DDOS/CC 自动屏蔽脚本
- iptables自动添加规则脚本
- 利用脚本自动配置iptables的NAT端口映射
- 使用iptables自动封IP的Shell脚本
- 系统自动弹出IE保护黑白名单的解决
- Iptables DDOS/CC 自动屏蔽脚本
- 详解Android 利用Iptables实现网络黑白名单(防火墙)
- Iptables DDOS/CC 自动屏蔽脚本
- linux下shell脚本自动上传或下载脚本及远程服务器相关vsftpd iptables配置
- rspamd 里面通过lua脚本实现每用户黑白名单
- mysql自动备份脚本
- linux自动重启tomcat脚本
- Autoit和AutoHotKey自动运行程序脚本
- CentOS python2.7 自动安装脚本
- Metasploit自动攻击和选择模块攻击详解