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

WAF的功能

支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。

支持URL白名单,将不需要过滤的URL进行定义。

支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。

支持CC攻击防护,单个URL指定时间的访问次数,超过设定值,直接返回403。

支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。

支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。

支持URL参数过滤,原理同上。

支持日志记录,将所有拒绝的操作,记录到日志中去

WAF的特点

异常检测协议

Web应用防火墙会对HTTP的请求进行异常检测,拒绝不符合HTTP标准的请求。并且,它也可以只允许HTTP协议的部分选项通过,从而减少攻击的影响范围。甚至,一些Web应用防火墙还可以严格限定HTTP协议中那些过于松散或未被完全制定的选项。

增强的输入验证

增强输入验证,可以有效防止网页篡改、信息泄露、木马植入等恶意网络入侵行为。从而减小Web服务器被攻击的可能性。

及时补丁

修补Web安全漏洞,是Web应用开发者最头痛的问题,没人会知道下一秒有什么样的漏洞出现,会为Web应用带来什么样的危害。WAF可以为我们做这项工作了——只要有全面的漏洞信息WAF能在不到一个小时的时间内屏蔽掉这个漏洞。当然,这种屏蔽掉漏洞的方式不是非常完美的,并且没有安装对应的补丁本身就是一种安全威胁,但我们在没有选择的情况下,任何保护措施都比没有保护措施更好。

基于规则的保护和基于异常的保护

基于规则的保护可以提供各种Web应用的安全规则,WAF生产商会维护这个规则库,并时时为其更新。用户可以按照这些规则对应用进行全方面检测。还有的产品可以基于合法应用数据建立模型,并以此为依据判断应用数据的异常。但这需要对用户企业的应用具有十分透彻的了解才可能做到,可现实中这是十分困难的一件事情。

状态管理

WAF能够判断用户是否是第一次访问并且将请求重定向到默认登录页面并且记录事件。通过检测用户的整个操作行为我们可以更容易识别攻击。状态管理模式还能检测出异常事件(比如登陆失败),并且在达到极限值时进行处理。这对暴力攻击的识别和响应是十分有利的。

其他防护技术

WAF还有一些安全增强的功能,可以用来解决WEB程序员过分信任输入数据带来的问题。比如:隐藏表单域保护、抗入侵规避技术、响应监视和信息泄露保护。

WAF与网络防火墙的区别

  网络防火墙作为访问控制设备,主要工作在OSI模型三、四层,基于IP报文进行检测。只是对端口做限制,对TCP协议做封堵。其产品设计无需理解HTTP会话,也就决定了无法理解Web应用程序语言如HTML、SQL语言。因此,它不可能对HTTP通讯进行输入验证或攻击规则分析。针对Web网站的恶意攻击绝大部分都将封装为HTTP请求,从80或443端口顺利通过防火墙检测。
  一些定位比较综合、提供丰富功能的防火墙,也具备一定程度的应用层防御能力,如能根据TCP会话异常性及攻击特征阻止网络层的攻击,通过IP分拆和组合也能判断是否有攻击隐藏在多个数据包中,但从根本上说他仍然无法理解HTTP会话,难以应对如SQL注入、跨站脚本、cookie窃取、网页篡改等应用层攻击。
  web应用防火墙能在应用层理解分析HTTP会话,因此能有效的防止各类应用层攻击,同时他向下兼容,具备网络防火墙的功能。

使用Nginx实现403/404

403

使用yum安装nginx

[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install nginx

修改配置文件

root@localhost ~]# vim /etc/nginx/nginx.conf
# server模块中添加以下
        set $block_user_agent 0;
            if ( $http_user_agent ~ "Wget|AgentBench"){
                set $block_user_agent 1;
            }
            if ($block_user_agent = 1) {
                return 403;
            }

启动服务

[root@localhost ~]# systemctl start nginx

测试403

使用wget时返回403

[root@localhost ~]# wget 192.168.1.11/index.html
--2020-05-05 08:02:03--  http://192.168.1.11/index.html
Connecting to 192.168.1.11:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2020-05-05 08:02:03 ERROR 403: Forbidden.

404

修改配置文件

root@localhost ~]# vim /etc/nginx/nginx.conf
# server模块中添加以下
        location ~* "\.(sql|bak|zip|tgz|tar.gz)$"{
            return 404;
        }

访问这些后缀是返回404

重启服务

[root@localhost ~]# systemctl restart nginx

访问验证测试404

创建实验文件a.sql

[root@localhost ~]# echo '123' /usr/share/nginx/html/a.sql
[root@localhost ~]# curl 192.168.1.11/a.sql -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Tue, 05 May 2020 00:07:56 GMT
Content-Type: text/html
Content-Length: 3650
Connection: keep-alive
ETag: "5d958342-e42"

停止服务

[root@localhost ~]# systemctl stop nginx

实现WAF

WAF实现规划

分析步骤如下:解析http请求–>匹配规则–>防御动作–>记录日志

具体实现如下:

解析http请求:协议解析模块

匹配规则:规则检测模块,匹配规则库

防御动作:return 403 或者跳转到自定义界面

日志记录:可以记录到elk中,画出饼图,建议使用json格式

使用openResty配置waf防火墙,不需要编译nginx

安装LuaJIT

安装依赖包和创建程序用户

[root@localhost ~]# yum -y install readline-devel pcre-devel openssl-devel
[root@localhost ~]# useradd -s /sbin/nologin -M www

下载luajit,并编译

[root@localhost ~]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
[root@localhost ~]# tar zxf LuaJIT-2.0.5.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/LuaJIT-2.0.5/
[root@localhost LuaJIT-2.0.5]# make && make install  

调整环境变量

