使用iptables實現軟件防火牆

iptables簡介:

iptables 是與 Linux 內核集成的 IP 信息包過濾系統。如果 Linux 系統連接到因特網或 LAN、服務器或連接 LAN 和因特網的代理服務器, 則該系統有利於在 Linux 系統上更好地控制 IP 信息包過濾和防火牆配置。

netfilter/iptables IP 信息包過濾系統是一種功能強大的工具,可用於添加、編輯和除去規則,這些規則是在做信息包過濾決定時,防火牆所遵循和組成的規則。這些規則存儲在專用的信 息包過濾表中,而這些表集成在 Linux 內核中。在信息包過濾表中,規則被分組放在我們所謂的鏈(chain)中。

雖然 netfilter/iptables IP 信息包過濾系統被稱爲單個實體,但它實際上由兩個組件netfilter 和 iptables 組成。

netfilter 組件也稱爲內核空間(kernelspace),是內核的一部分,由一些信息包過濾表組成,這些表包含內核用來控制信息包過濾處理的規則集。

iptables 組件是一種工具,也稱爲用戶空間(userspace),它使插入、修改和除去信息包過濾表中的規則變得容易。除非您正在使用 Red Hat Linux 7.1 或更高版本,否則需要下載該工具並安裝使用它。

重新編譯linux內核

一、合併kernel+layer7補丁

[root@localhost ~]#tar -jxvf linux-2.6.25.19.tar.bz2 -C /usr/src/
[root@localhost ~]#tar zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/

[root@localhost ~]#cd /usr/src/linux-2.6.25.19/
[root@localhost linux-2.6.25.19]#patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch

  2、配置新內核

[root@localhost linux-2.6.25.19]#cp /boot/config-2.6.18-8.el5 .config    //沿用舊的內核配置
[root@localhost linux-2.6.25.19]#make menuconfig

在“Networking --> Networking Options --&gt; Network Packet filtering framework (Netfilter) ”--&gt; Code Netfilter Configuration
將“Netfilter connection tracking suport (NEW)”選擇編譯爲模塊(M),需選取此項才能看到layer7支持的配置。

配置如下:     

1 2 3 4 5

將layer7、string、state、time、IPsec、iprange、connlimit,ftp等編譯成模塊。

6  8 11

9 10  12

2) ---&gt; IP: Netfilter Configuration
將“IPv4 connection tracking support (require for NAT)”編譯成模塊。

15

13

將“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”編譯成模塊。

14 
3、編譯及安裝模塊、新內核
[root@localhost linux-2.6.25.19]#make && make modules_install && make install
編譯安裝成後後,重啓選擇使用新的內核(2.6.25.19)引導系統

[root@localhost linux-2.6.25.19]#init 6

二、重新編譯iptables
    1、卸載現有iptables

[root@localhost ~]#rpm -e iptables iptstat --nodeps
    2、合併iptables+layer7補丁
[root@localhost ~]#tar jxvf iptables-1.4.2.tar.bz2 -C /usr/src/
[root@localhost ~]#cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/
[root@localhost iptables-1.4.1.1-for-kernel-2.6.20forward]#cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/
3、編譯安裝

root@localhost ~]#cd /usr/src/iptables-1.4.2/

[root@localhost iptables-1.4.2]#/configure --prefix=/ --with-ksource=/usr/src/linux-2.6.25.19
[root@localhost iptables-1.4.2]#make && make install

安裝l7-protocols模式包
[root@localhost ~]#tar zxvf l7-protocols-2008-10-04.tar.gz -C /etc/
[root@localhost ~]#mv /etc/l7-protocols-2008-10-04 /etc/l7-protocols

二,iptables 模塊介紹
  1. string(字符串匹配,可以用做內容過濾)

下面是例子:

iptables -I FORWARD -m string --string "騰訊" -j DROP
iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP
iptables -I FORWARD -d 192.168.3.0/24 -p tcp --sport 80 -m string --string "廣告" -j DROP 

2. connlimit(同時連接個數限制匹配)

cat connlimit/info

