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所必须的依赖包

yum install gcc-c++ libtool gmake make -yyum install pcre pcre-devel openssl openssl-devel zlib zlib-devel readline readline-devel-y

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

groupadd nginx useradd -d /home/nginx -g nginx -s /sbin/nginx nginx

3.下载编译并装置Openresty

wget https://openresty.org/download/openresty-1.17.8.2.tar.gztar xf openresty-1.17.8.2.tar.gzcd openresty-1.17.8.2./configure --prefix=/usr/local/openresty \--sbin-path=/usr/local/openresty/nginx/sbin/nginx \--conf-path=/usr/local/openresty/nginx/conf/nginx.conf \--pid-path=/usr/local/openresty/nginx/run/nginx.pid \--error-log-path=/usr/local/openresty/nginx/logs/error.log \--http-log-path=/usr/local/openresty/nginx/logs/access.log \--user=nginx \--group=nginx \--with-pcre \--with-stream \--with-threads \--with-file-aio \--with-http_v2_module \--with-http_ssl_module \--with-http_realip_module \--with-http_gzip_static_module \--with-http_stub_status_modulegamke && gmake install

4.为Openresty增加环境变量

vim /etc/profile.d/openresty.shexport PATH=/usr/local/openresty/bin:$PATH

5.增加location配置确认联合了Nginx与Lua的Openresty部署胜利

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

下面曾经实现了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模块

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

2.waf文件介绍

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

以上access.lualib.luainit.lua都是性能实现的lua代码,如果不具备lua的开发能力,咱们个别不会去进行改变 config.lua为各个性能的配置文件 rule-config目录寄存了各种进攻策略规定 咱们须要常常改变config.lua和存储策略的文件

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

Openresty引入WAF模块

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

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

