CentOS7.7下源码安装naxsi及配置策略
服务器操作系统:CentOS7.7 64位 需要安装依赖环境如下:
1.环境可以实现通过lnmp一件安装脚本进行部署,测试的话可以不用部署,如果一起安装过nginx,可以关闭nginx,然后备份配置文件,再重新编译加载naxsi模块
关闭已经启动nginx,这里如果没有nginx那么可以跳过此步骤,直接进行安装nginx
netstat -tulnp | grep "80"
pkill nginx
cp /usr/local/nginx/conf/nginx.conf /tmp/nginx.conf
cp /usr/local/nginx/conf/extra/app.conf /tmp/cntf.conf
rm -rf /usr/local/nginx*
2.下载naxsi,重新将此模块编译到nginx中
git clone https://github.com/nbs-system/naxsi.git
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd /usr/local/nginx-1.16.1
./configure --prefix=/usr/local/nginx-1.16.1 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-file-aio --with-http_dav_module --add-module=../naxsi/naxsi_src/
make && make install # 这里使用的版本编译安装会显示一个same file错误,这个不影响
ln -s /usr/local/nginx-1.16.1 /usr/local/nginx 添加nginx版本目录为软连接
cd /usr/local/nginx/conf/
mv nginx.conf nginx.conf.bak && egrep -v "^$|#" nginx.conf.bak >> nginx.conf # 这里是拿出非注释掉的配置
3.在nginx主配置文件中引入naxsi核心规则文件,这里要放在http里面
cp /usr/local/naxsi/naxsi_config/naxsi_core.rules /usr/local/nginx/conf/
vim /usr/local/nginx/conf/nginx.conf
添加如下内容:
user nginx;
worker_processes 1;
worker_rlimit_core 500M;
working_directory /tmp/;
error_log /var/log/nginx/naxsi.log;
events {
worker_connections 1024;
use epoll;
# multi_accept on;
}
http {
include /usr/local/nginx/conf/naxsi_core.rules; #引用核心规则
include /usr/local/nginx/conf/mime.types;
include /usr/local/nginx/conf/cntf.conf;
server_names_hash_bucket_size 128;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
}
复制好的naxsi_core.rules 不用更改
vim /usr/local/nginx/conf/naxsi.rules
添加如下内容:
SecRulesEnabled; # 启用naxsi
LearningMode; # 是否启用学习模式,只记录,不拦截,方便自己设置白名单
DeniedUrl "/50x.html";
# check rules
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
然后开始新建子配置文件,就是server的文件
vim /usr/local/nginx/conf/cntf.conf
添加如下配置信息:
server {
proxy_set_header Proxy-Connection "";
listen *:80;
access_log /tmp/nginx_access.log;
error_log /tmp/nginx_error.log debug;
location / {
include /usr/local/nginx/conf/naxsi.rules; #引用子规则
try_files $uri $uri/ =404;
}
location /RequestDenied { #配置拦截后拒绝访问时展示的页面
return 418;
error_page 500 502 503 504 /50x.html;
root html;
}
}
这里配置生成日志的位置也可以在naxsi.rules里面添加
上述如果没有什么问题,那么就可以将nginx添加到系统环境变量,方便使用
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx 添加nginx为软连接
4.测试nginx配置并启动
nginx -t
nginx
如果更改了配置,需要重启执行如下:
nginx -s reload
5.测试访问
http://42.159.83.201/?id=2%27or%201-3
![](https://img.haomeiwen.com/i6086910/ad9f6f23bcb5f04f.png)
网友评论