Nginx使用Naxsi搭建Web應用防火墻(WAF),防xss、防註入×××
官網地址:https://github.com/nbs-system/naxsi
Naxsi 不要求任何特定的依賴,它需要的 libpcre ,libssl ,zlib ,gzip 這些 Nginx 已經集成了。
- 下載Naxsi模塊
[root@Super soft]# wget https://github.com/nbs-system/naxsi/archive/master.zip [root@Super soft]# tar zxvf naxsi-master.zip
- 重新編譯Nginx添加Naxsi模塊
[root@Super ~]# cd /opt/openresty/nginx/sbin/ #進入當前運行的目錄 [root@Super sbin]# ./nginx -V #查看之前編譯參數 nginx version: openresty/1.11.2.5 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-http_realip_module --with-pcre --with-http_ssl_module [root@Super sbin]# cd /opt/soft/openresty-1.11.2.5/ #進入源碼目錄 [root@Super openresty-1.11.2.5]# ./configure --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-http_realip_module --with-pcre --with-http_ssl_module --add-module=/opt/soft/naxsi-master/naxsi_src/ #重新編譯nginx,加入之前編譯的參數,再加入naxsi模塊,指定到naxsi的源碼目錄中的naxsi_src目錄 ...... ./configure: error: no /opt/soft/openresty-1.11.2.5/../ngx_devel_kit-0.3.0/config was found #報錯,openresty的./nginx -V中模塊路徑../xxx的都是openresty默認添加的,這些路徑是在根目錄的build下,編譯時候會自動加上 ERROR: failed to run command: sh ./configure --prefix=/opt/openresty/nginx/nginx \... [root@Super openresty-1.11.2.5]# [root@Super openresty-1.11.2.5]# ./configure --prefix=/opt/openresty/ --add-module=/opt/soft/naxsi-master/naxsi_src/ --with-luajit #只需要加這幾項 [root@Super openresty-1.11.2.5]# gmake [root@Super openresty-1.11.2.5]# find . -type f -iname nginx ./build/nginx-1.11.2/objs/nginx [root@Super openresty-1.11.2.5]# cp /opt/openresty/nginx/sbin/nginx{,.20180803bak} #這步很重要 [root@Super openresty-1.11.2.5]# cp ./build/nginx-1.11.2/objs/nginx /opt/openresty/nginx/sbin/nginx cp: overwrite ‘/opt/openresty/nginx/sbin/nginx’? y cp: cannot create regular file ‘/opt/openresty/nginx/sbin/nginx’: Text file busy [root@Super openresty-1.11.2.5]# killall -9 nginx [root@Super openresty-1.11.2.5]# cp build/nginx-1.11.2/objs/nginx /opt/openresty/nginx/sbin/ cp: overwrite ‘/opt/openresty/nginx/sbin/nginx’? y [root@Super openresty-1.11.2.5]# cd !$ cd /opt/openresty/nginx/sbin/ [root@Super sbin]# ./nginx -V nginx version: openresty/1.11.2.5 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/opt/openresty//nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --add-module=/opt/soft/naxsi-master/naxsi_src --with-http_ssl_module [root@Super sbin]#
復制Naxsi核心配置文件到nginx/conf下
[root@Super build]# cp /opt/soft/naxsi-master/naxsi_config/naxsi_core.rules /opt/openresty/nginx/conf/
在nginx.conf配置文件中添加Naxsi核心配置文件
[root@Super build]# cp /opt/openresty/nginx/conf/nginx.conf{,.20190804bak} [root@Super build]# vim /opt/openresty/nginx/conf/nginx.conf ...... http { include mime.types; include naxsi_core.rules; #加載naxsi 核心規則文件 default_type application/octet-stream; ...... } ......
配置Naxsi規則,新建文件naxsi.rules
[root@Super build]# vim /opt/openresty/nginx/conf/naxsi.rules
#LearningMode 啟用學習模式,即攔截請求後不拒絕訪問,只將觸發規則的請求寫入error_log選項定義的文件中。如果對規則產生的影響不太清楚,可以先設置為學習模式。啟用學習模式不能起到攔截非法請求的防禦的效果。建議先使用學習模式,規則測試完成後再啟用攔截模式。這樣可以避免出現對網站、服務器某些不可知的影響。
#SecRulesEnabled|SecRulesDisabled
SecRulesEnabled; #啟用Naxsi模塊
DeniedUrl "/RequestDenied"; #拒絕的請求發送到內部URL
#check rules 設置各規則不同的觸發閾值。 一旦該閾值觸發,請求將被阻塞。
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
#naxsi log
error_log logs/naxsi.log; #nginx相對路徑都是相對於根目錄
[root@Super build]#
虛擬主機添加支持Naxsi防×××
[root@Super build]# vim /opt/openresty/nginx/conf/nginx.conf
......
http {
......
server {
......
location / {
......
include naxsi.rules;
......
}
......
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /RequestDenied { #定義naxsi.rules中DeniedUrl返回的代碼
return 403;
}
error_page 403 /403.html;
location = /403.html {
root html;
}
......
}
......
}
......
root@Super build]# vim /opt/openresty/nginx/html/403.html
<html>
<head>
<title>Error 403 Request Denied</title>
</head>
<body>
<h2>Error 403 Request Denied</h2>
For some reasons,you request has been denied.
</body>
<</html>
[root@Super build]# /opt/openresty/nginx/sbin/nginx -t -c /opt/openresty/nginx/conf/nginx.conf
nginx: the configuration file /opt/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/openresty/nginx/conf/nginx.conf test is successful
[root@Super build]# cd /opt/openresty/nginx/sbin/
[root@Super sbin]# ./nginx -s reload
測試Naxsi
在一臺測試機上執行:curl http://192.168.100.127/?a=%3C
[root@localhost ~]# curl http://192.168.100.127/?a=%3C
<html>
<head>
<title>Error 403 Request Denied</title>
</head>
<body>
<h2>Error 403 Request Denied</h2>
For some reasons,you request has been denied.
</body>
<</html>
[root@localhost ~]#
nginx服務器日誌
[root@Super sbin]# tail -500f /opt/openresty/nginx/logs/naxsi.log
2018/08/04 21:53:03 [error] 4824#0: *7 NAXSI_FMT: ip=10.10.13.100&server=192.168.100.127&uri=/&learning=0&vers=0.56&total_processed=3&total_blocked=1&block=1&cscore0=$XSS&score0=8&zone0=ARGS&id0=1302&var_name0=a, client: 10.10.13.100, server: localhost, request: "GET /?a=%3C HTTP/1.1", host: "192.168.100.127"
如果錯誤日誌出現NAXSI_FMT的信息說明成功。
還可以添加白名單naxsi_BasicRule.conf
[root@Super sbin]# vim /opt/openresty/nginx/conf/naxsi_BasicRule.conf
BasicRule wl:0 "mz:$ARGS_VAR:script";
BasicRule wl:0 "mz:$ARGS_VAR:id";
[root@Super sbin]# ./nginx -s reload
表示xss×××正常是被攔截的,若被添加白名單,則不被攔截:此處是Get 參數名若為id 或者script,則不被攔截。
測試Naxsi
正常輸入http://192.168.100.127 可以正常訪問
在上面的地址後加上正常參數,例如:http://192.168.100.127/?id=1 或者 http://192.168.100.127/?id=1%20AND%201=1 返回的頁面和上面一樣,因為配置到白名單了
如果訪問http://192.168.100.127/?name=40//and//1=1 會提示Error 403 Request Denied,含有條件註入被攔截了
訪問http://192.168.100.127/?name=%28%29 會提示Error 403 Request Denied,含有特殊字符
白名單規則語法:
BasicRule wl:ID [negative] [mz:[$URL:target_url]|[match_zone]|[$ARGS_VAR:varname]|[$BODY_VARS:varname]|[$HEADERS_VAR:varname]|[NAME]]
wl:ID (White List ID) 哪些攔截規則會進入白名單
wl:0:把所有攔截規則加入白名單
wl:42:把ID為42的攔截規則加入白名單
wl:42,41,43:把ID為42, 41和43的攔截規則加入白名單
wl:-42:把所有攔截規則加入白名單,除了ID為42的攔截規則
mz:(Match Zones)
ARGS: GET的整個參數,如: foo=bar&in=%20
$ARGS_VAR: GET參數的參數名, 如:foo=bar&in=%20中的foo和in
$ARGS_VAR_X: 正則匹配的GET參數的參數名
HEADERS: 整個HTTP協議頭
$HEADERS_VAR: HTTP協議頭的名字
$HEADERS_VAR_X: 正則匹配的HTTP協議頭的名字
BODY: POST的整個參數內容
$BODY_VAR: POST參數的參數名
$BODY_VAR_X: 正則匹配的POST參數的參數名
URL: URL(?前的)
URL_X: 正則匹配的URL(?前的)
FILE_EXT: 文件名 (POST上傳文件時上傳的文件名)
Naxsi 社區提供了一些常用的白名單規則,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules 下載白名單規則。
Nginx使用Naxsi搭建Web應用防火墻(WAF),防xss、防註入×××