Openresty实现web应用防火墙(waf)

Openresty简介

OpenResty 是由中国人章亦春发起,把nginx和各种三方模块的一个打包而成的软件平台,核心就是nginx+lua脚本语言。主要是因为nginx是C语言编写,修改很复杂,而lua语言则简单得多,国内很多大公司如360、京东、gitee等都在用来作为web应用防火墙。

安装openresty

部署系统为Centos7.9

1、安装依赖库

------------------------------------------------------------------------------------------
pcre-devel: 扩展的正则表达式引擎,为了使Nginx处理更复杂的正则表达式机制
openssl-devel:–with-http_ssl_module使用该模块必需装openssl库,来实现http支持https协议
zlib-devel:zlib库是网络通信压缩库,ngx_http_gzip_module(gzip压缩模块)所必需的
readline-devel:readline是安装Openresty所必须的依赖包
-------------------------------------------------------------------------------------------
yum install gcc-c++ libtool gmake make -y
yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel readline readline-devel -y

2、创建nginx用户组

Nginx的Master主进程以root用户身份运行,而worker子进程我们指定它为nginx用户运行

groupadd nginx
useradd -d /home/nginx -g nginx -s /sbin/nologin nginx

3、编译并安装openresty

下载:wget https://openresty.org/download/openresty-1.19.9.1.tar.gz

解压tar.gz包后,进入openresty-1.19.9.1目录下,然后开始编译

./configure --prefix=/usr/local/openresty --sbin-path=/usr/local/openresty/nginx/sbin/nginx --conf-path=/usr/local/openresty/nginx/conf/nginx.conf --pid-path=/usr/local/openresty/nginx/run/nginx.pid --user=nginx --group=nginx --with-pcre --with-stream --with-threads --with-http_v2_module --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-luajit 

gmake && gmake install 

4、编辑nginx.conf文件,配置location如下:

location / {

              default_type text/html;
              content_by_lua_block {
                  ngx.say("<p>hello, world</p>")
              }
          }

5、启动nginx,浏览器访问显示hello,world,表明openresty安装成功
在这里插入图片描述

部署WAF

自定义WAF的实现主要是使用nginx+Lua,实现方案有两种:

其一:可以选择使用原生的Nginx,增加Lua模块实现部署

其二:直接使用OpenResty

这里采用第二种方案,github上已经有人通过lua实现了waf的功能(https://github.com/unixhot/waf)

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

1、下载waf模块

git clone https://github.com/unixhot/waf.git

cp -a ./waf/waf /usr/local/openresty/nginx/conf/

2、waf目录内文件说明

ls -al /usr/local/openresty/nginx/conf/waf/

access.lua  init.lua  lib.lub #waf功能实现lua代码
config.lua  #配置文件
rule-config  # 防御规则文件

rule-config目录内文件说明

ls -al  /usr/local/openresty/nginx/conf/waf/rule-config/

args.rule          #异常get请求参数策略文件
blackip.rule       #IP黑名单策略文件
cookie.rule        #Cookie策略文件
post.rule          #异常post请求参数策略文件
url.rule           #异常url策略文件
useragent.rule     #异常useragent策略文件
whiteip.rule       #IP白名单策略文件
whiteurl.rule      #URL白名单策略文件

3、openresty引入waf模块

3.1、编辑nginx.conf文件,在http{}中增加以下内容:

http {
      ...
      ...

      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";

       ...

}

3.2、设置软连接

[root@localhost /]# ln -s /usr/local/openresty/lualib/resty/ /usr/local/openresty/nginx/conf/waf/resty

4、测试waf防御效果

在这里插入图片描述

从结果上看,waf已经起了作用,这是因为命中了url规则文件

[root@localhost rule-config]# cat url.rule 
\.(htaccess|bash_history)
\.(bak|inc|old|mdb|sql|backup|java|class|tgz|gz|tar|zip)$
(phpmyadmin|jmx-console|admin-console|jmxinvokerservlet)
java\.lang
\.svn\/
/(attachments|upimg|images|css|uploadfiles|html|uploads|templets|static|template|data|inc|forumdata|upload|includes|cache|avatar)/(\\w+).(php|jsp)
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值