Linux的Firewall與tcp_wrappers

圖文.我本善良 (asako@parzival.org) Microsoft Certified Systems Engineer (MCSE)


●前言●
一般來說linux的密碼我們會用shadow來保護。電子郵件大概有人也會說使用PGP,但
是一般的網路連線呢?可能會有人舉手回答說防火牆,那防火牆跟tcp_wrappers有什麼
關係呢?筆者個人認為,如果說Firewall是第一道防線的話,第二道防線大概就是
tcp_wrappers了,我們可以利用不同的防護程式來增加防護的能力之外,並且還能增加
被駭客破台的困難度。設計一個完善的防火牆規則本來就不是一件很容易的事情,但是
我們能透過簡單的程式化困難為容易,讓新手更能快樂的接觸美麗的Linux新世界。

●本文●

●使用防火牆●
在防火牆的方面筆者建議新手到這裡抓http://www.pointman.org抓PMfirewall目前最
新的版本為pmfirewall-1.1.4.taq.gz,而這支程式所測試過可以用的linux版本有,
Redhat 6.x、Mandrake 6.x, 7.x、Caldera OpenLinux 2.3、Debian 2.1 或者更高的
版本、SuSE 6.x Slackware 7.x、 Stampede Linux,而且可能還有更多未測試過的版
本也可以用。當我們把這支shell script抓回來之後上傳至我們要安裝的伺服器上然後
開始我們安裝的過程。
1:請使用最高權限的root登入到主機。
2:解開tar的壓縮程式,如tar zxvf 〝檔案名稱〝。如圖一:



3:試者閱讀一下README的檔案。

4:利用cd的指令變更目錄到pmfirewall的目錄裡。如圖二:



5:在console端的指令模式下執行〝./install.sh〝開始安裝。如圖三:



而我們將會看到如以下的訊息。

Welcome to the pmfirewall setup script.
.....................................................................

You will now be prompted for your system configuration. Normally the
defaults are sufficient, but be sure they are correct!

Directory to place config files [/usr/local/pmfirewall]:
//指定您的pmfirewall系統設定檔要存放的目錄為何。

Any custom settings contained in the pmfirewall.rules.local file will
need to be added back into the local file..
//設定完pmfirewall之後你可以自定防火牆規則加入在pmfirewall.rules.local檔案
裡。

Using ipchains found at: /sbin/ipchains.
//使用ipchains在/sbin/ipchains

What is your External Interface?

External Interface [eth0]:
//您連外的網路卡,內定值為eth0,但是您可以們更依您的環境來更改。

You will now be asked questions which will help customize PMFirewall.

Some networks may need to bypass your firewall.
While this is not recommended, you have the option below.

WARNING: An entry of 0/0 will open your firewall to everyone and
render it useless!
//警告:如果您輸入0/0那您的防火牆將會開放給任何人存取而變成沒有用。

Are there any IP ranges which require unrestricted access? (y/N):
//您有任何的IP或範圍可以自由的進出您的防火牆嗎?

Some hostile networks may need to be totally blocked.
Only add an IP range here if you wish to block them completely.

WARNING: An entry of 0/0 will block access to everyone and make
all rules added after this useless.
//警告:如果您輸入0/0那任何人將無法存取您的主機而且在此規則之後所加入的都將
沒有用。

Are there any IP ranges which should be blocked completely? (y/N):
//您有任何的IP或範圍去拒絕它的取存嗎?

Is your IP address assigned via DHCP?  (y/N):
//您的eth0的IP是使用DHCP取得的嗎?

Are you running a FTP Server on ports: 20/21 (y/N):
//您有執行FTP Server在連接埠20/21嗎?

Are you running a SSH Server on port: 22 (y/N):
//您有執行SSH Server在連接埠22嗎?

Are you running a Telnet Server on port: 23 (y/N):
//您有執行Telnet Server在連接埠23嗎?

Are you running a SMTP Server on port: 25 (y/N):
//您有執行SMTP Server在連接埠25嗎?(郵件伺服器)

