設定 iptables
.K.T. | 2008 年 07 月 18 日 | 下午 1:09剛剛在資安論壇閒逛的時候
看到一篇有關於 iptables(註:1) 設定的文章
寫得淺顯易懂
想說來做個筆記
另外
作者的 Blog 也有這篇文章
大家可以來這裡看看
iptables 的指令如下:
iptables -t 表 -A 鍊 -p 協定 -s 來源IP網段 --sport 來源Port號 -d 目的地IP網段 --dport 目的地Port號 -j 接受或拒絕
其中的「表(Table)」預設為 filter
如果要將 Server 當成防火牆用的話
還會用到 nat 及 mangle 這兩個表
如果只是要加強本機的安全性
大概只要用到 filter 這個表就夠了
其中的「鍊(Chain)」
以表 filter 來說
預設有 INPUT、OUTPUT 及 FORWARD
分別針對不同的網路流向做設定
當然也可以自定自己的 Chain
像我裝的 Fedora
就幫我設定了一個叫 RH-Firewall-1-INPUT 的 Chain
「協定」、「IP 網段」及「Port 號」應該就不用多做說明
而「接受或拒絕」
分別是 ACCEPT 及 REJECT
所以 iptable 完整的指令
長的像這樣
iptables -t filter -A INPUT -p tcp -s 192.168.30.0/24 --sport 1024:65535 -d 192.168.30.2 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p tcp -s 192.168.30.2 --sport 22 -d 192.168.30.0/24 --dport 1024:65535 -j ACCEPT
iptables -t filter -P INPUT DROP(註:2)
另外需要補充的是
「!」是指「除了,以外」的意思
還有「--sport 1024:65535」代表的是
從 Port 1024 到 Port 65535
如果說
再裝上 L7-filter(註:3)
就可以用 iptables 當作 Layer 7 的防火牆囉!
註:
- iptables:Linux 預設的防火牆軟體,可以針對網路封包的 IP、Port、MAC 以及連線狀態如 SYN、ACK 等資料進行分析,以過濾網路封包。 [↩]
- 這個範例所做的,是只允許內部網路192.168.30.0/24 使用ssh的連線進入192.168.30.2。 [↩]
- L7-filter:L7-filter(Application Layer Packet Classifier for Linux)是 Linux netfilter 的外掛模組,它能讓 Linux 的 iptables 支援 Layer 7(Application 應用層)封包過濾功能,限制 P2P、即時通訊等使用動態埠口的網路軟體。 [↩]







若是對於初學者,我建議初學者先看完 TCP/IP 基礎
再來學防火牆設定。
BSD 到目前為止好像都沒有 iptable 可用,據說 iptable 有 BSD 的 ipfw 的血統
( Prior to iptables, the predominant software packages for creating Linux firewalls were ipchains in Linux 2.2; and ipfwadm in Linux 2.0 which in turn was based on BSD's ipfw.
http://en.wikipedia.org/wiki/Iptables )
相對於 ipfw,我個人比較喜歡 pf ( http://www.openbsd.org/faq/pf/ )
據說,曾有人做過 ipfw 和 pf 的效能評比,不過說實在的,以我這種「小咖」來說,
因為電腦沒幾台、網路流量又太低,都麻很夠用,乖乖的學一種,用習慣就好。
TCP/IP 基礎
http://www.study-area.org/network/network_ip.htm
感謝樓上的補充!