iptables-layer7的應用

如果讓iptables支持七層過濾,我們首先需要打補丁,編譯內核等等。這需要耗費一些時間。網上也有許多這樣的文章。這裏就不作介紹。

編譯內核時根據自己需要酌情添加模塊。下面是我的案例需要添加的模塊支持。

/配置內核時,在“Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) ”處主要注意兩個地方:
1) ---> Core Netfilter Configuration
//將“Netfilter connection tracking suport (NEW)”選擇編譯爲模塊(M),需選取此項才能看到layer7支持的配置。
//將FTP,layer7、string、state、time、IPsec、iprange、connlimit……等編譯成模塊,根據需要看着辦。

2) ---> IP: Netfilter Configuration
//將“IPv4 connection tracking support (require for NAT)”編譯成模塊。
//將“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”編譯成模塊。

通過一個案例簡單理解iptables的規則

公司有三個部門

工程部門 192.168.145.11--145.20

軟件部門 192.168.145.21-145.30
經理辦 192.168.145.31-145.40

上班時間 (週一---週五 08:20:00)
工程部門 上班時間ftp 不允許qq http 迅雷 下班後無限制

軟件部門 http 不允許非法站點sina ,不允許使用迅雷 qq,連接數 最多3個
下班後無限制

經理辦公室 http qq 都可以,下班後無限制

dmz區域www服務器進行發佈

拓撲圖

p_w_picpath

linux主機添加網卡至三塊

ip地址:

eth0:192.168.145.200

eth1:192.168.101.20

eth2:192.168.2.254

ftp服務器:192.168.101.21

[root@localhost ~]# service network restart

公司內部網關指向192.168.145.200

dmz主機網關指向192.168.2.254

編譯過的內核

[root@localhost ~]# uname -r
2.6.25.19

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

7 net.ipv4.ip_forward = 1 //打開路由轉發功能

[root@localhost ~]# sysctl –p //使修改生效

對來源是192.168.145.0 網段的進行nat轉換。把源地址變爲eth1的接口地址

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

root@localhost ~]# modprobe ip_nat_ftp //加載模塊

首先把filter表格中規則設爲拒絕所有

由於我是通過終端ssh登錄上去的,先把22端口設爲允許通過

[root@localhost ~]# iptables -A INPUT -s 192.168.145.10 -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -d 192.168.145.10 -p tcp --sport 22 -j ACCEPT

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

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

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

[root@localhost ~]# date
2012年 09月 18日 星期二 10:00:03 CST

工程部門的時間和ftp規則

[root@localhost ~]# iptables -t filter –A FORWARD -m iprange --src-range 192.168.145.11-192.168.145.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 iprange --src-range 192.168.145.11-192.168.145.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

工程部門測試

p_w_picpath p_w_picpath

公司內部的人下班時間沒有限制

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

域名指向電信dns服務器222.88.88.88

[root@localhost ~]# date 091820012012
2012年 09月 18日 星期二 20:01:00 CST

p_w_picpath

[root@localhost ~]# date 091811032012
2012年 09月 18日 星期二 11:03:00 CST

軟件部門規則

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

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j DROP

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -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

工程部門測試,dns服務器設爲222.88.88.88

p_w_picpath p_w_picpath

經理辦公室

[root@localhost ~]# iptables -A FORWARD -m iprange --src-range 192.168.145.31-192.168.145.40 -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.145.31-192.168.145.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -j ACCEPT

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

p_w_picpath

dmz區域服務器向外發佈

[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.101.20 -p tcp --dport 80 -i eth1 -j DNAT --to 192.168.2.100

[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.2.100 -p tcp --dport 80 -j ACCEPT

p_w_picpath

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