這個增加一個iptables匹配允許你限制每個客戶ip地址的併發tcp連接,即同時連接到一個服務器個數. 
例子:

allow 2 telnet connections per client host (允許每個客戶機同時兩個telnet連接)
iptables -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT 
iptables -p tcp --syn --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT
這下面例子限制80端口最多同時16個連接請求) 
iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT
模塊 connlimit 作用:連接限制 
--connlimit-above n 限制爲多少個
--connlimit-mask n 這組主機的掩碼,默認是connlimit-mask 32 ,即每ip.
例如:只允許每個ip同時5個80端口轉發,超過的丟棄:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP
例如:只允許每組C類ip同時10個80端口轉發:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP
例如:爲了防止DOS太多連接進來,那麼可以允許最多15個初始連接,超過的丟棄.
/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT 
3. time(時間匹配)

Match only if today is one of the given days. (format: Mon,Tue,Wed,Thu,Fri,Sat,Sun ; default everyday)
(只是匹配已經給出的天,格式Mon,Tue,Wed,Thu,Fri,Sat,Sun ;默認每天)
(開始日期 日期)
Match only if it is after `date' (Inclusive, format: YYYY]]]] h,m,s start from 0 ; default to 1970)
(只是匹配這個開始日期值之後的(包括,格式: YYYY]]]] h,m,s start from 0 ; 默認是1970):
Match only if it is before `date' (Inclusive, format: YYYY]]]] h,m,s start from 0 ; default to 2037)
(只是匹配這個開始日期值之前的(包括,格式: YYYY]]]] h,m,s start from 0 ; 默認是2037):
Example: (例子:)
-A INPUT -m time --timestart 8:00 --timestop 18:00 --days Mon,Tue,Wed,Thu,Fri
will match packets that have an arrival timestamp in the range 8:00-&gt;18:00 from Monday to Friday.
(上面將匹配從到達日期是星期一至星期五時間從8:00至18:00的包)
-A OUTPUT -m time --timestart 8:00 --timestop 18:00 --Days Mon --date-stop 2010
will match the packets (locally generated) that have a departure timestamp in the range 8:00-&gt;18:00 on Monday only, until 2010
(上面將匹配本地產生的時間範圍直到2010年爲止的每個星期一8:00至18:00的包)
NOTE: the time match does not track changes in daylight savings time

4. iprange (ip範圍匹配)  

This patch makes possible to match source/destination IP addresses against inclusive IP address ranges.
翻譯: 這個補丁令匹配源/目標 IP地址可以倚着給出的地址範圍進行匹配
Examples:(例子)
iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT
這個例子是允許源ip地址範圍192.168.1.5-192.168.1.124的包通過
iptables -A FORWARD -m iprange --dst-range 10.0.0.0-10.255.255.255 -j ACCEPT
這個例子是允許目標ip地址範圍10.0.0.0-10.255.255.255的包通過

5. geoip(根據地理位置匹配)

This patch makes possible to match a packet by its source or destination country.
翻譯:這個補丁令一個包能夠根據源或目的國家(地區)匹配
GeoIP options: (選項:)
--src-cc, --source-country country
Match packet coming from (one of) the specified country(ies)
根據包的來源(或非來源)地區匹配
--dst-cc, --destination-country country
Match packet going to (one of) the specified country(ies)
根據包的去向(或非去向)地區匹配
NOTE: The country is inputed by its ISO3166 code.
注意:這個國家地區列表放在ISO3166編碼裏
The only extra files you need is a binary db (geoipdb.bin) & its index file (geoipdb.idx).Both files are generated from a countries & subnets database with the csv2bin tool,available at www.cookinglinux.org/geoip/. Both files MUST also be moved in /var/geoip/ as the shared library is statically looking for that pathname (ex.: /var/geoip/geoipdb.bin).
這個你需要額外的二進位文件geoipdb.bin 和它的索引文件geoipdb.idx.這兩個文件是國家地區網絡數據庫,是用csv2bin 工具生成的,可以在www.cookinglinux.org/geoip/得到.這些文件必須放在/var/geoip/下,作爲一個共享庫查找路徑名字如/var/geoip/geoipdb.bin

