SSH暴力破解攻击与防护功能

Home / Article MrLee 2018-2-16 4694

在互联网上暴露了端口的话,是很容易被一些心怀不轨的人扫描到然后进行暴力破解之类的。为了服务器的安全,要做到各种防御措施。

SSH暴力破解检测方法

可以用此方法检测下多少人在“盯着”你的云服务器

cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2" = "$1;}'

通过上面的命令可以查看有哪些IP尝试过连接和破解次数,如下所示:

103.30.93.54 = 1
113.172.40.86 = 1
113.184.180.45 = 1
113.190.224.216 = 1
113.194.132.6 = 1
117.0.248.55 = 1
120.42.52.82 = 1
123.21.148.70 = 1
125.209.102.68 = 1
140.205.225.189 = 56
140.205.225.190 = 56
140.205.225.200 = 56
14.177.245.40 = 1
14.187.240.196 = 1
156.214.204.40 = 1
156.221.118.246 = 1
1.58.249.27 = 1
......

一大堆就不一一列出来了,大部分都是国外的,美国居多。

然后直接用crontab进行5分钟进行一次sh脚本检测。sh脚本内容

#!/bin/bash
#i为awk输出的ip字符串
for i in `awk '/Failed/{print $(NF-3)}' /var/log/secure|sort|uniq -c|sort -rn|awk '{if ($1>10){print $2}}'`
do
iptables -I INPUT -p tcp -s $i -j DROP
done


本文链接:https://www.it72.com/12312.htm

推荐阅读
最新回复 (1)
  • 百晓生 2018-2-25
    引用 2

    1、查看
    iptables -nvL –line-number

    -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
    -n 不对ip地址进行反查,加上这个参数显示速度会快很多
    -v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
    –line-number 显示规则的序列号,这个参数在删除或修改规则时会用到

    2、添加
    添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。

    当前规则:

    [root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.4          0.0.0.0/0

    添加一条规则到尾部:

    [root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

    再插入一条规则到第三行,将行数直接写到规则链的后面:

    [root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

    查看:

    [root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.3          0.0.0.0/04    DROP       all  --  192.168.1.4          0.0.0.0/05    DROP       all  --  192.168.1.5          0.0.0.0/0

    可以看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。

    3、删除
    删除用-D参数

    删除之前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):

    [root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP

    有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。

    [root@test ~]# iptables -nv --line-number
    iptables v1.4.7: no command specified
    Try `iptables -h' or 'iptables --help' for more information.
    [root@test ~]# iptables -nL --line-number
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.3          0.0.0.0/0

    删除第二行规则

    [root@test ~]# iptables -D INPUT 2

    4、修改
    修改使用-R参数

    先看下当前规则:

    [root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.5          0.0.0.0/0

    将第三条规则改为ACCEPT:

    [root@test ~]# iptables -R INPUT 3 -j ACCEPT

    再查看下:

    [root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

    第三条规则的target已改为ACCEPT。

返回