产品信息简介¶
| 名称 | 开源协议 | 开发语言 | 开源时间 | 活跃度 | 代码质量 | 最新版本 | 厂商 |
|---|---|---|---|---|---|---|---|
| ngx_waf | BSD 3-Clause License | C | 2020-08-18(1.0.0) | 983⭐️ | A | 2022-01-07 (6.1.8) | 个人开发者 |
产品描述¶
方便、高性能、兼容 ModSecurity 的 Nginx 防火墙模块.实现基础的防护,即 IP 检测、Url 检测、Get 参数检测、Cookie 检测、Post 检测、Referer 检测和 CC 防御. 具体参考开发者blog nginx 防火墙模块开发总结
安装¶
兼容性说明¶
- 操作系统:Linux
- Nginx兼容:本模块只保证对
nginx-1.18.0或更新的版本的兼容性。 - 模块兼容性问题:本模块与 ngx_http_rewrite_module (opens new window)存在兼容性问题。
静态模块¶
注意 编译安装模块可能需要一些依赖,比如 gcc,请自行解决依赖问题,本文不提供这类信息。
重要信息
编译安装一个新的模块需要知道当前的 nginx 的
configure脚本的参数,您可以通过运行nginx -V来获取。 下面是一个例子。
nginx version: nginx/1.19.6
built by gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
built with OpenSSL 1.1.1i 8 Dec 2020
TLS SNI support enabled
configure arguments: --with-mail=dynamic --with-openssl=/usr/local/src/openssl-OpenSSL_1_1_1i --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_geoip_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module --with-http_stub_status_module --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O3 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'
务必记住 configure arguments: 后面的内容,下文中将使用 ARG 来代替这块内容。
- 首先下载对应版本的 nginx,下载页面 (opens new window)。 下面将以
nginx-1.20.1为例。
cd /usr/local/src
wget https://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxf nginx-1.20.1.tar.gz
- 然后下载本模块的源码,下文将使用稳定版的源码。
cd /usr/local/src
git clone -b lts https://github.com/ADD-SP/ngx_waf.git
- 接下来应该运行配置脚本。
cd /usr/local/src/nginx-1.20.1
./configure ARG --add-module=/usr/local/src/ngx_waf
sed -i 's/^\(CFLAGS.*\)/\1 -fstack-protector-strong -Wno-sign-compare/' objs/Makefile
- 开始编译
# 不使用并行编译
make
# 使用并行编译
make -j$(nproc)
- 关闭 nginx,然后替换 nginx 的二进制文件, 此处假设 nginx 的二进制文件的绝对路径为
/usr/local/nginx/sbin/nginx。
cp objs/nginx /usr/local/nginx/sbin/nginx
热部署 如果您不想在替换二进制文件时关闭 nginx,可以参考官方文档的热部署方案 (opens new window)。
动态模块¶
下载预构建的模块¶
您可以通过执行脚本 assets/download.sh 来下载动态模块。下面是一些用例。
# 用于 nginx-1.20.1 的 LTS 版的模块
sh assets/download.sh 1.20.1 lts
# 用于 nginx-1.21.1 的 LTS 版的模块
sh assets/download.sh 1.21.1 lts
# 用于 nginx-1.20.1 的最新版的模块
sh assets/download.sh 1.20.1 current
# 用于 nginx-1.21.1 的最新版的模块
sh assets/download.sh 1.21.1 current
执行脚本后你会看到类似下面这样的输出。
checking for command ... yes
checking for libc implementation ... yes
+ GNU C libary
Pulling remote image addsp/ngx_waf-prebuild:ngx-1.21.1-module-beta-glibc
......
......
......
Download complete!
如果你看到 Download complete! 则说明下载成功,模块会被保存在当前目录下。 你可以将其拷贝到一个目录下,然后在 nginx.conf 的顶部添加一行。
load_module "/path/to/ngx_http_waf_module.so";
然后关闭 nginx 并运行 nginx -t。如果没有出错则说明模块被正常加载,反之则说明您的 nginx 不支持预构建的模块,请编译安装模块。
编译动态模块¶
编译安装动态模块并不需要重新编译整个 nginx,只需要重新编译所有的模块,所以 速度相对静态模块快一些,这也是本文档推荐的方式。
下载 nginx 源码和模块源码的过程同静态模块,不再赘述。
运行配置脚本
./configure --add-dynamic-module=/usr/local/src/ngx_waf --with-compat
sed -i 's/^\(CFLAGS.*\)/\1 -fstack-protector-strong -Wno-sign-compare/' objs/Makefile
然后开始编译动态模块
make modules
接着您应该关闭 nginx,然后将动态模块拷贝到模块目录, 此处假设模块目录的绝对路径为 /usr/local/nginx/modules。
cp objs/*.so /usr/local/nginx/modules
最后在 nginx 的配置文件顶部添加一行
load_module "/usr/local/nginx/modules/ngx_http_waf_module.so";
cd /usr/local/src \
&& wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz -O libmaxminddb.tar.gz && mkdir libmaxminddb \
&& tar -zxf "libmaxminddb.tar.gz" -C libmaxminddb --strip-components=1 \
&& cd libmaxminddb \
&& ./configure --prefix=/usr/local/libmaxminddb \
&& make -j $(nproc) \
&& make install \
&& cd /usr/local/src \
&& git clone -b v3.0.5 https://github.com/SpiderLabs/ModSecurity.git \
&& cd ModSecurity \
&& chmod +x build.sh \
&& ./build.sh \
&& git submodule init \
&& git submodule update \
&& ./configure --prefix=/usr/local/modsecurity --with-maxmind=/usr/local/libmaxminddb \
&& make -j $(nproc) \ \
&& make install \
&& export LIB_MODSECURITY=/usr/local/modsecurity \
&& cd /usr/local/src/nginx-1.22.0
ModSecurity - v3.0.5 for Linux
Mandatory dependencies
+ libInjection ....v3.9.2-46-gbfba51f
+ SecLang tests ....a3d4405
Optional dependencies
+ GeoIP/MaxMind ....found
* (MaxMind) v
/usr/local/libmaxminddb/lib//libmaxminddb.so, /usr/local/libmaxminddb/include, -DWITH_MAXMIND -I/usr/local/libmaxminddb/include
+ LibCURL ....not found
+ YAJL ....not found
+ LMDB ....not found
+ LibXML2 ....not found
+ SSDEEP ....not found
+ LUA ....not found
Other Options
+ Test Utilities ....disabled
+ SecDebugLog ....enabled
+ afl fuzzer ....disabled
+ library examples ....enabled
+ Building parser ....disabled
+ Treating pm operations as critical section ....disabled
备注 1. 测试服务器集群都安装了Nginx服务,需要使用虚拟机搭建 2. 安装Nginx参照Nginx1.22.0安装 3. 自研WAF之路——Modsecurity安装教程 - FreeBuf网络安全行业门户 4. ModSecurity停止维护