使用Nginx+Lua实现自定义WAF

版权声明:全部抄自赵班长的GitHub上waf项目

功能列表:

支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。
支持URL白名单,将不需要过滤的URL进行定义。
支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
支持CC攻击防护,单个URL指定时间的访问次数,超过设定值,直接返回403。
支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。
支持URL参数过滤,原理同上。
支持日志记录,将所有拒绝的操作,记录到日志中去。
日志记录为JSON格式,便于日志分析,例如使用ELKStack进行攻击日志收集、存储、搜索和展示。

Nginx + Lua部署

安装依赖包:

# yum install -y readline-devel pcre-devel openssl-devel
# cd /usr/local/src
下载并编译安装openresty
# wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz
# tar zxf ngx_openresty-1.9.3.2.tar.gz
# cd ngx_openresty-1.9.3.2
# ./configure --prefix=/usr/local/openresty-1.9.3.2 \
--with-luajit --with-http_stub_status_module \
--with-pcre --with-pcre-jit
# gmake && gmake install
# ln -s /usr/local/openresty-1.9.3.2/ /usr/local/openresty
openresty安装
 vim /usr/local/openresty/nginx/conf/nginx.conf
server {
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
}
# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf test is successful
# /usr/local/openresty/nginx/sbin/nginx

测试:

curl http://192.168.199.33/hello

WAF部署:

git clone https://github.com/unixhot/waf.git
cp -a ./waf/waf /usr/local/openresty/nginx/conf/
修改Nginx的配置文件,加入以下配置。注意路径,同时WAF日志默认存放在/tmp/日期_waf.log
    lua_shared_dict limit 50m;
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";

重新载入nginx:

/usr/local/openresty/nginx/sbin/nginx –t
/usr/local/openresty/nginx/sbin/nginx -s reload

测试:

访问以sql为后缀的文件

也可以直接跳转到某个页面:

修改conf.lua

config_waf_output = "redirect"    #默认是html
config_waf_redirect_url = "http://www.cnblogs.com/panwenbin-logs/" #自定义url

测试语法,重新载入nginx

[root@localhost waf]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf test is successful
[root@localhost waf]# /usr/local/openresty/nginx/sbin/nginx -s reload

在访问看看是否能跳转

具体规则见url.rule文件

防CC攻击:

以ab命令为例:

再访问一次(一分钟之内):

发现第一次非2xx请求为89(成功11个)

第二次非2xx请求为100(成功0个)

whiteip.rule  IP白名单(直接写入IP保存,无需重启)

blackip.rule  IP黑名单(直接写入IP保存,无需重启)

使用Nginx+Lua实现自定义WAF的更多相关文章

  1. nginx + Lua 实现自定义WAF

    文章摘自:https://github.com/unixhot/waf wget git@github.com:unixhot/waf.git

  2. Nginx详解二十八:Nginx架构篇Nginx+Lua的安全waf防火墙

    Nginx+Lua的安全waf防火墙 看一下别人写好的:https://github.com/loveshell/ngx_lua_waf 先安装git:yum -y install git 在/opt ...

  3. Nginx + Lua 搭建网站WAF防火墙

    前言 对于项目里面只是使用代理等常用功能,在线安装即可,如需制定化模块,则推荐编译安装 PS:本文不仅仅包含Nginx相关的知识点,还包含了逆天学习方法(对待新事物的处理) 官方网站:https:// ...

  4. nginx+lua构建简单waf网页防火墙

    需求背景 类似于论坛型的网站经常会被黑掉,除了增加硬件防护感觉效果还是不太好,还会偶尔被黑,waf的功能正好实现了这个需求. waf的作用: 防止sql注入,本地包含,部分溢出,fuzzing测试,x ...

  5. 使用NGINX+LUA实现WAF功能 和nginx 防盗链

    使用NGINX+LUA实现WAF功能 一.了解WAF 1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: WAF) ...

  6. 使用Nginx+Lua实现waf

    使用Nginx+Lua实现waf 技术内容来自:https://github.com/loveshell/ngx_lua_waf 软件包需求: 1 .Nginx兼容性[最后测试到1.13.6] [ro ...

  7. Nginx使用Lua模块实现WAF

    前言:最近一段时间在写加密数据功能,对安全相关知识还是缺少积累,无意间接触到了WAF相关知识,刚好Nginx可以实现WAF功能,也简单学习了Lua这门语言,分享下 一.WAF产生的背景 过去企业通常会 ...

  8. nginx+lua实现灰度发布/waf防火墙

    nginx+lua 实现灰度发布 waf防火墙 课程链接:[课程]Nginx 与 Lua 实现灰度发布与 WAF 防火墙(完)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili 参考博客 Nginx ...

  9. Nginx基础 - Nginx+Lua实现灰度发布与WAF

    1.Nginx加载Lua环境默认情况下Nginx不支持Lua模块, 需要安装LuaJIT解释器, 并且需要重新编译Nginx, 建议使用openrestry 1)环境准备 [root@localhos ...

随机推荐

  1. webpack 3 升级到 webpack 4,遇到问题解决

    报错:Error: Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead 解 ...

  2. TypeScript 知识点

    TypeScript 通过 类型批注 提供静态类型以在编译时启动类型检查. 基本批注类型是number.bool.string.而弱或动态类型是any. typescript 使用 作用 语句 全局安 ...

  3. enum和数据库entity互转

    注意,code和desc都是string的,数据库的entity是integer,dto的是enum,所以需要一个转换 entity转dto EnumGender.getEnum(String.val ...

  4. Idea 无法显示mybatis-generator插件

    Idea在pom.xml中添加了mybatis-generator插件后,右侧的maven projects中并未显示这个插件? 解决方法: 新建一个plugins标签和pluginManager同级 ...

  5. Spark Hadoop Free 安装遇到的问题

    运行 ./sbin/start-master.sh : SparkCommand:/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -cp /home/se ...

  6. 报错: unable to register MBean

    解决方法: 在SpringBoot项目中配置文件加上 spring.jmx.enabled=false https://stackoverflow.com/questions/27440985/una ...

  7. Ubuntu 14.04 正式版 12.4

    安装Ubuntu 14.04后要做的5件事情 4月17日,开源免费系统Ubuntu官方正式宣布发布Ubuntu 14.04 LTS(代号Trusty Tahr)正式版.官方声称该版本主打云计算,在云平 ...

  8. socket.io入门整理教程

    socket.io入门整理  发布于 5 年前  作者 dtrex  124983 次浏览  最后一次编辑是 1 年前 我自己在用socket.io开发,对官方网站上的文档,进行简单的整理,然后自己写 ...

  9. Ngui Tween 组合动画 group

    使用NGUI的Tween做补间动画,难免会涉及组合各种Tween.最常用的就是 Scale+Alpha组合 做淡入淡出了.那么如何控制 播放完一个Tween 后在 播放另一个Tween呢? 利用del ...

  10. vue中mounted中无法获取到dom元素

    一.解决方案: 加上异步setTimeout,延迟获取dom的代码的执行 mounted() { // debugger this.$nextTick(()=> { setTimeout(()= ...