Convert Figma logo to code with AI

loveshell logongx_lua_waf

ngx_lua_waf是一个基于lua-nginx-module(openresty)的web应用防火墙

3,933
1,445
3,933
85

Top Related Projects

ModSecurity is an open source, cross platform web application firewall (WAF) engine for Apache, IIS and Nginx. It has a robust event-based programming language which provides protection from a range of attacks against web applications and allows for HTTP traffic monitoring, logging and real-time analysis.

High-performance WAF built on the OpenResty stack

Quick Overview

ngx_lua_waf is a web application firewall (WAF) implemented using Lua for NGINX. It provides protection against various web attacks, including SQL injection, XSS, and more. The project aims to offer a lightweight and efficient WAF solution that can be easily integrated with NGINX servers.

Pros

  • Easy integration with NGINX servers
  • Lightweight and efficient, with minimal performance impact
  • Customizable rules and configurations
  • Active development and community support

Cons

  • Requires Lua knowledge for advanced customization
  • May require fine-tuning to avoid false positives
  • Limited documentation compared to some commercial WAF solutions
  • Potential compatibility issues with certain NGINX modules

Code Examples

  1. Configuring basic WAF rules:
-- In config.lua
config_waf_enable = "on"
config_attack_log = "on"
config_log_dir = "/usr/local/nginx/logs/"
config_rule_dir = "/usr/local/nginx/conf/waf/wafconf/"
config_white_url_list = "white-url.rule"
config_black_url_list = "black-url.rule"
config_white_ip_list = "white-ip.rule"
config_black_ip_list = "black-ip.rule"
  1. Customizing WAF behavior:
-- In init.lua
local content_length = tonumber(ngx.req.get_headers()['content-length'])
local method = ngx.req.get_method()
if method == "POST" then
   if content_length > 10485760 then
      ngx.exit(ngx.HTTP_FORBIDDEN)
   end
end
  1. Adding a custom rule:
-- In user-agent.rule
(?i)(bot|spider|crawl|harvest)

Getting Started

  1. Clone the repository:

    git clone https://github.com/loveshell/ngx_lua_waf.git
    
  2. Copy the waf folder to your NGINX configuration directory:

    cp -R ngx_lua_waf/waf /usr/local/nginx/conf/
    
  3. Add the following to your NGINX configuration:

    lua_package_path "/usr/local/nginx/conf/waf/?.lua";
    init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
    access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
    
  4. Restart NGINX to apply the changes:

    nginx -s reload
    

Competitor Comparisons

ModSecurity is an open source, cross platform web application firewall (WAF) engine for Apache, IIS and Nginx. It has a robust event-based programming language which provides protection from a range of attacks against web applications and allows for HTTP traffic monitoring, logging and real-time analysis.

Pros of ModSecurity

  • More comprehensive and mature Web Application Firewall (WAF) solution
  • Supports multiple web servers including Apache, Nginx, and IIS
  • Extensive rule set and regular updates from the OWASP community

Cons of ModSecurity

  • Can be resource-intensive, potentially impacting server performance
  • More complex configuration and setup process
  • Steeper learning curve for customization and rule management

Code Comparison

ModSecurity (example rule):

SecRule REQUEST_HEADERS:User-Agent "nikto" \
    "id:1000,phase:1,deny,log,msg:'Nikto Scan Detected'"

ngx_lua_waf (example rule):

if ngx.var.http_user_agent and ngx.var.http_user_agent:find("nikto") then
    ngx.exit(ngx.HTTP_FORBIDDEN)
end

ModSecurity offers a more structured and feature-rich rule syntax, while ngx_lua_waf provides a simpler, Lua-based approach. ModSecurity's rules are generally more verbose but offer greater flexibility and control. ngx_lua_waf's rules are more concise and easier to read for those familiar with Lua, but may lack some of the advanced features found in ModSecurity.

Both solutions aim to protect web applications from various attacks, but ModSecurity is a more comprehensive and widely adopted solution, while ngx_lua_waf is a lightweight alternative specifically designed for Nginx with LuaJIT.

High-performance WAF built on the OpenResty stack

Pros of lua-resty-waf

  • More actively maintained with recent updates and contributions
  • Extensive documentation and configuration options
  • Better performance due to optimized Lua code and caching mechanisms

Cons of lua-resty-waf

  • More complex setup and configuration process
  • Steeper learning curve for users new to WAF systems
  • Requires additional dependencies and modules

Code Comparison

ngx_lua_waf:

function waf_main()
    if white_ip_check() then
    elseif black_ip_check() then
    elseif user_agent_attack_check() then
    elseif cc_attack_check() then
    elseif cookie_attack_check() then
    elseif url_attack_check() then
    elseif url_args_attack_check() then
    end
