modsecurity本來是Apache上的一款開源waf,能夠有效的加強web安全性,目前已經支持nginx和IIS,配合nginx的靈活和高效,能夠打形成生產級的WAF,是保護和審覈web安全的利器。php
系統:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0html
依賴關係:
tengine(nginx)依賴: pcre 、zlib、 openssl, 這三個包centos 6.5 系統源裏都有:nginx
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
modsecurty依賴的包:pcre httpd-devel libxml2 aprgit
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
下載modsecurity for nginx 解壓,進入解壓後目錄執行:github
./autogen.sh ./configure --enable-standalone-module --disable-mlogc make
在編譯standalone後,nginx編譯時能夠經過"--add-module"添加modsecurity模塊:web
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine make && make install
modsecurity傾向於過濾和阻止web危險,之因此強大就在於規則,OWASP提供的規則是於社區志願者維護的,被稱爲核心規則CRS(corerules),規則可靠強大,固然也能夠自定義規則來知足各類需求。sql
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
複製modsecurity源碼目錄下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目錄下,並將modsecurity.conf-recommended從新命名爲modsecurity.conf。centos
編輯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
在須要啓用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