開源WAF工具ModSecurity

0 前言

  ModSecurity是一個開源的跨平臺Web應用程序防火牆(WAF)引擎,用於Apache,IIS和Nginx,由Trustwave的SpiderLabs開發。做爲WAF產品,ModSecurity專門關注HTTP流量,當發出HTTP請求時,ModSecurity檢查請求的全部部分,若是請求是惡意的,它會被阻止和記錄。  html

優點

完美兼容nginx,是nginx官方推薦的WAF 支持OWASP規則 3.0版本比老版本更新更快,更加穩定,而且獲得了nginx、Inc和Trustwave等團隊的積極支持 免費

功能

SQL Injection (SQLi):阻止SQL注入 Cross Site Scripting (XSS):阻止跨站腳本攻擊 Local File Inclusion (LFI):阻止利用本地文件包含漏洞進行攻擊 Remote File Inclusione(RFI):阻止利用遠程文件包含漏洞進行攻擊 Remote Code Execution (RCE):阻止利用遠程命令執行漏洞進行攻擊 PHP Code Injectiod:阻止PHP代碼注入 HTTP Protocol Violations:阻止違反HTTP協議的惡意訪問 HTTPoxy:阻止利用遠程代理感染漏洞進行攻擊 Shellshock:阻止利用Shellshock漏洞進行攻擊 Session Fixation:阻止利用Session會話ID不變的漏洞進行攻擊 Scanner Detection:阻止黑客掃描網站 Metadata/Error Leakages:阻止源代碼/錯誤信息泄露 Project Honey Pot Blacklist:蜜罐項目黑名單 GeoIP Country Blocking:根據判斷IP地址歸屬地來進行IP阻斷

劣勢

不支持檢查響應體的規則,若是配置中包含這些規則,則會被忽略,nginx的的sub_filter指令能夠用來檢查狀語從句:重寫響應數據,OWASP中相關規則是95X。 不支持OWASP核心規則集DDoS規則REQUEST-912-DOS- PROTECTION.conf,nginx自己支持配置DDoS限制 不支持在審計日誌中包含請求和響應主體

  以上內容摘自:ModSecurity:一款優秀的開源WAFnginx

00 Preface

  本篇介紹如何在CentOS7.6上安裝ModSecurity。上面的給出的連接內容比較雜亂,故從新整理以記錄。c++

安裝

安裝nginx

  若是有nginx,可忽略;若是沒有請參考:RHEL/CentOS 安裝最新版Nginxgit

安裝依賴

# yum install epel-release -y # yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre pcre-devel libxml2 libxml2-devel autoconf automake lmdb-devel ssdeep-devel ssdeep-libs lua-devel libmaxminddb-devel git apt-utils autoconf automake build-essential git libcurl4-openssl-dev libgeoip-dev liblmdb-dev ibpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev -y

編譯ModSecurity

  咱們用的是v3版本,咱們在/opt目錄下進行安裝。github

# cd /opt/    # 切換到/opt # git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity    # 下載
# cd ModSecurity/ # git submodule init     # 初始化
# git submodule update    # 更新
# ./build.sh
# ./configure
# make
# make install

【注】在執行build.sh會出現以下錯誤,可忽略。web

fatal: No names found, cannot describe anything

ModSecurity-nginx鏈接器

  咱們如今須要將ModSecurity-nginx編入。vim

# cd /opt/ # git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
# nginx -v            # 查看當前nginx版本 nginx version: nginx/1.17.5
# wget http://nginx.org/download/nginx-1.17.5.tar.gz
# tar -xvf nginx-1.17.5.tar.gz # ls ModSecurity ModSecurity-nginx  nginx-1.17.5  nginx-1.17.5.tar.gz # cd nginx-1.17.5/ # ./configure --with-compat --add-dynamic-module=../ModSecurity-nginx # 若是出現不兼容的問題,請去掉--with-compat參數 # make modules # 會生成以下*.so # ls ./objs/ngx_http_modsecurity_module.so ./objs/ngx_http_modsecurity_module.so       # 查看
# cp ./objs/ngx_http_modsecurity_module.so /etc/nginx/modules/   
# 移動位置
# vim /etc/nginx/nginx.conf  load_module /etc/nginx/modules/ngx_http_modsecurity_module.so; # 添加到配置文件首行 # nginx -t                                     # 測試經過 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

測試

ECHO測試 

  新增配置文件:/etc/nginx/conf.d/echo.conf :app

# service nginx start   # 啓動nginx Redirecting to /bin/systemctl start nginx.service # vim /etc/nginx/conf.d/echo.conf server { listen localhost:8085; location / { default_type text/plain; return 200 "Thank you for requesting ${request_uri}\n"; } } # nginx -s reload    # 重載配置 # nginx -t        # 檢測 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# curl -D - http://localhost:8085
HTTP/1.1 200 OK Server: nginx/1.17.5 Date: Mon, 18 Nov 2019 05:35:40 GMT Content-Type: text/plain Content-Length: 27 Connection: keep-alive Thank you for requesting / [root@localhost ~]# curl -D - http://localhost:8085/notexist
HTTP/1.1 200 OK Server: nginx/1.17.5 Date: Mon, 18 Nov 2019 05:35:49 GMT Content-Type: text/plain Content-Length: 35 Connection: keep-alive Thank you for requesting /notexist

  能夠看到正常echo。curl

配置反向代理

  新增配置文件:/etc/nginx/conf.d/proxy.conf ,內容以下:ide

