为什么选择 ngx_waf
基础防护:如 IP 或 IP 网段的黑白名单、URI 黑白名单和请求体黑名单等。
使用简单:配置文件和规则文件书写简单,可读性强。
高性能:使用高效的 IP 检查算法和缓存机制。
高级防护:兼容 ModSecurity (opens new window),因此你可以使用开放式网络应用安全项目(OWASP)® 的核心规则库 (opens new window)。
友好爬虫验证:支持验证 Google、Bing、Baidu 和 Yandex 的爬虫并自动放行,避免错误拦截。
验证码:支持三种验证码:hCaptcha、reCAPTCHAv2 和 reCAPTCHAv3。
功能
兼容 ModSecurity (opens new window)。此功能仅限最新的 Current 版本。
SQL 注入防护(Powered By libinjection (opens new window))。
XSS 攻击防护(Powered By libinjection (opens new window))。
支持 IPV4 和 IPV6。
支持开启验证码(CAPTCHA),支持 hCaptcha (opens new window)、reCAPTCHAv2 (opens new window)和 reCAPTCHAv3 (opens new window)。
支持识别友好爬虫(如 BaiduSpider)并自动放行(基于 User-Agent 和 IP 的识别)。此功能仅限最新的 Current 版本。
CC 防御,超出限制后自动拉黑对应 IP 一段时间或者使用验证码做人机识别。
IP 黑白名单,同时支持类似 192.168.0.0/16 和 fe80::/10,即支持点分十进制和冒号十六进制表示法和网段划分。
POST 黑名单。
URL 黑白名单
查询字符串(Query String)黑名单。
UserAgent 黑名单。
Cookie 黑名单。
Referer 黑白名单。
联系方式
Telegram 频道: https://t.me/ngx_waf(opens new window)
Telegram 群组(英文): https://t.me/group_ngx_waf(opens new window)
Telegram 群主(中文):https://t.me/group_ngx_waf_cn(opens new window)
打赏
打赏就算了,如果您愿意,您可以帮助宣传一下本项目。比如发个贴,推荐给身边有需求的人什么的。
我从来没碰过钱,我对钱没有兴趣。
测试套件
本项目使用一个 Perl 开发的数据驱动型的测试套件进行测试。 感谢项目 Test::Nginx (opens new window)及其开发者们。
你可以通过下列命令来运行测试。
# 这行命令的执行时间比较长,但是以后再测试的时候就不需要运行了。
cpan Test::Nginx
# 你需要指定一个临时目录。
# 如果目录不存在会自动创建。
# 如果目录已经会被存在则会先**删除**再创建。
export MODULE_TEST_PATH=/path/to/temp/dir
# 如果你安装了动态模块则需要指定动态模块的绝对路径,反之则无需执行这行命令。
export MODULE_PATH=/path/to/ngx_http_waf_module.so
cd ./test/test-nginx
sh ./init.sh
sh ./start.sh ./t/*.t
性能测试
性能测试
闲谈
nginx 防火墙模块开发总结(opens new window)
欢迎访问我的博客:https://www.addesp.com/ (opens new window)。
感谢
ModSecurity (opens new window):开源且跨平台的 WAF 引擎。
uthash (opens new window):C 语言的哈希表、数组、链表等容器库。
libcurl (opens new window):支持多种协议文件传输库。
cJSON (opens new window):C 语言的轻量级 JSON 解析库。
libinjection (opens new window):SQL 注入检测库。
libsodium (opens new window):C 语言密码函数库。
test-nginx (opens new window): 数据驱动的 nginx 测试套件,可用于 nginx C 模块的开发和 OpenResty Lua 库的开发。
lastversion (opens new window):一个轻巧的命令行工具,帮助你下载或安装一个项目的特定版本。
ngx_lua_waf (opens new window):一个基于 lua-nginx-module (openresty) 的 web 应用防火墙。
nginx-book (opens new window):Nginx开发从入门到精通
nginx-development-guide (opens new window):Nginx 开发指南。
版本说明
语义化版本
本项目遵循语义化版本 2.0.0 (opens new window)。
长期维护版(LTS)
长期维护版至少维护一年,并且维护期间只会修复 bug,特别的是严重的 bug,一些比较轻的 bug 可能不会修复。
这个版本在大多数情况下是稳定的,但是没有功能性更新。
git clone -b master https://github.com/ADD-SP/ngx_waf.git
# 或
git clone -b lts https://github.com/ADD-SP/ngx_waf.git
最新版(Current)
最新版的 bug 也会被修复,但是最新版包含了全部的更新,比如新功能、功能变动、功能删除、性能优化等。
这个版本不如 LTS 版稳定,但是会有功能性更新。
git clone -b current https://github.com/ADD-SP/ngx_waf.git
静态模块
重要信息
编译安装一个新的模块需要知道当前的 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,花费的时间相对于安装动态模块比较长。
首先下载对应版本的 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)。
宝塔面板安装
1.在 shell 中运行命令
# Centos7 yum update yum install libtool yum install https://archives.fedoraproject.org/pub/archive/fedora/linux/updates/23/x86_64/b/bison-3.0.4-3.fc23.x86_64.rpm yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel git libcurl libcurl-devel cd /usr/local/src git clone https://github.com/jedisct1/libsodium.git --branch stable cd libsodium ./configure --prefix=/usr/local/libsodium --with-pic make -j$(nproc) make install # 如果你使用 Current 版本请添加这些代码 # 安装 ModSecurity v3 cd /usr/local/src git clone -b v3.0.10 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 make -j$(nproc) make install # ======== 分割线 ======== # Ubuntu apt update apt install -y libsodium23 \ libsodium-dev \ libcurl4-openssl-dev \ git \ libmodsecurity-dev \ libmodsecurity3
2.在软件商店中卸载 nginx。
编辑文件 /etc/profile,在末尾追加下列内容。
export LIB_UTHASH=/www/server/nginx/src/uthash
# 如果操作系统的 Ubuntu 则不用写下面这两行 export LIB_SODIUM=/usr/local/libsodium export LIB_MODSECURITY=/usr/local/modsecurity
3.在 shell 中运行下列命令
source /etc/profile
4.清空宝塔面板缓存,重启宝塔面板,重新登录宝塔面板。
5.在软件商店中重新安装 nginx,安装方式选择「编译安装」。
6.选择「添加自定义模块」,填写好之后点击「提交」
模块名称:ngx_waf
模块描述:方便且高性能的 Nginx 防火墙模块
模块参数:
--add-module=/www/server/nginx/src/ngx_waf --with-cc-opt=-std=gnu99
前置脚本(LTS 版):
mkdir -p /www/server/nginx/src cd /www/server/nginx/src git clone -b lts https://github.com/ADD-SP/ngx_waf.git rm -rf /usr/local/src/ngx_waf cp -r ngx_waf /usr/local/src/ngx_waf cd ngx_waf make git clone https://github.com/libinjection/libinjection.git inc/libinjection cd /www/server/nginx/src git clone https://github.com/troydhanson/uthash.git inc/uthash
前置脚本(Current 版):
mkdir -p /www/server/nginx/src cd /www/server/nginx/src git clone -b current https://github.com/ADD-SP/ngx_waf.git rm -rf /usr/local/src/ngx_waf cp -r ngx_waf /usr/local/src/ngx_waf cd ngx_waf git clone -b v1.7.16 https://github.com/DaveGamble/cJSON.git lib/cjson git clone -b v2.3.0 https://github.com/troydhanson/uthash.git lib/uthash cd /www/server/nginx/src
这时你会看到 ngx_waf 已经添加进去了,在模块列表中打上勾之后点击「提交」等待安装完成。
安装成功后删除第三步中向文件 /etc/profile 中添加的内容。
报错了怎么办?
你可以选择去 Github 提 issue 或者自己解决,编译安装第三方 nginx 模块就要有自己解决问题的觉悟。
项目地址:
帮助文档
备用链接
可惜的是今夕没能用上遇到了编译错误无法解决
有没有成功的大佬带带我 (~ ̄▽ ̄)→
1.执行这段的时候会出现
./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-http_auth_request_module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/usr/local/src/ngx_waf
下面的错误
configuring additional modules adding module in /www/server/nginx/src/ngx_devel_kit + ngx_devel_kit was configured adding module in /www/server/nginx/src/lua_nginx_module checking for LuaJIT 2.x ... not found ./configure: error: unsupported LuaJIT version; ngx_http_lua_module requires LuaJIT 2.x.
2.这导致了编译的时候出现这段错误
&& make install \ && export LIB_MODSECURITY=/usr/local/modsecurity \ && cd /www/server/nginx/src make: *** No rule to make target `build', needed by `default'. Stop. make: *** No rule to make target `install'. Stop.
评论抢沙发