Web应用防护系统(也称:网站应用级入侵防御系统 。英文:Web Application Firewall,简称: WAF)。利用国际上公认的一种说法:Web应用 防火墙是通过执行一系列针对HTTP/HTTPS的 安全策略来专门为Web应用提供保护的一款产品。

nginx+lua安装方法

方法一:安装nginx并整合lua模块

安装LuaJIT

LuaJIT的意思是Lua Just-In-Time,是即时的Lua代码解释器。必须去github下载否则运行是会出现报错,项目地址:https://github.com/openresty/luajit2

git clone https://github.com/openresty/luajit2.git
cd luajit2
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit

安装完成后将如下环境变量加入到/etc/profile中,并执行source /etc/profile

export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1

安装ngx_devel_kit(NDK)

版本地址:https://github.com/vision5/ngx_devel_kit/tags

下载并解压

cd /mnt
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -xzvf v0.3.1.tar.gz

安装最新版的lua-nginx-module

版本地址:https://github.com/openresty/lua-nginx-module/tags

下载最新稳定版并解压

cd /mnt
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
tar -xzvf v0.10.16rc5.tar.gz

编译Nginx并加入lua模块

cd /mnt/nginx-1.18.0
./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx  --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-openssl=/mnt/openssl-1.1.1g --with-zlib=/mnt/zlib-1.2.11 --with-pcre=/mnt/pcre-8.44 --add-module=/mnt/lua-nginx-module-0.10.15 --add-module=/mnt/ngx_devel_kit-0.3.1

::: tip 提示

其中的openssl,pcre以及zlib需要额外下载并解压到/mnt目录下

:::

启动nginx发现报错

[root@k8s-node mnt]# nginx
nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解决办法

echo "/usr/local/luajit/lib/" >> /etc/ld.so.conf
ldconfig

在默认的location中加入如下指令,访问测试

content_by_lua 'ngx.say("hello, lua")';

安装成功

方法二 :直接安装OpenResty

::: tip

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

:::

最新版Openresty

cd /opt
tar -xzvf openresty-1.15.8.3.tar.gz
./configure --prefix=/opt/openresty --with-pcre=/opt/pcre-8.44 --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.1.1g --with-poll_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module
gmake
gmake install

按方法一测试可以访问即可

WAF模块安装

ngx_lua_waf项目地址:https://github.com/loveshell/ngx_lua_waf.git

这里以Openresty为例,Nginx方法类似

cd /opt/openresty/lualib
git clone https://github.com/loveshell/ngx_lua_waf.git waf

在openresry的nginx.conf配置文件中http{}代码块添加如下配置项:

lua_package_path "/opt/openresty/lualib/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file  "/opt/openresty/lualib/waf/init.lua";
access_by_lua_file "/opt/openresty/lualib/waf/waf.lua";

整个waf目录结构如下:

[root@k8s-node lualib]# tree waf
waf
├── config.lua
├── init.lua
├── wafconf
│   ├── args
│   ├── cookie
│   ├── post
│   ├── url
│   ├── user-agent
│   └── whiteurl
└── waf.lua1 directory, 9 files

config.lua中定义了整个waf的配置,详细如下:

#拦截规则的存放目录
RulePath = "/opt/openresty/lualib/waf/wafconf/"
#是否开启拦截日志记录
attacklog = "on"
#拦截日志的记录目录,nginx的worker进程需要对该目录有权限
logdir = "/opt/openresty/nginx/logs/"
#是否开启url拦截
UrlDeny="on"
#是否开启拦截重定向
Redirect="on"
#是否开启cookie攻击防护
CookieMatch="on"
#是否开启post攻击防护
postMatch="on"
whiteModule="on"
#禁止访问的文件扩展名
black_fileExt={"php","jsp"}
#IP地址白名单
ipWhitelist={"127.0.0.1"}
#IP地址黑名单
ipBlocklist={"1.0.0.1"}
#是否开启CC攻击防护
CCDeny="on"
#定义CC攻击速率,该例为每60秒100次请求,表示60秒内最多同一页面最多刷新100次,超过后自动拉入黑名单,60秒后解除,不影响访问其他页面
CCrate="100/60"
#定义重定向后的html页面
html=[[...]]

