使用 IPTables 保护数据库服务器

使用 IPTables 保护数据库服务器

我的应用程序 (WordPress) 和数据库 (MySQL) 位于不同的服务器上;它们通过托管服务提供商提供的专用网络连接,并且我已经所有初步步骤(据我所知)为了安全

通常,我遵循以下 IPTables 规则:

*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

#  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

但对于我的独立 (MySQL) 数据库服务器,我发现规则需要进行一些更改。例如,我需要为 MySQL 打开端口 3306,操作非常简单:

-A INPUT -p tcp --dport 443 -j ACCEPT

除此之外,我不知道如何修改它以便只有应用服务器能够连接到数据库(即支持远程连接)。那么,我该怎么做呢?

答案1

所以你需要

 -A INPUT -p tcp -s $INTERNAL_WEB_SERVER_IP --dport 3306 -j ACCEPT

仅允许您的 Web 服务器与 mysql 通信。正如 dmourati 所提到的,允许 ping 通信是个好主意。在我看来,它能解决的问题比它带来的安全问题多得多。

iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -d $SERVER_IP -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 0 -s $SERVER_IP -d 0/0 -m state --state ESTABLISHED,RELATED -j ACCEPT

您提到的出站规则意味着您的数据库服务器可以建立任何它需要的传出连接。基本上,来自您的数据库服务器的任何和所有流量都将被允许。

相关内容