使用Openresty实现WAF防火墙功能

Openresty简介

OpenResty® 是一个结合了 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

Openresty安装

以下采用CentOS 7.6进行部署

1.安装依赖开发组件

  • pcre-devel: 扩展的正则表达式引擎,为了使Nginx处理更复杂的正则表达式机制
  • openssl-devel:–with-http_ssl_module使用该模块必需装openssl库,来实现http支持https协议
  • zlib-devel:zlib库是网络通信压缩库,ngx_http_gzip_module(gzip压缩模块)所必需的
  • readline-devel:readline是安装Openresty所必须的依赖包
  1. yum install gcc-c++ libtool gmake make -y
  2. yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel readline readline-devel-y

2.创建nginx用户组
Nginx的Master主进程以root用户身份运行,而worker子进程我们指定它为nginx用户运行

  1. groupadd nginx
  2. useradd -d /home/nginx -g nginx -s /sbin/nginx nginx

3.下载编译并安装Openresty

  1. wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
  2. tar xf openresty-1.17.8.2.tar.gz
  3. cd openresty-1.17.8.2
  4. ./configure --prefix=/usr/local/openresty \
  5. --sbin-path=/usr/local/openresty/nginx/sbin/nginx \
  6. --conf-path=/usr/local/openresty/nginx/conf/nginx.conf \
  7. --pid-path=/usr/local/openresty/nginx/run/nginx.pid \
  8. --error-log-path=/usr/local/openresty/nginx/logs/error.log \
  9. --http-log-path=/usr/local/openresty/nginx/logs/access.log \
  10. --user=nginx \
  11. --group=nginx \
  12. --with-pcre \
  13. --with-stream \
  14. --with-threads \
  15. --with-file-aio \
  16. --with-http_v2_module \
  17. --with-http_ssl_module \
  18. --with-http_realip_module \
  19. --with-http_gzip_static_module \
  20. --with-http_stub_status_module
  21. gamke && gmake install

4.为Openresty添加环境变量

  1. vim /etc/profile.d/openresty.sh
  2. export PATH=/usr/local/openresty/bin:$PATH

5.添加location配置确认结合了Nginx与Lua的Openresty部署成功

  1. location /hello {
  2. default_type text/html;
  3. content_by_lua_block {
  4. ngx.say("HelloWorld") #通过调用lua来打印HelloWorld
  5. }
  6. }

上面已经实现了Openresty的部署,下面将结合WAF实现防火墙

什么是WAF

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

实现WAF

实现WAF的方式有两种:

  1. 使用nginx+lua来实现WAF,须在编译nginx的时候配置上lua
  2. 部署OpenResty,不需要在编译nginx的时候指定lua

这里我们采用的是第二种

WAF一句话描述,就是解析HTTP请求(协议解析模块),规则检测(规则模块),做不同的防御动作(动作模块),并将防御过程(日志模块)记录下来。所以本文中的WAF的实现由五个模块(配置模块、协议解析模块、规则模块、动作模块、错误处理模块)组成。

WAF的功能

  1. 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。
  2. 支持URL白名单,将不需要过滤的URL进行定义。
  3. 支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
  4. 支持CC攻击防护,单个URL指定时间的访问次数,超过设定值,直接返回403。
  5. 支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
  6. 支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。
  7. 支持URL参数过滤,原理同上。
  8. 支持日志记录,将所有拒绝的操作,记录到日志中去。
  9. 日志记录为JSON格式,便于日志分析,例如使用ELKStack进行攻击日志收集、存储、搜索和展示。

部署WAF

WAF已经有人通过lua写出了这个开源的功能,在此直接拿来用即可。
GitHub地址:https://github.com/unixhot/waf

1.下载waf模块

  1. git clone https://github.com/unixhot/waf.git
  2. cp -a ./waf/waf /usr/local/openresty/nginx/conf/

