IPFilter ( NAT, Firewall, Port Forward )

作者:Andrew Choi (蔡健發)

Since : 16 Jan 2001

Last Update : 16 Jan 2001


聲明:任何形式的摘抄必須保留上述作者和http地址

備注:如果要看最新版本文章 Solaris 10 OS ( IP Filter and NAT ) 設定,請到這裡,http://solaris.tophk.net/basicsetting/ipfilter/index.htm

1. 前言
2. 相關網頁及軟件
3. 安裝IPFilter
4. IPFilter簡單設定及則行
5. 用 Microsoft Windows 測試是否正常運作
6. 另到 Solaris 在開機後自動啟動IPFilter
7. IPFilter Port Direct 實例

1. 前言:

安裝 NAT (Network Address Translation) 的目的是為了使到公司內部使用 Virtual IP 的員工能夠共用到 Internet 的資源。而且安裝 NATSolaris 主機便會變成一個 GatewayRouter 的功能。我所使用的是 IP FilterIP Filter 不單只是提供 Router 功能,而且,它是一個 Firewall

而我覺得最有趣的是Port Direct的功能。

什麼是Port Direct?

一般情況,使用 Virtual IP的電腦只能進行單向性的工作。

例如:你可以使用Virtual IP的電腦則行FTP指令去InternetFTP Server下載或上載資料,但你不能夠使用 Virtual IP的電腦建立FTP Server。因為,Virtual IPInternet上是不存在的,在一般情況下,是需要使用Real IP才可以建立FTP ServerInternet上。但使用了IP FilterPort Direct功能便可以解決這一個問題。

在安裝IP Filter的主機通常都是使用Real IP的,或從連線到ADSL Modem而得到Real IP,我們可以在IP Filter加入一些設定便可以達到目的。

例如:現在有兩台電腦,一台是使用Real IP(202.12.3.1) 及Virtual IP (192.168.10.1) 而且安裝了IP Filter的主機,而另一台是使用Virtual IP(192.168.10.2)FTP Server。當使用者想連接到Virtual IPFTP Server時,他只要使用 FTP 202.12.3.1,便等於連接上 192.168.10.2FTP Server

2. 相關網頁及軟件:

IP Filter
for Network Address Translation. http://coombs.anu.edu.au/~avalon ( download )

3. 安裝IP Filter

把檔案放在 /usr/local/ip-fil3.4.23.tar.gz

# cd /usr/local
# gzip -d ip-fil3.4.23.tar.gz
# tar -vxf ip-fil3.4.23.tar.gz
# cd ip-fil3.4.23
# make solaris ( 不要使用
GNU make,如果你沒有安裝GNU make便不用理會)
# cd SunOS5
# make package
# cd i386-5.8
# pkgadd -d ipf.pkg

# echo > /etc/gateways

IP Filter
會安裝在 /opt/ipf

4. IP Filter簡單設定及則行:

  • 我公司使用的 IP Range192.168.10.1 192.168.10.255

  • 檢查那個 Interface 連上 Internet :

    # ifconfig -a
    lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
    inet 127.0.0.1 netmask ff000000
    elxl0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
    inet 192.168.10.1 netmask ffffff00 broadcast 192.168.10.255
    ether 0:10:5a:5d:7d:e0
    elxl1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
    inet 192.168.20.1 netmask ffffff00 broadcast 192.168.20.255
    ether 0:4:75:86:fa:63
    sppp0: flags=10008d1<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST,IPv4> mtu 1492 index 6
    inet 218.17.7.108 --> 218.17.6.1 netmask ffffff00
    ether 0:0:0:0:0:0

我是使用ADSL Modem連線上網的,所以 sppp0 就是連上 Internet interface,而且你可以看到 sppp0 是用 Real IP 的。

  • 設定 /etc/opt/ipf/ipnat.conf

# pico /etc/opt/ipf/ipnat.conf ( pico is a Editor, you may use your own editor or vi )
map sppp0 192.168.10.0/24 -> 0/32 proxy port ftp ftp/tcp
map sppp0 192.168.10.0/24 -> 0/32 portmap tcp/udp auto
map sppp0 192.168.10.0/24 -> 0/32

  • 則行 IP Filter

    # /etc/init.d/ipfboot start
    # ndd -set /dev/ip ip_forwarding 1

  • 停止 IP Filter

    # /etc/init.d/ipfboot stop
    # ndd -set /dev/ip ip_forwarding 0

5. Microsoft Windows 測試是否正常運作

  • 把以下資料設定在 Microsoft Windows 的網絡卡上
  • Gateway = 192.168.10.1 ( 因為IP Filter 主機是 192.168.10.1 )
    IP address = 192.168.10.2 or Other
    Subnet Mask = 255.255.255.0
    DNS = 152.101.101.101 or Other

  • DOS 模式下測試 Internet 是否正常

    c:\> ping 152.101.101.101
    Reply from 152.101.101.101: bytes=32 time=104ms TTL=241
    Reply from 152.101.101.101: bytes=32 time=127ms TTL=241
    Reply from 152.101.101.101: bytes=32 time=101ms TTL=241
    Reply from 152.101.101.101: bytes=32 time=101ms TTL=241

    Ping statistics for 152.101.101.101:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 101ms, Maximum = 127ms, Average = 108ms

    如果正常,這代表你的電腦已經與 Internet 連線

    c:\> ping www.netvigator.com
    Pinging www.netvigator.com [218.102.23.36] with 32 bytes of data:

    Reply from 218.102.23.36: bytes=32 time=50ms TTL=241
    Reply from 218.102.23.36: bytes=32 time=49ms TTL=241
    Reply from 218.102.23.36: bytes=32 time=49ms TTL=241
    Reply from 218.102.23.36: bytes=32 time=49ms TTL=241

    Ping statistics for 218.102.23.36:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 49ms, Maximum = 50ms, Average = 49ms

    如果不正常,請檢查清楚 WindowsDNS 設定

