什么是WAF

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

实现WAF

两种方式

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

功能列表

  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较为方便

以下操作的前提是:Centos 7.2 系统

部署OpenResty

# 安装依赖包
yum install -y readline-devel pcre-devel openssl-devel zlib-devel
cd /usr/local/src
# 下载编译安装openresty
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
tar -zxv -f openresty-1.15.8.1.tar.gz
cd openresty-1.15.8.1
./configure --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit
gmake && gmake install

使用yum方式安装,推荐使用

同时结合配置yum下载的rpm包不删除,可以保留安装包

yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty

测试openresty安装,在nginx的配置文件加入

server {
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
}

保存退出,访问ip/hellp,出现HelloWorld证明安装成功。

部署WAF

# 从github克隆waf
git clone https://github.com/unixhot/waf.git
cp -a ./waf/waf /usr/local/openresty/nginx/conf/
waf目录:/usr/local/openresty/nginx/conf/waf
lua配置文件:/usr/local/openresty/nginx/conf/waf/config.lua
Waf的ip黑名单:/usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
Waf的ip白名单:/usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
Waf的规则存放目录:/usr/local/openresty/nginx/conf/waf/rule-config

修改nginx的配置文件,在http里加入

#WAF
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";

保存退出。

#测试配置
/usr/local/openresty/nginx/sbin/nginx –t
#重新加载配置
/usr/local/openresty/nginx/sbin/nginx reload

至此,WAF已经配置成功.

测试WAF

  1. 模拟sql注入,访问ip/test.sql,看是否出现拦截

  2. 模拟参数检测,访问ip/?id=test,看是否出现拦截

  3. 黑白名单

    #白名单路径
    /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
    #黑名单路径
    /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
### WAF配置文件详细说明
#路径
/usr/local/openresty/nginx/conf/waf/config.lua
#详细说明,lua文件中,--为注释
--WAF config file,enable = "on",disable = "off"
--waf status waf状态是否开启
config_waf_enable = "on"
--log dir 日志位置,json格式,根据实际情况修改
config_log_dir = "/tmp"
--rule setting 匹配规则地址,根据实际情况修改
config_rule_dir = "/usr/local/openresty/nginx/conf/waf/rule-config"
--enable/disable white url 是否开启url检测
config_white_url_check = "on"
--enable/disable white ip 是否开启白名单ip检测
config_white_ip_check = "on"
--enable/disable block ip 是否开启黑名单ip检测
config_black_ip_check = "on"
--enable/disable url filtering 是否开启url过滤
config_url_check = "on"
--enalbe/disable url args filtering 是否开启参数检测
config_url_args_check = "on"
--enable/disable user agent filtering 是否开启ua检测
config_user_agent_check = "on"
--enable/disable cookie deny filtering 是否开启cookie检测
config_cookie_check = "on"
--enable/disable cc filtering 是否开启CC检测
config_cc_check = "on"
--cc rate the xxx of xxx seconds 允许一个ip60秒内只能访问10次
config_cc_rate = "10/60"
--enable/disable post filtering 是否开启post检测
config_post_check = "on"
--config waf output redirect/html 拦截开启跳转还是一个html页面
config_waf_output = "html"
--if config_waf_output ,setting url 跳转地址和输出页面
config_waf_redirect_url = "https://www.gov.cn/"
--访问被拦截后出现的页面
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"> 您的行为已违反本网站相关规定,注意操作规范。
</body>
</html>
]]

一些说明

过滤规则在waf/rule-config/下,可根据需求自行调整,每条规则需换行,或者用|分割

	args里面的规则get参数进行过滤的
url是只在get请求url过滤的规则
post是只在post请求过滤的规则
whitelist是白名单,里面的url匹配到不做过滤
blackip.rule是黑名单
user-agent是对user-agent的过滤规则 默认没有开启post参数过滤,在文件access.lua中取消注释即可 WAF日志默认存放在/tmp/日期_waf.log

config.lua文件中的提示信息:

放在两个中括号中

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>网站防火墙</title>
<style>
p {
line-height:20px;
}
ul{ list-style-type:none;}
li{ list-style-type:none;}
</style>
</head> <body style=" padding:0; margin:0; font:14px/1.5 Microsoft Yahei, 宋体,sans-serif; color:#555;"> <div style="margin: 0 auto; width:1000px; padding-top:70px; overflow:hidden;"> <div style="width:600px; float:left;">
<div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">网站防火墙 </div>
<div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的请求带有不合法参数,已被网站管理员设置拦截!</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的内容包含危险的攻击请求</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解决:</p>
<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)检查提交内容;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)如网站托管,请联系空间提供商;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3)普通网站访客,请联系网站管理员;</li></ul>
</div>
</div>
</div>
</body></html>

