[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 系統管理 > FreeBSD > 正文
如何設定防火牆IP Firewall
BBS水木清華站 (2001-04-16 14:19:47)
IP Firewall 可以有效的增進系統的安全,但是除非你對 IP Firewall 有 
相當的了解,否則你可能把你的機器弄的無法上網路。 

警告 !!  絕對不要從遠端連線設定 IPFW,一定要從 console !! 
         不然設定錯誤,你的連線可能會被中斷 !! 

你必須在 kernel config file 中加入 options IPFIREWALL 及 options 
IPFIREWALL_VERBOSE,再重新編譯 kernel。 

在你開始以前,請先 man ipfw。 

1. 編輯 /etc/sysconfig,把 firewall=NO 改成 firewall=YES 
2. 修改 /etc/rc.firewall 
   你可以參照裡面的□例,如果看不懂的話,請 man ipfw 
   以下是一個簡單的例子: 

echo "IP Firewall Rules ...." 
# true to enable, false to disable 
 
if true; then 
        # basic information 
        net="140.113.139.0" 
        mask="255.255.255.0" 
        mask_b="255.255.0.0" 
        myip="140.113.139.1" 
 
        # flush all rules 
        # FreeBSD-2.2 1996/Sep 以後的版本,請使用 ipfw -f flush (見後 PS) 
        /sbin/ipfw flush 
 
        # Allow all connections at last 
        # IPFW default rule : "65535 deny all from any to any" 
        /sbin/ipfw add 65000 pass all from any to any 
 
        # Allow all connections (the latest match 65000) 本行最後才 match ! 
        /sbin/ipfw add 65000 pass all from any to any 
 
        # Allow all localhost connections (first match) 本行最先 match ! 
        /sbin/ipfw add 1000 pass all from 127.0.0.1 to 127.0.0.1 
        /sbin/ipfw add 1010 pass all from ${myip} to a 
 
        # Friendly sites (fully access rights) 
        # 這些 host 最好用 IP,或是要紀錄在 /etc/hosts 中 
        /sbin/ipfw add 1100 pass all from Friend1 to any 
        /sbin/ipfw add 1120 pass all from Server to any 
        /sbin/ipfw add 1130 pass all from FreeBSD.csie.nctu.edu.tw to any 
 
        # deny everything (asshole host) 
        /sbin/ipfw add 2000 deny all from 123.123.123.123:${mask} to any 
 
        # deny remote syslogging to get ride of security problem 
        /sbin/ipfw add 3000 deny udp from any to any 514 
 
        # deny users out of campus from doing "rusers", "rwall" 
        /sbin/ipfw add 4000 pass udp from ${net}:${mask_b} to any 1034,1035 
        /sbin/ipfw add 4010 deny udp from any to any 1034,1035 
 
        # deny most hosts from tcpspray me 
        /sbin/ipfw add 5000 deny tcp from any to any 9 
        /sbin/ipfw add 5010 deny udp from any to any 9 
fi 
 
3. 執行 sh /etc/rc.firewall,然後測試各種網路連線看看 

後記: 
IPFW 一啟動後,內定是『拒絕所有連線』!!! 
(IPFW 內定 rule : 65535 deny all from any to any) 
這點要特別注意,因此如果你下了 ipfw flush 指令,你立刻跟網路隔絕, 
連 localhost 也無法連接。 
因此通常我們會在最後一個 rule (如 65534) 允許所有連線,然後把 deny 
的 rules 都放在前面。 

如果你在系統啟動之後修改了 /etc/rc.firewall,只要用 
sh /etc/rc.firewall 就可以載入新的 rules。 
如果你在遠端控制,要注意新加的 rule 不要把自己給 ban 掉了啊。 

PS. 在 1996/Sep 以後的 ipfw 在執行 ipfw flush 時會詢問使用者以求 
    確認 (因為 ipfw flush 會導致連線中斷),因此你在撰寫 /etc/ 
    rc.firewall 時請使用 ipfw -f flush。 
    你可以打 ipfw -h 看看你的 ipfw 有沒有支援 -f 的選項。  (http://www.fanqiang.com)
    進入【UNIX論壇

相關文章

======
 

★  樊強制作 歡迎分享  ★