end

lua-resty-waf:

local waf = require "resty.waf"
local waf_instance = waf:new()

waf_instance:set_option("debug", true)
waf_instance:set_option("mode", "ACTIVE")
waf_instance:exec()

The code comparison shows that lua-resty-waf offers a more modular and configurable approach, while ngx_lua_waf uses a more straightforward, sequential check method. lua-resty-waf's design allows for easier customization and extension of WAF functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

##ngx_lua_waf

ngx_lua_waf是我刚入职趣游时候开发的一个基于ngx_lua的web应用防火墙。

代码很简单,开发初衷主要是使用简单,高性能和轻量级。

现在开源出来,遵从MIT许可协议。其中包含我们的过滤规则。如果大家有什么建议和想fa,欢迎和我一起完善。

###用途:

防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
防止svn/备份之类文件泄漏
防止ApacheBench之类压力测试工具的攻击
屏蔽常见的扫描黑客工具,扫描器
屏蔽异常的网络请求
屏蔽图片附件类目录php执行权限
防止webshell上传

###推荐安装:

推荐使用lujit2.1做lua支持

ngx_lua如果是0.9.2以上版本,建议正则过滤函数改为ngx.re.find,匹配效率会提高三倍左右。

###使用说明:

nginx安装路径假设为:/usr/local/nginx/conf/

把ngx_lua_waf下载到conf目录下,解压命名为waf

在nginx.conf的http段添加

	lua_package_path "/usr/local/nginx/conf/waf/?.lua";
    lua_shared_dict limit 10m;
    init_by_lua_file  /usr/local/nginx/conf/waf/init.lua; 
	access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;

配置config.lua里的waf规则目录(一般在waf/conf/目录下)

    RulePath = "/usr/local/nginx/conf/waf/wafconf/"

绝对路径如有变动,需对应修改

然后重启nginx即可

###配置文件详细说明:

	RulePath = "/usr/local/nginx/conf/waf/wafconf/"
    --规则存放目录
    attacklog = "off"
    --是否开启攻击信息记录,需要配置logdir
    logdir = "/usr/local/nginx/logs/hack/"
    --log存储目录,该目录需要用户自己新建,切需要nginx用户的可写权限
    UrlDeny="on"
    --是否拦截url访问
    Redirect="on"
    --是否拦截后重定向
    CookieMatch = "on"
    --是否拦截cookie攻击
    postMatch = "on" 
    --是否拦截post攻击
    whiteModule = "on" 
    --是否开启URL白名单
    black_fileExt={"php","jsp"}
    --填写不允许上传文件后缀类型
    ipWhitelist={"127.0.0.1"}
    --ip白名单,多个ip用逗号分隔
    ipBlocklist={"1.0.0.1"}
    --ip黑名单,多个ip用逗号分隔
    CCDeny="on"
    --是否开启拦截cc攻击(需要nginx.conf的http段增加lua_shared_dict limit 10m;)
    CCrate = "100/60"
    --设置cc攻击频率,单位为秒.
    --默认1分钟同一个IP只能请求同一个地址100次
    html=[[Please go away~~]]
    --警告内容,可在中括号内自定义
    备注:不要乱动双引号,区分大小写
    

###检查规则是否生效

部署完毕可以尝试如下命令:

    curl http://xxxx/test.php?id=../etc/passwd
    返回"Please go away~~"字样,说明规则生效。

注意:默认,本机在白名单不过滤,可自行调整config.lua配置

###效果图如下:

sec

sec

###规则更新:

考虑到正则的缓存问题,动态规则会影响性能,所以暂没用共享内存字典和redis之类东西做动态管理。

规则更新可以把规则文件放置到其他服务器,通过crontab任务定时下载来更新规则,nginx reload即可生效。以保障ngx lua waf的高性能。

只记录过滤日志,不开启过滤,在代码里在check前面加上--注释即可,如果需要过滤,反之

###一些说明:

过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割

	args里面的规则get参数进行过滤的
	url是只在get请求url过滤的规则		
	post是只在post请求过滤的规则		
	whitelist是白名单,里面的url匹配到不做过滤		
	user-agent是对user-agent的过滤规则


默认开启了get和post过滤,需要开启cookie过滤的,编辑waf.lua取消部分--注释即可

日志文件名称格式如下:虚拟主机名_sec.log

Copyright

Weibo神奇的魔法师
Forumhttp://bbs.linuxtone.org/
CopyrightCopyright (c) 2013- loveshell
LicenseMIT License

感谢ngx_lua模块的开发者@agentzh,春哥是我所接触过开源精神最好的人