在SPARC/Solrias10上安装ModSecurity插件
在有分区的系统上安装ModSecurity,与没有分区的安装过程有所不同,因为有分区的系统上,根目录下的系统目录一般是不允许写的,例如:
/usr/
/bin/
/sbin
等等,所以在使用pkgadd命令在安装时,要记得用“-R”命令指定不同于默认安装目录的路径,并且,由于依赖库使用的不同默认安装目录,所以在编译ModSecurity时,也要修改相应的源代码,以指定正确的依赖组件的位置。
在按照本文所讲步骤安装时,有个大前提是您的系统上必须先安装C编译器,我安装的是gcc V2.4.6
god@heaven # gcc --version
gcc (GCC) 3.4.6
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
一、下载最新的ModSecurity源码包,写此文时最新版本是2.5.12
官方网站: http://www.modsecurity.org/
下载点: http://nchc.dl.sourceforge.net/project/mod-security/modsecurity-apache/2.5.12/modsecurity-apache_2.5.12.tar.gz
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
二、检查依赖组件:
打开modsecurity-apache_2.5.12.tar.gz,在目录modsecurity-apache_2.5.12\doc下找到的手册文件首页index.html,然后根据说明打开单页HTML或多页HTML或PDF格式的手册。我选择是单页HTML格式。
在手册中明确指出ModSecurity依赖的组件如下:
1、Apache2.x版本
2、Apache中,mod_unique_id组件必须已经安装
3、libapr 和 libapr-util (http://apr.apache.org/)
4、libpcre (http://www.pcre.org/)
5、libxml2 (http://xmlsoft.org/downloads.html)
6、liblua v5.1.x (http://www.lua.org/download.html)该组件可选,只在需要使用Lua引擎时才需要
7、libcurl v7.15.1或更高版本 (http://curl.haxx.se/libcurl/)该组件在需要使用mlogc组件收集ModSecurity的审计日志时需要
对于Solrias10/SPARC环境,相应的组件下载地址是:
1、Apache2.2.14 ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/apache-2.2.14-sol10-sparc-local.gz
2、libapr ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/apr-1.2.2-sol10-sparc-local.gz
3、libapr-util ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/aprutil-1.2.2-sol10-sparc-local.gz
4、libpcre ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/pcre-7.9-sol10-sparc-local.gz
5、libxml2 ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/libxml2-2.6.31-sol10-sparc-local.gz (Solrias10中已经自带)
6、liblua v5.1.4 ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/lua-5.1.4-sol10-sparc-local.gz
7、libcurl v7.20.0 ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/curl-7.20.0-sol10-sparc-local.gz
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
三、安装组件
1、安装Apache
一般情况下,Apache是已经安装好的,所以不再详述安装过程,我的环境下Apache2.2.10安装目录是:
/app1/usr/local/apache2.2.10/
要注意的是在安装时要指定安装mod_unique_id组件
2、安装 libapr 和 libapr-util
这两个组件是我以前预先安装好的,所以只写一下安装命令
god@heaven # gunzip apr-1.2.2-sol10-sparc-local.gz
god@heaven # pkgadd -R /app1 -d apr-1.2.2-sol10-sparc-local
注意,在pkgadd 命令后的“-R”参数,是指定安装路径的根目录是/app1,这样指定后,该组件实际的安装路径是
/app1/usr/local/apr
god@heaven # gunzip aprutil-1.2.2-sol10-sparc-local.gz
god@heaven # pkgadd -R /app1 -d aprutil-1.2.2-sol10-sparc-local
3、安装libpcre
god@heaven # gunzip pcre-7.9-sol10-sparc-local.gz
god@heaven # pkgadd -R /app1/ -d pcre-7.9-sol10-sparc-local
4、安装libxml2
该组件操作系统已经自带(也可能是系统管理员在以前某个时间安装的),不再叙述安装过程
5、安装liblua v5.1.4
god@heaven # gunzip lua-5.1.4-sol10-sparc-local.gz
god@heaven # pkgadd -R /app1/ -d lua-5.1.4-sol10-sparc-local
6、安装libcurl v7.20.0
god@heaven # gunzip curl-7.20.0-sol10-sparc-local.gz
god@heaven # pkgadd -R /app1 -d ./curl-7.20.0-sol10-sparc-local
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
四、解压ModSecurity源码包
god@heaven # pwd
god@heaven # /app1
god@heaven # gunzip modsecurity-apache_2.5.12.tar.gz
god@heaven # tar -xvf modsecurity-apache_2.5.12.tar
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
五、配置编译环境
进入到ModSecurity源码目录
god@heaven # cd /app1/modsecurity-apache_2.5.12/apache2
执行配置命令
1、第一次执行
god@heaven # ./configure
……
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/app1/sparc/modsecurity-apache_2.5.12/apache2':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
说明gcc编译器没找到,用以下命令找到gcc的安装位置
root@xmds7-2 # find / -name gcc
/usr/sfw/lib/gcc
/usr/sfw/bin/gcc
/usr/sfw/libexec/gcc
/usr/local/bin/gcc gcc可执行程序在这里
/usr/local/doc/gcc
/usr/local/lib/gcc
/usr/local/libexec/gcc
在PATH环境变量中加入gcc所在的路径即可
god@heaven # export PATH=$PATH:/usr/local/bin/
2、再次执行 ./configure
root@xmds7-2 # ./configure
……
checking for strtol... yes
checking for fchmod... yes
configure: looking for Apache module support via DSO through APXS
configure: error: couldn't find APXS
配置程序找不到APXS的位置,我们进入到Apache2.2.10安装目录并在 bin 目录下找到apxs,apxs的全路径是:
/app1/usr/local/apache2.2.10/bin/apxs
3、第三次执行 ./configure
带上参数,告诉配置程序apxs所在的位置
god@heaven # ./configure --with-apxs=/app1/usr/local/apache2.2.10/bin/apxs
……
configure: looking for Apache module support via DSO through APXS
configure: found apxs at /app1/usr/local/apache2.2.10/bin/apxs
configure: checking httpd version
configure: httpd is recent enough
checking for libpcre config script... no
configure: *** pcre library not found.
configure: error: pcre library is required
没找到pcre库的位置,我们知道pcre安装的位置,直接指定即可
4、第四次执行 ./configure
带参数,指定pcre库的位置
god@heaven # ./configure --with-apxs=/app1/usr/local/apache2.2.10/bin/apxs --with-pcre=/app1/usr/local/bin/
……
configure: looking for Apache module support via DSO through APXS
configure: found apxs at /app1/usr/local/apache2.2.10/bin/apxs
configure: checking httpd version
configure: httpd is recent enough
checking for libpcre config script... /app1/usr/local/bin//pcre-config
configure: using '-L/usr/local/lib -R/usr/local/lib -lpcre' for pcre Library
checking for libapr config script... no
configure: *** apr library not found.
configure: error: apr library is required
报找不到apr库,我们在命令行直接指定就可以了,同样的,在后一步若报找不到apu库时,直接指定即可,最后的完整配置命令如下:
./configure --with-apxs=/app1/usr/local/apache2.2.10/bin/apxs --with-pcre=/app1/usr/local/bin/ --with-apr=/app1/usr/local/apache2.2.10/ --with-apu=/app1/usr/local/apache2.2.10/
其中的三个参数的含义解释如下:
--with-apxs=/app1/usr/local/apache2.2.10/bin/apxs 指定Apache的apxs组件可执行程序的位置,该组件是动态安装Apache组件的工具,安装Apache时必须要安装它
--with-pcre=/app1/usr/local/bin/ 指定pcre库的可执行程序安装位置
--with-apr=/app1/usr/local/apache2.2.10/ 指定apr库的位置,按照我上面步骤安装的该组件就是这个位置
--with-apu=/app1/usr/local/apache2.2.10/ 指定apu库的位置
在我的环境中,只有加上这三个参数,configure才能执行成功
5、第五次执行 ./configure
god@heaven # ./configure --with-apxs=/app1/usr/local/apache2.2.10/bin/apxs --with-pcre=/app1/usr/local/bin/ --with-apr=/app1/usr/local/apache2.2.10/ --with-apu=/app1/usr/local/apache2.2.10/
……
config.status: creating Makefile
config.status: creating build/apxs-wrapper
config.status: creating mlogc-src/mlogc-batch-load.pl
config.status: creating t/run-unit-tests.pl
config.status: creating t/run-regression-tests.pl
config.status: creating t/gen_rx-pm.pl
config.status: creating t/csv_rx-pm.pl
config.status: creating t/regression/server_root/conf/httpd.conf
config.status: creating ../tools/rules-updater.pl
config.status: creating mlogc-src/Makefile
config.status: creating mod_security2_config.h
god@heaven #
配置成功
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
六、编译源码
执行编译命令
1、第一次执行make
god@heaven # make
/app1/usr/local/apache2.2.10/build/libtool --silent --mode=compile gcc -prefer-pic -DSOLARIS2=10 -D_POSIX_PTHREAD_SEMANTICS -D_REENTRANT -D_LARGEFILE64_SOURCE -g -O2 -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -O2 -g -Wall -DWITH_PCRE_STUDY -DMODSEC_PCRE_MATCH_LIMIT=1500 -DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500 -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -I. -I/app1/sparc/httpd-2.2.10/srclib/apr/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/xml/expat/lib -I/app1/usr/local/ssl/include -I/usr/local/ssl/include -I/usr/local/include -I/usr/include/libxml2 -c -o mod_security2.lo mod_security2.c && touch mod_security2.slo
In file included from modsecurity.h:38,
from mod_security2.c:24:
msc_pcre.h:24:18: pcre.h: 无此文件或目录
apxs:Error: Command failed with rc=65536
.
*** Error code 1
The following command caused the error:
src=""; \
for f in mod_security2 apache2_config apache2_io apache2_util re re_operators re_actions re_tfns re_variables msc_logging msc_xml msc_multipart modsecurity msc_parsers msc_util msc_pcre persist_dbm msc_reqbody pdf_protect msc_geo acmp msc_lua msc_release; do \
src="$src $f.c"; \
done; \
rm -f msc_test msc_test.o msc_test.lo msc_test.slo; \
build/apxs-wrapper -c -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -I. -I/app1/sparc/httpd-2.2.10/srclib/apr/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/xml/expat/lib -I/app1/usr/local/ssl/include -I/usr/local/ssl/include -I/usr/local/include -I/usr/include/libxml2 -L/usr/local/lib -R/usr/local/lib -lpcre -L/usr/lib -R/usr/lib -lxml2 -lz -lpthread -lm -lsocket -lnsl -Wc,-O2 -Wc,-g -Wc,-Wall -Wc,-DWITH_PCRE_STUDY -Wc,-DMODSEC_PCRE_MATCH_LIMIT=1500 -Wc,-DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500 $src
make: Fatal error: Command failed for target `mod_security2.la'
报错: msc_pcre.h:24:18: pcre.h: 无此文件或目录
在msc_pcre.h文件中有include pcre.h,但在默认的路径找不到pcre.h,解决办法是修改msc_pcre.h:
god@heaven # vi msc_pcre.h
然后找到
#include <pcre.h>
改成:
#include </app1/usr/local/include/pcre.h>
保存并重新make
2、第二次执行make
god@heaven # make
/app1/usr/local/apache2.2.10/build/libtool --silent --mode=compile gcc -prefer-pic -DSOLARIS2=10 -D_POSIX_PTHREAD_SEMANTICS -D_REENTRANT -D_LARGEFILE64_SOURCE -g -O2 -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -O2 -g -Wall -DWITH_PCRE_STUDY -DMODSEC_PCRE_MATCH_LIMIT=1500 -DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500 -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -I. -I/app1/sparc/httpd-2.2.10/srclib/apr/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/xml/expat/lib -I/app1/usr/local/ssl/include -I/usr/local/ssl/include -I/usr/local/include -I/usr/include/libxml2 -c -o msc_release.lo msc_release.c && touch msc_release.slo
/app1/usr/local/apache2.2.10/build/libtool --silent --mode=link gcc -o mod_security2.la -R/usr/local/lib -R/usr/lib -L/usr/local/lib -L/usr/lib -lpcre -lxml2 -lz -lpthread -lm -lsocket -lnsl -rpath /app1/usr/local/apache2.2.10/modules -module -avoid-version msc_release.lo msc_lua.lo acmp.lo msc_geo.lo pdf_protect.lo msc_reqbody.lo persist_dbm.lo msc_pcre.lo msc_util.lo msc_parsers.lo modsecurity.lo msc_multipart.lo msc_xml.lo msc_logging.lo re_variables.lo re_tfns.lo re_actions.lo re_operators.lo re.lo apache2_util.lo apache2_io.lo apache2_config.lo mod_security2.lo
ld: fatal: library -lpcre: not found
ld: fatal: File processing errors. No output written to .libs/mod_security2.so
collect2: ld returned 1 exit status
apxs:Error: Command failed with rc=65536
.
*** Error code 1
The following command caused the error:
src=""; \
for f in mod_security2 apache2_config apache2_io apache2_util re re_operators re_actions re_tfns re_variables msc_logging msc_xml msc_multipart modsecurity msc_parsers msc_util msc_pcre persist_dbm msc_reqbody pdf_protect msc_geo acmp msc_lua msc_release; do \
src="$src $f.c"; \
done; \
rm -f msc_test msc_test.o msc_test.lo msc_test.slo; \
build/apxs-wrapper -c -I/app1/usr/local/apache2.2.10/include -I/app1/usr/local/apache2.2.10/include -I. -I/app1/sparc/httpd-2.2.10/srclib/apr/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/include -I/app1/sparc/httpd-2.2.10/srclib/apr-util/xml/expat/lib -I/app1/usr/local/ssl/include -I/usr/local/ssl/include -I/usr/local/include -I/usr/include/libxml2 -L/usr/local/lib -R/usr/local/lib -lpcre -L/usr/lib -R/usr/lib -lxml2 -lz -lpthread -lm -lsocket -lnsl -Wc,-O2 -Wc,-g -Wc,-Wall -Wc,-DWITH_PCRE_STUDY -Wc,-DMODSEC_PCRE_MATCH_LIMIT=1500 -Wc,-DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500 $src
make: Fatal error: Command failed for target `mod_security2.la'
报错:ld: fatal: library -lpcre: not found
没找到pcre动态库的位置,解决办法就是把pcre动态库(libpcre.so )所在的路径加入系统变量:
god@heaven # LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/app1/usr/local/lib;export LD_LIBRARY_PATH
3、第三次执行make
god@heaven # make
……
/app1/usr/local/apache2.2.10/build/libtool --silent --mode=link gcc -o mod_security2.la -R/usr/local/lib -R/usr/lib -L/usr/local/lib -L/usr/lib -lpcre -lxml2 -lz -lpthread -lm -lsocket -lnsl -rpath /app1/usr/local/apache2.2.10/modules -module -avoid-version msc_release.lo msc_lua.lo acmp.lo msc_geo.lo pdf_protect.lo msc_reqbody.lo persist_dbm.lo msc_pcre.lo msc_util.lo msc_parsers.lo modsecurity.lo msc_multipart.lo msc_xml.lo msc_logging.lo re_variables.lo re_tfns.lo re_actions.lo re_operators.lo re.lo apache2_util.lo apache2_io.lo apache2_config.lo mod_security2.lo
libtool: link: warning: library `/app1/usr/local/lib/libpcre.la' was moved.
终于看到没有报错的执行结果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
七、测试编译结果
god@heaven # make test
……
10) tfn "urlEncode": passed
11) tfn "urlEncode": passed
12) tfn "urlEncode": passed
13) tfn "urlEncode": passed
14) tfn "urlEncode": passed
15) tfn "urlEncode": passed
16) tfn "urlEncode": passed
17) tfn "urlEncode": passed
Passed: 17; Failed: 0
All tests passed (576).
测试结果全都pass了,好现象!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
八、安装日志收集组件
god@heaven # make mlogc
出错,在经过若干次尝试之后,暂时放弃日志组件的安装(mlogc)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
九、执行安装
god@heaven # make install
……
----------------------------------------------------------------------
Libraries have been installed in:
/app1/usr/local/apache2.2.10/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- use the `-RLIBDIR' linker flag
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /app1/usr/local/apache2.2.10/modules/mod_security2.so
安装成功,组件所在路径是
/app1/usr/local/apache2.2.10/modules/mod_security2.so
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
十、配置httpd.conf
1、备份httpd.conf
god@heaven # cd /app1/usr/local/apache2.2.10/
god@heaven # cp conf/httpd.conf conf/httpd.conf.20100305
2、修改httpd.conf
god@heaven # vi conf/httpd.conf
在httpd.conf中插入三段内容,如下:
#####插入下面的内容(For ModSecurity)#######
#On UNIX (and Windows if you did not copy the DLLs as stated above) you must load libxml2 and lua5.1
# before ModSecurity with something like this:
LoadFile /usr/lib/libxml2.so
LoadFile /app1/usr/local/lib/liblua5.1.so
#####插入结束#######
###设置动态加载的DSO模块
# Example:
# LoadModule foo_module modules/mod_foo.so
#
##如果需要提供基于文本文件的认证,加载此模块,否则注释掉
LoadModule authn_file_module modules/mod_authn_file.so
……
LoadModule userdir_module modules/mod_userdir.so
##此模块提供从文件系统的不同部分到文档树的映射和URL重定向,推荐加载
LoadModule alias_module modules/mod_alias.so
##如果需要基于一定规则实时重写URL请求,加载此模块(推荐),否则注释掉
LoadModule rewrite_module modules/mod_rewrite.so
#####插入下面的内容(For ModSecurity)#######
##加载ModSecurity过滤插件,以解决SQL注入和XSS问题
LoadModule security2_module modules/mod_security2.so
#####插入结束#######
……
###页面最后
#####插入下面的内容(For ModSecurity)#######
##ModSecurity插件的配置文件信息
<IfModule security2_module>
Include conf/modsecurity_crs/*.conf
Include conf/modsecurity_crs/base_rules/*.conf
</IfModule>
#####插入结束#######
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
十一、设置配置文件
ModSecurity2.X自带比较丰富的过滤规则库,我们只需要根据自己的需要把相应的配置文件复制到服务器即可
1、先在Apache的conf目录下建一个新目录modsecurity_crs,这个目录名要与httpd.conf文件中指定的路径一致:
conf/modsecurity_crs/
2、从ModSecurity源码包中把rules目录里的所有内容(包括子目录)复制到服务器上的onf/modsecurity_crs/目录下
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
十二、重启动Apache
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
到这里,如果以上步骤都顺利,那么ModSecurity也算安装成功了,你可以打开浏览器,试试SQL注入或跨站脚本,看看有没有被拦截掉。