vim /usr/local/openresty/nginx/conf/nginx.conf...http {lua_shared_dict limit 10m;lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";...}        

2.重启Openrestyd

openresty -tnginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successfulopenresty -s reload

3.查看nginx error.log 

正告如下:failed to load the 'resty.core' module 加载 resty.core 外围模块失败,而后上面还有十几行找不到文件的日志

cat /usr/local/openresty/nginx/logs/error.log2020/07/27 15:39:38 [notice] 12728#12728: signal process started2020/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:    no field package.preload['resty.core']    no file '/usr/local/openresty/nginx/conf/waf/resty/core.lua'    no file '/usr/local/openresty/site/lualib/resty/core.so'    no file '/usr/local/openresty/lualib/resty/core.so'    no file './resty/core.so'    no file '/usr/local/lib/lua/5.1/resty/core.so'    no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/core.so'    no file '/usr/local/lib/lua/5.1/loadall.so'    no file '/usr/local/openresty/site/lualib/resty.so'    no file '/usr/local/openresty/lualib/resty.so'    no file './resty.so'    no file '/usr/local/lib/lua/5.1/resty.so'    no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'    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

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

而后批改nginx配置文件来引入此模块,如下格局增加到第二行的前面

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

5.而后保留退出重启看日志

openresty -t && openresty -s reload

确保日志无异样后则胜利引入WAF模块

WAF模块配置文件详解

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

cat /usr/local/openresty/nginx/conf/waf/config.lua--lua文件中,--为行正文,--[[ 这是块正文--]]config_waf_enable = "on"        --是否启用waf模块,值为 on 或 offconfig_log_dir = "/tmp"         --waf的日志地位,日志格局默认为jsonconfig_rule_dir = "/usr/local/openresty/nginx/conf/waf/rule-config" --策略规定目录地位,可依据状况变动config_white_url_check = "on"   --是否开启URL检测config_white_ip_check = "on"    --是否开启IP白名单检测config_black_ip_check = "on"    --是否开启IP黑名单检测config_url_check = "on"         --是否开启URL过滤config_url_args_check = "on"    --是否开启Get参数过滤config_user_agent_check = "on"  --是否开启UserAgent客户端过滤config_cookie_check = "on"      --是否开启cookie过滤config_cc_check = "on"          --是否开启cc攻打过滤config_cc_rate = "10/60"        --cc攻打的速率/工夫,单位为秒;默认示例中为单个IP地址在60秒内拜访同一个页面次数超过10次则认为是cc攻打,则主动禁止此IP地址拜访此页面60秒,60秒后解封(封禁过程中此IP地址仍然能够拜访其它页面,如果同一个页面拜访次数超过10次仍然会被禁止)config_post_check = "on"        --是否开启POST检测config_waf_output = "html"      --对于违反规定的申请则跳转到一个自定义html页面还是指定页面,值为 html 和 redirectconfig_waf_redirect_url = "https://www.unixhot.com"     --指定违反申请后跳转的指定html页面--指定违反规定后跳转的自定义html页面config_output_html=[[<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>网站防火墙</title></head><body><h1 align="center"> 欢送白帽子进行受权平安测试,安全漏洞请分割QQ:1111111。</body></html>]]

IP黑名单配置

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

cat /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule192.168.31.14

而后拜访Openresty地址,如下已返回403被禁止

IP白名单配置

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

cat /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule192.168.31.14

CC攻打过滤

须要在config.lua中开启config_cc_check = "on"参数,而后指定config_cc_rate = "10/60"速率和工夫 CC攻打只须要在config.lua配置文件中指定下面的两个参数即可如下指定在60秒内对于单个IP地址拜访单个页面的次数最大10次,超过10次则主动拉入黑名单,60秒后主动解除

vim /usr/local/openresty/nginx/conf/waf/config.luaconfig_cc_check = "on"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/ 的地址

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

如果你不想让他人拜访根下的/login,那么就能够写入到配置中

cat url.rule\.(htaccess|bash_history)\.(bak|inc|old|mdb|sql|backup|java|class|tgz|gz|tar|zip)$(phpmyadmin|jmx-console|admin-console|jmxinvokerservlet)java\.lang\.svn\//(attachments|upimg|images|css|uploadfiles|html|uploads|templets|static|template|data|inc|forumdata|upload|includes|cache|avatar)/(\\w+).(php|jsp)/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破绽扫描 ... 等等

cat useragent.rule(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进行拜访

#模仿网站下载curl http://192.168.31.219/ --user-agent 'HTTrack'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>网站防火墙</title></head><body><h1 align="center"> 欢送白帽子进行受权平安测试,安全漏洞请分割QQ:1111111。</body></html>#模仿nmap网络扫描curl http://192.168.31.219/ --user-agent 'nmap'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>网站防火墙</title></head><body><h1 align="center"> 欢送白帽子进行受权平安测试,安全漏洞请分割QQ:1111111。</body></html>

增加禁止Chrome浏览器拜访的UserAgent

#追随配置增加到最初cat useragent.rule(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"

参数默认封闭了如下:

cat args.rule\.\./\:\$\$\{select.+(from|limit)(?:(union(.*?)select))having|rongjitestsleep\((\s*)(\d*)(\s*)\)benchmark\((.*)\,(.*)\)base64_decode\((?:from\W+information_schema\W)(?:(?:current_)user|database|schema|connection_id)\s*\((?:etc\/\W*passwd)into(\s+)+(?:dump|out)file\s*group\s+by.+\(xwork.MethodAccessor(?: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)\(xwork\.MethodAccessor(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/java\.lang\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[\<(iframe|script|body|img|layer|div|meta|style|base|object|input)(onmouseover|onerror|onload)\=

咱们进行拜访 http://192.168.31.219/hello?aa=select id from mysql,失去如下,进行匹配

curl 'http://192.168.31.219/hello?aa=select id from mysql'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>网站防火墙</title></head><body><h1 align="center"> 欢送白帽子进行受权平安测试,安全漏洞请分割QQ:1111111。</body></html>

咱们也能够依据本人需要去配置,如下最初增加abcops

cat args.rule\.\./\:\$\$\{select.+(from|limit)(?:(union(.*?)select))having|rongjitestsleep\((\s*)(\d*)(\s*)\)benchmark\((.*)\,(.*)\)base64_decode\((?:from\W+information_schema\W)(?:(?:current_)user|database|schema|connection_id)\s*\((?:etc\/\W*passwd)into(\s+)+(?:dump|out)file\s*group\s+by.+\(xwork.MethodAccessor(?: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)\(xwork\.MethodAccessor(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/java\.lang\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[\<(iframe|script|body|img|layer|div|meta|style|base|object|input)(onmouseover|onerror|onload)\=abcops      

而后咱们进行拜访http://192.168.31.219/hello?aa=abcops也会匹配到规定

异样POST参数策略配置

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

默认POST申请封禁如下,POST封禁内容与GET类似

cat post.rule\.\./select.+(from|limit)(?:(union(.*?)select))having|rongjitestsleep\((\s*)(\d*)(\s*)\)benchmark\((.*)\,(.*)\)base64_decode\((?:from\W+information_schema\W)(?:(?:current_)user|database|schema|connection_id)\s*\((?:etc\/\W*passwd)into(\s+)+(?:dump|out)file\s*group\s+by.+\(xwork.MethodAccessor(?: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)\(xwork\.MethodAccessor(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/java\.lang\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[\<(iframe|script|body|img|layer|div|meta|style|base|object|input)(onmouseover|onerror|onload)\=

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

curl -XPOST 'http://192.168.31.219/hello?aa=select id from mysql'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>网站防火墙</title></head><body><h1 align="center"> 欢送白帽子进行受权平安测试,安全漏洞请分割QQ:1111111。</body></html>

如上命中规定,咱们查看Openrestry日志,查看是否为POST申请

tail -1 /usr/local/openresty/nginx/logs/access.log192.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"

福利:豆花同学为大家精心整顿了一份对于linux和python的学习材料大合集!有须要的小伙伴们,关注豆花集体公众号:python头条!回复关键词“材料合集”即可收费支付!