Monthly Archives: January 2005

iptables 使用简介

處理 firewall rule 的選項

且不忙著設計甚麼的,先來熱熱身︰

# iptables -A INPUT -j ACCEPT

大小楷絕不可以亂!輸入這條 rule 後,用 “iptables -L -v -n” 該看到類似以下結果︰

Chain INPUT (policy ACCEPT 2939 packets, 1124863 bytes)

 pkts bytes target     prot opt in     out     source               destination

     1   241 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0

它表示甚麼呢?-A 是 append,它會在 INPUT、FORWARD、OUTPUT 其中一條 “chain” 裡加一條 “rule” 而這條 rule 在隨後的參數將會見到。而 -j 呢,則是定義究竟那些 packet 該怎樣處理。現在是 ACCEPT,即讓它通過。如果不想讓任何 packet 通過呢?十分簡單︰

# iptables -R INPUT 1 -j DROP

今次由 ACCEPT 變 DROP,就不會有任何 packet 可以過關了。但…… -R 又是甚麼?那是 replace 的意思,隨後的 “INPUT 1” 即 replace 了第一條 chain,即剛才 ACCEPT 那一條呀!

當然,firewall 的功用不可能是禁止任何 traffic 吧!因此讓我們先清除它︰

# iptables -D INPUT 1

-D 表示 delete,用法和上面的 -R 一樣。除了 -A、-R、-D 外還有 -I 表示 insert;-A 是逐條逐條 rule 加上去,-I 則是在整串的 rule 中間加插某一條 rule,除此之外沒有其它分別了。

最後還有 -F 表示 flush,顧名思義它會把你辛辛苦苦 set 好的 rule 都沖進馬桶裡!

有關 IP address 的選項

再多點熱身才好辦事,試試吧︰

# iptables -A INPUT -s 199.95.206.201 -j DROP

為甚麼用這個 IP 作示範?總之是個討人厭的地方吧!不要問了!一句說完,就是 block 了這一個 IP 不准它有任何 traffic 進入自己的地盤。不單是一個 IP,一個 range 的 IP 也可以︰

# iptables -A INPUT -s 10.0.0.0/8 -j DROP

如果你的 LAN 是使用 192.168.0.x 的,那當然不希望有 10.x.x.x 地址出現!因此 DROP 了這種 IP 是很正常的。

上面兩個例子是用 -s(即 source IP)的,且看看它的反面,即 -d(destination)的例子︰

# iptables -A INPUT -d 192.168.0.1 -j DROP

假設你的 IP 是 192.168.0.2 吧,那麼這條 rule 對你完全沒有影響;相反,若你的 IP 是 192.168.0.1,那任何到你的機器的 packet 都會被 DROP 了。

-s 和 -d 可以放在一起用,而且它們和 IP address 之間可以放一個嘆號(!)表示 “not” 的意思。例如︰

# iptables -P INPUT DROP
# iptables -A INPUT -s ! 192.168.0.3 -d 192.168.0.0/24 -j ACCEPT

筆者漏了 -P 未介紹,那是 default policy 的意思,即預先定義如果甚麼 rule 也不能決定 packet 的去向的時候,會預設讓它通過呢,還是預設拒諸門外,還是做其它動作。這裡預設是 DROP。好了,下一句才是筆者想講的,意思是︰如果 source 不是 192.168.0.3,而 destination 是 192.168.0.x 任何一個 IP,都會讓它通過。那即是封殺了 192.168.0.3!

在看下一節前,先清一清舊的垃圾 rules 罷︰

# iptables -F

指定 network interface 的選項

屬於這類的選項很少,和上面的一樣,只有兩個︰-i 和 -o。-i 是指明 input 的 interface,只會在 INPUT chain 時有用;相反,-o 是 output interface,只會在 output 時有用。先看看實例︰

# iptables -A INPUT -i eth1 -s 192.168.0.0/24 -d 192.168.0.0/24 -j ACCEPT