启动nginx发现报错

[root@jssong conf]# nginx
nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: ...openresty/lualib/lua-resty-core/lib/resty/core/regex.lua:14: module 'resty.lrucache' not found:no field package.preload['resty.lrucache']no file '/opt/openresty/lualib/waf/resty/lrucache.lua'no file '/opt/openresty/lualib/lua-resty-core/lib/resty/lrucache.lua'no file '/opt/openresty/site/lualib/resty/lrucache.so'no file '/opt/openresty/lualib/resty/lrucache.so'no file './resty/lrucache.so'no file '/usr/local/lib/lua/5.1/resty/lrucache.so'no file '/opt/openresty/luajit/lib/lua/5.1/resty/lrucache.so'no file '/usr/local/lib/lua/5.1/loadall.so'no file '/opt/openresty/site/lualib/resty.so'no file '/opt/openresty/lualib/resty.so'no file './resty.so'no file '/usr/local/lib/lua/5.1/resty.so'no file '/opt/openresty/luajit/lib/lua/5.1/resty.so'no file '/usr/local/lib/lua/5.1/loadall.so') in /opt/openresty/nginx/conf/nginx.conf:133

原因是因为缺少 lua-resty-core 模块,从而找不到这些信息,所以我们要下载lua-resty-core模块然后引入到openresty

cd /opt/openresty/lualib
git clone https://github.com/openresty/lua-resty-core.git

lua-resty-core结构目录如下,主要是用到lib下的lua

