使用Nginx+Lua实现的WAF网站防护功能
admin
2023-03-05 20:41:08
0


一.OpenResty安装和测试

官方网站:https://openresty.org/cn/ 

OpenResty®

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。


LUA学习:http://blog.jobbole.com/70480/


1.安装OpenResty:

# 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/ngx_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


#报错提醒:

在./configure时的报错:

/usr/bin/env: perl: No such file or directory

出现这种错误可能是没有安装perl,解决yum install perl -y

[root@localhost 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
platform: linux (linux)
cp -rp bundle/ build
cd build
cd LuaJIT-2.1-20151028
Can't exec "cc": No such file or directory at ./configure line 588.
gmake TARGET_STRIP=@: CCDEBUG=-g CC=cc PREFIX=/usr/local/openresty-1.9.3.2/luajit
==== Building LuaJIT 2.1.0-beta1 ====
gmake -C src
gmake[1]: cc: Command not found
gmake[1]: Entering directory `/usr/local/ngx_openresty-1.9.3.2/build/LuaJIT-2.1-20151028/src'
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
Makefile:237: *** Unsupported target architecture.  Stop.
gmake[1]: Leaving directory `/usr/local/ngx_openresty-1.9.3.2/build/LuaJIT-2.1-20151028/src'
gmake: *** [default] Error 2
ERROR: failed to run command: gmake TARGET_STRIP=@: CCDEBUG=-g CC=cc PREFIX=/usr/local/openresty-1.9.3.2/luajit

解决:

yum install *gcc* -y


2.测试openresty安装:

vim /usr/local/openresty/nginx/conf/nginx.conf

   server {
            listen       80;
            server_name  localhost;    
        location / {
            root   html;
            index  index.html index.htm;
        }
        ......此处省略
       }

在此处省略处,添加一行

         location /hello {
             default_type text/html;
             content_by_lua_block {
             ngx.say("HelloWorld")
            }


3.启动openresty并测试:

/usr/local/openresty/nginx/sbin/nginx -t 

/usr/local/openresty/nginx/sbin/nginx


[root@localhost ~]# curl http://10.0.0.50/hello
HelloWorld


4. 性能测试

-- 1. 安装压力测试工具

[root@localhost ~]# yum install httpd-tools -y


-- 2. 测试

[root@localhost ~]# ab -c10 -n5000 http://10.0.0.50/hello
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 10.0.0.50 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        openresty/1.9.3.2
Server Hostname:        10.0.0.50
Server Port:            80
Document Path:          /hello
Document Length:        11 bytes
Concurrency Level:      10
Time taken for tests:   0.505 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      790000 bytes
HTML transferred:       55000 bytes
Requests per second:    9901.83 [#/sec] (mean)
Time per request:       1.010 [ms] (mean)
Time per request:       0.101 [ms] (mean, across all concurrent requests)
Transfer rate:          1527.82 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:     0    1   0.3      1       2
Waiting:        0    0   0.2      0       2
Total:          1    1   0.3      1       3
Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      2
  98%      2
  99%      2
 100%      3 (longest request)



...

需求产生 由于原生态的Nginx的一些安全防护功能有限,就研究能不能自己编写一个WAF,参考(照抄)Kindle大神的ngx_lua_waf,自己尝试写一个了,使用两天时间,边学Lua,边写。不过不是安全专业,只实现了一些比较简单的功能:

####功能列表:

  1. 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。

  2. 支持URL白名单,将不需要过滤的URL进行定义。

  3. 支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。

  4. 支持CC***防护,单个URL指定时间的访问次数,超过设定值,直接返回403。

  5. 支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。

  6. 支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。

  7. 支持URL参数过滤,原理同上。

  8. 支持日志记录,将所有拒绝的操作,记录到日志中去。

  9. 日志记录为JSON格式,便于日志分析,例如使用ELKStack进行***日志收集、存储、搜索和展示。

####WAF实现 WAF一句话描述,就是解析HTTP请求(协议解析模块),规则检测(规则模块),做不同的防御动作(动作模块),并将防御过程(日志模块)记录下来。所以本文中的WAF的实现由五个模块(配置模块、协议解析模块、规则模块、动作模块、错误处理模块)组成。


相关内容

热门资讯

51.79亿,携程真的疼了吗? 2026年7月25日,市场监管总局对携程反垄断案作出行政处罚:罚没款合计51.79亿元。但这个数字被...
一艘越南籍渔船在南沙遇险,29... 新华社三沙7月25日电(记者赵颖全)记者从三沙海上搜救中心获悉,25日傍晚,一艘越南籍渔船在南沙永暑...
成都如何耕好新型工业化“试验田... 7月24日,APEC数字周城市侧配套活动——制造强国建设专家研讨暨新型工业化推进交流会举行,院士专家...
活力中国调研行丨检测设备“长腿... 从排放控制的细分龙头到智能装备的新入局者,凯龙高科的转型路径,为观察传统制造业如何切入新赛道提供了一...
原创 别... 别被顶级奖项光环忽悠透了!拿菲尔兹奖的王虹,道出基础科研最现实的人间落差 开篇:大众脑补的名利双收,...
温州划龙桥惠耳——如何应对老年... 年近古稀的老人,近来时常出现耳鸣。周日,孩子们来了,跟她说话,声音小了她听不清,声音大了她嫌吵得慌,...
民调:包括共和党人在内,美国人... 据美国有线电视新闻网(CNN)当地时间24日报道,多项最新民调结果显示,包括许多共和党人在内,美国民...
一个人害得全县买不了榴莲,恶意... 作者:何涛 责编:任绍敏据央视报道,近日,湖南衡山县人民法院审结一起案件:当地一男子用AI伪造榴莲、...
“红霞”已加强为强台风,中央气... 今年第12号台风“红霞”已于今天晚上10时由台风级加强为强台风级,中央气象台发布今年首个台风红色预警...
携程为何被处51.79亿元重罚... 今天(25日),市场监管总局依法对携程集团有限公司滥用市场支配地位实施垄断行为作出行政处罚,罚没款合...