使用 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

您提到的出站規則意味著您的資料庫伺服器可以建立它被告知的任何傳出連線。基本上,來自資料庫伺服器的任何和所有流量都將被允許。

相關內容