CentOS 7.7 Nginx基于Lua模块实现WAF应用防火墙

1、WAF相关概念介绍:

(1)WAF简介:

WAF:Web 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 httpd-tools

3、安装LuaJIT 2.1

LuaJIT是采用C语言编写的Lua代码解释器,http://luajit.org/download.html中的稳定版本LuaJIT-2.0.5,版本太低,不建议使用,此次演示使用的是LuaJIT-2.1.0-beta3

# git clone https://github.com/openresty/luajit2.git

# cd luajit2

# make && make install PREFIX=/usr/local/luajit2

# ln -sv /usr/local/luajit2/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

4、测试Lua环境:

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

# lua /tmp/hello.lua

image.png

# lua

image.png

5、下载解压ngx_devel_kit模块:

NDKNginx Development Kit,是一个拓展Nginx服务器核心功能的模块,https://github.com/vision5/ngx_devel_kit

# tar -xf ngx_devel_kit-0.3.1.tar.gz

6、下载解压lua-nginx-module模块:

lua-nginx-module:将Lua的强大功能嵌入到Nginx服务器中,https://github.com/openresty/lua-nginx-module

# tar -xf lua-nginx-module-0.10.15.tar.gz

与其兼容的Nginx版本(不兼容目前最新稳定1.16.1版本):

image.png

7、编译Nginx稳定版本1.14.2

# useradd -s /sbin/nologin -M nginx

# tar -xf nginx-1.14.2.tar.gz -C /usr/src

# cd /usr/src/nginx-1.14.2/

export LUAJIT_LIB=/usr/local/luajit2/lib

export LUAJIT_INC=/usr/local/luajit2/include/luajit-2.1

# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --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-compat --with-pcre --add-module=/software/ngx_devel_kit-0.3.1 --add-module=/software/lua-nginx-module-0.10.15 --with-ld-opt="-Wl,-rpath,/usr/local/luajit2/lib"

# make -j2 && make install

备注:

(1)--add-module后的模块目录中要有config文件

(2)Makefile文件的需要执行makemake install

8、配置Nginx环境变量,并启动Nginx

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

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

# . /etc/profile.d/nginx.sh

# nginx -v

image.png

# nginx

image.png

# ss -tunlp | grep -w :80

image.png

9、测试Nginx Lua模块:

# cd /usr/local/nginx/conf

# cp nginx.conf{,.bak}

# vim nginx.conf

http配置段中新增代码:lua_load_resty_core off;

server配置段中新增如下location

location /lua {

default_type     'text/plain';

content_by_lua   'ngx.say("Hello Lua")';

}

# nginx -t

# nginx -s reload

image.png

备注:在http配置段中新增lua_load_resty_core off;代码,启动Nginx时就不会提示上述错误信息。

10、创建保存日志的目录:

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

11、下载解压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/nginx/conf

# cd /usr/local/nginx/conf

# mv ngx_lua_waf-0.7.2 waf

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

备注:waf目录主要结构

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

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

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

image.png

(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做匹配,匹配到则不做过滤。

12、确认config.lua配置文件中waf规则目录的路径是否正确:

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

备注:config.lua配置文件

指令

含义

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

规则存放目录

attacklog="on"

开启日志

logdir="/usr/local/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

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

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

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

lua_shared_dict  limit  10m;

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

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

# nginx -t

# nginx -s reload

14、测试WAF应用防火墙:

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

image.png

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

image.png

备注:ab命令选项

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

-c concurrency:同时并发执行的请求数,默认为1

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

image.png

192.168.0.120 [2020-02-17 00:47:17] "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/)"

猜你喜欢

转载自blog.51cto.com/qiuyue/2471541
7.7
今日推荐