快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

对几次输入ssh密码错误的IP进行iptables drop

 对几次输入ssh密码错误的IP进行iptables drop


把下面脚本放入/etc/crontab
      扫描ssh密码猜测次数超过5次的记录
* * * * * root /home/cnscn/sh/ssh_scan_crontab.sh >/dev/null 2>&1
$ cat /home/cnscn/sh/ssh_scan_crontab.sh
1.    #!/bin/bash
2.    # Author http://jabin.cublog.cn
3.    # Modify cnscn http://cnscn2008.cublog.cn
4.    # Modify xinyv
5.   
6.    #设置时区
7.    export LC_ALL=UTC
8.   
9.    # 获取前1 分钟内的secure 记录,统计ssh 认证失败的IP 和其 失败次数, 并用Iptables阻止之
10.   SCANNER=$(awk 'BEGIN{ tm=strftime("%b %e %H:%M",systime()-60);}  $0 ~ tm && /Failed password/ && /ssh2/ {print $(NF-3)}' /var/log/secure |sort|uniq -c |awk '{print $1"="$2;}')
11.  
12.  
13.   for i in $SCANNER
14.   do
15.   echo $i
16.          # 取认证失败次数
17.          NUM=`echo $i|awk -F= '{print $1}'`
18.  
19.          # 取其IP 地址
20.          IP=`echo $i|awk -F= '{print $2}'`
21.  
22.          # 若其在失败次数超过5 次且之前没有被阻断过,那么添加一条策略将其阻断,并记录日志
23.          if [ $NUM -gt 5 ] && [ -z "`/sbin/iptables -vnL INPUT|grep $IP`" ]
24.          then
25.                  /sbin/iptables -I INPUT -s $IP -j DROP
26.                  echo "/sbin/iptables -I INPUT -s $IP -j DROP" >> /home/cnscn/sh/ssh_scan_iptables.sh
27.                  logger -i -t "ssh_scan_crontab" -f /var/log/messages "$IP($NUM)..."
28.          fi
29.   done
30.   #End of Script
31.  
32.   把脚本/home/cnscn/sh/ssh_scan_iptables.sh加入到开机启动的myiptables.sh防火墙脚本
33.   $ cat myiptables.sh
34.   #!/bin/bash
35.   #chkconfig: 345 85 15
36.   #description: my iptables rules, which can auto run when system start
37.  
38.   # This is a script
39.   # Edit by liwei, cnscn
40.   # establish a static firewall
41.  
42.   #网络接口
43.   interdevice="eth0"
44.  
45.   #端口
46.   #21       ftp
47.   #15022    sshd
48.   #25       smtp
49.   #53       named
50.   #80       http
51.   #110      pop3
52.  
53.   #外界可以访问的端口
54.   Open_ports="21 20 22 80"
55.  
56.   #可以外出的端口,其它端口都可以外出
57.   Allow_ports="21 20 80 "
58.  
59.   #清除所有以前设置的规则
60.   iptables -F
61.   iptables -X
62.   iptables -t nat -F
63.   iptables -t nat -X
64.  
65.   #执行非法IP阻止规则 www.2cto.com
66.   /home/cnscn/sh/ssh_scan_iptables.sh
67.  
68.   #允许211.167.xxx.xxx, 防止自己输入错误而导致的IP被封锁
顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论