用iptables阻止中国

我刚刚登录GitLab服务器并注意到自从我上次检查服务器后它有18.974次登录失败 – 差不多5天。 我检查了Ip,似乎几乎所有人都来自中国,并试图通过SSH和Brute Force获取访问权限。 我开始阻止一些Ip,但后来我意识到这是一个巨大的浪费时间,更好的想法是阻止整个国家。

有什么方法可以阻止所有中国或任何其他国家的iptables?

我在互联网上发现了一些文章,但几乎所有文章都是bash脚本。 我是Linux上的新手,所以我真的不懂所有这些脚本。 我觉得iptables非常有趣,我想了解更多。

有任何想法吗 ? 谢谢!

使用iptables可以使用recent模块自动识别并随后阻止ssh的坏人。 以下细分必须您的通用ESTABLISHED,RELATED行之后:

 ... $IPTABLES -A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m state --state ESTABLISHED,RELATED -j ACCEPT ... # Secure Shell on port 22. # # Sometimes I uncomment the next line to simply disable external SSH access. # Particulalry useful when I am rebooting often, thereby losing my current BADGUY table. # $IPTABLES -A INPUT -i $EXTIF -m state --state NEW -p tcp -s $UNIVERSE -d $EXTIP --dport 22 -j DROP # Dynamic Badguy List. Detect and DROP Bad IPs that do password attacks on SSH. # Once they are on the BADGUY list then DROP all packets from them. # Sometimes make the lock time very long. Typically to try to get rid of coordinated attacks from China. $IPTABLES -A INPUT -i $EXTIF -m recent --update --hitcount 3 --seconds 90000 --name BADGUY_SSH -j LOG --log-prefix "SSH BAD:" --log-level info $IPTABLES -A INPUT -i $EXTIF -m recent --update --hitcount 3 --seconds 90000 --name BADGUY_SSH -j DROP $IPTABLES -A INPUT -i $EXTIF -p tcp -m tcp --dport 22 -m recent --set --name BADGUY_SSH -j ACCEPT 

现在,中国最近(过去一两年)的问题是,他们变得非常聪明,而且一旦他们被一个IP地址阻止,他们只需在同一个子网上切换到另一个IP地址并继续。 这可能会导致缺少最近的默认表条目(我认为默认值为200)。 我对此进行监控,然后查找实际的IP段,并永久阻止整个段。 在我的情况下,我不关心附带损害,即阻止某人无辜:

 # # After a coordinated attack involving several sub-nets from China, they are now banned forever. # List includes sub-nets from unknown origin, and perhaps Hong Kong # $IPTABLES -A INPUT -i $EXTIF -s 1.80.0.0/12 -d $UNIVERSE -j DROP $IPTABLES -A INPUT -i $EXTIF -s 27.148.0.0/14 -d $UNIVERSE -j DROP $IPTABLES -A INPUT -i $EXTIF -s 27.152.0.0/13 -d $UNIVERSE -j DROP $IPTABLES -A INPUT -i $EXTIF -s 43.229.0.0/16 -d $UNIVERSE -j DROP $IPTABLES -A INPUT -i $EXTIF -s 43.255.0.0/16 -d $UNIVERSE -j DROP ... 

在上面的地方:

 # The location of the iptables program # IPTABLES=/sbin/iptables #Setting the EXTERNAL and INTERNAL interfaces and addresses for the network # EXTIF="enp4s0" INTIF="enp2s0" EXTIP="...deleted..." INTNET="192.168.111.0/24" INTIP="192.168.111.1/32" UNIVERSE="0.0.0.0/0" 

您可以在此处获取 iptables或其他格式的中国或任何国家/地区的IP地址列表。 然而,这个名单既令人惊讶的长,又相当动态。 我自己,我决定不阻止整个清单。

您可能希望安装诸如fail2ban之类的内容,以便阻止尝试登录服务器并失败的ips。

中国阻止使用ipset

你不能手动为你的iptables添加几千个IP地址,甚至自动这样做是个坏主意,因为它会导致很多CPU负载(或者我已经读过)。 相反,我们可以使用专为此类设计的ipset。 ipset处理大的ip地址列表; 你只需创建一个列表然后告诉iptables在规则中使用该列表。

注意; 我假设以下所有内容都是以root身份完成的。 如果您的系统基于sudo,请相应地进行调整。

 apt-get install ipset 

接下来,我编写了一个小的Bash脚本来完成所有工作,您应该能够从其中的注释中理解这些工作。 创建一个文件:

 nano /etc/block-china.sh 

这是你要粘贴到它的内容:

 # Create the ipset list ipset -N china hash:net # remove any old list that might exist from previous runs of this script rm cn.zone # Pull the latest IP set for China wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone # Add each IP address from the downloaded list into the ipset 'china' for i in $(cat /etc/cn.zone ); do ipset -A china $i; done # Restore iptables /sbin/iptables-restore < /etc/iptables.firewall.rules 

保存文件。 使其可执行:

 chmod +x /etc/block-china.sh 

这还没有做任何事情,但在我们运行脚本时会在一分钟内完成。 首先,我们需要在iptables中添加一个规则,该规则引用上面脚本定义的新ipset列表:

 nano /etc/iptables.firewall.rules 

添加以下行:

 -A INPUT -p tcp -m set --match-set china src -j DROP 

保存文件。 要清楚,我的完整iptables.firewall.rules现在看起来像这样:

 *filter # Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0 -A INPUT -i lo -j ACCEPT -A INPUT -d 127.0.0.0/8 -j REJECT # Accept all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Block anything from China # These rules are pulled from ipset's china list # The source file is at /etc/cn.zone (which in turn is generated by a shell script at /etc/block-china.sh ) -A INPUT -p tcp -m set --match-set china src -j DROP # Allow all outbound traffic - you can modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL). -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # Allow SSH connections # # The -dport number should be the same port number you set in sshd_config # -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping -A INPUT -p icmp -j ACCEPT # Log iptables denied calls -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Drop all other inbound - default deny unless explicitly allowed policy -A INPUT -j DROP -A FORWARD -j DROP COMMIT 

目前,服务器没有任何变化,因为没有应用新的规则; 为此,请运行block-china.sh脚本:

 /etc/block-china.sh 

这应该显示一些输出,因为它提取了一个新的基于中文的IP列表,然后,几秒钟后,它将完成并让您回到命令提示符。

要测试它是否有效,请运行:

 iptables -L 

您现在应该看到阻止中国的新规则 - 输出应该如下所示:

 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere REJECT all -- anywhere loopback/8 reject-with icmp-port-unreachable ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED DROP tcp -- anywhere anywhere match-set china src ACCEPT tcp -- anywhere anywhere tcp dpt:http ACCEPT tcp -- anywhere anywhere tcp dpt:https ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh ACCEPT icmp -- anywhere anywhere LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: " DROP all -- anywhere anywhere Chain FORWARD (policy ACCEPT) target prot opt source destination DROP all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere 

几乎完成了! 这有效,并将继续在重新启动。 但是,IP地址会发生变化,该列表会随着时间的推移而变得陈旧。 如果要提取并应用更新的IP列表,可以再次运行block-china.sh脚本。

我们还可以设置机器通过cron作业自动完成:

 crontab -e 

添加如下行:

 * 5 * * * /etc/block-china.sh 

这将在每天凌晨5点运行/etc/block-china.sh。 运行脚本的用户需要是root用户或具有root权限。

资源