案例:三个部门,工程部 软件部 经理办公室实现iptables过滤




工程部门:192.168.2.10192.168.2.20 /30 192.168.2.10 192.168.2.11 192.168.2.12/30 192.168.2.16/30 192.168.2.20

软件部门: 192.168.2.21-192.168.2.30 /30 2.21/32 2.22/32 2.23/32 192.168.2.24/30[24-27]

2.28--2.30/32

经理办公室: 192.168.2.31-192.168.2.40

拓扑图:

linux redhat5.4   iptables+Layer7小案例 的实现

iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

[[email protected] ~]# iptables -t filter -A INPUT -s 192.168.2.1 -p tcp --dport 22 -j ACCEPT

[[email protected] ~]# iptables -t filter -A OUTPUT -d 192.168.2.1 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

[[email protected] ~]# iptables -P INPUT DROP

[[email protected] ~]# iptables -P OUTPUT DROP

[email protected] ~]# iptables -P FORWARD DROP

1: 工程部门

上班时间 的限制可以访问 ftp服务器 [192.168.1.166]filter forward

iptables -t filter -A FORWARD -m iprange --src-rang 192.168.2.10-192.168.2.20 -m time --timestart 08:00 --timestop 20:00

--weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -d 192.168.1.166 -j ACCEPT

iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

modprobe ip_nat_ftp

不允许聊天 qq L7

[[email protected] ~]# iptables -t filter -I FORWARD 2 -m iprange --src-rang 192.168.2.10-192.168.2.20 -m time --timestart 08:00 --timestop 20:00 --weekdays

Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP

不允许http上网 (NAT)

下班后无限制

[[email protected] ~]# iptables -t filter -I FORWARD 2 -s 192.168.2.0/24 -o eth1 -m time

--timestart 20:01 --timestop 07:59 -j ACCEPT

2: 软件部门

因为软件部门和经理办公室在上班时间都要上网,所以在上班时间DNS的数据包能够forward

[email protected] ~]# iptables -t filter -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 udp --dport 53 -j ACCEPT

上班时间 限制可以访问 http 新浪 中华网站 无限制 (SQUIID)

iptables -t nat -R PREROUTING 1 -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128

打开squid的配置文件,squid 默认是拒绝所有,在squid配置文件做可访问的策略

590

590 acl soft src 192.168.2.21-192.168.2.30/255.255.255.255

591 acl worktime MTWHF 08:00-20:00

592 http_access allow soft worktime

[[email protected] ~]# iptables -t filter -A INPUT -i eth0 -p tcp --dport 3128 -j ACCEPT

[[email protected] ~]# iptables -t filter -A OUTPUT -m state --state ESTABLISHED -j ACCEPT

iptables -t filter -A OUTPUT -o eth1 -p tcp --dport 80 -j ACCEPT

iptables -t filter -A INPUT -m state --state ESTABLISHED -j ACCEPT

iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT NDS解析能够出去

打开squid配置文件设置透明代理

http_port 3128 transparent

*.163.com 无图片

打开squid 的配置文件

acl soft src 192.168.2.21-192.168.2.30/255.255.255.255

acl worktime time MTWHF 08:00-20:00

acl badcontent urlpath_regex -i \.jpg$

acl badsite dstdom_regex -i sina

http_access deny soft worktime badsite

http_access allow soft worktime !badcontent

不允许聊天 qq (L7)

[[email protected] ~]# iptables -t filter -I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --timestart 08:00 --timestop 20:00 --weekdays

Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP

不允许迅雷下载资源

[[email protected] ~]# iptables -t filter -I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --timestart 08:00 --timestop 20:00 --weekdays

Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j DROP

最大连接数3

打开配置文件

acl badconn maxconn 3

http_access allow soft worktime !badcontent !badconn

3: 经理办公室

上班时间 http qq smtp pop3 (L7)

下班后无限制

4:DMZ

acl manager1 src 192.168.2.31-192.168.2.40/255.255.255.255

http_access allow manager1 worktime

上班时间可以QQ

[[email protected] ~]# iptables -t filter -I FORWARD 2 -m iprange --src-range 192.168.2.31-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays

Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j ACCEPT

发布 : www rdp (NAT PREROUTING)

iptables -t nat -A PREROUTING -d 192.168.1.44 -p tcp --dport 3389 -j DNAT --to 192.168.3.100

iptables -t filter -I FORWARD 1 -i eth1 -d 192.168.3.100 -p tcp --dport 3389 -j ACCEPT