centos防火墙-cmd使用脚本阻止awk ips

iyzzxitl  于 2022-11-07  发布在  其他
关注(0)|答案(1)|浏览(139)

有很多awk攻击到我的服务器。我试过阻止他们,但是太多了。有没有办法一次性阻止他们?

我使用此命令:

netstat -an|awk -F: '{print $2}'|sort|uniq -c|sort -nr|head

显示结果

[root@local ~]# netstat -an|awk -F: '{print $2}'|sort|uniq -c|sort -nr|head
   1080 80      107.189.8.33
    864 80      185.129.61.5
    485 80      23.154.177.11
    386 80      183.245.24.27
    318 80      185.243.218.32
    309 80      185.220.101.2
    276 80      61.153.251.150
    259 80        59.148.106.164
    235 80      185.175.119.113

在列出一个IP后,我会发现连接到80端口的IP超过100个。并阻止它们。

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="107.189.8.0/24" drop'

无论如何要制作一个.sh文件,找出awk ips的200多个连接,并将它们添加到防火墙的下拉列表中?
在这种情况下,需要排除127.0.0.1和我们自己的IPS。
希望有人能帮忙谢谢。

我已尝试使用此代码输出有问题的ips。

netstat -an|awk -F: '{print $2}'|sort|uniq -c|sort -nr|head > ccips.txt

在此之后,我用途:

awk '{sub("IP:", "", $3); print $3}' /root/ccips.txt | xargs -n1 -I{} firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address={} drop'

这可以阻挡所有攻击的ip。

我只是不知道如何将其导入到a.sh中,这可以在一个命令中完成。

ogsagwnx

ogsagwnx1#

我认为你想做的是创建一个像这样的IP文件,你不想阻止:

$ cat allowedIPs
127.0.0.1
whatever...

然后使用这样一个脚本来阻止所有不在该文件中的IP连接到端口80(未经测试,通过阅读您的代码猜测netstat -an的输出是什么):

$ cat blockIPs

# !/usr/bin/env bash

netstat -an |
awk -F: '
    NR == FNR {
        allowedIPs[$1]
        next
    }
    {
        split($2,portIP," ")
        port = portIP[1]
        ip = portIP[2]
    }
    (port == 80) && !(ip in allowedIPs) && !seen[ip]++ {
        print ip
    }
' allowedIPs - |
xargs -n1 -I{} firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address={} drop'

相关问题