Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> BSD

FreeBSD 6.1 ipfw 入門設定實錄

ipfw 是 FreeBSD 內建的防火牆,但要真正跑起來得先把核心編好、把規則寫對。以下是我在 6.1 上從最小化安裝到規則上線的完整過程,包含幾個踩過的坑。

一、安裝

選擇最小化安裝,並勾選 src 下的 sys(編譯核心用)、man 與 ports;在套件選擇時預先裝上 bash、cvsup-without-gui、perl,省得之後裝其他軟體時又得連網下載——這些包體積不小,網路不順時會等到崩潰。啟動時啟用 inetd 與 ssh。

二、升級 ports 樹

# cd /usr/ports/net/cvsup-without-gui
# make install clean
# cd /usr/share/examples/cvsup
# cp ports-supfile /root
# cd
# vi ports-supfile

網上有文章建議把 ports-all 註解掉、只升級需要的 ports,但考慮到軟體間的相依性,為避免出問題我並未更動,存檔退出後執行:

# cvsup -g -L 2 ports-supfile

看到 Shutting down connection to server / Finished successfully 就完成了。

三、編譯核心

# cd /sys/i386/conf
# cp GENERIC kernel_ipfw

編輯 kernel_ipfw,取消「Ctrl+Alt+Del 重啟」並啟用防火牆:

options SC_DISABLE_REBOOT
options IPFIREWALL
options IPFIREWALL_VERBOSE
options IPFIREWALL_VERBOSE_LIMIT=10
#options IPFIREWALL_DEFAULT_TO_ACCEPT

重點:防火牆編入核心後預設拒絕一切,也就是規則的最後一條 65535 deny ip from any to any 生效。若加上 IPFIREWALL_DEFAULT_TO_ACCEPT,預設就會全部放行;這裡刻意不加,寧可自己把規則寫清楚,比較放心。因為不做閘道,其他細項也先不加入。其他核心設定依機器實際情況調整。

# /usr/sbin/config kernel_ipfw
# cd ../compile/kernel_ipfw
# make cleandepend && make depend
# make && make install clean

若報錯就依提示修正,完成後 reboot,用 uname -a 確認新核心生效。

四、系統參數

在 /etc/rc.conf 加入:

sendmail_enable="NONE"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
update_motd="NO"
firewall_enable="YES"
firewall_quiet="NO"
firewall_logging="YES"
tcp_drop_synfin="NO"
tcp_restrict_rst="YES"
icmp_drop_redirect="YES"

不跑郵件伺服器就把 sendmail 全關;update_motd="NO" 是為了不再看到每次登入都跳出來的那個訊息畫面(順便把 /etc/motd 的內容清空)。做 web 伺服器時 tcp_drop_synfin 要設 YES 而非 NO。

這裡有個版本差異值得記下:防火牆啟用後預設規則是 deny ip from any to any。我在 FreeBSD 4.10 上只要加一條 ipfw add allow ip from any to any 就完全放行,但在 6.1 加了之後卻無效,可見不同版本的 ipfw 檢查機制不同。既然無效,只好換方法:

firewall_type="open"

重啟後防火牆即對外開放(這招在 4.10 是否有效,日後有機會再試)。另外,防火牆啟用後原本設定的網路位址可能失效,執行 sysinstall 重新設定 IP 等網路參數即可。

五、撰寫 ipfw 規則

先把 firewall_type 設為 open 讓自己不會被鎖在門外,再建立規則檔(例如 /etc/ipfw.rules):

#!/bin/sh
# DNS:用戶端與伺服器都走 udp 53
ipfw add allow udp from me to any 53 out
ipfw add allow udp from any 53 to me in
# DHCP:伺服器 67、用戶端 68
ipfw add allow udp from me 68 to any 67 out
ipfw add allow udp from any 67 to me 68 in
# ipfw add allow udp from any 68 to 255.255.255.255 67 out
# ipfw add allow udp from any 67 to 255.255.255.255 68 in

若知道 DNS 或 DHCP 伺服器的位址,把 any 換成實際 IP 更安全。建立 ICMP 規則時只能指定 type,不能指定 code。先放行雙向的 path-mtu:

ipfw add allow icmp from any to any icmptypes 3
ipfw add allow icmp from any to any icmptypes 4

接著決定 ping 與 traceroute 的方向性——我希望自己能用,但不希望外網任意 ping 我:

ipfw add allow icmp from me to any icmptypes 8 out
ipfw add allow icmp from any to me icmptypes 0 in
ipfw add allow icmp from any to any icmptypes 11 in

ICMP type 8 是查詢請求,type 0 是應答;只允許自己發出請求、接收應答,就能 ping 別人而別人 ping 不到我。若要讓對方也能 ping 我,把方向對調即可:

ipfw add allow icmp from any to me icmptype 8 in
ipfw add allow icmp from me to any icmptype 0 out

traceroute 會向外送 UDP 封包,要看到完整回應還需放行 ICMP type 11。

TCP 服務部分:

ipfw add check-state
ipfw add deny tcp from any to any in established
ipfw add allow tcp from any to any out setup keep-state
ipfw add allow tcp from any to me 21,22,80,3306 in
ipfw add allow tcp from me 22 to any out
ipfw add allow tcp from me to any out

最後那條是因為 web 服務從 80 進來、回應時埠號是隨機的。但這裡有個我還沒想清楚的疑慮:一般防火牆的檢查邏輯是「由內部先送出請求,回應才放行」,加了 allow tcp from me to any out 之後,是否變成內部可以隨意向外連線而不受檢查?若真是如此就相當危險——當伺服器時,客戶端上傳一段木馬不就能大搖大擺 outbound?前面那兩條 check-state 是否還起作用,我不確定,這部分還需要繼續研究。

Copyright © Linux教程網 All Rights Reserved