6. Nth(第n個包匹配)
# cat nth/info
Title: iptables nth match (標題: iptables第N個匹配)
Author: Fabrice MARIE (作者:名字,email地址)
Status: Works (狀況:運作)
Repository: base (貯倉庫: 基礎的)
# cat nth/help
This option adds an iptables `Nth' match, which allows you to match every Nth packet encountered. By default there are 16 different counters that can be used.
翻譯: 這個選項增加一個第N個匹配,允許你匹配每隔N個包,默認有16不同的計算方法可以使用
This match functions in one of two ways
1) Match ever Nth packet, and only the Nth packet.
example:(例子)
iptables -t mangle -A PREROUTING -m nth --every 10 -j DROP
This rule will drop every 10th packet.
這個規則將丟棄每隔10個包
2) Unique rule for every packet. This is an easy and quick method to produce load-balancing for both inbound and outbound.
爲每一個包應用一個獨特的規則,這樣是一個容易和快速的負載均衡方法
example: (例如)
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 0 -j SNAT --to-source 10.0.0.5
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 1 -j SNAT --to-source 10.0.0.6
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 2 -j SNAT --to-source 10.0.0.7
This example evenly splits connections between the three SNAT addresses.
上面例子由三個源ip地址平滑分割連接
By using the mangle table and iproute2, you can setup complex load-balanced routing. There's lot of other uses. Be creative!
配合iptables 的mangle表和高級路由iproute2,你能設置一個複合的負載平衡路由.還有其他的用途,具有創造性的設置
Suppported options are: (支持選項有:)
--every Nth Match every Nth packet (匹配每N 個包)
num Use counter 0-15 (default:0) (用計算器(默認是0))
num Initialize the counter at the number 'num' instead of 0. Must be between 0 and Nth-1
(初始化一個計算器,用這個num值而不是0. 必需是在0和N減1之間的數
num Match on 'num' packet. Must be between 0 and Nth-1. If --packet is used for a counter than
there must be Nth number of --packet rules, covering all values between 0 and Nth-1 inclusively.
匹配'num' 包,必需是在0和N減1之間的數,如果該包被用一個計算器,必需是第Nth數字包規則,並覆蓋0至Nth-1的所有值(這個翻譯得不好,請指正)

7. ipp2p(點對點匹配)
# cat ipp2p/info
Title: Detects some P2P packets (標題: 偵查P2P包)
Author: Eicke Friedrich (作者:名字,email地址)
Status: Stable (狀況:穩定的)
Repository: extra (貯倉庫: 額外的)
Recompile: netfilter, iptables (重新編譯:netfilter|iptables)
# cat ipp2p/help
This option makes possible to match some P2P packets therefore helps controlling such traffic.
Dropping all matches prohibits P2P networks.
Combined with conntrack,CONNMARK and a packet scheduler it can be used for accounting or shaping of P2P traffic.
這個選項能夠匹配某些P2P包,幫助控制流量.丟棄所有匹配的P2P.結合連接跟蹤,和一個包的調度器,它能被用作計算和整形一個P2流量
Examples: (例如:)
iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP
iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP
iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP
上例可以封殺很多bt等的P2P軟件.
更多參數更詳細的參考還有ipp2p/iptables/extensions/libipt_ipp2p.man或http://www.ipp2p.org
8. quota(配額匹配)
# cat quota/info Title: iptables quota match (標題: iptables配額匹配)
Author: Sam Johnston (作者:名字,email地址)
Status: worksforme (狀況:運作的)
Repository: base (貯倉庫: 基礎的)
# cat quota/help
This option adds CONFIG_IP_NF_MATCH_QUOTA, which implements network quotas by decrementing a byte counter with each packet.
這個選項增加一個模塊匹配包實現網絡包通過的配額
Supported options are: (支持選項有:)
--quota
The quota in bytes. (用bytes爲單位)
iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j DROP
上例是這樣的,在ip192.168.3.159的目標端口爲80匹配的500個字節內,將被丟棄.直到匹配完這500字節後,纔不會丟棄,就是說,纔可以打開網頁.
KNOWN BUGS: this does not work on SMP systems.
知道的bug:這個不能工作在SMP系統上,(SMP, symmetric multiprocessing 多處理技術)

三,案例配置

所用的拓撲圖

 image

1)軟件防火牆的網卡配置

1 2

外網卡的地址爲192.168.101.31

2)打開軟件防火牆路由轉發功能

[root@localhost ~]# vim /etc/sysctl.conf

image

[root@localhost ~]# sysctl –p

3)做snat允許192.168.2.0訪問外網

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth2 -j MASQUERADE

訪問外網測試

8 

4)把filter表的input,output,forward鏈的默認規則設置爲拒絕允許192.168.1.2通過ssh連接到軟件防火牆

[root@localhost ~]# iptables -A INPUT -s 192.168.1.2 -p tcp --dport 22 -j ACCEPT

[root@localhost ~]# iptables -P INPUT DROP

[root@localhost ~]# iptables -A OUTPUT -s 192.168.1.10 -p tcp --sport 22 -j ACCEPT
[root@localhost ~]# iptables -P OUTPUT DROP

[root@localhost ~]# iptables -P FORWARD DROP

查看input,output,forward鏈的默認規則

5

5)允許工程部在上班時間訪問ftp其它都拒絕

加載ftp模塊

[root@localhost ~]# modprobe ip_nat_ftp

允許工程部在上班時間訪問ftp,21端口

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.1-192.168.2.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT

允許返回的流量

[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --sport 21 -j ACCEPT

允許ftp 20端口的流量

[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --dport 20 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --sport 20 -j ACCEPT

6)測試上班時間不能上網

9

上班時間允許ftp訪問

12

7)設置下班無訪問限制

[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.2.0/24 -o eth2 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.2.0/24 -i eth2 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT

設置時間爲下班時間

10

測試上網訪問

15

 
8)用另外一種方式實現工程部的要求

清空filter表,默認規則不會被清除
[root@localhost ~]# iptables -t  filter -F  FORWARD

上班時間允許ftp
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.1-192.168.2.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT

對於已連接的流量允許其流量的返回
[root@localhost ~]# iptables -t filter -A FORWARD -m state --state  ESTABLISHED,RELATED -j ACCEPT

下班時間允許所有流量
[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.2.0/24 -o eth2 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT

設置時間爲上班時間測試ftp客戶機ip設爲192.168.2.11
[root@localhost ~]# date 091815302012

12 

上網訪問

9

設置爲下班時間測試 上網
[root@localhost ~]# date 091821172012

15

9)設置軟件部上班時間能夠訪問網絡但是不可以訪問帶有“sina”的網站

不允許訪問帶有“sina”的網站
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays  Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP

允許dns流量通過
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays  Mon,Tue,Wed,Thu,Fri -p udp --dport 53  -j ACCEPT

允許軟件部訪問網絡 
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays  Mon,Tue,Wed,Thu,Fri -p tcp --dport 80  -j ACCEPT

不允許軟件部登錄qq
[root@localhost ~]# iptables –I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP

不允許使用迅雷
[root@localhost ~]# iptables –I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j DROP

限制每個連接的最大連接數位3 
[root@localhost ~]# iptables –I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --syn --dport 80 -m connlimit --connlimit-above 3 -j DROP

10)上班時間訪問網絡客戶機地址爲192.168.2.22

17

訪問帶有“sina”的網站

18

11)經理部配置允許上網登錄qq,訪問網絡

允許dns流量

[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.2.41-192.168.2.60  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp  --dport 53  -j ACCEPT
[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.2.41-192.168.2.60  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp  --dport 80  -j ACCEPT

允許qq登錄
[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.2.41-192.168.2.60  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq  -j ACCEPT

測試訪問網絡

 19

 

12)發佈內網的www服務器
[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.101.31 -p tcp --dport 80 -i eth2 -j DNAT --to 192.168.1.21
[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.1.21 -p tcp --dport 80 -j ACCEPT

13)外網的一臺主機192.168.101.33訪問內網服務器

20

配置結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章