添加iptables/netfilter功能扩展模块的测试

陈伟堂 2010-04-01

由于iptables/netfilter中的扩展模块string、mport、comment、connlimit、psd、time还处于测试阶段,直到现在各种Linux发行版本中的内核都还没把这些模块包括入来,这些模块的功能十分实用,本人一直想把这些功能加入到单位的防火墙中去,但苦于手头上编译安装iptables/netfilter扩展模块的资料太少,以及对编译软件非常耗费时间的恐惧,因此一直没有动手,近日,在仔细研读了白金兄的《iptables添加模块HOWTO》一文后,决定抽空进行测试,待取得经验教训后,再把这些功能添加到单位的防火墙中去。

一、做好准备工作

安装CentOS 3.6 Linux操作系统(内核版本为2.4.21-37.EL),安装到选择安装系统软件时选择安装“Kernel development tools”,操作系统安装完成后就已自动把编译环境及内核源代码安装好,该版本的内核虽然不算很新,但能够满足测试的需要。由于我不想在编译内核上花费太多的时间,以及为了这次测试的顺利,因此我选择内核的模块编译安装方式。安装好两块网卡(eth0:192.168.11.5/255.255.255.0,GW:192.168.11.2,DNS:192.168.11.4,eth1:192.168.12.1/255.255.255.0),登录192.168.11.2网关服务器,增加一条指到测试机器的路由:route add -net 192.168.12.0/24 gw 192.168.11.5 eth2,否则,客户机将只有出口路由而没有返回路由,因而无法进行下面的相关测试。

进入/usr/src目录,下载iptables-1.3.1.tar.bz2:

下载patch-o-matic-ng-20050801.tar.bz2:

说明:iptables-1.3.4.tar.bz2及iptables-1.3.5.tar.bz2中的某些模块(如string)需要2.6.14及以上的内核。patch-o-matic-ng补丁包要下载最前日期的,否则有些功能无法启用。

二、安装

1、展开压缩包

# cd /usr/src

#tarxvfjiptables-1.3.1.tar.bz2

# tar xvfj patch-o-matic-ng-20050801.tar.bz2

2、给netfilter打补钉

# cd /usr/src/linux-2.4

#makemrproper

#makemenuconfig

#viMakefile

将EXTRAVERSION=-37.ELcustom改为EXTRAVERSION=-37.EL

#cd/usr/scr/patch-o-matic-ng-20050801

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmestring

出现提示后输入y,确认,下面一样按此操作。

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmecomment

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmeconnlimit

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmemport

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmepsd

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmeipp2p

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmeiprange

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmegeoip

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmetime

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmequota

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmenth

#KERNEL_DIR=/usr/src/linux-2.4IPTABLES_DIR=/usr/src/iptables-1.3.1./runmeipv4options

#cd/usr/src/linux-2.4

#makemenuconfig

进入Codematurityleveloptions,确认[*]Promptfordevelopmentand/orincompletecode/drivers要选中,然后进入Networkingoptiosn,再进入IP:Netfilterconfiguration,会看到增加很多模块,每个新增的后面都会出现“NEW”,把其中想要的选中为模块“M”,保存、退出,至此,给netfilter打补丁工作完成。

3、编译netfilter模块

a.这里只需要编译netfilter,不需要编译整个内核和模块,现在只用到ipv4的,所以ipv6我没有编译。

# cd /usr/src/linux-2.4

#makedep

#makemodulesSUBDIRS=net/ipv4/netfilter

b.建立一个新目录备份原来模块,以防编译出来的模块有问题:

# mkdir /usr/src/netfilter

#cp/lib/modules/`uname-r`/kernel/net/ipv4/netfilter/*.o/usr/src/netfilter/

c.应用新的模块

# cd /usr/src/linux-2.4

#cp-fnet/ipv4/netfilter/*.o/lib/modules/`uname-r`/kernel/net/ipv4/netfilter/

d.更新你的modules.dep,当出现错误时,把出错的模块删除后再做一次。

# depmod -a

    说明:开始时,只删除了ipchains_core.o、ipfwadm_core_o、ip_fw_*.o,后来运行防火墙脚本时,modprobe  ipt_nat_ftp及ipt_MASQUERADE模块时还是出现问题,于是继续删除ip_nat_core.o、ip_nat_core.o、ip_nat_standalone.o、ip_nat_helper.o模块后,问题解决。4、编译安装iptables-1.3.1

# cd /usr/src/iptables-1.3.1

#exportKERNEL_DIR=/usr/src/linux-2.4

#exportIPTABLES_DIR=/usr/src/iptables-1.3.1

# make BINDIR=/sbin LIBDIR=/lib MANDIR=/usr/share/man install  

三.应用测试

# cd /etc/rc.d# vi fwtest

# !/bin/shIPTABLES="/sbin/iptables"

/sbin/depmod -a

/sbin/modprobeip_tables

/sbin/modprobeip_conntrack

/sbin/modprobeiptable_filter

/sbin/modprobeiptable_nat

/sbin/modprobeipt_string

/sbin/modprobeipt_ipp2p

/sbin/modprobeipt_comment

/sbin/modprobe ipt_iprange

$IPTABLES -F

$IPTABLES-X

$IPTABLES-tnat-F

$IPTABLES-tnat-X

$IPTABLES-PINPUTDROP

$IPTABLES-POUTPUTDROP

$IPTABLES -P FORWARD ACCEPT

#

#从eth0出发的包要将源地址转换为192.168.11.5

#

$IPTABLES -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 192.168.11.5

#1.内容过滤

iptables-AFORWARD-mstring--string"sina"-jDROP

#2.备注应用

iptables-AFORWARD-s192.168.3.159-ptcp--dport80-jDROP-mcomment--comment"thebadguycannotonline"

iptables-AFORWARD-s192.168.3.159-mstring--string"qq.com"-jDROP-mcomment--comment"dennygotoqq.com"

#3.并发连接应用

iptables-AFORWARD-s192.168.3.159-ptcp--syn--dport80-mconnlimit--connlimit-above3--connlimit-mask24-jDROP

#4.ip范围应用

iptables-AFORWARD-miprange--src-range192.168.1.5-192.168.1.124-jACCEPT

#5.封杀BT类P2P软件

iptables-AFORWARD-mipp2p--edk--kazaa--bit-jDROP

iptables-AFORWARD-ptcp-mipp2p--ares-jDROP

iptables-AFORWARD-pudp-mipp2p--kazaa-jDROP

#

#打开转发功能

#

echo "1" > /proc/sys/net/ipv4/ip_forward

# chmod u+x fwtest# ./fwtest

说明:所增加的扩展功能中只测试了string功能,其它的有待以后再抽时间测试。
四、测试结果  如果FORWARD链的默认策略为DROP,则 iptables -A FORWARD -m string --string "sina" -j ACCEPT 不起作用,如果FORWARD链的默认策略为ACCEPT,则 iptables -A FORWARD -m string --string "sina" -j DROP 语句有效。这样,就无法在默认DROP策略下开放访问某些网站。

参考资料:

http://www.netfilter.org/documentation/HOWTO//netfilter-extensions-HOWTO.html

《iptables添加模块HOWTO》--platinum

《iptables/netfilter模块编译及应用》--KindGeorge
*注:这是我06年发布在西湖博客上的一篇旧文,现在迁移过来。我的西湖博客地址:http://linuxtech.xhschool.com/

相关推荐