Are you running a DNS Server on port: 53 (y/N):
//您有執行DNS Server在連接埠53嗎?(網域稱伺服器)

Are you running a Finger Server on port: 79 (y/N):
//您有執行Finger Server在連接埠79嗎?

Are you running a Web Server on port: 80 (y/N):
//您有執行Web Server在連接埠80嗎?(網頁伺服器)

Are you running a POP Server on port: 110 (y/N):
//您有執行POP Serve在連接埠110嗎?

Allow IDENT connections on port: 113 (y/N):
//您有執行IDENT使用者識別在連接埠113嗎?

Are you running a NNTP Server port: 119 (y/N):
//您有執行NNTP Server在連接埠119嗎?(NEWS伺服器)

Are you using NTP, it requires port: 123 (y/N):
//您有執行NTP在連接埠123嗎?(網路對時)

Do you wish to open NetBIOS/SAMBA ports 137-139 (not recommended)? (y/N):
//您要打開NetBIOS/SAMBA 在連接埠 137-139 (不建議)

Are you running an IMAP Server on port: 143 (y/N):
//您有執行IMAP Server在連接埠143嗎?

Are you running a SSL Web Server on port: 443 (y/N):
//您有執行SSL Web Server在連接埠443嗎?

Are you running Routed (RIP) on port: 520 (y/N):
//您有執行Routed(RIP)在連接埠520嗎?

Do you wish to open NFS port 2049 (not recommended)? (y/N):
//您要打開NFS連接埠2049嗎?(不建議)

Do you wish to open X-Server ports 5999-6003 (not recommended)? (y/N):
//您要打開X-Server連接埠5999-6003嗎?(不建議)

Are there any other ports you wish to open to the outside? (y/N):
//您還有其它的連接埠要打開嗎?

Start PMFirewall on bootup? (Y/n):
//系統啟動時要自動執行PMFirewall嗎?

PMFirewall has the ability to autodetect your IP address information. You
may override the atuodetection and specify your IP information here.
Overriding is not recommended if you are assigned a different IP address
each time you connect to the outside world.

Do you want pmfirewall to autodetect your IP address? (Y/n):
//您希望pmfirewall自動取得您的IP嗎?

Will this box Masquerade connections for other PC's (y/N):
//您是否要啟用IP偽裝分享頻寬給其它PC?
//如果您是選擇NO不要的話安裝就會跳至設定完成畫面。如果選擇Yes的話將會顯示以
下畫面。

What is your Internal Interface?
Internal Interface [eth1]:
//您LAN的網路卡為?內定值eht1。

Do you want pmfirewall to autodetect your internal IP address? (Y/n):
//您希望pmfirewall自動取得您LAN的網路卡的IP嗎?

Will you be running a DHCP Server (y/N):
//您有執行DHCP Server嗎?

恭喜您已經設定完成您的firewall您應該可以看到如圖四的畫面:



6:利用cd的指令變更目錄到pmfirewall的安裝目錄裡,系統的內定值為
/usr/local/pmfirewall,執行
〝./pmfirewall〝,我們將會看到以下防火牆指令的說明。
  USAGE:  pmfirewall [command]

  COMMANDS:
       start      Enables PMFirewall. //起動防火牆
       stop       Disables PMFirewall. //停止使用防火牆
       restart    Flushes and reloads the rules in PMFirewall. //清除之後再
重新起動防火牆
       masqstart  Enables IP Masquerading only (no firewall).  //起動IP偽
裝,不使用防火牆
       masqstop   Disables IP Masquerading only (no firewall). //停止使用IP
偽裝,不使用防火牆
       uninstall  Completely removes PMFirewall. //完全移除PMFirewall
       help       Displays this list of options. //顯示指令說明
7:執行〝./pmfirewall start〝,開始使用剛才設定的防火牆。

●了解hosts.allow及hosts.deny●
說簡單一點,這兩個檔案是用來控制哪些網域可以使用inetd.conf所提供的service,
如pop3、ftp及imap等。hosts.allow代表允許使用,hostsd.eny代表拒絕使用,我們看
一下以下hosts.aoolw的例子讓各位可以清楚一點如何去設定。