[root@jssong lualib]# tree lua-resty-core
lua-resty-core
├── dist.ini
├── lib
│   ├── ngx
│   │   ├── balancer.lua
│   │   ├── balancer.md
│   │   ├── base64.lua
│   │   ├── base64.md
│   │   ├── errlog.lua
│   │   ├── errlog.md
│   │   ├── ocsp.lua
│   │   ├── ocsp.md
│   │   ├── pipe.lua
│   │   ├── pipe.md
│   │   ├── process.lua
│   │   ├── process.md
│   │   ├── re.lua
│   │   ├── re.md
│   │   ├── req.lua
│   │   ├── req.md
│   │   ├── resp.lua
│   │   ├── resp.md
│   │   ├── semaphore.lua
│   │   ├── semaphore.md
│   │   ├── ssl
│   │   │   ├── session.lua
│   │   │   └── session.md
│   │   ├── ssl.lua
│   │   └── ssl.md
│   └── resty
│       ├── core
│       │   ├── base64.lua
│       │   ├── base.lua
│       │   ├── ctx.lua
│       │   ├── exit.lua
│       │   ├── hash.lua
│       │   ├── misc.lua
│       │   ├── ndk.lua
│       │   ├── phase.lua
│       │   ├── regex.lua
│       │   ├── request.lua
│       │   ├── response.lua
│       │   ├── shdict.lua
│       │   ├── time.lua
│       │   ├── uri.lua
│       │   ├── utils.lua
│       │   ├── var.lua
│       │   └── worker.lua
│       └── core.lua
├── Makefile
├── README.markdown
├── t
│   ├── balancer.t
│   ├── balancer-timeout.t
│   ├── cert
│   │   ├── chain
│   │   │   ├── chain-bad0.pem
│   │   │   ├── chain-bad2.pem
│   │   │   ├── chain.der
│   │   │   ├── chain.pem
│   │   │   ├── root-ca.crt
│   │   │   ├── test-com-bad.key.pem
│   │   │   ├── test-com.key.der
│   │   │   └── test-com.key.pem
│   │   ├── ocsp
│   │   │   ├── chain.pem
│   │   │   ├── ocsp-req.der
│   │   │   ├── ocsp-resp.der
│   │   │   ├── ocsp-resp-no-certs.der
│   │   │   ├── ocsp-resp-signed-by-orphaned.der
│   │   │   ├── ocsp-resp-signed-by-orphaned-no-certs.der
│   │   │   ├── revoked-chain.pem
│   │   │   ├── revoked-ocsp-resp.der
│   │   │   ├── test-com.crt
│   │   │   └── wrong-issuer-order-chain.pem
│   │   ├── test2.crt
│   │   ├── test2.key
│   │   ├── test.crt
│   │   ├── test.crt.der
│   │   ├── test.key
│   │   └── test.key.der
│   ├── count.t
│   ├── ctx.t
│   ├── decode-base64.t
│   ├── encode-base64.t
│   ├── errlog-raw-log.t
│   ├── errlog.t
│   ├── exit.t
│   ├── lib
│   │   └── helper.lua
│   ├── master-pid-single-process.t
│   ├── master-pid.t
│   ├── md5-bin.t
│   ├── md5.t
│   ├── misc.t
│   ├── ndk.t
│   ├── ngx-req.t
│   ├── ngx-resp.t
│   ├── ocsp.t
│   ├── os-getenv-hup.t
│   ├── os-getenv.t
│   ├── phase.t
│   ├── pipe-cpu-affinity.t
│   ├── pipe-stderr.t
│   ├── pipe-stdin.t
│   ├── pipe-stdout.t
│   ├── pipe.t
│   ├── process-type-cache.t
│   ├── process-type-hup.t
│   ├── process-type-master.t
│   ├── process-type-privileged-agent.t
│   ├── process-type-single.t
│   ├── process-type-worker.t
│   ├── re-base.t
│   ├── re-bugs.t
│   ├── re-find.t
│   ├── re-gmatch.t
│   ├── re-match.t
│   ├── re-opt.t
│   ├── request.t
│   ├── re-split.t
│   ├── response.t
│   ├── re-sub.t
│   ├── semaphore.t
│   ├── sha1-bin.t
│   ├── shared.t
│   ├── shdict.t
│   ├── ssl-session-fetch.t
│   ├── ssl-session-store.t
│   ├── ssl.t
│   ├── status.t
│   ├── stream
│   │   ├── balancer.t
│   │   ├── balancer-timeout.t
│   │   ├── errlog-raw-log.t
│   │   ├── errlog.t
│   │   ├── misc.t
│   │   ├── os-getenv-hup.t
│   │   ├── os-getenv.t
│   │   ├── re-base.t
│   │   ├── re-find.t
│   │   ├── re-gmatch.t
│   │   ├── re-match.t
│   │   ├── re-opt.t
│   │   ├── request.t
│   │   ├── re-split.t
│   │   ├── re-sub.t
│   │   ├── semaphore.t
│   │   ├── shdict.t
│   │   ├── ssl.t
│   │   └── time.t
│   ├── TestCore
│   │   └── Stream.pm
│   ├── TestCore.pm
│   ├── time.t
│   ├── uri.t
│   ├── utils.t
│   ├── var.t
│   ├── worker-count-5.t
│   └── worker.t
└── valgrind.suppress

在openrestry的配置文件nginx.conf配置文件中lua_package_path追加代码

lua_package_path "/opt/openresty/lualib/waf/?.lua;/opt/openresty/lualib/lua-resty-core/lib/?.lua;;";

启动nginx,报错消失。

拦截测试:

#出现拦截页面即表示安装成功
curl http://www.example.com/test.php?id=../etc/passwd

相关日志:

192.168.0.101 [2020-06-20 01:44:01] "GET localhost/index.php?id=/../../../etc/passwd" "-"  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36" "\.\./"
?.lua;/opt/openresty/lualib/lua-resty-core/lib/?.lua;;";

启动nginx,报错消失。

拦截测试:

#出现拦截页面即表示安装成功
curl http://www.example.com/test.php?id=../etc/passwd

相关日志:

192.168.0.101 [2020-06-20 01:44:01] "GET localhost/index.php?id=/../../../etc/passwd" "-"  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36" "\.\./"

