前言
因工作原因,接觸到了WAF,今天部署了一下Naxsi,記錄一下
GitHub
正文
環境
Centos 7
下載
更新yum
yum update -y
安裝必要依賴
yum install gcc gcc-c++ cmake ncurses ncurses-devel libxml2 libxml2-devel zlib zlib-devel gd gd-devel openssl openssl-devel curl curl-devel libtool pcre pcre-devel wget unzip vim
下載Nginx
可使用最新版
wget http://nginx.org/download/nginx-1.17.8.tar.gz
下載Naxsi
可使用最新版
wget https://github.com/nbs-system/naxsi/archive/0.56.tar.gz
解壓
tar -xvzf nginx-1.17.8.tar.gz
tar -xvzf 0.56.tar.gz
編譯安裝帶插件的Nginx
cd nginx-1.17.8
注意 --add-module 后面跟的路徑
./configure --prefix=/opt/nginx --add-module=/root/naxsi/naxsi-0.56/naxsi_src --user=nginx --group=nginx --with-http_ssl_module --with-http_geoip_module --without-mail_pop3_module --without-mail_smtp_module --without-mail_imap_module --without-http_uwsgi_module --without-http_scgi_module
make
make install
mkdir /var/log/nginx
安裝Naxsi插件
cp /root/naxsi/naxsi-0.56/naxsi_config/naxsi_core.rules /opt/nginx/conf/
vim /opt/nginx/conf/naxsi.rules
寫入以下內容保存
SecRulesEnabled;
DeniedUrl "/RequestDenied";
## check rules
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
上面的內容是攔截的規則, naxsi的流程是將每個請求URL解析,發現一個可疑處增加一些分數,從2到8分都有,然后根據此文件的規則確定規則
比如 CheckRule "$SQL >= 8" BLOCK; 的意思是如果SQL部分分數大於等於8返回錯誤的狀態碼(404)
更多詳細可看 Naxsi規則簡單說明
開啟Naxsi插件
vim /opt/nginx/conf/nginx.conf
修改為
user nginx nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
include /opt/nginx/conf/naxsi_core.rules;
default_type application/octet-stream;
access_log off;
error_log /var/log/nginx/error.log;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6].(?!.*SV1)";
server {
listen 80;
server_name localhost;
location / {
include /opt/nginx/conf/naxsi.rules; # 開啟插件
proxy_pass http://127.0.0.1:3456; # 通過后轉發到內部監聽業務地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
開啟Nginx
測試
/opt/nginx/sbin/nginx -t
啟動
/opt/nginx/sbin/nginx
測試
使用Post測試
訪問業務地址
比如一個Python的接口,路由是 / ,接收Post和Get請求,直接返回 helloworld
訪問時帶上SQL注入
http://x.x.x.x/?q="><script>alert(1)</script>
返回404代表已成功攔截

去掉sql注入訪問返回200

