一,准备原码.
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
(ipaddr注:FTP好象上不去,不知是不是作者写错了,可以到这下载最新补丁:http://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/ )
解压: 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 --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没下载到,所以没测试
在此仅为抛个砖头,更多的应用,要根据自己的需要来组合各个规则和模块了.


本来此篇文章和<<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


apache+mod_ssl

[不指定 2005/08/08 23:03 | by ipaddr ]
参改了一位前人的文章《Apache2 + mod_ssl + php5 完全安装实录》,在此表示感谢

1.安装openssl

一般,RH的系统会自带了RPM包的openssl,可以直接
rpm -ivh openssl*.rpm

另外,需在源代码安装,可参考:
下载最新版本的openssl: http://www.openssl.org/source/
# tar zxvf openssl-0.9.7d.tar.gz
# cd openssl-0.9.7d
# ./config
# make
# make install

2.整合安装mod_ssl

下载apache2: http://httpd.apache.org/download.cgi
我需要mod_ssl的支持,和apache1不同的是,mod_ssl不在是单独的模块,而是放在apache发行包里面了,默认是不启用的,config的时候选择上就可以了。
我使用DSO方式编译安装apache,同时将全部模块都编译好,以方便后来可能的需要。只要编辑httpd.conf,在里面去掉不想要的模块(注释或者删除对应模块的LoadModule行),就可以定制自己的apache咯。
# tar zxvf httpd-2.0.50.tar.gz
# ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl=shared --enable-mods-shared=all --with-ssl=/usr/local/ssl
# make
# make install


Apache有两种使用模块的方法,其一是永久性包含进核心;
如果操作系统支持动态共享对象(DSO),而且能为autoconf所检测,则模块还可以被动态编译。
DSO模块的存储是独立与核心的,可以被核心使用由mod_so模块提供的运行时刻配置指令包含或排除。
如果编译中包含有任何动态模块,则mod_so模块会被自动包含进核心。如果希望核心能够装载DSO,而不实际编译任何动态模块,需要明确指定--enable-so。
(http://kajaa.bbs.us/ApacheManual/install.html)

第一次按照上述方法编译的apache,启动的时候会报错:
# cd /usr/local/apache2
# ./bin/apachectl startssl
Syntax error on line 251 of /usr/local/apache/conf/httpd.conf:
Cannot load /usr/local/apache/modules/mod_ssl.so into server: /usr/local/apache/modules/mod_ssl.so: undefined symbol: X509_free

原因是什么呢?看 http://www.smartframeworks.com/qt-apache-ssl.html
因为按照下面的方法(参看:Apache2 + mod_ssl + php5 完全安装实录(2))安装的openssl默认是没有编译成动态链接库的,因为其文档说openssl的动态链接库还不成熟,可以使用 ./config shared 编译带动态链接库的openssl,但是还处于试验阶段。
解决这个问题的办法是:将mod_ssl静态的编译到apache里面。
请使用下面的方法重新来过:)
# ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl=static --with-ssl=/usr/local/ssl --enable-mods-shared=all
# make
# make install
(ipaddr注:好象要先mkdir /usr/local/ssl,另外,--enable-mods-shared=all可以不要)

这次启动apache的时候又发现一个错误:
# cd /usr/local/apache2
# ./bin/apachectl startssl
Syntax error on line 108 of /usr/local/apache2/conf/ssl.conf:
SSLCertificateFile: file '/usr/local/apache2/conf/ssl.crt/server.crt' does not exist or is empty

这又是什么原因呢?因为我们没有配置ssl,需要生成ssl需要的证书。
以前使用apache1+mod_ssl的时候,make之后有一个这样的步骤
$ make certificate
可以用来生成ssl所用到的证书。
现在没有这个工具了,只能自己动手生成了,对证书不熟悉的人,有一个工具可以使用:http://www.openssl.org/contrib/ssl.ca-0.1.tar.gz
# cd /usr/local/apache2/conf
# tar zxvf ssl.ca-0.1.tar.gz
# cd ssl.ca-0.1
# ./new-root-ca.sh                         (生成根证书)
No Root CA key round. Generating one
Generating RSA private key, 1024 bit long modulus
...........................++++++
....++++++
e is 65537 (0x10001)
Enter pass phrase for ca.key:              (输入一个密码)
Verifying - Enter pass phrase for ca.key:  (再输入一次密码)
......
Self-sign the root CA...                   (签署根证书)
Enter pass phrase for ca.key:              (输入刚刚设置的密码)
........
........                                   (下面开始签署)
Country Name (2 letter code) [MY]:CN
State or Province Name (full name) [Perak]:JiangSu
Locality Name (eg, city) [Sitiawan]:NanJing
Organization Name (eg, company) [My Directory Sdn Bhd]:Wiscom System Co.,Ltd
Organizational Unit Name (eg, section) [Certification Services Division]:ACSTAR
Common Name (eg, MD Root CA) []:WISCOM CA
Email Address []:acmail@wiscom.com.cn

这样就生成了ca.key和ca.crt两个文件,下面还要为我们的服务器生成一个证书:
# ./new-server-cert.sh server              (这个证书的名字是server)
......
......
Country Name (2 letter code) [MY]:CN
State or Province Name (full name) [Perak]:JiangSu
Locality Name (eg, city) [Sitiawan]:NanJing
Organization Name (eg, company) [My Directory Sdn Bhd]:Wiscom System Co.,Ltd
Organizational Unit Name (eg, section) [Secure Web Server]:ACSTAR
Common Name (eg, www.domain.com) []:acmail.wiscom.com.cn
Email Address []:acmail@wiscom.com.cn

这样就生成了server.csr和server.key这两个文件。
还需要签署一下才能使用的:
# ./sign-server-cert.sh server
CA signing: server.csr -> server.crt:
Using configuration from ca.config
Enter pass phrase for ./ca.key:             (输入上面设置的根证书密码)
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'CN'
stateOrProvinceName   :PRINTABLE:'JiangSu'
localityName          :PRINTABLE:'NanJing'
organizationName      :PRINTABLE:'Wiscom System Co.,Ltd'
organizationalUnitName:PRINTABLE:'ACSTAR'
commonName            :PRINTABLE:'acmail.wiscom.com.cn'
emailAddress          :IA5STRING:'acmail@wiscom.com.cn'
Certificate is to be certified until Jul 16 12:55:34 2005 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
CA verifying: server.crt <-> CA cert
server.crt: OK
(如果这里出现错误,最好重新来过,删除ssl.ca-0.1这个目录,从解压缩处重新开始。)

下面要按照ssl.conf里面的设置,将证书放在适当的位置。
# chmod 400 server.key
# cd ..
# mkdir ssl.key
# mv ssl.ca-0.1/server.key ssl.key
# mkdir ssl.crt
# mv ssl.ca-0.1/server.crt ssl.crt

然后就可以启动啦!
# cd /usr/local/apache2
# ./bin/apachectl startssl

对于这个提示:
httpd: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
只需要编辑httpd.conf,找到ServerName xxxx这一行,去掉前面的注释即可。

//很大部分参考《Apache2 + mod_ssl + php5 完全安装实录》,只做了一些小说明,原文作者写得太详细了。

在此基础上可直接安装PHP,不再重复PHP的安装了。

分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]