2.waf文件介绍

  1. ls -lrth /usr/local/openresty/nginx/conf/waf/
  2. 总用量 20K
  3. -rw-r--r-- 1 root root 408 7 27 09:30 access.lua
  4. -rw-r--r-- 1 root root 2.3K 7 27 09:30 lib.lua
  5. -rw-r--r-- 1 root root 5.4K 7 27 09:30 init.lua
  6. -rw-r--r-- 1 root root 1.3K 7 27 09:30 config.lua
  7. drwxr-xr-x 2 root root 158 7 27 09:57 rule-config

以上access.lualib.luainit.lua都是功能实现的lua代码,如果不具备lua的开发能力,我们一般不会去进行改动
config.lua为各个功能的配置文件
rule-config目录存放了各种防御策略规则
我们需要经常改动config.lua和存储策略的文件

  1. ls /usr/local/openresty/nginx/conf/waf/rule-config/ -rlth
  2. 总用量 24K
  3. -rw-r--r-- 1 root root 652 7 27 09:30 cookie.rule #Cookie策略文件
  4. -rw-r--r-- 1 root root 749 7 27 09:30 args.rule #异常Get参数策略文件
  5. -rw-r--r-- 1 root root 6 7 27 09:30 whiteurl.rule #白名单URL策略文件
  6. -rw-r--r-- 1 root root 0 7 27 09:30 whiteip.rule #IP白名单策略文件
  7. -rw-r--r-- 1 root root 173 7 27 09:30 useragent.rule #异常UserAgent策略文件
  8. -rw-r--r-- 1 root root 307 7 27 09:30 url.rule #异常URL策略文件
  9. -rw-r--r-- 1 root root 739 7 27 09:30 post.rule #异常POST参数策略文件
  10. -rw-r--r-- 1 root root 0 7 27 09:57 blackip.rule #IP黑名单策略文件

Openresty引入WAF模块

1.修改nginx配置来引入WAF模块

如下在Nginx中加入以下配置来引入WAF模块

  1. vim /usr/local/openresty/nginx/conf/nginx.conf
  2. ...
  3. http {
  4. lua_shared_dict limit 10m;
  5. lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
  6. init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
  7. access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
  8. ...
  9. }

2.重启Openrestyd

  1. openresty -t
  2. nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
  4. openresty -s reload

3.查看nginx error.log
警告如下:failed to load the 'resty.core' module 加载 resty.core 核心模块失败,然后下面还有十几行找不到文件的日志

  1. cat /usr/local/openresty/nginx/logs/error.log
  2. 2020/07/27 15:39:38 [notice] 12728#12728: signal process started
  3. 2020/07/27 15:39:38 [alert] 27311#27311: 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: module 'resty.core' not found:
  4. no field package.preload['resty.core']
  5. no file '/usr/local/openresty/nginx/conf/waf/resty/core.lua'
  6. no file '/usr/local/openresty/site/lualib/resty/core.so'
  7. no file '/usr/local/openresty/lualib/resty/core.so'
  8. no file './resty/core.so'
  9. no file '/usr/local/lib/lua/5.1/resty/core.so'
  10. no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/core.so'
  11. no file '/usr/local/lib/lua/5.1/loadall.so'
  12. no file '/usr/local/openresty/site/lualib/resty.so'
  13. no file '/usr/local/openresty/lualib/resty.so'
  14. no file './resty.so'
  15. no file '/usr/local/lib/lua/5.1/resty.so'
  16. no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
  17. no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/openresty/nginx/conf/nginx.conf:130

4.解决办法如下
上面告警是缺少 lua-resty-core 模块,从而找不到这些信息,所以我们要下载lua-resty-core模块然后引入到Openresty

  1. git clone https://github.com/openresty/lua-resty-core.git

然后修改nginx配置文件来引入此模块,如下格式添加到第二行的后面

  1. lua_shared_dict limit 10m;
  2. lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua;/usr/local/openresty/lua-resty-core/lib/?.lua;;";
  3. init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
  4. access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";

5.然后保存退出重启看日志

  1. openresty -t && openresty -s reload

