用 iptables 屏蔽中国

Car*_*dru 13 server ssh iptables firewall networking

我刚刚登录了 GitLab 服务器,注意到自从我上次检查服务器以来,它有 18.974 次登录失败 - 将近 5 天。我检查了 Ip,似乎几乎所有这些都来自中国,并试图通过 SSH 和 Brute Force 获得访问权限。我开始屏蔽一些 Ip,但后来我意识到这是一个巨大的浪费时间,更好的主意是屏蔽整个国家。

有什么办法可以用 iptables 阻止所有中国或任何其他国家吗?

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

有任何想法吗 ?谢谢!

小智 21

使用ipset拦截中国

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

笔记; 我假设以下全部内容都是以 root 身份完成的。如果您的系统基于 sudo,请进行相应调整。

apt-get install ipset
Run Code Online (Sandbox Code Playgroud)

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

nano /etc/block-china.sh
Run Code Online (Sandbox Code Playgroud)

这是您要粘贴到其中的内容:

# 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
Run Code Online (Sandbox Code Playgroud)

保存文件。使其可执行:

chmod +x /etc/block-china.sh
Run Code Online (Sandbox Code Playgroud)

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

nano /etc/iptables.firewall.rules
Run Code Online (Sandbox Code Playgroud)

添加以下行:

-A INPUT -p tcp -m set --match-set china src -j DROP
Run Code Online (Sandbox Code Playgroud)

保存文件。需要明确的是,我的完整 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
Run Code Online (Sandbox Code Playgroud)

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

/etc/block-china.sh
Run Code Online (Sandbox Code Playgroud)

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

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

iptables -L
Run Code Online (Sandbox Code Playgroud)

您现在应该会看到一条阻止中国的新规则——输出应该是这样的:

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
Run Code Online (Sandbox Code Playgroud)

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

我们还可以通过 cron 作业将机器设置为自动执行此操作:

crontab -e
Run Code Online (Sandbox Code Playgroud)

添加如下一行:

0 5 * * * /etc/block-china.sh
Run Code Online (Sandbox Code Playgroud)

这将在每天凌晨 5 点运行 /etc/block-china.sh。要在重新启动时启用它,请添加另一行,例如:

@reboot /etc/block-china.sh
Run Code Online (Sandbox Code Playgroud)

运行脚本的用户需要是 root 或具有 root 权限。

来源


Dou*_*ies 9

使用 iptables 自动识别,然后阻止 ssh 的坏人可以使用该recent模块完成。以下部分必须您的通用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
Run Code Online (Sandbox Code Playgroud)

现在,中国最近(过去一两年)的问题是他们变得非常聪明,而且通常一旦他们被一个 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
[…]
Run Code Online (Sandbox Code Playgroud)

在上面的地方:

# 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"
Run Code Online (Sandbox Code Playgroud)

您可以在此处以 iptables 或其他格式获取中国或任何国家/地区的完整 IP 地址列表。然而,这份名单出奇地长,而且相当动态。我自己决定不屏蔽整个列表。