linux查看密码错误ip,对几次输入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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值