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 等都进行一致的高性能响应
WAF介绍
web应用防护系统,web application Firewall,web应用防火墙是通过一些列针对http/https的安全策略来专门为web应用提供保护的一款产品
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进行攻击日志收集、存储、搜索和展示。
备注:CC攻击就是使用代理服务器向受害服务器发送大量貌似合法的请求,攻击者控制某些主机不停地发大量数据包给对方服务器造成服务器资源耗尽,一直到宕机崩溃
Color="echo -e \\033[31m"
End='\033[0m'
$Color"start install openresty"$End
sleep 4
yum install gcc-c++ pcre libtool gmake make pcre-devel zlib zib-devel openssl openssl-devel readline readline-devel -y &>/dev/null
if ! id nginx &>/dev/null;then
useradd -s /sbin/nologin -M nginx
fi
if [ ! -d /usr/local/src ];then
mkdir -p /usr/local/src
fi
cd /usr/local/src || exit 2
if [ ! -f openresty-1.17.8.2.tar.gz ];then
wget -P /usr/local/src/ https://openresty.org/download/openresty-1.17.8.2.tar.gz
fi
tar -xzf openresty-1.17.8.2.tar.gz -C /usr/local/
Openresty_dir=`echo "openresty-1.17.8.2.tar.gz" |sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$Openresty_dir /usr/local/openresty
cd /usr/local/$Openresty_dir || exit 1
./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_module
gmake && gmake install
if [ $? -eq 0 ];then
$Color"openresty install sucessful!"$End
else
$Color"Error:openresty install faile!"$End
exit 1
fi
cat >/etc/profile.d/openresty.sh<<-eof
export PATH=/usr/local/openresty/bin:$PATH
eof
cp /usr/local/openresty/nginx/conf/nginx.conf /usr/local/openresty/nginx/conf/nginx.conf.bak
cat >/usr/local/openresty/nginx/conf/nginx.conf <<-eof
user nginx;
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#revise by lxm start
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld") #通过调用lua来打印HelloWorld
}
}
#revise by lxm end
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
eof
#install WAF
yum install git -y
yum update -y nss curl libcurl
if git clone https://github.com/unixhot/waf.git &>/dev/null;then
cp -a ./waf/waf/ /usr/local/openresty/nginx/conf/
else
$Color"git waf code error"$End
exit 1
fi
git clone https://github.com/openresty/lua-resty-core.git
sed -i '/^http/alua_shared_dict limit 10m;lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua;/usr/local/openresty/lua-resty-core/lib/?.lua;;";\ninit_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";\naccess_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";' /usr/local/openresty/nginx/conf/nginx.conf
/usr/local/openresty/nginx/sbin/nginx -t && /usr/local/openresty/nginx/sbin/nginx -s reload
1、WAF配置文件内容解释说明:
cat /usr/local/openresty/nginx/conf/waf/config.lua
--lua文件中,--为行注释,
--[[
这是块注释
--]]
config_waf_enable = "on" --是否启用waf模块,值为 on 或 off
config_log_dir = "/tmp" --waf的日志位置,日志格式默认为json
config_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 和 redirect
config_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"> 测试测试
</body>
</html>
]]
IP黑白名单配置
cat /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
192.168.161.70
#白名单配置
cat /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
192.168.161.80
CC攻击配置
vim /usr/local/openresty/nginx/conf/waf/config.lua
config_cc_check = "on"
config_cc_rate = "10/60"