[root@localhost ~]# export LUAJIT_LIB=/usr/local/lib
[root@localhost ~]# export LUAJIT_INC=/usr/local/include/luajit-2.0
[root@localhost ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 

安装OpenResty®

下载

[root@localhost ~]# wget https://openresty.org/download/openresty-1.11.2.2.tar.gz

解压编译安装

[root@localhost ~]# tar zxf openresty-1.11.2.2.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/openresty-1.11.2.2/
[root@localhost openresty-1.11.2.2]# ./configure --prefix=/usr/local/openresty \
--user=www --group=www --with-luajit --with-http_v2_module --with-http_stub_status_module \
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6 \
--with-http_sub_module --with-pcre --with-pcre-jit --with-file-aio --with-http_dav_module
[root@localhost openresty-1.11.2.2]# gmake && gmake install

验证测试

[root@localhost ~]# vim /usr/local/openresty/nginx/conf/nginx.conf
       location /hello {
            default_type text/html;
            content_by_lua_block {
                ngx.say("HelloWorld"\n)
            }
        }

启动nginx服务并访问

[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx
[root@localhost ~]# curl 192.168.1.11/hello
HelloWorld

部署WAF

在github上克隆代码

[root@localhost ~]# git clone https://github.com/unixhot/waf.git
[root@localhost ~]# cp -a ./waf/waf/ /usr/local/openresty/nginx/conf/

修改配置文件

[root@localhost ~]# vim /usr/local/openresty/nginx/conf/nginx.conf
# http模块下添加
    lua_shared_dict limit 50m;    #防cc使用字典,大小50M
    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";

重启服务

[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx -s reload

根据文件中的配置创建日志目录

[root@localhost ~]# mkdir /tmp/waf_logs
[root@localhost ~]# chown nginx.nginx /tmp/waf_logs

waf的模块

配置模块

waf安装好以后,不要直接上生产,而是先记录日志,不做任何动作。确定wafF不产生误杀 config.lua配置模块

[root@localhost ~]# vim /usr/local/openresty/nginx/conf/waf/config.lua
--WAF config file,enable = "on",disable = "off"
--waf status
 config_waf_enable = "on"   #是否开启配置
 --log dir 
 config_log_dir = "/tmp/waf_logs"    #日志记录地址
 --rule setting 
config_rule_dir = "/usr/local/nginx/conf/waf/rule-config"      #匹配规则缩放地址
--enable/disable white url 
config_white_url_check = "on"  #是否开启url检测
--enable/disable white ip 
config_white_ip_check = "on"   #是否开启IP白名单检测
--enable/disable block ip 
config_black_ip_check = "on"   #是否开启ip黑名单检测
--enable/disable url filtering 
config_url_check = "on"      #是否开启url过滤
--enalbe/disable url args filtering 
config_url_args_check = "on"   #是否开启参数检测
--enable/disable user agent filtering 
config_user_agent_check = "on"  #是否开启ua检测
--enable/disable cookie deny filtering 
config_cookie_check = "on"    #是否开启cookie检测
--enable/disable cc filtering 
config_cc_check = "on"   #是否开启防cc攻击
--cc rate the xxx of xxx seconds 
config_cc_rate = "10/60"   #允许一个ip60秒内只能访问10次
--enable/disable post filtering 
config_post_check = "on"   #是否开启post检测
--config waf output redirect/html 
config_waf_output = "html"         #action一个html页面,也可以选择跳转
--if config_waf_output ,setting url 
config_waf_redirect_url = "https://www.feiyiblog.com"
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:841597730。
</body>
</html>
]]

access.lua规则模块

检测顺序:先检查白名单,通过即不检测;再检查黑名单,不通过即拒绝,检查UA,UA不通过即拒绝;检查cookie;URL检查;URL参数检查,post检查;

[root@localhost ~]# vim /usr/local/openresty/nginx/conf/waf/access.lua
require 'init'

function waf_main()
    if white_ip_check() then
    elseif black_ip_check() then
    elseif user_agent_attack_check() then
    elseif cc_attack_check() then
    elseif cookie_attack_check() then
    elseif white_url_check() then
    elseif url_attack_check() then
    elseif url_args_attack_check() then
    --elseif post_attack_check() then
    else
        return
    end
end

waf_main()

重启服务

[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx -s reload

访问测试

[root@localhost ~]# curl 192.168.1.11/a.sql
<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:841597730。
</body>
</html>

查看日志

[root@localhost ~]# cat /tmp/2020-05-05_waf.log
2020-05-05 09:16:53","attack_method":"Deny_URL","req_data":"-","server_name":"localhost"}
{"user_agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like 
Gecko) Chrome\/65.0.3314.0 Safari\/537.36 SE 2.X MetaSr 1.0","rule_tag":
"\\.(bak|inc|old|mdb|sql|backup|java|class|tgz|gz|tar|zip)$","req_url":"\/a.sql",
"client_ip":"192.168.1.2","local_time":"2020-05-05 09:16:53","attack_method":"Deny_URL",
"req_data":"-","server_name":"localhost"}

使用ab压测工具模拟防cc攻击

# 创建测试文件
[root@localhost ~]# vim /usr/local/openresty/nginx/html/index.php
cyj
[root@localhost ~]# yum -y install httpd-tools  # ab工具安装
[root@localhost ~]# ab -c 100 -n 100 http://192.168.1.11/index.php

模拟ip黑名单

[root@localhost ~]# vim /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
192.168.1.2

OpenResty_waf

模拟ip白名单

此文件中的ip不会匹配任何规则

[root@localhost ~]# vim /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
192.168.1.2

其他关于waf的操作还待研究…

评论




正在载入...
PoweredHexo
HostedAliyun
DNSAliyun
ThemeVolantis
UV
PV
BY-NC-SA 4.0