1、WAF相关概念介绍:

(1)WAF简介:

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

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

a、防止SQL注入、本地包含、部分溢出、Fuzzing测试、XSSWeb 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-moduleWeb应用防火墙,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.confhttp配置段中新增代码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/)"