使用Nginx+Openresty实现WAF功能的更多相关文章

  1. 使用NGINX+LUA实现WAF功能 和nginx 防盗链

    使用NGINX+LUA实现WAF功能 一.了解WAF 1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: WAF) ...

  2. 使用NGINX+Openresty和unixhot_waf开源防火墙实现WAF功能

    使用NGINX+Openresty实现WAF功能 一.了解WAF1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: ...

  3. 使用Nginx+Lua实现waf

    使用Nginx+Lua实现waf 技术内容来自:https://github.com/loveshell/ngx_lua_waf 软件包需求: 1 .Nginx兼容性[最后测试到1.13.6] [ro ...

  4. 安装nginx+ngx_lua支持WAF防护功能

    安装nginx+ngx_lua支持WAF防护功能 nginx lua模块淘宝开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力.nginx以 ...

  5. 基于 orange(nginx+openresty) + docker 实现微服务 网关功能

    摘要 基于 orange(nginx+openresty) + docker 实现微服务 网关功能 ;以实现 docker 独立容器 来跑 独立语言独立环境 在 同一个授权下 运行相关组合程序..年初 ...

  6. 我眼中的 Nginx(六):深入 Nginx/Openresty 服务里的 DNS 解析

    张超:又拍云系统开发高级工程师,负责又拍云 CDN 平台相关组件的更新及维护.Github ID: tokers,活跃于 OpenResty 社区和 Nginx 邮件列表等开源社区,专注于服务端技术的 ...

  7. IIS 7安装ModSecurity实现WAF功能

    ModSecurity 是一款开源Web应用防火墙,支持Apache/Nginx/IIS,可作为服务器基础安全设施,还是不错的选择. 系统环境:window 2008 R2+IIS 7 0X01 Mo ...

  8. Openresty增加waf配置

    Openresty增加waf配置 1. Ngx lua waf 说明 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击 防止svn/备份之类文件泄漏 防止Apach ...

  9. Nginx的一些基本功能极速入门

    本文主要介绍一些Nginx的最基本功能以及简单配置,但不包括Nginx的安装部署以及实现原理. 1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片 ...

随机推荐

  1. mysql 导入sql文件的几种形式

    1.没有登陆mysql的时候以文件的形式导入mysql数据 在students.sql文件中加入这些语句 create table t_student( id int primary key auto ...

  2. python批量json文件转xml文件脚本(附代码)

    场景:在使用了mask rcnn跑实验后标注了大量地json格式文件,现在打算使用yolo和faster rcnn 跑实验 所以需要将之前地json文件转为xml     但是找了很久,没发现有批量处 ...

  3. SpringMVC详细流程(一)

    Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模 ...

  4. 《淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树》

    淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树   曾经的学渣 2014-06-05 18:38:00 浏览1455 云数据库Oceanbase   OceanBase是 ...

  5. Python 可执行对象

    Python 可执行对象 eval/repr eval eval 可以执行字符串类型的表达式 (或 compile() 创建的代码对象(code object) ) 并返回执行结果 eval(expr ...

  6. 安装Rtools

    1.好多工具需要安装Rtools install.packages("installr") install.packages("stringr") ###依赖包 ...

  7. dirb参数解析

    -----------------DIRB v2.22 By The Dark Raver----------------- dirb <url_base> [<wordlist_f ...

  8. 【笔记】Clean Code(持续更新)

    这个暑假出来实习,第一次体会到在一个团队中开发的体验,与网上的网站看到的大为不同,以前看网上说什么程序员写了屎山代码,写了一堆模糊的注释或者说垃圾代码不写注释. 但在我的实习体验中,代码虽然看起来很多 ...

  9. [分布式学习]消息队列之rocketmq笔记

    文档地址 RocketMQ架构 哔哩哔哩上的视频 mq有很多,近期买了<分布式消息中间件实践>这本书,学习关于mq的相关知识.mq大致有有4个功能: 异步处理.比如业务端需要给用户发送邮件 ...

  10. 【Java语言特性学习之六】扩展知识点

    一.SPI机制 二.注解处理机制 三.java native关键字 https://www.cnblogs.com/KingIceMou/p/7239668.html