NginxLua实现WAF防火墙相关推荐

  1. OpenResty+Lua实现WAF防火墙

    OpenResty+Lua实现WAF防火墙 WAF功能 白名单 黑名单 防止SQL注入 防止CC攻击 若服务器收到CC攻击,一分钟内请求数大于60,则会将对方ip封进黑名单 文件结构 waf 项目文件 ...

  2. waf服务器部署位置,【原】WAF 防火墙 部署

    一.了解WAF 1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: WAF).利用国际上公认的一种说法:Web应用 ...

  3. WAF防火墙有什么用

    WAF防火墙有什么用,首先我们要先了解下Web应用防护系统(也称为:网站应用级入侵防御系统.英文:Web Application Firewall,简称: WAF).WAF防火墙有什么用?利用国际上公 ...

  4. Linux搭建waf防火墙,Linux入门教程:nginx安装配置naxsi waf防火墙,nginxwaf Naxsi 不依赖...

    Linux入门教程:nginx安装配置naxsi waf防火墙,nginxwaf Naxsi 不依赖 Naxsi 是第三方 nginx 模块 ,它和 Modsecurity 都是开源 WAF ,但是它 ...

  5. web防火墙和waf防火墙的区别

    首先说一下这两种防火墙很容易被很多老鸟或小白混淆,但web防火墙和waf防火墙不属于一个东西,真的,看我下面的介绍. Web应用防火墙,属于硬件级别防火墙 对网站流量进行恶意特征识别及防护,将正常.安 ...

  6. web防火墙和waf防火墙的区别和选择

    首先说下被很多老鸟或小白混要的一些说法,web防火墙和waf防火墙不属于一个东西.真的,看我解释. Web应用防火墙,属于硬件级别防火墙. 对网站流量进行恶意特征识别及防护,将正常.安全的流量回源到服 ...

  7. 【如何快速搭建企业级的WAF防火墙】

    如何快速搭建企业级的WAF防火墙 目标 1. 安装 OpenResty 2. 部署 Best-Nginx-Waf 2.1 下载 Best-Nginx-Waf 2.2 解压缩 2.3 将 best-ng ...

  8. php waf 搭建,如何构建属于自己的nginx waf防火墙 VeryNginx

    构建自己防火墙的原因 : 需要对特定链接进行防cc攻击  但对某些链接不需要进行防cc攻击 技术选型: 基于 nginx + lua 的 verynginx 1.首先verynginx 需要nginx ...

  9. WAF防火墙是什么呢

    WAF防火墙是什么呢,首先我们要先了解下Web应用防护系统(也称为:网站应用级入侵防御系统.英文:Web Application Firewall,简称: WAF).利用国际上公认的一种说法:Web应 ...

最新文章

  1. 2020-10-29 PYTORCH与Tensorflow速查表
  2. 为什么引入验证集来评估机器学习模型?只用训练集和测试集可以吗?
  3. 打造高效机房就这么简单(一)
  4. B. Om Nom and Dark Park
  5. 开发日记-20190407
  6. 在linux文件共享接口,入坑Linux-day13(使用vsftpd服务传输文件、使用Samba或NFS实现文件共享)...
  7. IOS应用开发版本控制工具之Versions使用,iosversions
  8. 搭建go项目web服务器,3.2 Go搭建一个Web服务器
  9. 细胞生长曲线拟合matlab,绘制细胞生长曲线及细胞群体倍增时间的简化计算
  10. 恒星物联-河道液位监测系统方案 液位监测
  11. 2020安洵杯参赛感言
  12. HTTP协议简介和系统命令curl,AIP调用 (ELK中运用)
  13. BoardCast BroadcastReceiver 基础
  14. alter命令的使用
  15. RNNoise超详细解析
  16. 注册测绘师资格拟认定人员公示名单
  17. Python字符串格式化 (%占位操作符)
  18. idea常用的十八个设置(程序员必会)
  19. 怎样设置word背景图片每一页都不同
  20. Python中基本输入和输出

热门文章

  1. STM32Cube MX USB虚拟U盘+FATFS+W25Q128
  2. Xcode4.6 自制iOS可用的 Framework
  3. 【Android 教程系列第 23 篇】 java 方法之间怎么添加分割线
  4. k8s 亲和、反亲和、污点、容忍
  5. CVPR2022知识蒸馏用于目标检测:Focal and Global Knowledge Distillation for Detectors
  6. 安卓 post请求与回调(绑接口)
  7. 解决Chrome、360自动填充用户名和密码行为带来的困扰
  8. 这篇文章让你实现时光机特效的操作
  9. 常见RGB 颜值表和半透明颜色值的写法
  10. 图卷积在语义分割上的应用(论文集)