使用mod_security加强Apache的安全性

发表于:2007-05-25来源:作者:点击数: 标签:modsecurity加强apache使用
使用mod_security加强Apache的 安全 性 发布日期: 2005-05-22 22:20 作者: os http://bbs.islab.org 目录: 一. 介绍 二. 准备 1. 需要的软件包 2. 下载和解压缩Apache HTTP软件包. 3. 下载和解压缩modsecurity软件包. 4. 下载modsecurity规则文件 三. 安装Ap
使用mod_security加强Apache的安全

发布日期: 2005-05-22 22:20

作者: os
http://bbs.islab.org

目录:
一. 介绍
二. 准备
1.  需要的软件包
2.  下载和解压缩Apache HTTP软件包.
3.  下载和解压缩modsecurity软件包.
4.  下载modsecurity规则文件
三. 安装Apache和Modsecurity
1.  安装Apache
2.  安装Modsecurity
四. Modsecurity配置文件
五. 更多


一. 介绍
mod_security是一个集入侵检测和防御引擎功能的开源web应用安全程序(或web应用程序防火墙).
它以Apache Web服务器的模块方式运行, 目标是增强web应用程序的安全性, 防止web应用程序
受到已知或未知的攻击.

本文使用的系统是Redhat linux高级服务器版3, 推荐大家使用modsecurity的最新稳定发行版,
现在是1.8.7, 安装方式是通过DSO(动态共享对象)把mod_security集成到Apache Web Server.
(提示, 本文的步骤在其它linux发行版上也是适用的.)

二. 准备
1. 需要的软件包
1). apache的源代码包
下载地址:
http://apache.justdn.org/httpd/apache_1.3.33.tar.gz

2). mod_security的源代码包
下载地址:
http://www.modsecurity.org/download/modsecurity-1.8.7.tar.gz

3). mod_security基本规则
http://fedoranews.org/jorge/mod_security/mod_security.conf

2. 下载和解压缩Apache HTTP软件包.
建议从apache的官方网站获得可靠的apache HTTP软件包.
下载网址:
http://apache.justdn.org/httpd/apache_1.3.33.tar.gz

1) 创建apache软件包存放的目录.
#mkdir -p /usr/local/src/webserver

2) 使用wget命令获得源代码包.
#wget
http://apache.tarchive.com/httpd/apache_1.3.33.tar.gz
(如不能下载, 可直接通过浏览器保存或ftp上传到webserver目录)

3) 确认下载软件的完整性.
首先, 我们下载和检查分离的签名文件apache_1.3.33.tar.gz.md5, 然后进行
比较软件包和软件包md5签名文件的md5 checksum.

检查apache_1.3.33.tar.gz.md5内容
# cat apache_1.3.33.tar.gz.md5
MD5 (apache_1.3.33.tar.gz) = 3dfd2c3778f37a2dfc22b97417a61407

检查apache_1.3.33.tar.gz的md5 checksum
# md5sum apache_1.3.33.tar.gz
3dfd2c3778f37a2dfc22b97417a61407  apache_1.3.33.tar.gz

比较上面两个步骤的md5 checksum内容是一致的, 从而可以确认apache软件包
的完整性.

4) 解压缩源代码包, 在webserver目录下会生成一个新的目录apache_1.3.33
#tar zpxf apache_1.3.33.tar.gz

3. 下载和解压缩modsecurity软件包.
建议从modsecurity官方网站获得可靠的软件包.
下载网址:
http://www.modsecurity.org/download/index.html

1) 使用wget命令获得源代码包.
#wget
http://www.modsecurity.org/download/modsecurity-1.8.7.tar.gz
(如不能下载, 可直接通过浏览器保存或ftp上传到webserver目录)

# ls -al modsecurity-1.8.7.tar.gz
-rw-r--r-- 1 root root 313004 Mar  5 23:47 modsecurity-1.8.7.tar.gz

# pwd
/usr/local/src/webserver