[root@localhost ~]# cat /etc/nginx/conf.d/proxy.conf server { listen 80; location / { proxy_pass http://localhost:8085;
 proxy_set_header Host $host; } }

  由於正常安裝後,nginx是有默認配置的:/etc/nginx/conf.d/default.conf,這個會影響到上面的正常生效。

[root@localhost ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak [root@localhost ~]# nginx -s reload [root@localhost ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# curl -D - http://localhost
HTTP/1.1 200 OK Server: nginx/1.17.5 Date: Mon, 18 Nov 2019 05:43:05 GMT Content-Type: text/plain Content-Length: 27 Connection: keep-alive Thank you for requesting / [root@localhost ~]# curl -D - http://localhost/noexist
HTTP/1.1 200 OK Server: nginx/1.17.5 Date: Mon, 18 Nov 2019 05:44:06 GMT Content-Type: text/plain Content-Length: 34 Connection: keep-alive Thank you for requesting /noexist [root@localhost ~]#

  能夠看到訪問默認的80端口,會反向代理到8085端口。

啓用WAF

  配置NGINX WAF以經過阻止某些請求來保護演示web應用程序。

[root@localhost ~]# mkdir /etc/nginx/modsec [root@localhost ~]# cd /etc/nginx/modsec [root@localhost modsec]# sudo wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended [root@localhost modsec]# sudo mv modsecurity.conf-recommended modsecurity.conf

  修改modsecurity.conf配置文件

[root@localhost modsec]# vim modsecurity.conf # -- Rule engine initialization ----------------------------------------------
... SecRuleEngine On <== 設置爲On

  修改nginx waf配置文件:/etc/nginx/modsec/main.conf ,添加響應規則。

# cat /etc/nginx/modsec/main.conf # Include the recommended configuration Include /etc/nginx/modsec/modsecurity.conf # A test rule SecRule ARGS:testparam "@contains test" "id:1234,deny,log,status:403"
  • Include:包括modsecurity.conf文件中建議的配置。
  • SecRule:建立一個規則,當查詢字符串中的testparam參數包含字符串test時,經過阻止請求並返回狀態代碼403來保護應用程序。

  修改nginx配置文件,來啓用WAF防禦。

# cat /etc/nginx/conf.d/proxy.conf server { listen 80; modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf; location / { proxy_pass http://localhost:8085;
 proxy_set_header Host $host; } }
  • modsecurity on:啓用Nginx WAF;
  • modsecurity_rules_file:指定規則文件路徑。
[root@localhost modsec]# cp /opt/ModSecurity/unicode.mapping /etc/nginx/modsec/    # 須要先拷貝下unicode.mapping文件 [root@localhost modsec]# nginx -s reload        # 重載配置 [root@localhost modsec]# nginx -t            # 測試 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

  測試參數中帶有test,會被禁止。

[root@localhost modsec]# curl -D - http://localhost/foo?testparam=thisisatestofmodsecurity
HTTP/1.1 403 Forbidden Server: nginx/1.17.5 Date: Mon, 18 Nov 2019 05:59:10 GMT Content-Type: text/html Content-Length: 153 Connection: keep-alive <html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.17.5</center>
</body>
</html>

日誌記錄功能

  修改nginx配置文件:/etc/nginx/nginx.conf,

# vim /etc/nginx/nginx.conf load_module /opt/nginx-1.17.5/objs/ngx_http_modsecurity_module.so;    # 加載模塊 user nginx; worker_processes 1; error_log /var/log/nginx/error.log info;      # 將錯誤日誌設置爲info級別
[root@localhost modsec]# nginx -s reload       # 重載配置 [root@localhost modsec]# nginx -t           # 測試 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost modsec]# curl -D - http://localhost/foo?testparam=thisisatestofmodsecurity      # 再次訪問
HTTP/1.1 403 Forbidden Server: nginx/1.17.5 Date: Mon, 18 Nov 2019 06:02:09 GMT Content-Type: text/html Content-Length: 153 Connection: keep-alive <html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.17.5</center>
</body>
</html> [root@localhost modsec]# tail -5 /var/log/nginx/error.log       # 查看錯誤日誌文件 2019/11/18 14:01:57 [notice] 24845#24845: worker process 25847 exited with code 0
2019/11/18 14:01:57 [notice] 24845#24845: signal 29 (SIGIO) received 2019/11/18 14:01:59 [notice] 25880#25880: ModSecurity-nginx v1.0.0 (rules loaded inline/local/remote: 0/7/0) 2019/11/18 14:02:09 [error] 25879#25879: *11 [client 127.0.0.1] ModSecurity: Access denied with code 403 (phase 1). Matched "Operator `Contains' with parameter `test' against variable `ARGS:testparam' (Value: `thisisatestofmodsecurity' ) [file "/etc/nginx/modsec/main.conf"] [line "4"] [id "1234"] [rev ""] [msg ""] [data ""] [severity "0"] [ver ""] [maturity "0"] [accuracy "0"] [hostname "127.0.0.1"] [uri "/foo"] [unique_id "157405692985.199277"] [ref "o7,4v19,24"], client: 127.0.0.1, server: , request: "GET /foo?testparam=thisisatestofmodsecurity HTTP/1.1", host: "localhost" 2019/11/18 14:02:09 [info] 25879#25879: *11 client 127.0.0.1 closed keepalive connection

參考

       ModSecurity:一款優秀的開源WAF

    https://www.freebuf.com/sectool/211354.html

  Installing NGINX WAF

     https://docs.nginx.com/nginx-waf/admin-guide/nginx-plus-modsecurity-waf-installation-logging/#

原文出處:https://www.cnblogs.com/Hi-blog/p/ModSecurity.html

相關文章
相關標籤/搜索