Linux iptables lay7配置(下)

来源:互联网 发布:linux查看某个进程 编辑:程序博客网 时间:2024/04/29 07:17

Iptables L7应用层过滤

好的,上次我们简单的讨论了下iptales的用法,其中有很多的用法我们都还没说,例如非常重要的SNAT ,DNAT转换,字符匹配过滤,同是定义多端口,最大连接数限制,访问时间限制,匹配速率限制,还有就是现在企业中经常要用到的l7layer应用层过滤!!下面我们来讨论这些非常重要有用但是又很具挑战难度的东东

主要内容

iptables的实践应用,L7应用层过滤

目标:

熟练应用ipatbles的用法,要做到烂熟于心

我们接着上次继续探讨iptables更深层的用法。这次我们基于上次的理论介绍主要通过实验来熟悉每个命令的用法.

实验环境:

内网2:10.0.2.100 telnet vsftp http

内网1:10.0.1.100

外网:10.0.3.100

firewall:eth0:10.0.2.1 eth1:10.0.3.1 ssh

clip_image004

SNAT DNAT:

ü SNAT

ü --to-source IPport

ü DNAT

ü --to-destination IP:port

路由测试

[root@station93 ~]# sysctl -p

net.ipv4.ip_forward = 1

NAT转换测试:

外网访问iptables10.0.3.1端口时,iptables均把地址DNAT成内网10.0.2.100服务器的httpd服务,注意,这里外网不知道内网有10.0.2.100这个地址,只知道10.0.3.1

DNAT 转换;==========》一个IP可实现多个功能

[root@station93 ~]# iptables -t nat -A PREROUTING -s 10.0.3.100 -d 10.0.3.1 -p tcp --dport 80 -j DNAT --to-dest 10.0.2.100

[root@station93 ~]# iptables -t nat -A PREROUTING -s 10.0.3.100 -d 10.0.3.1 -p tcp --dport 22 -j DNAT --to-dest 10.0.1.100

SNAT转化测试:=========è内网用户只需要一个IP地址即可全部上网

内网用户通过iptables外网网关实现SNAT转化的功能

[root@station93 ~]# iptables -t nat -A POSTROUTING -s 10.0.2.100 -p tcp --dport 80 -j SNAT --to-source 10.0.3.1

[root@3 httpd]# tail access_log============》这里查看日志时我们会法相其实是10.0.3.1来访问外网服务器的,即iptables的外网网关

10.0.3.1 - - [09/Mar/2010:20:43:59 +0800] "GET / HTTP/1.1" 403 3985 "-" "ELinks/0.11.1 (textmode; Linux; 77x24-2)"

基于状态的限制

v -m state

ü NEW

ü ESTABLISHED

ü RELATED

ü INVALID

只允许3.100访问2.100的web服务,不允许2.100自发的回应3.100的

主要用来防止本机中毒,自发的向外发出信息泄密

[root@station93 ~]# iptables -A FORWARD -s 10.0.3.100 -d 10.0.2.100 -p tcp --dport80 -m state --state NEW,ESTABLISHED -j ACCEPT

[root@station93 ~]# iptables -R FORWARD 2 -s 10.0.2.100 -d 10.0.3.100 -p tcp --sport 80 -m state --stateESTABLISHED,RELATED -j ACCEPT

测试:

[root@3 ~]# elinks 10.0.2.100 ======》出现测试页

[root@2 ~]# elinks 10.0.3.100 ========》失败

v -m multiport

ü --source-ports port1,port2,port3,…

ü --destination-ports port1,port2,port3,…

ü --ports port1,port2,port3,…

企业中防火墙的实际应用

在实际的企业应用中,我们以上学习的iptables只是使远远不够的,举个简单的例子,我们在平时的工作中往往需要限制qq,msn,迅雷,快车等聊天下载工具的使用,而我们以上学的都是写非常简单点的限制,而这这些工具的服务器的众多,端口多变,我们通过普通的方法是很难控制住的,银次我们需要给予Layer的第七层应用的控制,这里我们需要重新编译升级内核。

需要的软件包:

v The 2.4 or 2.6 Linux kernel source (2.6 strongly preferred) from kernel.org

v The iptables source from netfilter.org

v "l7-filter kernel version" package (netfilter-layer7-vX.Y.tar.gz)

v "Protocol definitions" package (l7-protocols-YYYY-MM-DD.tar.gz)

步骤如下:

ü # tar zxvf linux-2.6.28.10.tar.gz -C /usr/src

ü # tar zxvf netfilter-layer7-v2.22.tar.gz -C /usr/src

ü # ln –s /usr/src/linux-2.6.28.10/ /usr/src/linux

ü # cd /usr/src/linux/

ü # patch -p1 < ../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch

# cp /boot/config-2.6.18-164.el5 /usr/src/linux/.config

# make menuconfig 这里我们需要实现安装好对应的开发包 pirut & 命令编辑

clip_image006

我们需要选择好下面的这些模块:

v Networking support → Networking Options →Network packet filtering framework →Code Netfilter Configuration

ü &lt;M> Netfilter connection tracking support

ü <M> “layer7” match support

ü <M> “string” match support

ü <M> FTP protocol support

ü <M> “time” match support

ü <M> “iprange” match support

