防火墙规则及layer7的应用

案例

公司有三个部门

工程部门  192.168.2.10--2.20

软件部门  192.168.2.21-2.30

经理办    192.168.2.31-2.40

上班时间 (周一---周五 08:00-20:00)

          工程部门 允许访问ftp,下班后无限制;

          软件部门  允许访问http,不允许非法站点sina ,连接数 最多3个,不允许QQ聊天,下班后无限制。

      经理办公室 允许访问http,qq ,下班后无限制。

dmz区域服务器进行发布。

 


1.开启转发功能

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

net.ipv4.ip_forward = 1             

[root@server ~]# sysctl -p

[root@server ~]# vim /etc/resolv.conf

nameserver 202.85.85.85

nameserver 202.88.88.88

2.允许ssh连接通过

--所有用户都可以出去

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

[root@server ~]# iptables -t filter -A INPUT –s 10.106.42.169 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

 

[root@server ~]# iptables -t filter -A OUTPUT -d 10.106.42.169 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

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

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

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

允许127.0.0.1与用户进程通信

[root@server ~]# ping 127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

ping: sendmsg: Operation not permitted

[root@server ~]# iptables -t filter -A INPUT -i lo -j ACCEPT

[root@server ~]# iptables -t filter -A OUTPUT -o lo -j ACCEPT

--apache 和用户的其他进程启动不了,需要把其打开

[root@server ~]# ping 127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.035 ms 

[root@server ~]# iptables -L -n -v --line-number

3.工程部

加载支持ftp的模块

[root@server ~]# modprobe ip_nat_ftp

[root@server ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.10-192.168.2.20 -m time --timestart

08:00 --timestop 20:00 -p tcp --dport 21 -j ACCEPT

 

[root@server ~]# date

Fri Aug 26 02:59:08 CST 2011

[root@server ~]# date 100816582011

Sat Oct  8 16:58:00 CST 2011

 

下班时间适用所以部门:

[root@server ~]# iptables -t filter -I FORWARD 2 -m time --timestart 20:01 --timestop 23:59 -o eth0 -j ACCEPT

[root@server ~]# iptables -t filter -I FORWARD 2 -m time --timestart 00:00 --timestop 07:59 -o eth0 -j ACCEPT

 

 

4.软件部门

 

访问外网时要允许dns解析

[root@server ~]# 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 -p udp --dport 53 -j ACCEPT

 

 

--禁止上班时间访问sina

[root@server ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --timestart 08:00 --timestop 20:00 -m string --string "sina" --algo bm -j ACCEPT

 

--可以访问http

[root@server ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --timestart

08:00 --timestop 20:00 -p tcp --dport 80 -j ACCEPT

5.L7

--不允许聊天

[root@server ~]# iptables -t filter -I FORWARD 1 -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --timestart 08:00 --timestop 20:00 -m layer7 --l7proto qq -j DROP

[root@server ~]#

--最大连接数为三

[root@server ~]# iptables -t filter -I FORWARD 1 -m iprange --src-range 192.168.2.21-192.168.2.30 -m time --timestart 08:00 --timestop 20:00 -p tcp --dport 80 -m connlimit --connlimit-above 3 -j DROP

[root@server ~]#

--windows查看连接数:

#netstat –na |more

 

C:\Documents and Settings\Administrator>netstat -na |more

 

Active Connections

 

  Proto  Local Address          Foreign Address        State

  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING

  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING

  TCP    0.0.0.0:6125           0.0.0.0:0              LISTENING

  TCP    0.0.0.0:33673          0.0.0.0:0              LISTENING

  TCP    10.106.42.169:139      0.0.0.0:0              LISTENING

  TCP    10.106.42.169:1028     123.125.114.81:80      ESTABLISHED

  TCP    10.106.42.169:1046     61.200.81.136:80       ESTABLISHED

  TCP    10.106.42.169:3913     119.75.214.13:443      ESTABLISHED

  TCP    10.106.42.169:4189     10.106.42.171:22       ESTABLISHED

  TCP    127.0.0.1:1025         0.0.0.0:0              LISTENING

  TCP    127.0.0.1:1173         127.0.0.1:1174         ESTABLISHED

  TCP    127.0.0.1:1174         127.0.0.1:1173         ESTABLISHED

  TCP    127.0.0.1:19000        0.0.0.0:0              LISTENING

  UDP    0.0.0.0:445            *:*

 

 

6.经理办

[root@server ~]# iptables -t nat -A PREROUTING -d 10.106.42.171 -p tcp --dport 3389 -j DNAT --to 192.168.3.100

[root@server ~]# iptables -t filter -A FORWARD -d 192.168.3.100 -p tcp --dport 3389 -j ACCEPT

[root@server ~]#