一、找到压缩包进行编译安装:
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/hello,出现HelloWorld证明安装成功。
三、接下来部署WAF
在github里找waf的压缩包,解压后移动到/usr/local/openresty/nginx/conf/
cp -a ./waf/waf /usr/local/openresty/nginx/conf/
也可以从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,看是否出现拦截
配置反向代理
upstream my_server {
server 10.0.0.2:8080; (Nginx地址)
keepalive 2000;
}
server {
listen 80; (需要访问的地址 的端口号)
server_name 10.0.0.1; (需要访问的地址 )
client_max_body_size 1024M;
location / {
proxy_pass http://my_server/;
proxy_set_header Host $host:$server_port;
}
#路径
/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>
]]