什么是WAF
Web应用防护系统(也称为:网站应用级入侵防御系统。英文:Web Application Firewall,简称: WAF)。利用国际上公认的一种说法:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提供保护的一款产品。
实现WAF
两种方式
- 使用nginx+lua来实现WAF,须在编译nginx的时候配置上lua
- 部署OpenResty,不需要在编译nginx的时候指定lua
功能列表
- 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。
- 支持URL白名单,将不需要过滤的URL进行定义。
- 支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
- 支持CC攻击防护,单个URL指定时间的访问次数,超过设定值,直接返回403。
- 支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
- 支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。
- 支持URL参数过滤,原理同上。
- 支持日志记录,将所有拒绝的操作,记录到日志中去。
- 日志记录为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
测试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
模拟sql注入,访问
ip/test.sql
,看是否出现拦截模拟参数检测,访问
ip/?id=test
,看是否出现拦截黑白名单
#白名单路径 /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> ]]