nginx配合modsecurity實現WAF功能
modsecurity原本是Apache上的一款開源waf,可以有效的增強web安全性,目前已經支援nginx和IIS,配合nginx的靈活和高效,可以打造成生產級的WAF,是保護和稽核web安全的利器。
一.準備工作
系統:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- 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