2) 确认下载软件的完整性.
首先, 我们下载和检查分离的签名文件modsecurity-1.8.7.tar.gz.md5, 然后进行
比较软件包和软件包md5签名文件的md5 checksum.

下载modsecurity的签名文件
#wget
http://www.modsecurity.org/download/modsecurity-1.8.7.tar.gz.md5

检查modsecurity-1.8.7.tar.gz.md5内容
#cat modsecurity-1.8.7.tar.gz.md5
0dd48656e451c711358c097dc80e0369  modsecurity-1.8.7.tar.gz

检查modsecurity-1.8.7.tar.gz的md5 checksum
# md5sum modsecurity-1.8.7.tar.gz
0dd48656e451c711358c097dc80e0369  modsecurity-1.8.7.tar.gz

比较上面两个步骤的md5 checksum内容是一致的, 从而可以确认modsecurity软件包
的完整性.

3) 解压缩源代码包, 在webserver目录下会生成一个新的目录modsecurity-1.8.7
#tar zpxf modsecurity-1.8.7.tar.gz

4. 下载modsecurity规则文件
#cd /usr/local/src/webserver
#wget
http://fedoranews.org/jorge/mod_security/mod_security.conf


三. 安装Apache和Modsecurity

1. 安装Apache

请参考文章:
在Redhat Linux 9上通过DSO方式搭建Apache HTTP服务器
http://bbs.islab.org/showthread.php?t=370

注: 在Red Hat 9上安装Apache的步骤亦可用其它系统上.

2. 安装Modsecurity

到modsecurity的apache 1.X 模块目录运行apxs(你必须要root权限才能运行下面命令).

#cd /usr/local/src/webserver/modsecurity-1.8.7/apache1

[root@reakosys apache1]# /www/bin/apxs -cia mod_security.c
gcc -DLINUX=22 -DHAVE_SET_DUMPABLE -I/usr/include/gdbm -DUSE_HSREGEX -fpic
   -DSHARED_MODULE -I/www/include  -c mod_security.c
