2008-06-04 10:00 快快樂樂設定iptables

iptables是Linux上一個非常強大功能的封包控制工具
不但可以拿來做防火牆之用 還可以修改封包的內容
這次練習 我是用LNX-BBC當Firewall
只介紹iptables如何設定 對他的原理不會太過深入去討論
首先我們來看這張從iptables官方網站下抓下來的圖
(圖一)
封包從上面進來 一直往下處理 然後分成左右(A or B) 兩條路線
再合而為一 一直到送出去
A路線是指
當封包經過Routing descision處理後 該封包是要轉送出去的 不是給本身我這台firewall 就走A路線
B路線是指
當封包經過Routing descision處理後 該封包是要傳送給我本身這台Firewall來進一步處理的 就走B
接下來 介紹實驗環境
我用VMWARE 做模擬 一條條規則來設定並介紹
前期作業
1. 確定各介面IP Address 設定正確
2. 啟動ip forwarding的功能 (預設是關閉)
一定要打開 不然的話 192.168.10.10 可以ping 192.168.30.2 但ping不到192.168.30.10
echo "1" > /proc/sys/net/ipv4/ip_forward
3. iptables 的指令 可以如下面一樣來設定 比較好記
iptables -t 表 -A 鍊 -p 協定 -s 來源IP網段 --sport 來源port號 -d 目的地IP網段 --dport 目的地port號 -j 接受或拒絕
開始我們的設定啦
1. 拒絕特定的網段進入
RFC#3330 介紹一些IP 網段 這些網段如果是從Internet來的 必須要擋掉 比如 10.0.0.0/8
該在哪邊設定這條規則呢? 我們從圖一看 在nat表的PREROUTING 這地方設 很適合
在他做Routing descision處理前 只要來源是10.0.0.0/8 就給他擋掉 指令寫法如下
iptables -t nat -A PREROUTING -s 10.0.0.0/8 -j DROP
我們用iptables -t nat -L 來展示nat表的規則
2. DMZ區"只"開放HTTP服務
我們從圖一看 路線應該是走A 所以在filter表的FORWARD鍊 這地方設定規則 很適合
#拒絕所有的服務
iptables -t filter -P FORWARD DROP
# 開放HTTP服務 (要設定兩行 因為一條是要求的封包 另一條是回應的封包)
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 80 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 80 --dport 1024:65535 -j ACCEPT
我們用iptables -t filter -L 來展示filter表的規則
展出結果
3. DMZ區再開放ftp服務
同上 一樣是在filter表的FORWARD鍊 這地方設定規則 但要注意一點
ftp要開放兩個port 一個command port是#21 另一個是data port #20
#Open FTP command port
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 21 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 21 --dport 1024:65535 -j ACCEPT
#Open FTP data port
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 20 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 20 --dport 1024:65535 -j ACCEPT
我們用iptables -t filter -L 來展示filter表的規則
展出結果
4. DMZ區再開放SSH服務
同上 一樣是在filter表的FORWARD鍊 這地方設定規則
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 22 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 22 --dport 1024:65535 -j ACCEPT
我們用iptables -t filter -L 來展示filter表的規則
展出結果
5. DMZ區再開放ICMP協定(只允許 192.168.20.0/24 跟 192.168.30.0/24 互ping)
iptables -t filter -A FORWARD -p icmp -s 192.168.30.0/24 -d 192.168.20.0/24 -j ACCEPT
iptables -t filter -A FORWARD -p icmp -s 192.168.20.0/24 -d 192.168.30.0/24 -j ACCEPT
我們用iptables -t filter -L 來展示filter表的規則
展出結果
6. 只允許internal網路的TCP和UDP(不允許ICMP)的流量 使用NAT轉換成192.168.10.2(介面eth0的ip)連出去
這邊要做兩件事 第一是開放內連外 及 外連內的TCP和UDP(不允許ICMP)的流量
第二設定內連外 要使用NAT 轉成192.168.10.2 讓外面看不到192.168.30.0/24的IP 只看到192.168.10.2的連線
第一件事設定很簡單 "!"是指"除了,以外"的意思
iptables -t filter -A FORWARD -p ! icmp -s 192.168.30.0/24 -j ACCEPT
iptables -t filter -A FORWARD -p ! icmp -d 192.168.30.0/24 -j ACCEPT
第二件事 就是在nat表的POSTROUTING鍊 這地方設
iptables -t nat -A POSTROUTING -s 192.168.30.0/24 -o eth0 -j SNAT --to-source 192.168.10.2
我們用iptables -t filter -L 來展示filter表的規則
展出結果