6. 另到 Solaris 在開機後自動啟動IP Filter

# ln -s /etc/init.d/ipfboot /etc/rc2.d/S65ipfboot
# cd /etc/init.d
# pico ip_forwarding
( pico is a Editor, you may use your own editor or vi )
# ln -s /etc/init.d/ip_forwarding /etc/rc2.d/S77ip_forwarding

把以下 script 放入 /etc/init.d/ip_forwarding

#!/sbin/sh
#
# Description: This file is a start/stop script for IP forwarding.
#

case "$1" in
'start')
echo "Starting IP forwarding... \c"
ndd -set /dev/ip ip_forwarding 1
echo "Done."
;;

'stop')
echo "Stopping IP forwarding... \c"
ndd -set /dev/ip ip_forwarding 0
echo "Done."
;;

*)
echo "Usage: $0 { start | stop }"
;;
esac
exit 0

7. IP Filter Port Direct 實例

  • 實例一

    Computer 1
    連接了 ADSL Modem 上網後,便取得 IP address 202.123.2.1
    當外界的Internet Network使用者觀看
    202.123.2.1的網頁時,IP Filter 會指向 Computer 3 192.168.10.3
    當外界的Internet Network使用者使用202.123.2.1 FTP Server 時,IP Filter 會指向 Computer 2 192.168.10.2

設定如下:

192.168.10.3 is Web Server
Web Server Port Address is Port 80

192.168.10.2 is FTP Server
Ftp Server Port Address is Port 21

# ifconfig -a
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
elxl0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
inet 192.168.10.1 netmask ffffff00 broadcast 192.168.10.255
ether 0:10:5a:5d:7d:e0
elxl1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
inet 192.168.20.1 netmask ffffff00 broadcast 192.168.20.255
ether 0:4:75:86:fa:63
sppp0: flags=10008d1<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST,IPv4> mtu 1492 index 6
inet 202.123.2.1 --> 218.17.6.1 netmask ffffff00
ether 0:0:0:0:0:0

sppp0 是連接InternetInterface

/etc/opt/ipf/ipnat.conf 加入以資料,然後再從新啟動 IP Filter

rdr sppp0 0.0.0.0/0 port 21 -> 192.168.10.2 port 21
rdr sppp0 0.0.0.0/0 port 80 -> 192.168.10.3 port 80

上面兩行的意思是不論 sppp0 是什麼 ip address ,而有人從外界的Internet 網絡要求 port 21 或 port 80 題供服務,IP Filter 便會要求 192.168.10.2 port 21 或 192.168.10.3 port 80 題供服務。


  • 實例二

    Windows XP
    有一個功能是 " 遠端桌面連線 " , " 遠端桌面連線 "的 Port addressPort 3389。參考實列一的方法很容易便可以使到外界的 Windows XP 利用 " 遠端桌面連線 " 連接到使用 Virtual IP Windows XP。但是,如果不止一台使用 Virtual IPWindows XP 需要由外界連接的話,實列一 的方法便不行了。因為,只得一個 Port 3389。 所以要有特別的設定方法來分別連線到那一台 Windows XP。方法就是外界的 Windows XP 在連線時,不要只使用 Port 3389。例如:使用 Port 10000 連線到 Computer 2Port 10001 連線到 Computer 3,如此類推。

設定如下:

" 遠端桌面連線 " 的 Port addressPort 3389

202.123.2.1 Port 10000 = 192.168.10.2 Port 3389
202.123.2.1 Port 10001 = 192.168.10.3 Port 3389


/etc/opt/ipf/ipnat.conf 加入以資料,然後再從新啟動 IP Filter

rdr sppp0 0.0.0.0/0 port 10000 -> 192.168.10.2 port 3389
rdr sppp0 0.0.0.0/0 port 10001 -> 192.168.10.3 port 3389

上面兩行的意思是不論 sppp0 是什麼 ip address ,有人從外界的Internet 網絡要求 port 10000 題供服務,IP Filter 便會要求 192.168.10.2 port 3389題供服務。而有人從外界的Internet 網絡要求 port 10001 題供服務,IP Filter 便會要求 192.168.10.3 port 3389 題供服務。

所以 Windows XP " 遠端桌面連線 " 連線時要用 202.123.2.1:10000 連線到 Computer 2 ,用 202.123.2.1:10001 連線到 Computer 3。

  • 實例三

    Transparent proxy connections

    這是一個很重要的題目。太累啦 ! 都是有時間再寫吧。 ^_^

 

 


E-mail : andrew@tophk.net