ubuntu or centos 屏蔽 ssh 恶意爆破 ip

0x00

服务器经常被别人恶意爆破,如何将这些 IP 加入黑名单?

0x01

  • ubuntu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
cat /var/log/auth.log|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /tmp/blackIP.txt
TRYCOUNT="5"
for i in `cat /tmp/blackIP.txt`
do
IP=`echo $i |awk -F= '{print $1}'`
NUM=`echo $i|awk -F= '{print $2}'`
if [ $NUM -gt $TRYCOUNT ];
then
grep $IP /etc/hosts.deny > /dev/null
if [ $? -gt 0 ];
then
echo "#"`date` >> /etc/hosts.deny
echo "ALL:$IP:deny" >> /etc/hosts.deny
fi
fi
done
  • centos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
cat /var/log/secure |awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /tmp/blackIP.txt
TRYCOUNT="5"
for i in `cat /tmp/blackIP.txt`
do
IP=`echo $i |awk -F= '{print $1}'`
NUM=`echo $i|awk -F= '{print $2}'`
if [ $NUM -gt $TRYCOUNT ];
then
grep $IP /etc/hosts.deny > /dev/null
if [ $? -gt 0 ];
then
echo "#"`date` >> /etc/hosts.deny
echo "ALL:$IP:deny" >> /etc/hosts.deny
fi
fi
done

可以结合 crontab 定时任务,进行爆破 ip 的封禁。

0x02

封面出处:https://www.pixiv.net/member_illust.php?mode=medium&illust_id=42405579

Author: ronething
Link: https://blog.ronething.cn/20180801-banip.html
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.