| 其功能很是令人感觉很兴奋,例如:comment (备注匹配) ,string(字符串匹配,可以用做内容过滤),iprang(ip范围匹配),time(时间匹配),ipp2p(点对点匹配),connlimit(同时连接个数匹配),Nth(第n个包匹配),geoip(根据国家地区匹配). ipp2p(点对点匹配), quota(配额匹配),还有很多...... netfilter/iptables模块编译及应用 powered by KindGeorge # http://kindgeorge.at.3322.org 2005.4.2 ChinaUnix.net 相信很多人都会用iptables,我也一直用,并且天天用.特别是看完platinum的<<如何给iptables添加新的模块>>介绍后,觉得有必要深入了解一下它的拓展功能.于是立刻下载,先查看一下它的说明, 其功能很是令人感觉很兴奋,例如:comment (备注匹配) ,string(字符串匹配,可以用做内容过滤),iprang(ip范围匹配),time(时间匹配),ipp2p(点对点匹配),connlimit(同时连接个数匹配),Nth(第n个包匹配),geoip(根据国家地区匹配). ipp2p(点对点匹配), quota(配额匹配),还有很多......之后编译,几经测试,在rh7.3 kernel2.4.18-3和rh9.0 kernel2.4.20-8下均成功实现添加扩展功能.以下是介绍其部分功能,及编译方法.环境rh9.0 kernel2.4.20-8. root身份. 一,准备原码. 1. 内核原码:为了减少复杂性,不编译所有内核和模块,建议找一个跟当前版本一样的内核原码,推荐安装时光盘的 a. [root@kindgeorge] uname -r (查看当前版本) 2.4.20-8 可以cd /usr/src 查看是否有这个目录2.4.20-8 b. 或者[root@kindgeorge]rpm -qa|grep kernel kernel-source-2.4.20-8 如果有这个说明已安装了. 如果没有安装,可以在RH第二张光盘中拷贝过来或安装 rpm -ivh kernel-source-2.4.18-3.i386.rpm. 安装后会在/usr/src/出现linux-2.4连接和linux-2.4.20-8目录. c.在http://www.kernel.org 或www.redhat.com下载一个和当前版本的内核原码. 2. 先获取最新的信息,当然要到http://www.netfilter.org 或 http://www.iptables.org (这两个网址是一样的). 网站左方download栏已经说明了最新版本的iptables-1.3.1 [root@kindgeorge src] cd /usr/src/ a. 获取最新iptables : wget http://www.netfilter.org/files/iptables-1.3.1.tar.bz2 解压:tar xjvf iptables-1.3.1.tar.bz2 b. 获取最新patch-o-matic-ng的地址:ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/ [root@kindgeorge src] wget ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/patch-o-matic-ng-20050331.tar.bz2 解压: tar xjvf patch-o-matic-ng-20050331.tar.bz2
二.安装说明 (一).处理内核源码. 1. [root@kindgeorge src]# cd /usr/src/linux-2.4 2. [root@kindgeorge linux-2.4]# vi Makefile, VERSION = 2 PATCHLEVEL = 4 SUBLEVEL = 20 EXTRAVERSION = -8custom 将“EXTRAVERSION = -8custom”改为“EXTRAVERSION = -8” 即: VERSION = 2 PATCHLEVEL = 4 SUBLEVEL = 20 EXTRAVERSION = -8 为何要修改呢?因为不同版本的内容是不可以使用的,当不修改时,则变成这个版本(2.4.20-8custom)了,不同时是这样出错的: /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: kernel-module version mismatch (版本错误) /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o was compiled for kernel version 2.4.20-8custom while this kernel is version 2.4.20-8. /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: insmod /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o failed /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: insmod ipt_iprange failed
3. [root@kindgeorge linux-2.4]# make mrproper 4. [root@kindgeorge linux-2.4]# make oldconfig 'make oldconfig' - 采用以前的 .config 文件 (编译时十分有用) 技巧:在make menuconfig时,我们面对众多的选项常常不知道该如何选择,此时可以把安装时的配置文件copy到/usr/src/linux-2.4中:cp /boot/config-2.4.* /usr/src/linux-2.4/.config,再用make menuconfig编译,它会读取.config中原来的配置信息. (二).给netfilter打补丁 解开tar xjvf patch-o-matic-ng-20050331.tar.bz2 包后,进入该目录,就会发现有很多目录,其实每个目录对应一个模块. 我们可以这样来选择,根据不同贮仓库submitted|pending|base|extra,例如: KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme base . 或:KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme extra 执行后,会测试是否已经应用和提示你是否应用该模块,但这样会遍历所有模块,有很多是用不着的,并且可能和系统版本有冲突,如果不管三七二十一全部选择的话,一般都会在编译和使用时出错.所以推荐用cat /模块目录名/info 和cat /模块目录名/help 看过后,认为适合自己,才选择. 我是针对在上面看过后,有目的的一个一个的应用的,这样做: KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme string 执行后,会测试是否已经应用和提示你是否应用该模块,按"y"应用.然后继续下一个 KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme comment KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme connlimit KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme time KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme iprange KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme geoip KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme nth KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme ipp2p KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme quota 上面全部完成后, cd /usr/src/linux-2.4 make menuconfig,确认[*] Prompt for development and/or incomplete code/drivers要选中 然后进入Networking options 再进入IP:Netfilter Configuration,会看到增加很多模块,每个新增的后面都会出现"NEW",把其想要的选中为模块"M" 保存、退出,至此,给netfilter打补丁工作完成 (三).编译netfilter模块 1.这里只需要编译netfilter,不需要编译整个内核和模块.这里我只需要ipv4的,ipv6我还没用到,所以不管了 cd /usr/src/linux-2.4 make dep make modules SUBDIRS=net/ipv4/netfilter 2.建立一个新目录备份原来模块,以防万一: mkdir /usr/src/netfilter cp /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/*.o /usr/src/netfilter/ 3.应用新的模块 cp -f /usr/src/linux-2.4/net/ipv4/netfilter/*.o /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ 4.更新你的modules.dep depmod -a 当出现这个时,可以不用理会,因为ipchains, ipfwadm模块都没用,也可以把出错的删除. depmod: *** Unresolved symbols in /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipchains_core.o depmod: *** Unresolved symbols in /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipfwadm_core.o (四).编译安装新的iptables 解压后有目录iptables-1.3.1 cd /usr/src/iptables-1.3.1 export KERNEL_DIR=/usr/src/linux-2.4 export IPTABLES_DIR=/usr/src/iptables-1.3.1 make BINDIR=/sbin LIBDIR=/lib MANDIR=/usr/share/man install
三.安装完成,测试及应用 1.内容过滤 iptables -I FORWARD -m string --string "腾讯" -j DROP iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP iptables -I FORWARD -d 192.168.3.0/24 -m string --string "宽频影院" -j DROP iptables -I FORWARD -s 192.168.3.0/24 -m string --string "色情" -j DROP iptables -I FORWARD -p tcp --sport 80 -m string --string "广告" -j DROP 2.备注应用 iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -j DROP -m comment --comment "the bad guy can not online" iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP -m comment --comment "denny go to qq.com" 3.并发连接应用 模块 connlimit 作用:连接限制 --connlimit-above n 限制为多少个 --connlimit-mask n 这组主机的掩码,默认是connlimit-mask 32 ,即每ip. 这个主要可以限制内网用户的网络使用,对服务器而言则可以限制每个ip发起的连接数...比较实用 例如:只允许每个ip同时5个80端口转发,超过的丢弃: iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP 例如:只允许每组ip同时10个80端口转发: iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP 例如:为了防止DOS太多连接进来,那么可以允许最多15个初始连接,超过的丢弃. /sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP /sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
4.ip范围应用 iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT 5.每隔N个匹配 iptables -t mangle -A PREROUTING -m nth --every 10 -j DROP 6.封杀BT类P2P软件只要一句: iptables -A FORWARD -m ipp2p --ipp2p -j DROP 或: iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP 7.配额匹配 iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j DROP iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j ACCEPT 以上均测试通过,只有geoip的geoipdb.bin没下载到,所以没测试 在此仅为抛个砖头,更多的应用,要根据自己的需要来组合各个规则和模块了. 8.相关 本来此篇文章和<<netfilter/iptables模块功能中文介绍>>是写在一起的,由于篇幅太长,所以份成两篇. 如果有更新请见blog: http://kindgeorge.at.3322.org 相关资料: kindgeorge的 http://blog.chinaunix.net/article.php?articleId=19778&blogId=4543 http://bbs.chinaunix.net/forum/viewtopic.php?t=525525 netfilter/iptables模块功能中文介绍 platinum的 http://bbs.chinaunix.net/forum/viewtopic.php?t=505370 如何给iptables添加新的模块v2.2(含视频教程) hongfengyue的 http://www.chinaunix.net/jh/4/508312.html iptables 添加模块 (for kernel 2.6) 官方: http://www.netfilter.org/documentation/HOWTO/netfilter-extensions-HOWTO.html
|