确保日志无异常后则成功引入WAF模块

WAF模块配置文件详解

来学习一下waf/config.lua配置文件中的内容

  1. cat /usr/local/openresty/nginx/conf/waf/config.lua
  2. --lua文件中,--为行注释,
  3. --[[
  4. 这是块注释
  5. --]]
  6. config_waf_enable = "on" --是否启用waf模块,值为 on off
  7. config_log_dir = "/tmp" --waf的日志位置,日志格式默认为json
  8. config_rule_dir = "/usr/local/openresty/nginx/conf/waf/rule-config" --策略规则目录位置,可根据情况变动
  9. config_white_url_check = "on" --是否开启URL检测
  10. config_white_ip_check = "on" --是否开启IP白名单检测
  11. config_black_ip_check = "on" --是否开启IP黑名单检测
  12. config_url_check = "on" --是否开启URL过滤
  13. config_url_args_check = "on" --是否开启Get参数过滤
  14. config_user_agent_check = "on" --是否开启UserAgent客户端过滤
  15. config_cookie_check = "on" --是否开启cookie过滤
  16. config_cc_check = "on" --是否开启cc攻击过滤
  17. config_cc_rate = "10/60" --cc攻击的速率/时间,单位为秒;默认示例中为单个IP地址在60秒内访问同一个页面次数超过10次则认为是cc攻击,则自动禁止此IP地址访问此页面60秒,60秒后解封(封禁过程中此IP地址依然可以访问其它页面,如果同一个页面访问次数超过10次依然会被禁止)
  18. config_post_check = "on" --是否开启POST检测
  19. config_waf_output = "html" --对于违反规则的请求则跳转到一个自定义html页面还是指定页面,值为 html redirect
  20. config_waf_redirect_url = "https://www.unixhot.com" --指定违反请求后跳转的指定html页面
  21. --指定违反规则后跳转的自定义html页面
  22. config_output_html=[[
  23. <html>
  24. <head>
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  26. <meta http-equiv="Content-Language" content="zh-cn" />
  27. <title>网站防火墙</title>
  28. </head>
  29. <body>
  30. <h1 align="center"> 欢迎白帽子进行授权安全测试,安全漏洞请联系QQ1111111
  31. </body>
  32. </html>
  33. ]]

IP黑名单配置

需要在config.lua中开启config_black_ip_check = "on"参数
IP黑名单配置非常简单,这个与Nginx的ngx_http_access_module模块原理是一致的,只需要把拒绝的地址加入到 waf/rule-config/blackip.rule文件中即可

  1. cat /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
  2. 192.168.31.14

然后访问Openresty地址,如下已返回403被禁止

IP白名单配置

需要在config.lua中开启config_white_ip_check = "on"参数
IP白名单与黑名单相反,添加到IP白名单中的IP不受WAF限制,具体请自行测试

  1. cat /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
  2. 192.168.31.14

CC攻击过滤

需要在config.lua中开启config_cc_check = "on"参数,然后指定config_cc_rate = "10/60"速率和时间
CC攻击只需要在config.lua配置文件中指定上面的两个参数即可

如下指定在60秒内对于单个IP地址访问单个页面的次数最大10次,超过10次则自动拉入黑名单,60秒后自动解除

  1. vim /usr/local/openresty/nginx/conf/waf/config.lua
  2. config_cc_check = "on"
  3. config_cc_rate = "10/60"

然后进行测试,如下刷新10次以后就变为来403

我们换个页面再次刷新,如下换个页面可以正常访问,不过连续对一个页面60秒内刷新10次以后将也被拉入黑名单

注:以上的请求速率和时间只能作为参考,大家线上使用具体还要根据相应环境进行调整

异常URL策略配置