很簡單,接受所有 LAN 的 traffic 罷了。不過還加上 “-i eth1″,防止 LAN 外有人刻意送入一些假的 packet,扮成是內部的 traffic。但這個其實已不需要了,因 kernel 本身已有機制可防止這種情況,輸入以下一句就行,解釋就免了。^_^

# echo ‘1’ > /proc/sys/net/ipv4/conf/all/rp_filter

還有 -o……用一個簡單的例子吧。

# iptables -A OUTPUT -o ppp0 -d 205.138.3.22 -j REJECT

其實並不簡單。首先,-j REJECT 和 -j DROP 的相同之處都是拒絕,但 DROP 是當沒事發生,REJECT 則是送出回應說這個 packet 被 REJECT 了。另外,緊記︰-o 是和 OUTPUT chain 一起用的!至於那個 IP 嘛……總之這條 rule 可防止你的資料在不知情的情況下被送出到某間專收集全世界所有人的上網習慣等資料的公司吧!

這恐怕暫時已經夠一部份讀者們學一段時間了。至於其它早已懂得這方面知識的朋友,筆者謹此致歉!將來希望會有機會和大家探討一些較深入的設定呢!

iptables 主網址︰
http://netfilter.kernelnotes.org
http://netfilter.filewatcher.org
http://netfilter.samba.org

ipchains 網址︰
http://netfilter.kernelnotes.org/ipchains/
http://netfilter.filewatcher.org/ipchains/
http://netfilter.samba.org/ipchains/

教育网IP地址段

本路由表由鸿剑青个人正理收集。
route add -net 59.64.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 59.65.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 59.66.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 59.67.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 59.68.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 59.69.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 59.70.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 166.111.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 162.105.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.4.128.0 netmask 255.255.128.0 gw 222.23.144.65
route add -net 202.38.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.110.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.111.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.112.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.113.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.114.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.115.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.116.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.117.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.118.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.119.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.119.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.120.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.121.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.122.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.123.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.124.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.125.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.126.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.127.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 202.128.0.0 netmask 255.128.0.0 gw 222.23.144.65
route add -net 210.0.0.0 netmask 255.128.0.0 gw 222.23.144.65
route add -net 211.0.0.0 netmask 255.128.0.0 gw 222.23.144.65
route add -net 211.137.100.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 211.141.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.144.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.153.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.154.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.155.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.156.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.157.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.158.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.161.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 211.162.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.0.0.0 netmask 255.128.0.0 gw 222.23.144.65
route add -net 218.192.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.193.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.194.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.195.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.196.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.197.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.198.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.199.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.200.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.241.80.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 218.244.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.245.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.246.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 218.247.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 219.128.0.0 netmask 255.128.0.0 gw 222.23.144.65
route add -net 220.170.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.171.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.173.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.174.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.175.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.178.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.184.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.192.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.194.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.198.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.200.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 220.201.4.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 220.201.162.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 220.201.81.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 220.248.60.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 220.248.61.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 220.248.62.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 220.249.191.0 netmask 255.255.255.0 gw 222.23.144.65
route add -net 221.4.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.6.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.8.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.10.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.11.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.12.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.13.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.14.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.136.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.194.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.203.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.207.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.208.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.209.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.226.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 221.237.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.16.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.17.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.18.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.19.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.20.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.21.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.22.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.23.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.24.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.25.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.26.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.27.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.28.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.29.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.30.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.31.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.33.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.35.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.40.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.47.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.49.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.51.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.53.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.54.0.0 netmask 255.255.0.0 gw 222.23.144.65< br />route add -net 222.55.0
.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.62.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.84.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.94.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.132.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.136.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.192.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.193.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.194.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.195.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.196.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.197.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.198.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.199.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.200.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.201.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.202.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.203.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.204.0.0 netmask 255.255.0.0 gw 222.23.144.65
route add -net 222.205.0.0 netmask 255.255.0.0 gw 222.23.144.65