千家信息网

CentOS 7.7 OpenResty实现WAF应用防火墙

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,1、WAF相关概念介绍:(1)WAF简介:WAF:Web Appalication Firewall,Web应用防火墙,是一种工作在应用层的、通过一系列针对HTTP/HTTPS的安全策略为Web应用提
千家信息网最后更新 2026年11月01日CentOS 7.7 OpenResty实现WAF应用防火墙

1、WAF相关概念介绍:

(1)WAF简介:

WAF:Web Appalication Firewall,Web应用防火墙,是一种工作在应用层的、通过一系列针对HTTP/HTTPS的安全策略为Web应用提供安全防护的产品。

(2)WAF可以实现如下功能:

a、防止SQL注入、本地包含、部分溢出、Fuzzing测试、XSS等Web Attack;

b、防止SVN/备份之类的文件泄漏;

c、防止Apache Bench之类的压测工具Attack;

d、屏蔽常见的Hacker扫描工具;

e、屏蔽异常的网络请求;

f、屏蔽图片附件类目录的PHP执行权限;

g、防止Webshell上传等。

2、安装依赖软件包:

# yum -y install gcc gcc-c++ make zlib zlib-devel openssl openssl-devel pcre pcre-devel perl-devel perl-ExtUtils-Embed gd-devel libxml2 libxml2-devel libxslt libxslt-devel GeoIP GeoIP-devel GeoIP-data git libuuid-devel libblkid-devel libudev-devel fuse-devel libedit-devel libatomic_ops-devel httpd-tools

3、编译安装OpenResty:

# useradd -s /sbin/nologin -M nginx

# mkdir -pv /usr/local/openresty/nginx/logs/

# tar -xf openresty-1.15.8.2.tar.gz -C /usr/src

# cd /usr/src/openresty-1.15.8.2/

# ./configure --prefix=/usr/local/openresty --user=nginx --group=nginx --with-threads --with-file-aio --with-http_iconv_module --with-luajit --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_preread_module --with-pcre --with-pcre-jit --with-libatomic --http-log-path=/usr/local/openresty/nginx/logs/access.log

# gmake && gmake install

4、配置环境变量,并启动OpenResty:

# vim /etc/profile.d/openresty.sh

export PATH=/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin:$PATH

# . /etc/profile.d/openresty.sh

# nginx -v

# nginx

# ss -tunlp | grep -w :80

5、测试Lua环境:

# vim /tmp/hello.lua --> print("Hello Lua")

# lua /tmp/hello.lua

# lua

6、测试OpenResty Lua模块:

# cd /usr/local/openresty/nginx/conf

# cp nginx.conf{,.bak}

# vim nginx.conf,在server配置段中新增如下location:

location /lua {

default_type text/html;

content_by_lua_block {

ngx.say("Hello Lua")

}

}

# nginx -t

# nginx -s reload

7、创建保存***日志的目录:

# mkdir -pv /usr/local/openresty/nginx/logs/hack

8、下载解压ngx_lua_waf模块:

ngx_lua_waf:基于lua-nginx-module的Web应用防火墙,https://github.com/loveshell/ngx_lua_waf。

# tar -xf ngx_lua_waf-0.7.2.tar.gz -C /usr/local/openresty/nginx/conf

# cd /usr/local/openresty/nginx/conf

# mv ngx_lua_waf-0.7.2 waf

# chown -R nginx.nginx /usr/local/openresty

备注:waf目录主要结构

(1)config.lua:配置文件;

(2)init.lua:规则函数;

(3)waf.lua:定义WAF检测顺序;

(4)wafconf:保存过滤规则的目录,每条规则需换行或用|分割;

(5)wafconf/args:按照GET参数过滤(默认已开启);

(6)wafconf/cookie:按照Cookie过滤;

(7)wafconf/post:按照POST请求过滤(默认已开启);

(8)wafconf/url:按照GET请求URL过滤;

(9)wafconf/user-agent:按照User Agent过滤;

(10)wafconf/whiteurl:按照白名单中的URL做匹配,匹配到则不做过滤。

9、修改config.lua配置文件中waf规则目录的路径:

# vim /usr/local/openresty/nginx/conf/waf/config.lua --> RulePath="/usr/local/openresty/nginx/conf/waf/wafconf/"

备注:config.lua配置文件

指令

含义

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

规则存放目录

attacklog="on"

开启日志

logdir="/usr/local/openresty/nginx/logs/hack/"

Log日志目录

UrlDeny="on"

拦截URL访问

Redirect="on"

拦截后重定向

CookieMatch="on"

拦截Cookie Attack

postMatch="on"

拦截Post Attack

whiteModule="on"

开启URL白名单

black_fileExt={"php","jsp"}

不允许上传的文件后缀类型

ipWhitelist={"127.0.0.1"}

IP白名单,多个IP之间使用逗号分隔

ipBlocklist={"1.0.0.1"}

IP黑名单,多个IP之间使用逗号分隔

CCDeny="on"

开启拦截CC Attack(需要在nginx.conf的http配置段中新增代码lua_shared_dict limit 10m;)

CCrate="100/60"

设置CC Attack频率,单位为秒

默认1分钟同一个IP只能请求同一个地址100次

10、修改nginx.conf配置文件:

# vim /usr/local/openresty/nginx/conf/nginx.conf,在http配置段中新增如下代码:

lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";

lua_shared_dict limit 10m;

init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";

access_by_lua_file "/usr/local/openresty/nginx/conf/waf/waf.lua";

# nginx -t

# nginx -s reload

11、测试WAF应用防火墙:

(1)模拟URL参数检测:http://192.168.0.121/lua?id=../etc/shadow

(2)使用ab命令模拟CC Attack:# ab -n 10000 -c 100 http://192.168.0.121/lua

备注:ab命令选项

a、-n requests:执行的请求总数,默认为1

b、-c concurrency:一次并发执行的请求数,默认为1

(3)查看日志:# tail -3 /usr/local/openresty/nginx/logs/hack/localhost_2020-02-18_sec.log

192.168.0.121 [2020-02-18 00:47:49] "UA localhost/lua" "-" "ApacheBench/2.3" "(HTTrack|harvest|audit|dirbuster|pangolin|nmap|sqln|-scan|hydra|Parser|libwww|BBBike|sqlmap|w3af|owasp|Nikto|fimap|havij|PycURL|zmeu|BabyKrokodil|netsparker|httperf|bench| SF/)"


配置 目录 文件 应用 规则 日志 测试 防火墙 防火 名单 备注 中新 安全 之间 代码 参数 命令 多个 工具 模块 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技产业 科技股互联网 美国互联网科技巨头监管 互联网科技的英文 互联网科技网站 天扬科技互联网广告 互联网科技大会案例 美国互联网前沿科技 成都仁贤领冠互联网科技有限公司 华脉科技工业互联网产品 中国最先进互联网科技公司 顶级互联网科技成果有哪些 苏州五指互联网络科技有限 速维有品互联网信息科技(苏州)有限公司 智帮互联网科技公司最近动向 富国互联网科技002692 宁波跨境商互联网科技有限公司 吉林省码上互联网科技电话 上海万顺科技互联网有限公司招聘 绵阳真晨互联网科技有限公司 互联网加科技 科技互联网企业 A股互联网金融科技公司 上海喔噻互联网科技 狮子座互联网科技培训 国外互联网科技网站 杭州壁岐互联网科技有限公司 民营互联网公司国防科技 佛山市翊博互联网络科技有限公司 米泉互联网科技专业怎么样
0