C0nw0nk/Nginx-Lua-Anti-DDoS

GitHub: C0nw0nk/Nginx-Lua-Anti-DDoS

一款基于 Lua 和 JavaScript 验证拼图的 Nginx Anti-DDoS 脚本,提供类似 Cloudflare「我正在遭受攻击模式」的应用层防护能力。

Stars: 1551 | Forks: 308

[![语言](https://img.shields.io/github/languages/count/C0nw0nk/Nginx-Lua-Anti-DDoS) ![主要语言](https://img.shields.io/github/languages/top/C0nw0nk/Nginx-Lua-Anti-DDoS) ![文件大小](https://img.shields.io/github/size/C0nw0nk/Nginx-Lua-Anti-DDoS/lua/anti_ddos_challenge.lua)](https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/wiki/funding) [![构建并发布 RPM/DEB 软件包](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/d3788cd8a0104040.svg)](https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/actions/workflows/build-publish.yml) [![Cloudflare 我正在遭受攻击模式!](https://blog.cloudflare.com/content/images/im_under_attack_page.png.scaled500.png)](https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/wiki/funding) [适用于现代 Nginx Lua 构建的 Master 分支](https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/tree/master) - [旧版过时的 Nginx Lua 构建请使用此分支](https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/tree/old-outdated-nginx-lua-builds) # Nginx-Lua-Anti-DDoS 这是一个使用 Lua 保护 Nginx Web 服务器的 Anti-DDoS 脚本,采用基于 Javascript 的验证拼图,灵感来源于 Cloudflare 的“我正在遭受攻击”模式。我结合了自己的 Lua、Javascript、HTML 和 HTTP 知识,构建了自己的 Anti-DDoS 验证 HTML 页面拼图。 使用我的免费 DDoS 保护来缓解任何规模的 DDoS 攻击。不要被 DDoS 攻击! 如果你在攻击期间使用我的脚本,访客将会收到一个大约持续五秒的中间页面,在此期间我会分析流量以确保它是合法的人类访客。 这可以保护你免受多种形式的 DDoS 攻击,同时适用于 HTTP 和 HTTPS / SSL 流量。 攻击规模无限制 正常运行时间保证 # 功能 : 这些是我目前在该脚本中内置的部分功能。 ## 安全性 限制 IP 请求 / 泛洪攻击 如果检测到 DDoS,自动开启“遭受攻击”模式 我正在遭受攻击模式(DDoS 验证 HTML 页面) IP 地址白名单 IP 子网范围白名单 IP 地址黑名单 IP 子网范围黑名单 User-Agent 白名单 User-Agent 黑名单 保护区 / 限制访问字段 用户名 / 密码框,以限制对站点 / 路径的访问。 启用或禁用对验证拼图解题失败或成功的用户进行日志记录。(Fail2Ban 用户可以利用此功能从日志文件中封禁机器人、AI 工具和 IP 地址) Range 头过滤 大多数下载 / 视频流媒体站点和服务使用 range 头,这允许你过滤和阻断 slowhttp / slowloris 类型的攻击 ## WAF (Web 应用防火墙) IPv4 和 IPv6 阻断和白名单,包括子网范围。 User-Agent 阻断和白名单,以阻止恶意机器人和漏洞利用 / 扫描器。 能够检查 POST 数据 / 字段并阻止恶意的 POST 请求 / 漏洞利用。 能够检查 URL 是否包含恶意内容,如 SQL/SQI 注入、XSS 攻击 / 漏洞利用。 能够检查查询字符串和参数是否包含恶意内容 / 漏洞利用。 能够检查客户端连接提供的所有请求头。 能够检查 cookies 是否存在漏洞利用。 ## 缓存 速度和性能 查询字符串排序 查询字符串白名单 查询字符串移除(这是一个黑名单,但它只会从 URL 中删除 / 移除参数,而不会阻止请求) 文件压缩 / 缩小,移除空白字符和无效代码 / 行,如 JS JavaScript, CSS 样式表, HTML 等 ## 自定义错误页面响应和网页输出 自定义错误页面拦截,用你自己的错误页面替换 隐藏 Web 应用程序错误,例如 PHP 错误、MySQL 错误,它会拦截它们并显示自定义错误页面,而不是向访客展示敏感信息 修改网页输出以替换页面 / 文件中的内容 # 信息 : 如果你有任何 bug、问题或困难,请提交一个 Issue 请求。 https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/issues 如果你 fork 或进行了任何更改以改进此项目或修复问题,请务必为同样使用此项目的社区提交一个 pull request。 https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/pulls ## 请确保使用最新的 Nginx+Lua 构建版本和库,以避免任何问题。 # 用法 / 安装 : 编辑 `anti_ddos_challenge.lua` 中的设置以满足你自己的独特需求或改进我的工作。(请分享你的解决方案和补充) https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/blob/master/lua/anti_ddos_challenge.lua 将此添加到你的 Nginx 配置文件夹中。 `nginx/conf/lua/` 一旦安装到你的 `nginx/conf/` 文件夹中。 将此添加到你的 HTTP 块中,或者根据你希望此脚本运行的位置(单个 location、整个 server 还是服务器上的每一个网站),将其放在 server 或 location 块中。 ``` lua_shared_dict antiddos 70m; #Anti-DDoS shared memory zone to track requests per each unique user lua_shared_dict antiddos_blocked 70m; #Anti-DDoS shared memory where blocked users are put lua_shared_dict ddos_counter 10m; #Anti-DDoS shared memory zone to track total number of blocked users lua_shared_dict jspuzzle_tracker 70m; #Anti-DDoS shared memory zone monitors each unique ip and number of times they stack up failing to solve the puzzle access_by_lua_file anti_ddos_challenge.lua; ``` ### 示例 nginx.conf : 这将在 nginx 服务器上的所有网站上运行 ``` http { #shared memory addresses in http block lua_shared_dict antiddos 70m; #Anti-DDoS shared memory zone to track requests per each unique user lua_shared_dict antiddos_blocked 70m; #Anti-DDoS shared memory where blocked users are put lua_shared_dict ddos_counter 10m; #Anti-DDoS shared memory zone to track total number of blocked users lua_shared_dict jspuzzle_tracker 70m; #Anti-DDoS shared memory zone monitors each unique ip and number of times they stack up failing to solve the puzzle #nginx config settings etc access_by_lua_file anti_ddos_challenge.lua; #more config settings and some server stuff } ``` 这将使其仅在此网站上运行 ``` http { #shared memory addresses in http block lua_shared_dict antiddos 70m; #Anti-DDoS shared memory zone to track requests per each unique user lua_shared_dict antiddos_blocked 70m; #Anti-DDoS shared memory where blocked users are put lua_shared_dict ddos_counter 10m; #Anti-DDoS shared memory zone to track total number of blocked users lua_shared_dict jspuzzle_tracker 70m; #Anti-DDoS shared memory zone monitors each unique ip and number of times they stack up failing to solve the puzzle } server { #nginx config settings etc access_by_lua_file anti_ddos_challenge.lua; #more config settings and some server stuff } ``` 这将仅在此 location 块中运行 ``` http { #shared memory addresses in http block lua_shared_dict antiddos 70m; #Anti-DDoS shared memory zone to track requests per each unique user lua_shared_dict antiddos_blocked 70m; #Anti-DDoS shared memory where blocked users are put lua_shared_dict ddos_counter 10m; #Anti-DDoS shared memory zone to track total number of blocked users lua_shared_dict jspuzzle_tracker 70m; #Anti-DDoS shared memory zone monitors each unique ip and number of times they stack up failing to solve the puzzle } location / { #nginx config settings etc access_by_lua_file anti_ddos_challenge.lua; #more config settings and some server stuff } ``` ### 其他设置选项 https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/wiki 有关设置脚本与 Tor .onion 服务、Cloudflares 代理服务一起运行,以及脚本的配置选项,请查看 wiki。 # 需求 : 无!:D 你只需要 Nginx + Lua 即可使用我的脚本。 ###### 哪里可以下载 Nginx + Lua ? Openresty 在此处提供了适用于 Windows Linux 等的 Nginx + Lua 构建版本。 https://openresty.org/en/download.html Nginx4windows 在此处提供了带有 Lua 的 Windows 特定构建版本。 http://nginx-win.ecsds.eu/ 或者你可以在此处下载 Nginx 的源代码,并自己编译带有 Lua 的 Nginx。 https://nginx.org/en/download.html # 关于 : 我受到 Cloudflare 功能“我正在遭受攻击模式”的启发而创建了此项目 https://www.cloudflare.com/ 虽然有类似 BitMitigate 这样的站点和服务,但我更喜欢我自己的脚本而不是他们的方法。 ``` If you're under attack and have this feature enabled during the attack, visitors will receive an interstitial page for about five seconds while we analyze the traffic to make sure it is a legitimate human visitor. Advanced DDoS Attack Protection Unmetered DDoS mitigation to maintain performance and availability Denial of Service attacks continue to grow in sophistication and force: more distributed, greater volumes of traffic, and encroaching on the application layer. A successful attack increases unnecessary costs on your infrastructure and IT/security staff. More importantly, it hurts your revenue, customer satisfaction, and brand. To combat attacks and stay online, you’ll need a solution that’s resilient scalable, and intelligent. Mitigate a DDoS attack of any size or duration, Don't get ddos attacked! ``` 我非常喜欢那个功能,除了在我所有通过 Cloudflare 代理的站点上启用它之外,我决定将其作为我自己服务器上的一个功能,这样即使流量不来自 Cloudflare 网络,也能得到控制和验证!(点点滴滴都有帮助,对吧!) 感谢 @Cloudflare 的灵感以及你们社区的所有关爱,非常感谢 @openresty 社区,你们太棒了,Lua 太棒了,你们都非常了不起! 让我们一起构建一个更好的互联网!速度、隐私、安全和压缩都至关重要! 以下是我最喜欢的社区的链接 :) http://openresty.org/en/ https://community.cloudflare.com/ # 可抵御的攻击类型 : ``` All Layer 7 Attacks Mitigating Historic Attacks DoS DoS Implications DDoS All Brute Force Attacks Zero day exploits Social Engineering Rainbow Tables Password Cracking Tools Password Lists Dictionary Attacks Time Delay Any Hosting Provider Any CMS or Custom Website Unlimited Attempt Frequency Search Attacks HTTP Basic Authentication HTTP Digest Authentication HTML Form Based Authentication Mask Attacks Rule-Based Search Attacks Combinator Attacks Botnet Attacks Unauthorized IPs IP Whitelisting Bruter THC Hydra John the Ripper Brutus Ophcrack unauthorized logins Injection Broken Authentication and Session Management Sensitive Data Exposure XML External Entities (XXE) Broken Access Control Security Misconfiguration Cross-Site Scripting (XSS) Insecure Deserialization Using Components with Known Vulnerabilities Insufficient Logging & Monitoring And many others… ``` # 功能 : # 高级 DDoS 攻击防护 我的脚本为你提供不限流量的 DDoS 缓解,以免费维持性能和可用性 拒绝服务攻击在复杂程度和力度上不断增长:更加分布式、更大的流量体积,并且正在侵入应用层。 一次成功的攻击会增加你的基础设施和 IT/安全人员的不必要成本。更重要的是,它会损害你的收入、客户满意度和品牌。 为了对抗攻击并保持在线,你需要一个具有弹性、可扩展且智能的解决方案。 #### 常见类型的 DDoS 攻击 # 阻止恶意 Bot 滥用 阻止滥用 Bot 通过内容抓取、欺诈性结账和账户接管来破坏互联网资产。 # 防止客户数据泄露 防止攻击者破坏敏感的客户数据,例如用户凭证、信用卡信息和其他个人身份信息。 #### 分层安全防御 分层安全方法将多种 DDoS 缓解功能结合到一个服务中。它可以防止恶意流量造成的中断,同时允许良性流量通过,保持网站、应用程序和 API 的高可用性和高性能。 #### HTTP Flood (第 7 层) HTTP Flood 攻击从多个来源产生大量 HTTP、GET 或 POST 请求,针对应用层,导致服务降级或不可用。 抵御最大规模的攻击 # 共享网络情报 / 集体情报 随着每一个新资产、贡献者和使用此脚本的人,你的帮助和对本脚本的贡献让每个人的网络都更加安全。你正在帮助识别和阻断整个互联网骨干 / 基础设施中不断演变的新威胁。 # 没有性能妥协 通过将我的脚本集成到你的服务器中,消除安全引起的延迟。你不再需要依赖像 Cloudflare、BitMitigate、Sucuri 或其他此类 CDN 云分布式网络或公司这样的第三方服务,我已经免费为你提供了这个工具。 # Web 应用防火墙 企业级 Web 应用防火墙 (WAF) 保护你的互联网资产免受常见漏洞(如 SQL 注入攻击、跨站脚本攻击和跨站请求伪造)的侵害,并保护你现有的基础设施。 # 速率限制 控制以阻止可疑访客 速率限制可防止拒绝服务攻击、暴力登录尝试以及针对应用层的其他类型的滥用行为。 速率限制提供了配置阈值、定义响应以及深入了解网站、应用程序或 API 端点的特定 URL 的能力。它增加了精细的 HTTP/HTTPS 流量控制。这也通过消除不可预测的流量峰值或攻击来降低带宽成本。 # 保护任何 Web 应用程序 此脚本可以保护所有已构建的 Web 应用程序。 ``` Drupal WordPress Joomla Flash Magento PHP Plone WHMCS Atlassian Products Adult video script avs KVS Kernel Video Sharing Clip Bucket Tube sites Content Management Systems Social networks scripts backends proxy proxies PHP Python Porn sites xxx adult gaming networks servers sites forums vbulletin phpbb mybb smf simple machines forum xenforo web hosting And many more... ``` # 政府 政府网关和网站的保护。随着外国机构针对关键基础设施,这将帮助所有政府和关键民用基础设施保持在线。 # 支付 电子商务 内容管理 如果你使用 Joomla、Drupal、Wordpress、phpbb、mybb、vbulletin 等流行的 CMS 或论坛软件,这将确保最大的正常运行时间和保护。 # 军事 MoD 基础设施的军事级保护。MoD 国防部 / 武装部队网站。保护警察和军队核心或执法部门。 # 加密货币 此脚本非常适合加密货币站点,因为基于钱包控制的性质以及加密网站的访问权限,它在允许访问之前会验证流量是否可以运行 javascript 并且是合法的,从而保护像钱包访问这样的敏感内容。每一个拥有 swap 或 dex / cex(中心化或去中心化交易所)的加密网站都会发现这是其加密交易者之间直接进行交易的点对点市场中必不可少的要求。 # Tor 网络 / Project .onion : 你也可以使用此脚本来保护 Tor 网络上的服务器和站点,防止 .onion 链接遭受 ddos。它可以帮助阻止对深网 / 暗网的攻击,以及对于通过 tor 浏览器浏览
标签:CC 攻击防御, CISA项目, DDoS 防护, IP 白名单, JS 挑战, Layer 7, Lua, Nginx, rizin, Under Attack Mode, WAF, Web 安全, Web 服务器, 中间件, 人机验证, 反爬虫, 多模态安全, 应用层防护, 开源安全工具, 恶意流量过滤, 数据可视化, 暴力破解防护, 流量清洗, 渗透测试防御, 网络安全, 逆向工程平台, 配置错误, 隐私保护