這是 Linux 中 iptables 的一個好的起點嗎?

這是 Linux 中 iptables 的一個好的起點嗎?

我是 iptables 的新手,我一直在嘗試建立一個防火牆,其目的是保護網路伺服器。以下規則是我迄今為止整理的規則,我想聽聽這些規則是否有意義 - 以及我是否遺漏了任何必要的內容?

除了連接埠 80 之外,我還需要打開連接埠 3306 (mysql) 和 22 (ssh) 以用於外部連接。

任何反饋都將受到高度讚賞!

#!/bin/sh

# Clear all existing rules.
iptables -F

# ACCEPT connections for loopback network connection, 127.0.0.1.
iptables -A INPUT -i lo -j ACCEPT

# ALLOW established traffic
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# DROP packets that are NEW but does not have the SYN but set.
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

# DROP fragmented packets, as there is no way to tell the source and destination ports of such a packet.
iptables -A INPUT -f -j DROP

# DROP packets with all tcp flags set (XMAS packets).
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

# DROP packets with no tcp flags set (NULL packets).
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

# ALLOW ssh traffic (and prevent against DoS attacks)
iptables -A INPUT -p tcp --dport ssh -m limit --limit 1/s  -j ACCEPT

# ALLOW http traffic (and prevent against DoS attacks)
iptables -A INPUT -p tcp --dport http -m limit --limit 5/s -j ACCEPT

# ALLOW mysql traffic (and prevent against DoS attacks)
iptables -A INPUT -p tcp --dport mysql -m limit --limit 25/s -j ACCEPT

# DROP any other traffic.
iptables -A INPUT -j DROP

答案1

試試 shorewall,它提供了一個合理的開箱即用的防火牆。啟用從網路存取您想要的服務。有針對一個、兩個和三個介面的範例規則集。該文件很好並且得到積極維護。

我預計您會想要限制哪些位址可以存取 MySQL,這很容易做到。您也可以透過連接埠碰撞(連接埠關閉)來保護 SSH,除非您最近偵測過另一個連接埠。

答案2

  1. 您確實想要允許 ICMP。
  2. 5/秒對於 HTTP 來說可能不夠
  3. 我沒有看到 XMAS/NULL 封包的規則有什麼意義
  4. 也沒有看到特殊的新資料包規則的原因

ETA:5。我只需要每秒發送 1 個 SYN 封包到您的伺服器即可拒絕您的 ssh 存取。

答案3

我會考慮使用 NARC 之類的東西來進行 iptables 規則配置:

http://www.knowplace.org/pages/howtos/firewalling_with_netfilter_iptables/netfilter_automatic_rule_configurator.php

該軟體包已經存在一些合理的預設設置,您應該能夠信任這些預設。

答案4

從伺服器到互聯網的傳出通訊的過濾器也很重要。特別是建議 SMTP 僅允許一台伺服器。

我管理 Mikrotik 防火牆,我習慣這樣做:

  • 丟棄來自互聯網的傳入資料包,其中包含來自私有範圍的目標位址
  • 丟棄連接埠掃描(每秒超過 5 個連接埠檢查)
  • SSH 到另一個連接埠(我知道這很晦澀,但它有效!:-))並限制來源 IP
  • 丟棄路由器的廣播
  • 丟棄 TCP RST

還有一些。我建議閱讀以下內容:http://wiki.mikrotik.com/wiki/Dmitry_on_firewallingMikrotik 的文法很簡單,對於初學者來說有很好的指導意義。

相關內容