iptables-layer7的應用

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

編譯內核時根據本身須要酌情添加模塊。下面是個人案例須要添加的模塊支持。vim

/配置內核時,在「Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) 」處主要注意兩個地方:
1) ---> Core Netfilter Configuration
//將「Netfilter connection tracking suport (NEW)」選擇編譯爲模塊(M),需選取此項才能看到layer7支持的配置。
//將FTP,layer七、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」編譯成模塊。ssh

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

公司有三個部門ide

工程部門 192.168.145.11--145.20測試

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

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

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

經理辦公室 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

相關文章
相關標籤/搜索