需要在config.lua中开启config_url_check = "on"参数
然后定义rule-config/url.rule文件,url.rule文件默认为如下,如果匹配到规则的将跳转到由config.lua中config_waf_output = "html"参数指定的页面

  1. 禁止URL访问 .htaccess|.bash_history 的文件
  2. 禁止URL访问包含带有phpmyadmin|jmx-console|admin-console|jmxinvokerservlet地址
  3. 禁止URL访问包含 java.lang 的地址
  4. 禁止URL访问包含 .svn/ 的地址
  1. cat url.rule
  2. \.(htaccess|bash_history)
  3. \.(bak|inc|old|mdb|sql|backup|java|class|tgz|gz|tar|zip)$
  4. (phpmyadmin|jmx-console|admin-console|jmxinvokerservlet)
  5. java\.lang
  6. \.svn\/
  7. /(attachments|upimg|images|css|uploadfiles|html|uploads|templets|static|template|data|inc|forumdata|upload|includes|cache|avatar)/(\\w+).(php|jsp)

假如你不想让别人访问根下的/login,那么就可以写入到配置中

  1. cat url.rule
  2. \.(htaccess|bash_history)
  3. \.(bak|inc|old|mdb|sql|backup|java|class|tgz|gz|tar|zip)$
  4. (phpmyadmin|jmx-console|admin-console|jmxinvokerservlet)
  5. java\.lang
  6. \.svn\/
  7. /(attachments|upimg|images|css|uploadfiles|html|uploads|templets|static|template|data|inc|forumdata|upload|includes|cache|avatar)/(\\w+).(php|jsp)
  8. /login

然后进行重启后访问,如下就跳转到了我们在config.lua中指定的页面,此页面可根据需求进行修改。如果上面默认的url规则匹配到了你的地址,那么你就可以把相应配置去掉

异常UserAgent策略配置

需要在config.lua中开启config_user_agent_check = "on"参数

WAF模块中默认封锁了以下UserAgent,如
HTTrack网站下载
namp网络扫描
audit网络审计
dirbuster网站目录扫描
pangolin SQL注入工具
scan网络扫描
hydra密码暴力破解
libwww漏洞工具
sqlmap自动SQL注入工具
w3af网络扫描
Nikto Web漏洞扫描

等等

  1. cat useragent.rule
  2. (HTTrack|harvest|audit|dirbuster|pangolin|nmap|sqln|-scan|hydra|Parser|libwww|BBBike|sqlmap|w3af|owasp|Nikto|fimap|havij|PycURL|zmeu|BabyKrokodil|netsparker|httperf|bench)

我们正常访问URL是没问题的,下面来模拟一个非法的UserAgent进行访问

  1. #模拟网站下载
  2. curl http://192.168.31.219/ --user-agent 'HTTrack'
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta http-equiv="Content-Language" content="zh-cn" />
  7. <title>网站防火墙</title>
  8. </head>
  9. <body>
  10. <h1 align="center"> 欢迎白帽子进行授权安全测试,安全漏洞请联系QQ1111111
  11. </body>
  12. </html>
  13. #模拟nmap网络扫描
  14. curl http://192.168.31.219/ --user-agent 'nmap'
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  18. <meta http-equiv="Content-Language" content="zh-cn" />
  19. <title>网站防火墙</title>
  20. </head>
  21. <body>
  22. <h1 align="center"> 欢迎白帽子进行授权安全测试,安全漏洞请联系QQ1111111
  23. </body>
  24. </html>

添加禁止Chrome浏览器访问的UserAgent

  1. #跟随配置添加到最后
  2. cat useragent.rule
  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|Chrome)

然后重启Openrestry,通过Chrome浏览器进行访问

如上所示全部命中了WAF的规则

异常Get参数策略配置

需要在config.lua配置中开启config_url_args_check = "on"参数