防火牆(FW)本身自己的安全設定 當然也很重要
談到防火牆(FW)自己 那路線就是走B了
7. 允許FW可以使用自己的Loopback介面 (請回來看圖一 自己判斷該在哪設定這規則)
iptables -t filter -A INPUT -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT
我們用iptables -t filter -L 來展示filter表的規則
展出結果
8. "只"允許內部網路192.168.30.0/24 使用ssh的連線進入FW的192.168.30.2
iptables -t filter -A INPUT -p tcp -s 192.168.30.0/24 --sport 1024:65535 -d 192.168.30.2 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p tcp -s 192.168.30.2 --sport 22 -d 192.168.30.0/24 --dport 1024:65535 -j ACCEPT
iptables -t filter -P INPUT DROP
另外 開啟ssh服務的指令為
/etc/init.d/sshd start
我們用iptables -t filter -L 來展示filter表的規則
展出結果(我這台電腦的IP網段是屬於VMnet3:192.168.30.0/24)
9. 關於攻擊 我這邊用nmap來測試 網路可找到一堆規則來抵擋
# null-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL NONE -j LOG \
#--log-prefix "IPTABLES NULL-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL NONE -j DROP
# xmas-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL ALL -j LOG \
#--log-prefix "IPTABLES XMAS-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL ALL -j DROP
# synfin-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL SYN,FIN -j LOG \
#--log-prefix "IPTABLES SYNFIN-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL SYN,FIN -j DROP
# nmap-xmas-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,FIN -j LOG \
#--log-prefix "IPTABLES NMAP-XMAS-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,FIN -j DROP
# fin-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL FIN -j LOG \
#--log-prefix "IPTABLES FIN-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL FIN -j DROP
# nmap-id
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,SYN,FIN -j LOG \
#--log-prefix "IPTABLES NMAP-ID:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,SYN,FIN -j DROP
上面的設定做法是 當有符合一種nmap特徵 先記錄起來 存在/var/log/messages
再把他抵檔掉
但.. 光是一個nmap 就有上百種以上的方式來掃瞄 我不可能一一為這些來設定
而且規則越多 網路效能是越低
所以我的建議是 完全斷掉內網與外網的所有網路連繫 只讓DMZ 做為內外網的中介
我們只需要專注在DMZ區的安全設定 便可以一起保護到內網的安全了
以下 是我為了報告內容 寫的很爛的shell script 來完成的設定
vi ./fw.sh
#!/bin/bash
#Program:
#This program is used to setup firewall rules.
#History:
# 2008.05.31 Peter
PATH=/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# clear iptables rules
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
iptables -t filter -F
iptables -t filter -X
iptables -t filter -Z
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -Z
# Internet area firewall rules
# Some RFC IP address can not into eth0.
iptables -t nat -A PREROUTING -s 10.0.0.0/8 -j DROP
iptables -t nat -A PREROUTING -s 172.16.0.0/12 -j DROP
iptables -t nat -A PREROUTING -s 169.254.0.0/16 -j DROP
# Any internet traffic form Internet can not go to Internal area.
iptables -t filter -P FORWARD DROP
#Open HTTP Port
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 80 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 80 --dport 1024:65535 -j ACCEPT
#Open FTP command port
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 21 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 21 --dport 1024:65535 -j ACCEPT
#Open FTP data port
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 -d 192.168.20.0/24 --dport 20 -j ACCEPT
iptables -t filter -A FORWARD -p tcp -s 192.168.20.0/24 --sport 20 --dport 1024:65535 -j ACCEPT
#Open SSH port
iptables -t filter -A FORWARD -p tcp --sport 1024:65535 --dport 22 -d 192.168.20.0/24 -j ACCEPT
iptables -t filter -A FORWARD -p tcp --sport 22 -s 192.168.20.0/24 --dport 1024:65535 -j ACCEPT
# DMZ area firewall rules
# Open ICMP between DMZ and internal area
iptables -t filter -A FORWARD -p icmp -s 192.168.30.0/24 -d 192.168.20.0/24 -j ACCEPT
iptables -t filter -A FORWARD -p icmp -s 192.168.20.0/24 -d 192.168.30.0/24 -j ACCEPT
# Internal area firewall rules
# Use NAT goes to internet but deny ICMP
iptables -t filter -A FORWARD -p ! icmp -s 192.168.30.0/24 -j ACCEPT
iptables -t filter -A FORWARD -p ! icmp -d 192.168.30.0/24 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.30.0/24 -o eth0 -j SNAT --to-source 192.168.10.2
# Set up firewall
# enable ip forwarding
echo "1" > /proc/sys/net/ipv4/ip_forward
# loopback interface
iptables -t filter -A INPUT -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT
#only allow 192.168.30.0/24 uses ssh into firewall
iptables -t filter -A INPUT -p tcp --sport 1024:65535 -s 192.168.30.0/24 --dport 22 -d 192.168.30.2 -j ACCEPT
iptables -t filter -A INPUT -p tcp --sport 22 -s 192.168.30.2 --dport 1024:65535 -d 192.168.30.0/24 -j ACCEPT
iptables -t filter -P INPUT DROP
#Attack
# null-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL NONE -j LOG \
#--log-prefix "IPTABLES NULL-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL NONE -j DROP
# xmas-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL ALL -j LOG \
#--log-prefix "IPTABLES XMAS-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL ALL -j DROP
# synfin-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL SYN,FIN -j LOG \
#--log-prefix "IPTABLES SYNFIN-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL SYN,FIN -j DROP
# nmap-xmas-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,FIN -j LOG \
#--log-prefix "IPTABLES NMAP-XMAS-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,FIN -j DROP
# fin-scan
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL FIN -j LOG \
#--log-prefix "IPTABLES FIN-SCAN:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL FIN -j DROP
# nmap-id
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,SYN,FIN -j LOG \
#--log-prefix "IPTABLES NMAP-ID:"
iptables -t nat -A PREROUTING -p TCP --tcp-flags ALL URG,PSH,SYN,FIN -j DROP
以上是我的期末報告心享 希望對大家有幫助
參考資料:
LNX-BBC http://www.lnx-bbc.com/
netfilter (官方網站) http://www.netfilter.org/documentation/index.html#documentation-howto
Linux 防火牆入門 http://linux.tnc.edu.tw/techdoc/firewall/t1.html
設置 Linux 防火牆 http://www.spps.tp.edu.tw/documents/memo/iptables/iptables.htm
鳥哥的 Linux 私房菜 http://linux.vbird.org/linux_server/0250simple_firewall.php
後記 我在課堂上 只demo到三分之一 老師就說可以了 因為他認為我已經了解iptables的用法了 就喊停了
害我準備了好久~~ 還事先練習很多次~
最初發表 / 最後更新: 2008.06.04 / 2008.06.04
我要評分:



