Loading... modsecurity是一个国外的开源WAF,前身只支持Apache,后来因为各种因素也开始支持Nginx了。 <!-- more --> 但v2版本一直以来饱受诟病的是性能问题,高性能的Nginx配上了modsecurity v2就变成了低性能。 modsecurity v3的诞生解决了v2的性能问题,同时还有一些别的更新,这篇文章记录一下在Debian10上把modsecurity v3加载到APT安装的Nginx内 ## 所需依赖: ```shell apt -y install build-essential autoconf zlib1g-dev pkgconf \ libtool libpcre++-dev libssl-dev libgeoip-dev libgd-dev \ libxml2-dev liblmdb-dev libyajl-dev libxslt1-dev \ libcurl4-openssl-dev curl git ``` ### 编译安装libmodsecurity: ```shell cd /opt git clone https://github.com/SpiderLabs/ModSecurity cd ModSecurity git submodule init git submodule update ./build.sh ./configure make -j$(nproc) make install ``` ## 准备工作: 查看当前的Nginx版本以及编译时用到的参数: ```shell nginx -V ``` 从Nginx官网下载同版本的源码/解压/进入到源码目录: ```shell cd /opt git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx wget https://nginx.org/download/nginx-1.20.1.tar.gz tar -xzvf nginx-1.20.1.tar.gz cd nginx-1.20.1 ``` 注意 国内可能无法拉取 这时候需要准备一个梯子 自建或其他方式获取后 `shell`和`git`需要分别设置 ```shell #shell中开启http和https🪜 export http_proxy=http://127.0.0.1:1087 export https_proxy=http://127.0.0.1:1087 #shell取消🪜 使用下面命令或重启 unset http_proxy unset https_proxy #git中开启http和https🪜 git config --global http.proxy http://127.0.0.1:1087 git config --global https.proxy http://127.0.0.1:1087 #git取消🪜 使用下面命令或重启 git config --global --unset http.proxy git config --global --unset https.proxy ``` ### 配置nginx ```shell ./configure --add-dynamic-module=/opt/ModSecurity-nginx \ --<除去原Nginx的所有add-module参数后,剩下的所有编译参数都应该加在这里> ``` ### Nginx配置查询 Debian10 使用apt安装的nginx-1.20.1 `nginx -V`输出如下信息 可作为参考 ```shell nginx version: nginx/1.20.1 built by gcc 8.3.0 (Debian 8.3.0-6) built with OpenSSL 1.1.1d 10 Sep 2019 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-http_v2_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.1/debian/debuild-base/nginx-1.20.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' ``` ### 加入ModSecurity组件 结合**ModSecurity-nginx**配置和apt配置 如下 ```shell ./configure \ --add-dynamic-module=../ModSecurity-nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-http_v2_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.1/debian/debuild-base/nginx-1.20.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' ``` ### 编译modsecurity连接器模块: ```shell make modules ``` 完成之后把`modsecurity`连接器模块复制到`nginx`的模块加载目录: ```shell cp objs/ngx_http_modsecurity_module.so /usr/lib/nginx/modules/ ``` ### 编辑Nginx主配置文件: ```shell nano /etc/nginx/nginx.conf ``` 文件的顶行加入: ```shell load_module modules/ngx_http_modsecurity_module.so; ``` 现在应该测试一下看Nginx的配置有没报错: ```shell nginx -t ``` ### 配置WAF规则了 没问题的话,现在就可以配置WAF规则了,owasp是modsecurity维护的一个额外规则: ```shell mkdir /etc/nginx/modsec && cd /etc/nginx/modsec git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git mv /etc/nginx/modsec/owasp-modsecurity-crs/crs-setup.conf.example /etc/nginx/modsec/owasp-modsecurity-crs/crs-setup.conf ``` `modsecurity.conf-recommended`这个文件也要重命名并移动到相关目录,`unicode.mapping`也需要移动,否则`Nginx`启动的时候会报错 最后修改:2024 年 01 月 20 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 81 如果觉得我的文章对你有用,请随意赞赏
2 条评论
对于modsecurity 主打一个误报高,对此有一个国货之光 长亭 雷池 Safeline 是非常炸裂的一个WAF。将出一篇文章详细介绍和安装使用
期待 期待