御渊WAF 是一个基于 Lua 和 Nginx (OpenResty) 的高性能 Web 应用防火墙,专注于防爬虫、IP地理位置过滤和常见Web攻击防护。
✅ 高性能 - 基于OpenResty,QPS可达5000+
✅ 易部署 - 5分钟快速部署,无需额外依赖
✅ 可定制 - 灵活的规则引擎,支持自定义规则
✅ 轻量级 - 单worker进程内存占用<200MB
✅ 开源免费 - MIT许可证,商业友好
- SQL注入防护 - 多层次检测,支持多种数据库语法
- XSS防护 - 反射型、存储型、DOM型全覆盖
- 命令注入防护 - Shell命令和代码注入检测
- 文件包含防护 - LFI/RFI检测和防护
- 路径遍历防护 - 防止目录遍历攻击
- 敏感文件访问控制 - 保护配置文件和备份文件
- 多维度检测
- User-Agent 识别 (10000+爬虫规则)
- 行为分析 (请求频率、路径模式)
- HTTP指纹识别
- 蜜罐陷阱
- 智能防护策略
- JS挑战
- 验证码验证
- 阶梯式封禁
- 合法爬虫白名单 (Google, Bing等)
- 国家级和城市级精确过滤
- 支持黑白名单模式
- CIDR范围过滤
- IPv4/IPv6全支持
- 高性能缓存 (命中率>95%)
- 基于 MaxMind GeoLite2
- 多维度限流
- 单IP限流
- URI限流
- 全局限流
- 灵活的算法
- 令牌桶算法
- 滑动窗口算法
- 漏桶算法
- CC攻击防护
- 实时流量分析
- 自动封禁
- 动态阈值调整
- 单机 QPS > 100,000
- 平均延迟 < 1ms (缓存命中)
- CPU占用 < 10%
- 多层缓存 - shared_dict + Redis
- 异步处理 - 日志和统计异步化
重要提示: 本项目进行了全面的安全加固。敏感信息(密码、API令牌等)应该通过环境变量传入,而不是存储在配置文件中。
- ✅ 敏感信息通过环境变量管理(Redis密码、API令牌等)
- ✅ 配置文件模板与实际配置分离(.example模式)
- ✅ 自动检测并拒绝硬编码的敏感信息
- ✅ Git自动忽略敏感文件和配置
- ✅ IP代理头验证防止IP欺骗
- ✅ 完整的部署安全指南
- OpenResty >= 1.19.3.1
- LuaJIT >= 2.1
- 操作系统: Linux / macOS / FreeBSD
- 安装 OpenResty
# macOS
brew install openresty/brew/openresty
# Ubuntu/Debian
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
sudo apt-get -y install software-properties-common
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install -y openresty- 安装依赖
sudo luarocks install lua-cjson
sudo luarocks install lua-resty-redis
sudo luarocks install lua-resty-maxminddb # GeoIP支持- 下载御渊WAF
cd /usr/local
git clone https://github.com/yourusername/YuyuanWaf.git
cd YuyuanWaf- 配置WAF
# 修改配置文件中的路径
vi conf/waf.conf
vi lua/config.lua- 配置Nginx
在Nginx配置中添加:
# nginx.conf
http {
# 包含WAF配置
include /usr/local/YuyuanWaf/conf/waf.conf;
server {
listen 80;
server_name example.com;
# 启用WAF
access_by_lua_block {
waf.run()
}
location / {
proxy_pass http://backend;
}
}
}- 启动Nginx
# 测试配置
sudo /usr/local/openresty/nginx/sbin/nginx -t
# 启动
sudo /usr/local/openresty/nginx/sbin/nginx详细安装说明请查看 INSTALL.md
-- lua/config.lua
-- WAF运行模式
mode = "protection", -- off | detection | protection
-- IP过滤
ip_filter = {
enabled = true,
blacklist_file = "rules/ip_blacklist.txt",
},
-- GeoIP过滤
geoip = {
enabled = true,
blacklist_countries = {"KP", "IR"}, -- 屏蔽国家
},
-- 防爬虫
anti_crawler = {
enabled = true,
score_threshold = 70,
action = "challenge", -- log | challenge | captcha | block
},
-- 频率限制
rate_limit = {
enabled = true,
per_ip = {
rate = 10, -- 每秒10次
burst = 20, -- 突发20次
},
},
-- 攻击防护
attack_defense = {
enabled = true,
sql_injection = {enabled = true},
xss = {enabled = true},
command_injection = {enabled = true},
},# rules/ip_blacklist.txt
192.168.1.100
10.0.0.0/8
# rules/ip_whitelist.txt
127.0.0.1
192.168.1.0/24# rules/country_blacklist.txt
KP # 朝鲜
IR # 伊朗
SY # 叙利亚anti_crawler = {
enabled = true,
score_threshold = 70,
action = "challenge",
ua_check = {enabled = true},
behavior_analysis = {
enabled = true,
request_threshold = 100, -- 每分钟100次
},
js_challenge = {enabled = true},
}geoip = {
enabled = true,
blacklist_countries = {"KP", "IR", "SY"},
-- 或使用白名单模式(仅允许特定国家访问)
whitelist_mode = false,
whitelist_countries = {"CN", "US", "JP"},
}rate_limit = {
enabled = true,
per_ip = {
rate = 10,
burst = 20,
},
},
cc_defense = {
enabled = true,
threshold = 10000, -- QPS阈值
action = "challenge",
auto_ban = {
enabled = true,
duration = 3600, -- 封禁1小时
},
}attack_defense = {
enabled = true,
sql_injection = {
enabled = true,
check_args = true,
check_post = true,
},
xss = {
enabled = true,
check_args = true,
check_post = true,
},
}基于实际测试数据:
| 指标 | 数值 |
|---|---|
| 单机QPS | > 100,000 |
| 平均延迟 (缓存命中) | < 1ms |
| 平均延迟 (缓存未命中) | < 5ms |
| CPU占用 | < 10% |
| 内存占用 | < 200MB |
| 缓存命中率 | > 95% |
查看 ROADMAP.md 了解详细的开发计划。
- WAF核心引擎
- 配置管理系统
- IP黑白名单
- 基础日志系统
- SQL注入防护
- XSS防护
- 命令注入防护
- 文件包含防护
- 规则引擎
- UA检测
- 行为分析
- 频率限制
- JS挑战/验证码
- 指纹识别
- GeoIP数据库集成
- 国家黑白名单
- IP段过滤
- 缓存优化
我们欢迎所有形式的贡献!
# 1. Fork 本仓库
# 2. 创建特性分支
git checkout -b feature/AmazingFeature
# 3. 提交更改
git commit -m 'feat: add some amazing feature'
# 4. 推送到分支
git push origin feature/AmazingFeature
# 5. 开启 Pull Request详见 贡献指南
- Issues - 报告问题
- 邮件列表 - [email protected]
感谢所有贡献者!
本项目采用 MIT 许可证 - 详见 LICENSE 文件
- ✅ 商业使用
- ✅ 修改源码
- ✅ 分发
- ✅ 私有使用
感谢以下项目的启发:
- ModSecurity
- NAXSI
- lua-resty-waf
感谢所有为本项目做出贡献的开发者!
- 问题反馈: GitHub Issues
- 讨论交流: GitHub Discussions
- 邮件: [email protected]
如果这个项目对你有帮助,请给一个 ⭐️
Made with ❤️ by YuyuanWaf Team
御渊WAF - 保护您的Web应用安全 🛡️