ü <M> “connlimit” match support

ü <M> “state” match support

ü <M> “conntrack” connection match support

ü <M> “mac” address match support

ü <M> "multiport" Multiple port match support

v Networking support → Networking Options →Network packet filtering framework → IP: Netfilter Configuration

ü <M> IPv4 connection tracking support (required for NAT)

ü <M> Full NAT

? <M> MASQUERADE target support <M> NETMAP target support <M> REDIRECT target support

v Start compiling and installing

ü make

ü make modules_install

ü make install

v Edit the /boot/grub/grub.conf, set the default booting kernel to the new kernel

v Restart

v # cp /etc/rc.d/init.d/iptables ~/iptables

v # rpm -e iptables-ipv6 iptables iptstate --nodeps

v # tar jxvf iptables-1.4.6.tar.bz2 –C /usr/src

v # cd /usr/src/iptables-1.4.6

v # cp ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.* ./extensions/

v ./configure --prefix=/usr --with-ksource=/usr/src/linux

v make

v make install

v # tar zxvf l7-protocols-2009-05-28.tar.gz

v # cd l7-protocols-2009-05-28

v # make install

v # mv ~/iptables /etc/rc.d/init.d/

v #ln –sv /usr/sbin/iptables /sbin/iptables

v 注意这里第一次启动无法service iptables start 需要先setup,再service iptables start

2.6.28.10 内核中 ip_conntrack_ftp 改名为 nf_conntrack_ftp

加模块: modprobe nf_conntrack_ftp

支持的协议种类:

v bittorrent

ü P2P filesharing / publishing tool

v edonkey

ü eDonkey2000 - P2P filesharing

v kugoo

ü KuGoo - a Chinese P2P program

v msn-filetransfer

ü MSN (Micosoft Network) Messenger file transfers

v msnmessenger

ü Microsoft Network chat client

v pplive

ü Chinese P2P streaming video

v qq

v xunlei

用法

ü iptables [specify table & chain] -m layer7 --l7proto [protocol name] -j [action]

禁止外网访问firewall的web ,ssh,telnet服务

[root@station93 ~]# iptables -A INPUT -p tcp -m multiport --source-ports 22,25,80 -j DROP

v connlimit

v [!] --connlimit-above n 最大连接数

ü Allows you to restrict the number of parallel connections to a server per client IP address (or client address block).

ü Example:

? iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT

? Allow 2 ssh connections per client host

限制外网的最大连接数是2

[root@station93 ~]# iptables -A OUTPUT -p tcp --syn --dport 22-m connlimit --connlimit-above 2 -j REJECT

v String

ü 有两种算法:bm kmp

ü This modules matches a given string by using some pattern matching strategy.

ü It requires a linux kernel &gt;= 2.6.14.

ü [!] --string pattern

? Matches the given pattern.

ü --algo {bm|kmp}

? Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)

ü Examples:

? iptables –A OUTPUT –p udp --dport 53 -m string --string “verycd” --algo bm -j DROP

? iptables -A OUTPUT -p tcp --dport 80 -m string --string “***” --algo kmp -j DROP

限制内网浏览外网上包含secert 字符串的网页:

[root@3 html]# echo "secert" &gt;&gt; index2.html

[root@3 html]# cat index2.html

secret

this is the second test

secret

[root@station93 ~]# iptables -A FORWARD -s 10.0.2.100 -d 10.0.3.100 -p tcp --sport 80 -m string --string "secert" --algo bm -j DROP

v time

ü This matches if the packet arrival time/date is within a given range.

ü --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]

ü --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]

? Only match during the given time, The possible time range is 1970-01-01 T00:00:00 to 2038-01-19 T04:17:07

ü --timestart hh:mm[:ss]

ü --timestop hh:mm[:ss]

? The possible time range is 00:00:00 to 23:59:59.

? Leading zeroes are allowed.

v time

ü [!] --monthdays day[,day...]

? Only match on the given days of the month. Possible values are 1 to 31.

ü [!] --weekdays day[,day...]

? Only match on the given weekdays. Possible values are Mon, Tue, Wed, Thu, Fri, Sat, Sun, or values from 1 to 7, respectively.

ü Examples:

? -m time --weekdays Sa,Su

? -m time --timestart 12:30 --timestop 13:30

[root@station93 ~]# iptables -A FORWARD -m time --timestart 00:00 --timestop 8:00 -j DROP

v -j LOG

ü --log-level level

? Level of logging

ü --log-prefix prefix

? Prefix log messages with the specified prefix; up to 29 letters long, and useful for distinguishing messages in the logs.

ü --log-tcp-options

? Log options from the TCP packet header.

ü --log-ip-options

? Log options from the IP packet header.

[root@station93 ~]# iptables -A FORWARD -s 10.0.3.100 –d 10.0.2.100 –p tcp –dport -j LOG --log-prefix "abc"

基于l7layer的规则则十分类似,在其内部已经为我们定义好,我们只需要调用其就行了。

例如:禁止内网用户在上班时间使用qq

[root@station93 ~]# iptables -A FORWARD -s 10.0.2.100 -d 10.0.3.100 -p tcp --dport 80 -m time --timestart 8:00 --timestop 18:00 -m layer7 --l7proto qq -j DROP