防火墙自动屏蔽游戏服务攻击行为

防火墙自动屏蔽游戏服务攻击行为
陈信 20120803

所有连接的统计(支持ipv4,ipv6):
netstat -an |grep -v -E "(:*|Active\ |Proto\ |unix)"|awk '{print $5}'|awk -F ':' '{print $1 $4}'|grep -v "^$"|sort |uniq -c|sort --key=1n

已经建立的有效连接统计(支持ipv4,ipv6):
netstat -an |grep "ESTABLISHED"|awk '{print $5}'|awk -F ':' '{print $1 $4}'|grep -v "^$"|sort |uniq -c|sort --key=1n

除开内网有效连接(防止mysql连接池过大):

所有连接的统计(支持ipv4,ipv6):
netstat -an |grep -v -E "(:*|Active\ |Proto\ |unix)"|awk '{print $5}'|awk -F ':' '{print $1 $4}'|grep -v "^$"|sort |uniq -c|sort --key=1n |grep -v -E "(\ 192.168|\ 10.|127.0.0.1)"

已经建立的有效连接统计(支持ipv4,ipv6):
netstat -an |grep "ESTABLISHED"|awk '{print $5}'|awk -F ':' '{print $1 $4}'|grep -v "^$"|sort |uniq -c|sort --key=1n|grep -v -E "(\ 192.168|\ 10.|127.0.0.1)"

注意观察最后几行连接较多的:
2 203.145.92.214
2 203.145.92.215
3 121.202.132.17
3 202.175.20.110
3 61.93.238.70
4 121.202.132.16
如果有连接数达到100个的,就是攻击行为了,需要对其进行IP的drop操作;

自动执行连接数监测、屏蔽攻击的iptables、发送攻击者ip地址报警短信 脚本如下:

放入crontab中,每隔一个小时检查一次连接状况;

大于10个的发送报警短信、大于100个的直接屏蔽;

iptables -A INPUT -p all -s 192.168.80.119 -j DROP

iptables –ADI(动作) INPUT/OUTPUT/FORWARD(链) –io lo/eth0(接口卡)-p all/tcp/udp/icmp(协议) –s/d IP –-sport/dport portnumber –j ACCEPT/DROP(执行动作)

端口范围:
iptables -A INPUT -p tcp --dport 8001:8999 -j DROP

查看规则链数值:
iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 192.168.80.119 anywhere

删除规则链:
iptables -D INPUT 1

iptables自动阻断攻击者IP
陈信
20120804
说明
iptables.sh 主要检测程序;放入到/root/admin/下,可以由crontab自动调用,建议每10分钟或半小时调用一次;
established_201208041352.txt 临时记录统计到的最多连接的几个IP地址和连接数,之后自动清除该文件;
attack_ip_scaned.txt 检测到的攻击者IP信息与连接数、攻击时间;
attack_history_ip_by_hand.txt 可以手动将怀疑是攻击者IP的地址放入该文件,由脚本自动加载,手动输入的IP格式不能错,每行一个IP;

cat iptables.sh

!/bin/bash

iptables chains auto scan the attack IP address,and drop the IP address automatic. for digital sky ltd...

Chenxin 20120803

引入系统环境变量#

if [ -f /etc/init.d/functions ];then
. /etc/init.d/functions;
fi

清除原先的规则

iptables -F;
iptables -F -t nat;
iptables -X;

设置默认规则

iptables -P INPUT ACCEPT;
iptables -P OUTPUT ACCEPT;
iptables -P FORWARD ACCEPT;

scan_date=date +%Y%m%d%H%M;
attack_date=date +%Y%m%d;
inet_addr=ifconfig |grep "inet addr:"|awk '{print $2}'|grep -v -E "(:127.0|:192.168)"
mkdir -p /home/admin/iptables_tmp_file/;
cd /home/admin/iptables_tmp_file/;
find /home/admin/iptables_tmp_file/ -mtime +1 -name "established_*.txt" -exec rm -rf {} ;

所有连接含尝试TCP握手

netstat -an |grep -v -E "(:*|Active\ |Proto\ |unix)"|awk '{print $5}'|awk -F ':' '{print $1 $4}'|grep -v "^$"|sort |uniq -c|sort --key=1n |grep -v -E "(\ 192.168|\ 10.|127.0.0.1)"|tail -n 5 >/home/admin/iptables_tmp_file/all_"$scan_date".txt;

握手成功的所有连接

netstat -an |grep "ESTABLISHED"|awk '{print $5}'|awk -F ':' '{print $1 $4}'|grep -v "^$"|sort |uniq -c|sort --key=1n|grep -v -E "(\ 192.168|\ 10.|127.0.0.1)"|tail -n 5 >/home/admin/iptables_tmp_file/established_"$scan_date".txt;

IP与连接数列表,连接服务器最多的前5个

list=cat established_"$scan_date".txt|tac
line_number=cat established_"$scan_date".txt|wc -l

for ((i=1;i<="$line_number";i++))
do {
ip_address=echo "$list"|sed -n "$i"p|awk '{print $2}'
ip_count=echo "$list"|sed -n "$i"p|awk '{print $1}'

判断IP地址是否合法

if [[ $ip_address =~ [1]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then
OIFS=$IFS #先将系统的IFS的默认值放入到OIFS临时变量中
IFS='.' #赋予.给IFS
ip_address_tmp=($ip_address)
IFS=$OIFS #恢复IFS变量的默认值,如果不恢复会造成系统异常
if [[ ${ip_address_tmp[0]} -le 255 && ${ip_address_tmp[1]} -le 255 && ${ip_address_tmp[2]} -le 255 && ${ip_address_tmp[3]} -le 255 ]];then
if [ $ip_count -ge 100 ];then
echo "# $ip_address $ip_count may be the attack IP address,the program will block it temporary... #";
iptables -A INPUT -p all -s $ip_address -j DROP
echo "$attack_date $ip_address $ip_count" >> attack_ip_scaned.txt #记录攻击者IP,方便以后查询
#报警攻击者IP信息,时间,服务器地址,短信通知#
curl http://211.103.155.246:8080/sms_send2.do?corp_id=539008&corp_pwd=xd008&corp_service=1069yd&mobile=13668169289&msg_content="$ip_address"_"$scan_date"is_attacking_our"$inet_addr"_server&ext=6666
touch attack_history_ip_by_hand.txt #可以手动将需要drop的IP地址(经常攻击者)放入,每次程序会自动加载
for j_ip in cat attack_history_ip_by_hand.txt
do {
echo "# $j_ip is the history attack IP address , we will block it access the server automatic...#"
iptables -A INPUT -p all -s $j_ip -j DROP
} done
fi
else
echo "$ip_address is wrong format address!!!"
fi
else
echo "$ip_address is wrong format address!!!"
fi
} done


  1. 0-9 ↩︎

posted @ 2020-04-21 13:46  ChanixChen  阅读(271)  评论(0)    收藏  举报