1:ALL EXCEPT ipop3d : 192.168.1.
提供所有的service除了ipop3d之外給192.168.1.x,其中x代表整個class C。

2:(ALL EXCEPT ipop3d (EXCEPT in.ftpd)) : 192.168.1.,192.168.2.
提供所有的service除了ipop3d及in.ftpd以外給192.168.1.x及192.168.2.x,其中x代
表整個class C

3:ALL: .168.95.1
提供所有的service給168.95.1結尾的ip使用,

4:ALL: 192.168.1. EXCEPT 192.168.1.1
提供所有的service給192.168.1.x其中x代表整個class C,除了192.168.1.1這個ip無
法使用。

5:ALL:131.155.72.0/255.255.254.0
提供所有的service給131.155.72.0至131.155.73.255這些IP都可以使用。

6:ALL: ALL
所有的人都無法使用service

●hosts.deny的進階用法●

ALL: ALL: spawn (echo Security notice from host `/bin/hostname`; \
echo; /usr/sbin/safe_finger @%h ) | \
/bin/mail -s "%d-%h security" root & \
: twist ( /bin/echo -e "\n\nWARNING connection not allowed. Your attempt has
bee
n logged. \n\n\n警告您尚未允許登入,您的連線將會被記錄,並且作為以後的參考
\n\n ". )
 我們利用 spawn 的功能執行echo秀出訊息、/bin/hostname求得主機名稱、及
/usr/sbin/safe_finger求得遠端電腦的IP, 把所得的內容導入給/bin/mail程式當作本
文,並把信件寄給本機的root最高管理者,而-s "%d-%h security"的意思是把遠端嘗
試連線的service及IP當作信件主旨寄給root。信件的全文如圖五:



而twist是把遠端連線切斷後秀在營幕上的訊息。如圖六:



這樣下來一般想入侵的人大概就會收手了,據筆者的實際經驗來說很少有人重覆嘗試入
侵,也許是因為我已經跟想入侵的人說了我有記錄。

●hosts.deny與ipchains 結合●
假設還真的有人一直嘗試連線,而我們每天看信也很煩,那不彷即時把連線的封包給
DENY掉。但我們總不能當我們linux重開機之後這些曾經想侵入的資料沒有記錄下來,
所以我們再利用spawn把資料導入至
/usr/local/pmfirewall/pmfirewall.rules.local,所以當我們下次重新開機之後執行
PMFirewall之後也自動執行我們自動設定的ipchains的規則而完整的程式如下。
ALL: ALL: spawn (echo Security notice from host `/bin/hostname`; \
echo; /usr/sbin/safe_finger @%h ) | \
/bin/mail -s "%d-%h security" root & \
: spawn ( `/sbin/ipchains -A input -p all -s %h -d 0/0 -j DENY &` ) \
: spawn ( echo /sbin/ipchains -A input -p all -s %h -d 0/0 -j DENY >>
/usr/local/pmfirewall/pmfirewall.rules.local ) \
: twist ( /bin/echo -e "\n\nWARNING connection not allowed. Your attempt has
bee
n logged. \n\n\n警告您尚未允許登入,您的連線將會被記錄,並且作為以後的參考
\n\n ". )
這樣作不只我們會有信件上的記錄。也會在ipchains上加入這個IP的DENY的資料。也就
是說,這個IP再也無法連線至這台主機了。各位讀者可視情況加入之。

●結語●
當網管的人大概都聽過最安全的伺服器就是把網路線給拔掉的笑話,這也道出了只要
是連在網際網路上的伺服器就會有被入侵的可能性,如果能把網路規劃成DMZ或者加裝
ipmasqadm來轉遞各種service至內部網路的server也不失為另一種加強安全性的作法。
防火牆只是減少被入侵的機會而並不能杜絕,管理者仍需隨時注意各種安全的通報,及
程式的bugs的報告。適時的更新程式,才是最佳的安全之路。



--