系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

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

时间:2020-02-17来源:系统城作者:电脑系统城

1、WAF相关概念介绍:
  1. WAF简介:
WAF:Web Appalication Firewall,Web应用防火墙,是一种工作在应用层的、通过一系列针对HTTP/HTTPS的安全策略为Web应用提供安全防护的产品。
  1. WAF可以实现如下功能:
  2. 防止SQL注入、本地包含、部分溢出、Fuzzing测试、XSS等Web Attack;
  3. 防止SVN/备份之类的文件泄漏;
  4. 防止Apache Bench之类的压测工具Attack;
  5. 屏蔽常见的Hacker扫描工具;
  6. 屏蔽异常的网络请求;
  7. 屏蔽图片附件类目录的PHP执行权限;
  8. 防止Webshell上传等。
  9. 安装依赖软件包:
# 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
  1. 安装LuaJIT 2.1:
  2. 是采用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
  1. 测试Lua环境:
# vim /tmp/hello.lua --> print("Hello Lua")
# lua /tmp/hello.lua
image.png
# lua
image.png
  1. 下载解压ngx_devel_kit模块:
  2. Nginx Development Kit,是一个拓展Nginx服务器核心功能的模块,https://github.com/vision5/ngx_devel_kit
# tar -xf ngx_devel_kit-0.3.1.tar.gz
  1. 载解压lua-nginx-module模块:
  2. :将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
  1. 编译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 
# make -j2 && make install
  •  
  • --add-module后的模块目录中要有config文件
  • 有Makefile文件的需要执行make和make install
  • 配置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
  1. 测试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时就不会提示上述错误信息。
  • 创建保存日志的目录:
# mkdir -pv /usr/local/nginx/logs/hack
  1. 下载解压ngx_lua_waf模块:
  2. :基于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目录主要结构
  • config.lua:配置文件;
  • init.lua:规则函数;
  • waf.lua:定义WAF检测顺序;
image.png
  1. wafconf:保存过滤规则的目录,每条规则需换行或用|分割;
  2. wafconf/args:按照GET参数过滤(默认已开启);
  3. wafconf/cookie:按照Cookie过滤;
  4. wafconf/post:按照POST请求过滤(默认已开启);
  5. wafconf/url:按照GET请求URL过滤;
  6. wafconf/user-agent:按照User Agent过滤;
  7. afconf/whiteurl:按照白名单中的URL做匹配,匹配到则不做过滤。
  8. 确认config.lua配置文件中waf规则目录的路径是否正确:
# vim /usr/local/nginx/conf/waf/config.lua --> RulePath="/usr/local/nginx/conf/waf/wafconf/"
  • config.lua配置文件
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • 日志目录
  •  
  • URL访问
  •  
  •  
  •  
  • Cookie Attack
  •  
  • Post Attack
  •  
  • URL白名单
  •  
  •  
  •  
  1. 白名单,多个IP之间使用逗号分隔
  •  
  1. 黑名单,多个IP之间使用逗号分隔
  •  
  • CC Attack(需要在nginx.conf的http配置段中新增代码lua_shared_dict limit 10m;)
  1.  
  • CC Attack频率,单位为秒
  • 1分钟同一个IP只能请求同一个地址100次
  1. 修改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
  1. 测试WAF应用防火墙:
  2. 模拟URL参数检测:http://192.168.0.120/lua?id=../etc/passwd
image.png
  1. 使用ab命令模拟CC Attack:# ab -n 20000 -c 100 http://192.168.0.120/lua
image.png
 
  • ab命令选项
-n requests:需要执行的请求总数,默认为1
-c concurrency:同时并发执行的请求数,默认为1
  1. 查看日志:# tail /usr/local/nginx/logs/hack/localhost_2020-02-17_sec.log
image.png
  1.  [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/)"
分享到:

相关信息

  • linux定时关机设置教程

    当linux在运作时不能直接关闭电源容易将档案系统损毁,因此需要用shutdown以安全的方式关闭,那么这个操作该怎么实现呢?下面就为大家带来了详细教程。...

    2022-11-07

  • linux强制删除文件教程

    由于linux系统和我们常用的windows系统是不一样的,所以如果是初学者,可能会不知道linux怎么强制删除文件,其实我们只要打开终端,使用命令就可以删除了。...

    2022-11-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载