netfilter/iptables模块编译及应用
作者: 佚名 来源: Linux 宝库 点击: 日期: 2006-12-05
--> 相信很多人都会用iptables,我也一直用,并且天天用。以下是介绍其部分功能,及编译方法。 环境: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 或http://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 : 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.并发连接应用 iptables -I FORWARD -s 192.168.3.159 -p tcp --syn --dport 80 -m connlimit --connlimit-above 3 --connlimit-mask 24 -j DROP 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 --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 -
【文章评论 】
【收藏本文 】
【推荐好友 】
【打印本文 】
【论坛讨论 】
相关文章:
文章评论: (条)
责任编辑:一分 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。