默认封锁了如下:

  1. cat args.rule
  2. \.\./
  3. \:\$
  4. \$\{
  5. select.+(from|limit)
  6. (?:(union(.*?)select))
  7. having|rongjitest
  8. sleep\((\s*)(\d*)(\s*)\)
  9. benchmark\((.*)\,(.*)\)
  10. base64_decode\(
  11. (?:from\W+information_schema\W)
  12. (?:(?:current_)user|database|schema|connection_id)\s*\(
  13. (?:etc\/\W*passwd)
  14. into(\s+)+(?:dump|out)file\s*
  15. group\s+by.+\(
  16. xwork.MethodAccessor
  17. (?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
  18. xwork\.MethodAccessor
  19. (gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
  20. java\.lang
  21. \$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
  22. \<(iframe|script|body|img|layer|div|meta|style|base|object|input)
  23. (onmouseover|onerror|onload)\=

我们进行访问 http://192.168.31.219/hello?aa=select id from mysql,得到如下,进行匹配

  1. curl 'http://192.168.31.219/hello?aa=select id from mysql'
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Language" content="zh-cn" />
  6. <title>网站防火墙</title>
  7. </head>
  8. <body>
  9. <h1 align="center"> 欢迎白帽子进行授权安全测试,安全漏洞请联系QQ1111111
  10. </body>
  11. </html>

我们也可以根据自己需求去配置,如下最后添加abcops

  1. cat args.rule
  2. \.\./
  3. \:\$
  4. \$\{
  5. select.+(from|limit)
  6. (?:(union(.*?)select))
  7. having|rongjitest
  8. sleep\((\s*)(\d*)(\s*)\)
  9. benchmark\((.*)\,(.*)\)
  10. base64_decode\(
  11. (?:from\W+information_schema\W)
  12. (?:(?:current_)user|database|schema|connection_id)\s*\(
  13. (?:etc\/\W*passwd)
  14. into(\s+)+(?:dump|out)file\s*
  15. group\s+by.+\(
  16. xwork.MethodAccessor
  17. (?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
  18. xwork\.MethodAccessor
  19. (gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
  20. java\.lang
  21. \$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
  22. \<(iframe|script|body|img|layer|div|meta|style|base|object|input)
  23. (onmouseover|onerror|onload)\=
  24. abcops

然后我们进行访问http://192.168.31.219/hello?aa=abcops也会匹配到规则

异常POST参数策略配置

需要在config.lua中开启config_post_check = "on"选项

默认POST请求封禁如下,POST封禁内容与GET相似

  1. cat post.rule
  2. \.\./
  3. select.+(from|limit)
  4. (?:(union(.*?)select))
  5. having|rongjitest
  6. sleep\((\s*)(\d*)(\s*)\)
  7. benchmark\((.*)\,(.*)\)
  8. base64_decode\(
  9. (?:from\W+information_schema\W)
  10. (?:(?:current_)user|database|schema|connection_id)\s*\(
  11. (?:etc\/\W*passwd)
  12. into(\s+)+(?:dump|out)file\s*
  13. group\s+by.+\(
  14. xwork.MethodAccessor
  15. (?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
  16. xwork\.MethodAccessor
  17. (gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
  18. java\.lang
  19. \$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
  20. \<(iframe|script|body|img|layer|div|meta|style|base|object|input)
  21. (onmouseover|onerror|onload)\=

直接对POST策略进行提交请求,通过curl -XPOST来进行提交POST请求

  1. curl -XPOST 'http://192.168.31.219/hello?aa=select id from mysql'
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Language" content="zh-cn" />
  6. <title>网站防火墙</title>
  7. </head>
  8. <body>
  9. <h1 align="center"> 欢迎白帽子进行授权安全测试,安全漏洞请联系QQ1111111
  10. </body>
  11. </html>

如上命中规则,我们查看Openrestry日志,查看是否为POST请求

  1. tail -1 /usr/local/openresty/nginx/logs/access.log
  2. 192.168.31.217 - - [27/Jul/2020:18:21:32 +0800] "POST /hello?aa=select id from mysql HTTP/1.1" 403 313 "-" "curl/7.29.0"

原文地址:https://abcops.cn/1732.html
原文作者:好好青年

猜你喜欢

转载自www.cnblogs.com/cheyunhua/p/13395745.html