gcc -shared -o mod_security.so mod_security.o
[activating module `security' in /www/conf/httpd.conf]
cp mod_security.so /www/libexec/mod_security.so
chmod 755 /www/libexec/mod_security.so
cp /www/conf/httpd.conf /www/conf/httpd.conf.bak
cp /www/conf/httpd.conf.new /www/conf/httpd.conf
rm /www/conf/httpd.conf.new

复制modsecurity规则文件到apache的conf目录
#cd /usr/local/src/webserver
#cp mod_security.conf /www/conf/

创建日志目录
#mkdir - /var/log/httpd

修改apache配置文件
#vi /www/conf/httpd.conf
在文件末尾添加
Include conf/mod_security.conf

注: 到此为止, httpd.conf文件已包含下面三行代码(1,2条为自动, 3条手动添加)
LoadModule security_module    libexec/mod_security.so
AddModule mod_security.c
Include conf/mod_security.conf

然后重启你的Apache Web Server..
# apachectl restart
/usr/sbin/apachectl restart: httpd not running, trying to start
/usr/sbin/apachectl restart: httpd started

检查一下是否生成日志文件
# ls -al
total 8
drwxr-xr-x    2 root     root   4096 May 22 21:29 .
drwxr-xr-x   10 root     root   4096 May 22 21:02 ..
-rw-------    1 root     root      0 May 22 21:18 audit_log
-rw-------    1 root     root      0 May 22 21:18 modsec_debug_log

modsecurity的配置文件mod_security.conf位于apache的配置目录conf中, 日志文件
是/var/log/httpd/audit_log.

modsecurity配置文件包含了一些非常基础的规则设置, 这对于一般的站点而言是足够了.
如果你有什么特殊的需求, 更多可以参考下面的网址:
http://www.modsecurity.org/documentation/index.html

下面将介绍mod_security.conf配置文件.


四. Modsecurity配置文件

<IfModule mod_security.c>

   # Turn the filtering engine On or Off
   SecFilterEngine On

分析每一个http请求

   # Make sure that URL encoding is valid
   SecFilterCheckURLEncoding On

URL编码确认

   # Only allow bytes from this range
   SecFilterForceByteRange 32 126

字节范围检查, 以有效防止stack overflow attacks(栈溢出攻击).

   # The audit engine works independently and
   # can be turned On of Off on the per-server or
   # on the per-directory basis
   SecAuditEngine RelevantOnly

有效解决apache日志对某个用户或攻击者信息记录的不足. 如果要
对某一个用户或攻击者发出的一个请求的详细记录, 可以访问
/var/log/httpd/audit_log文件.

   # The name of the audit log file
   SecAuditLog /var/log/httpd/audit_log

   SecFilterDebugLog /var/log/httpd/modsec_debug_log
   SecFilterDebugLevel 0

设置调试模式下的输出文件.

   # Should mod_security inspect POST payloads
   #SecFilterScanPOST On

   # Action to take by default
   SecFilterDefaultAction "deny,log,status:406"

设置特别的行动, 406为行动名称, 前面的三个为行动参数.

   # Redirect user on filter match
   #SecFilter xxx redirect:http://www.webkreator.com

   # Execute the external script on filter match
   #SecFilter yyy log,exec:/home/ivanr/apache/bin/report-attack.pl

   # Simple filter
   #SecFilter 111
   
   # Only check the QUERY_STRING variable
   #SecFilterSelective QUERY_STRING 222

   # Only check the body of the POST request
   #SecFilterSelective POST_PAYLOAD 333

   # Only check arguments (will work for GET and POST)
   #SecFilterSelective ARGS 444

   # Test filter
   #SecFilter "/cgi-bin/keyword"

   # Another test filter, will be denied with 404 but not logged
   # action supplied as a parameter overrides the default action
   #SecFilter 999 "deny,nolog,status:404"

   # Prevent OS specific keywords
   #SecFilter /etc/password

   # Prevent path traversal (..) attacks
   SecFilter "\.\./"

阻止目录周游攻击.

   # Weaker XSS protection but allows common HTML tags
   SecFilter "<( |\n)*script"

对不安全的(跨站点脚本)XSS进行保护, 但允许普通的HTML标识.

   # Prevent XSS atacks (HTML/Javascript injection)
   SecFilter "<(.|\n)+>"

防止XSS攻击 (HTML/Javascript注射)

   # Very crude filters to prevent SQL injection attacks
   SecFilter "delete[[:space:]]+from"
   SecFilter "insert[[:space:]]+into"
   SecFilter "select.+from"

防止SQL注射攻击

   # Require HTTP_USER_AGENT and HTTP_HOST headers
   SecFilterSelective "HTTP_USER_AGENT|HTTP_HOST" "^$"

需要 HTTP_USER_AGENT和HTTP_HOST头.

   # Forbid file upload
   #SecFilterSelective "HTTP_CONTENT_TYPE" multipart/form-data

   # Only watch argument p1
   #SecFilterSelective "ARG_p1" 555

   # Watch all arguments except p1
   #SecFilterSelective "ARGS|!ARG_p2" 666

   # Only allow our own test utility to send requests (or Mozilla)
   #SecFilterSelective HTTP_USER_AGENT "!(mod_security|mozilla)"

   # Do not allow variables with this name
   #SecFilterSelective ARGS_NAMES 777

   # Do now allow this variable value (names are ok)
   #SecFilterSelective ARGS_VALUES 888

   # Stop spamming through FormMail
   # note the exclamation mark at the beginning
   # of the filter - only requests that match this regex will
   # be allowed
   #<Location /cgi-bin/FormMail>
       #SecFilterSelective "ARG_recipient" "!@webkreator.com$"
   #</Location>

   # when allowing upload, only allow images
   # note that this is not foolproof, a determined attacker
   # could get around this
   #<Location /fileupload.php>
       #SecFilterInheritance Off
       #SecFilterSelective POST_PAYLOAD "!image/(jpeg|bmp|gif)"
   #</Location>

</IfModule>

使用mod_security加强Apache的安全性

发布日期: 2005-05-22 22:20

作者: os
http://bbs.islab.org

目录:
一. 介绍
二. 准备
1.  需要的软件包
2.  下载和解压缩Apache HTTP软件包.
3.  下载和解压缩modsecurity软件包.
4.  下载modsecurity规则文件
三. 安装Apache和Modsecurity
1.  安装Apache
2.  安装Modsecurity
四. Modsecurity配置文件
五. 更多


一. 介绍
mod_security是一个集入侵检测和防御引擎功能的开源web应用安全程序(或web应用程序防火墙).
它以Apache Web服务器的模块方式运行, 目标是增强web应用程序的安全性, 防止web应用程序
受到已知或未知的攻击.

本文使用的系统是Redhat linux高级服务器版3, 推荐大家使用modsecurity的最新稳定发行版,
现在是1.8.7, 安装方式是通过DSO(动态共享对象)把mod_security集成到Apache Web Server.
(提示, 本文的步骤在其它linux发行版上也是适用的.)

二. 准备
1. 需要的软件包
1). apache的源代码包
下载地址:
http://apache.justdn.org/httpd/apache_1.3.33.tar.gz

2). mod_security的源代码包
下载地址:
http://www.modsecurity.org/download/modsecurity-1.8.7.tar.gz

3). mod_security基本规则
http://fedoranews.org/jorge/mod_security/mod_security.conf

2. 下载和解压缩Apache HTTP软件包.
建议从apache的官方网站获得可靠的apache HTTP软件包.
下载网址:
http://apache.justdn.org/httpd/apache_1.3.33.tar.gz

1) 创建apache软件包存放的目录.
#mkdir -p /usr/local/src/webserver

2) 使用wget命令获得源代码包.
#wget
http://apache.tarchive.com/httpd/apache_1.3.33.tar.gz
(如不能下载, 可直接通过浏览器保存或ftp上传到webserver目录)

3) 确认下载软件的完整性.
首先, 我们下载和检查分离的签名文件apache_1.3.33.tar.gz.md5, 然后进行
比较软件包和软件包md5签名文件的md5 checksum.

检查apache_1.3.33.tar.gz.md5内容
# cat apache_1.3.33.tar.gz.md5
MD5 (apache_1.3.33.tar.gz) = 3dfd2c3778f37a2dfc22b97417a61407

检查apache_1.3.33.tar.gz的md5 checksum
# md5sum apache_1.3.33.tar.gz
3dfd2c3778f37a2dfc22b97417a61407  apache_1.3.33.tar.gz

比较上面两个步骤的md5 checksum内容是一致的, 从而可以确认apache软件包
的完整性.

4) 解压缩源代码包, 在webserver目录下会生成一个新的目录apache_1.3.33
#tar zpxf apache_1.3.33.tar.gz

3. 下载和解压缩modsecurity软件包.
建议从modsecurity官方网站获得可靠的软件包.
下载网址:
http://www.modsecurity.org/download/index.html

1) 使用wget命令获得源代码包.
#wget
http://www.modsecurity.org/download/modsecurity-1.8.7.tar.gz
(如不能下载, 可直接通过浏览器保存或ftp上传到webserver目录)

# ls -al modsecurity-1.8.7.tar.gz
-rw-r--r-- 1 root root 313004 Mar  5 23:47 modsecurity-1.8.7.tar.gz

# pwd
/usr/local/src/webserver

2) 确认下载软件的完整性.
首先, 我们下载和检查分离的签名文件modsecurity-1.8.7.tar.gz.md5, 然后进行
比较软件包和软件包md5签名文件的md5 checksum.

下载modsecurity的签名文件
#wget
http://www.modsecurity.org/download/modsecurity-1.8.7.tar.gz.md5

检查modsecurity-1.8.7.tar.gz.md5内容
#cat modsecurity-1.8.7.tar.gz.md5
0dd48656e451c711358c097dc80e0369  modsecurity-1.8.7.tar.gz

检查modsecurity-1.8.7.tar.gz的md5 checksum
# md5sum modsecurity-1.8.7.tar.gz
0dd48656e451c711358c097dc80e0369  modsecurity-1.8.7.tar.gz

比较上面两个步骤的md5 checksum内容是一致的, 从而可以确认modsecurity软件包
的完整性.

3) 解压缩源代码包, 在webserver目录下会生成一个新的目录modsecurity-1.8.7
#tar zpxf modsecurity-1.8.7.tar.gz

4. 下载modsecurity规则文件
#cd /usr/local/src/webserver
#wget
http://fedoranews.org/jorge/mod_security/mod_security.conf


三. 安装Apache和Modsecurity

1. 安装Apache

请参考文章:
在Redhat Linux 9上通过DSO方式搭建Apache HTTP服务器
http://bbs.islab.org/showthread.php?t=370

注: 在Red Hat 9上安装Apache的步骤亦可用其它系统上.

2. 安装Modsecurity

到modsecurity的apache 1.X 模块目录运行apxs(你必须要root权限才能运行下面命令).

#cd /usr/local/src/webserver/modsecurity-1.8.7/apache1

[root@reakosys apache1]# /www/bin/apxs -cia mod_security.c
gcc -DLINUX=22 -DHAVE_SET_DUMPABLE -I/usr/include/gdbm -DUSE_HSREGEX -fpic
   -DSHARED_MODULE -I/www/include  -c mod_security.c
gcc -shared -o mod_security.so mod_security.o
[activating module `security' in /www/conf/httpd.conf]
cp mod_security.so /www/libexec/mod_security.so
chmod 755 /www/libexec/mod_security.so
cp /www/conf/httpd.conf /www/conf/httpd.conf.bak
cp /www/conf/httpd.conf.new /www/conf/httpd.conf
rm /www/conf/httpd.conf.new

复制modsecurity规则文件到apache的conf目录
#cd /usr/local/src/webserver
#cp mod_security.conf /www/conf/

创建日志目录
#mkdir - /var/log/httpd

修改apache配置文件
#vi /www/conf/httpd.conf
在文件末尾添加
Include conf/mod_security.conf

注: 到此为止, httpd.conf文件已包含下面三行代码(1,2条为自动, 3条手动添加)
LoadModule security_module    libexec/mod_security.so
AddModule mod_security.c
Include conf/mod_security.conf

然后重启你的Apache Web Server..
# apachectl restart
/usr/sbin/apachectl restart: httpd not running, trying to start
/usr/sbin/apachectl restart: httpd started

检查一下是否生成日志文件
# ls -al
total 8
drwxr-xr-x    2 root     root   4096 May 22 21:29 .
drwxr-xr-x   10 root     root   4096 May 22 21:02 ..
-rw-------    1 root     root      0 May 22 21:18 audit_log
-rw-------    1 root     root      0 May 22 21:18 modsec_debug_log

modsecurity的配置文件mod_security.conf位于apache的配置目录conf中, 日志文件
是/var/log/httpd/audit_log.

modsecurity配置文件包含了一些非常基础的规则设置, 这对于一般的站点而言是足够了.
如果你有什么特殊的需求, 更多可以参考下面的网址:
http://www.modsecurity.org/documentation/index.html

下面将介绍mod_security.conf配置文件.


四. Modsecurity配置文件

<IfModule mod_security.c>

   # Turn the filtering engine On or Off
   SecFilterEngine On

分析每一个http请求

   # Make sure that URL encoding is valid
   SecFilterCheckURLEncoding On

URL编码确认

   # Only allow bytes from this range
   SecFilterForceByteRange 32 126

字节范围检查, 以有效防止stack overflow attacks(栈溢出攻击).

   # The audit engine works independently and
   # can be turned On of Off on the per-server or
   # on the per-directory basis
   SecAuditEngine RelevantOnly

有效解决apache日志对某个用户或攻击者信息记录的不足. 如果要
对某一个用户或攻击者发出的一个请求的详细记录, 可以访问
/var/log/httpd/audit_log文件.

   # The name of the audit log file
   SecAuditLog /var/log/httpd/audit_log

   SecFilterDebugLog /var/log/httpd/modsec_debug_log
   SecFilterDebugLevel 0

设置调试模式下的输出文件.

   # Should mod_security inspect POST payloads
   #SecFilterScanPOST On

   # Action to take by default
   SecFilterDefaultAction "deny,log,status:406"

设置特别的行动, 406为行动名称, 前面的三个为行动参数.

   # Redirect user on filter match
   #SecFilter xxx redirect:http://www.webkreator.com

   # Execute the external script on filter match
   #SecFilter yyy log,exec:/home/ivanr/apache/bin/report-attack.pl

   # Simple filter
   #SecFilter 111
   
   # Only check the QUERY_STRING variable
   #SecFilterSelective QUERY_STRING 222

   # Only check the body of the POST request
   #SecFilterSelective POST_PAYLOAD 333

   # Only check arguments (will work for GET and POST)
   #SecFilterSelective ARGS 444

   # Test filter
   #SecFilter "/cgi-bin/keyword"

   # Another test filter, will be denied with 404 but not logged
   # action supplied as a parameter overrides the default action
   #SecFilter 999 "deny,nolog,status:404"

   # Prevent OS specific keywords
   #SecFilter /etc/password

   # Prevent path traversal (..) attacks
   SecFilter "\.\./"

阻止目录周游攻击.

   # Weaker XSS protection but allows common HTML tags
   SecFilter "<( |\n)*script"

对不安全的(跨站点脚本)XSS进行保护, 但允许普通的HTML标识.

   # Prevent XSS atacks (HTML/Javascript injection)
   SecFilter "<(.|\n)+>"

防止XSS攻击 (HTML/Javascript注射)

   # Very crude filters to prevent SQL injection attacks
   SecFilter "delete[[:space:]]+from"
   SecFilter "insert[[:space:]]+into"
   SecFilter "select.+from"

防止SQL注射攻击

   # Require HTTP_USER_AGENT and HTTP_HOST headers
   SecFilterSelective "HTTP_USER_AGENT|HTTP_HOST" "^$"

需要 HTTP_USER_AGENT和HTTP_HOST头.

   # Forbid file upload
   #SecFilterSelective "HTTP_CONTENT_TYPE" multipart/form-data

   # Only watch argument p1
   #SecFilterSelective "ARG_p1" 555

   # Watch all arguments except p1
   #SecFilterSelective "ARGS|!ARG_p2" 666

   # Only allow our own test utility to send requests (or Mozilla)
   #SecFilterSelective HTTP_USER_AGENT "!(mod_security|mozilla)"

   # Do not allow variables with this name
   #SecFilterSelective ARGS_NAMES 777

   # Do now allow this variable value (names are ok)
   #SecFilterSelective ARGS_VALUES 888

   # Stop spamming through FormMail
   # note the exclamation mark at the beginning
   # of the filter - only requests that match this regex will
   # be allowed
   #<Location /cgi-bin/FormMail>
       #SecFilterSelective "ARG_recipient" "!@webkreator.com$"
   #</Location>

   # when allowing upload, only allow images
   # note that this is not foolproof, a determined attacker
   # could get around this
   #<Location /fileupload.php>
       #SecFilterInheritance Off
       #SecFilterSelective POST_PAYLOAD "!image/(jpeg|bmp|gif)"
   #</Location>

</IfModule>

原文转自:http://www.ltesting.net