modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。

一.准备工作

系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0

  1. tenginehttp://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  2. modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
  3. OWASP规则集https://github.com/SpiderLabs/owasp-modsecurity-crs

依赖关系:
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:

yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel

modsecurty依赖的包:pcre httpd-devel libxml2 apr

yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel

二.启用standalone模块并编译

下载modsecurity for nginx 解压,进入解压后目录执行:

./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make 

三.nginx添加modsecurity模块

在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:

./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
make && make install

四.添加规则

modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。

1.下载OWASP规则:

git clone https://github.com/SpiderLabs/owasp-modsecurity-crs

mv owasp-modsecurity-crs /opt/tengine/conf/

cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf

2.启用OWASP规则:

复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。

编辑modsecurity.conf 文件,将SecRuleEngine设置为 on

owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。

Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf

五.配置nginx

在需要启用modsecurity的主机的location下面加入下面两行即可:

ModSecurityEnabled on;  
ModSecurityConfig modsecurity.conf;

下面是两个示例配置,php虚拟主机:

server {
      listen      80;
      server_name 52os.net www.52os.net;

      location ~ \.php$ {
      ModSecurityEnabled on;  
      ModSecurityConfig modsecurity.conf;

      root /web/wordpress;
      index index.php index.html index.htm;

      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
      include        fastcgi_params;
      }
  }

upstream负载均衡:

upstream 52os.net {
    server 192.168.1.100:8080;
    server 192.168.1.101:8080 backup;
}

server {
listen 80;
server_name 52os.net www.52os.net;

location / {
    ModSecurityEnabled on;  
    ModSecurityConfig modsecurity.conf;  

        proxy_pass http://online;
        proxy_redirect         off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

六.测试

我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:

<?php
    phpinfo();    
?>

在浏览器中访问:

http://www.52os.net/phpinfo.php?id=1 正常显示。
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。

说明sql注入和xss已经被过滤了

七、安装过程中排错

1.缺少APXS会报错

configure: looking for Apache module support via DSO through APXS
configure: error: couldn't find APXS

apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
解决方法:

yum install httpd-devel

2.没有pcre

configure: *** pcre library not found.
configure: error: pcre library is required

解决方法:

yum install pcre pcre-devel

3.没有libxml2

configure: *** xml library not found.
configure: error: libxml2 is required

解决方法:

yum install  libxml2 libxml2-devel

4.执行 /opt/tengine/sbin/nginx -m 时有警告

Tengine version: Tengine/2.1.0 (nginx/1.6.2)
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!

原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log

2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.

解决方法,移除低版本的APR (1.3.9)

yum remove apr

5.Error.log中有: Audit log: Failed to lock global mutex

2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]

解决方法:
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:

SecAuditLogDirMode 0777
SecAuditLogFileMode 0550
SecAuditLogStorageDir /var/log/modsecurity
SecAuditLogType Concurrent

参考文章:
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
http://drops.wooyun.org/tips/2614