Nginx + Lua 搭建网站WAF防火墙
前言
对于项目里面只是使用代理等常用功能,在线安装即可,如需制定化模块,则推荐编译安装
PS:本文不仅仅包含Nginx相关的知识点,还包含了逆天学习方法(对待新事物的处理)
官方网站:https://nginx.org/
Nginx书籍:
1.在线安装
1.1.修改yum源地址
清华源:https://mirrors.tuna.tsinghua.edu.cn/help/centos/
更新软件包缓存:yum makecache
1.2.在线安装Nginx
在线安装比较简单,参考官方文档即可:https://nginx.org/en/linux_packages.html
PS:线上选
stable
的就行了,记得把$releasever
改成你的版本号,eg:7
安装图示:
# 创建nginx的yum
vi /etc/yum.repos.d/nginx.repo
# 内容如下:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
# 在线安装
yum install nginx -y
1.3.端口放行
放行80端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
PS:规则生效:
firewall-cmd --reload
1.4.验证安装
2.知识拓展
2.1.编译参数
离线安装可以参考在线安装的配置:nginx -V
:编译参数(nginx -v
:查看版本)
编译参数详解(点我展开)
# 1.编译选项
## Nginx的安装主目录
--prefix=/etc/nginx \
## Nginx的执行文件路径
--sbin-path=/usr/sbin/nginx \
## Nginx的模块目录
--modules-path=/usr/lib64/nginx/modules \
## Nginx的配置文件路径
--conf-path=/etc/nginx/nginx.conf \
## Nginx的错误日志路径
--error-log-path=/var/log/nginx/error.log \
## Nginx的访问日志
--http-log-path=/var/log/nginx/access.log \
## Nginx的pid文件路径
--pid-path=/var/run/nginx.pid \
## Nginx的lock路径
--lock-path=/var/run/nginx.lock \
# 2.编译选项(执行对应模块时Nginx缓存文件的存放地址)
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
# 3.设置Nginx权限组(虽然root权限安装,但可以指定nginx的运行权限)
--user=nginx \
--group=nginx \
# 4.优化
## 启用gzip压缩模块(常用)
--with-http_gzip_static_module \
--with-http_gunzip_module \
# 文件使用aio异步操作
--with-file-aio \
## C系列优化
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
## 设置附加的参数,链接系统库
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
# HTTP内容替换
--with-http_sub_module \
# 其他优化选项 or 模块
--with-compat \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
2.2.安装目录
在线安装的包都可以通过:rpm -ql xxx
查看安装到哪些目录
安装目录详解(点我展开)
[root@localhost dnt]# rpm -ql nginx
# Nginx使用用logrotate服务对日志进行切割的配置文件(eg:按天切割)
/etc/logrotate.d/nginx
# Nginx的核心目录
/etc/nginx
# 主要配置文件,Nginx启动的时候会读取
/etc/nginx/nginx.conf
/etc/nginx/conf.d
# nginx.conf没变更久读default.conf(默认Server加载的文件)
/etc/nginx/conf.d/default.conf
# Nginx对Python的wsgi配置
/etc/nginx/uwsgi_params
# fastcgi配置
/etc/nginx/fastcgi_params
# scgi配置
/etc/nginx/scgi_params
# Nginx缓存目录
/var/cache/nginx
# Nginx日志目录
/var/log/nginx
# Nginx默认网站存放的路径
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
# 设置http的Content-Type与扩展名对应关系的配置文件
/etc/nginx/mime.types
# Nginx模块所在目录
/usr/lib64/nginx/modules
/etc/nginx/modules
# 二进制执行文件
/usr/sbin/nginx
/usr/sbin/nginx-debug
# 编码转换的映射文件
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf
# 配置CentOS守护进程对Nginx的管理方式
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
# Nginx的文档
/usr/share/doc/nginx-1.16.0
/usr/share/doc/nginx-1.16.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
# Nginx检测更新命令
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
2.3.默认配置
配置语法检查:nginx -t -c /etc/nginx/nginx.conf
PS:不重启的方式加载配置:
Nginx -s reload -c /etc/nginx/nginx.conf
全局以及服务级别的配置:
参数 | 说明 |
---|---|
user |
使用用户来运行nginx |
worker_processes |
工作进程数 |
error_log |
nginx的错误日记 |
pid | nginx启动时的pid |
events相关配置:
参数 | 说明 |
---|---|
worker_connections |
每个进程的最大连接数 |
use |
工作进程数 |
常用中间件配置:
http {
......
server {
listen 80; # 端口号
server_name localhost; # 域名
# 路径访问控制(默认访问路径,eg:/ ==> 根目录)
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm index.py; # 首页配置
}
error_page 500 502 503 504 /50x.html; # 错误页面(可以自定义添404页面,error_page 404 /404.html;...)
# 访问xxx/50x.html的时候去指定目录找
location = /50x.html {
root /usr/share/nginx/html; # 错误页面所在路径
}
}
# 一个server配置一个虚拟 or 独立的站点(通过listen和server_name来区别多个server)
server {
......
}
}
2.4.systemctl配置
nginx:(等会编译安装的时候可以参考)
[root@localhost dnt]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
nginx-debug:
[root@localhost dnt]# cat /usr/lib/systemd/system/nginx-debug.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx-debug -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
3.编译安装
3.1.安装编译环境
一步到位:yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
简单拆分解析一下:
- Nginx使用
C/C++
编写的,安装一下依赖:yum install gcc-c++ -y
- Nginx需要使用PCRE来进行正则解析:
yum install pcre pcre-devel -y
- 现在服务器和浏览器一般都是使用gzip:
yum install -y zlib zlib-devel -y
- 让Nginx支持https:
yum install openssl openssl-devel -y
3.2.Nginx编译安装
3.2.1.下载解压
先编译安装一下,后面说lua模块的时候再重新编译下就行了
下载:curl -o nginx.tar.gz http://nginx.org/download/nginx-1.16.0.tar.gz
解压:tar -zxvf nginx.tar.gz
3.2.2.配置编译参数
参考前面说的在线版Nginx来设置编译参数的配置:
PS:
nginx -V
切换到nginx的解压目录:cd nginx-1.16.0
然后执行下面命令
PS:root权限编译哦~
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
3.2.3.进行编译安装
接着编译安装:make && make install
PS:提速:
make -j 4 && make install
3.2.4.配置systemctl
利用systemctl添加自定义系统服务
# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
PS:如果不生效可以重载下systemctl:systemctl daemon-reload
3.2.5.端口放行
放行80端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
PS:规则生效:
firewall-cmd --reload
3.2.6.验证
运行的时候如果出现nginx: [emerg] getpwnam("nginx") failed
的错误可以参考我写这篇文章:https://www.cnblogs.com/dotnetcrazy/p/11304783.html
PS:核心:
useradd -s /sbin/nologin -M nginx
3.3.编译安装Lua模块
大体思路
默认是不支持Lua的,所以需要自己编译安装下
PS:记得安装下Lua库:
yum install lua lua-devel -y
主要就3步走:
- 安装Lua即时编译器:
LuaJIT
- 安装Nginx模块:
ngx_devel_kit
andlua-nginx-module
- 重新编译Nginx:复制在线安装的编译参数(
nginx -V
)然后添加两个参数--add-module=../ngx_devel_kit-0.3.1
--add-module=../lua-nginx-module-0.10.15
3.3.1.编译安装luajit并导入环境变量
解压缩
# 编译安装
make install PREFIX=/usr/local/LuaJIT
# 导入环境变量
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
3.3.2.共享lua动态库
加载lua库到ld.so.conf文件
echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
执行ldconfig
让动态函式库加载到缓存中
3.3.3.配置nginx的编译参数
完整参数附录:
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.15
3.3.4.重新编译安装nginx
编译安装:make && make install
3.3.5.验证Lua模块
验证下Lua是否已经可用:
在nginx.config的server节点下添加:
PS:
vi /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
charset utf-8; # 默认编码为utf-8
location / {
root html;
index index.html index.htm;
}
...
# 测试Nginx的Lua(添加这一段)
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("欢迎访问逸鹏说道公众号~")';
}
...
}
检查配置:nginx -t -c /etc/nginx/nginx.conf
PS:配置生效:
nginx -s reload -c /etc/nginx/nginx.conf
看看效果:
扩展:你可以试试获取ip哦~
# 获取客户端ip
location /myip {
default_type 'text/plain';
content_by_lua '
clientIP = ngx.req.get_headers()["x_forwarded_for"]
ngx.say("IP:",clientIP)
';
}
4.Nginx+Lua搭建WAF防火墙
市面上比较常用一块开源项目:ngx_lua_waf
- 拦截Cookie类型工具
- 拦截异常post请求
- 拦截CC洪水攻击
- 拦截URL
- 拦截arg(提交的参数)
4.1.环境
clone代码并移动到nginx的waf目录下
简单说下里面的规则分别有啥用:
- args里面的规则get参数进行过滤的
- url是只在get请求url过滤的规则
- post是只在post请求过滤的规则
- whitelist是白名单,里面的url匹配到不做过滤
- user-agent是对user-agent的过滤规则
4.2.配置
修改必要配置
详细说明我引用一下我的上篇文章:
参数简单说明下:红色字体部分需要修改
nginx.config
的http
下添加如下内容:
lua_package_path "/etc/nginx/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file /etc/nginx/waf/init.lua;
access_by_lua_file /etc/nginx/waf/waf.lua;
4.3.生效
配置语法检查:nginx -t -c /etc/nginx/nginx.conf
PS:不重启的方式加载配置:
Nginx -s reload -c /etc/nginx/nginx.conf
4.4.简单验证
PS:其实绕过很简单,看看他默认规则即可,这款WAF的强大之处在于轻量级,而且规则可以自定化
过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割
举个例子:http://192.168.0.10/hello?id=1 or 1=1
PS:默认规则没有这点的防护
那么我们可以在args规则中添加比如\sor\s+
,然后nginx -s reload
一下就行了
PS:如果是从post进行注入,或者cookie中转注入,那么在对应规则里面添加就行,我这边只是演示下防火墙被绕过该怎么解决~(多看看日志)
4.5.CC验证
留个课后作业:使用ab来测试下nginx+lua的waf对cc的防御效果
提示:可以使用ab -n 2000 -c 200 http://192.168.0.10
来简单测试
PS:测试前curl http://192.168.0.10/hello 看看返回内容,测试后再curl看看返回内容
扩展:隐藏Nginx版本信息
防止被黑客进行针对性渗透,隐藏下版本信息
PS:其他配置今天就不详细讲解了,下次讲Nginx的时候会说的
原来:
配置下:vi /etc/nginx/nginx.conf
http下添加:
server_tokens off;
检查下语法:nginx -t
不重启的方式加载配置文件:nginx -s reload
现在效果:
Nginx + Lua 搭建网站WAF防火墙的更多相关文章
- Nginx详解二十八:Nginx架构篇Nginx+Lua的安全waf防火墙
Nginx+Lua的安全waf防火墙 看一下别人写好的:https://github.com/loveshell/ngx_lua_waf 先安装git:yum -y install git 在/opt ...
- 使用Nginx+Lua实现自定义WAF
使用Nginx+Lua实现自定义WAF 版权声明:全部抄自赵班长的GitHub上waf项目 功能列表: 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝. 支持URL白名单,将不需要过滤的URL ...
- nginx+lua构建简单waf网页防火墙
需求背景 类似于论坛型的网站经常会被黑掉,除了增加硬件防护感觉效果还是不太好,还会偶尔被黑,waf的功能正好实现了这个需求. waf的作用: 防止sql注入,本地包含,部分溢出,fuzzing测试,x ...
- nginx + lua 构建网站防护waf(一)
最近在帮朋友维护一个站点.这个站点是一个Php网站.坑爹的是用IIS做代理.出了无数问题之后忍无可忍,于是要我帮他切换到nginx上面,前期被不断的扫描和CC.最后找到了waf这样一个解决方案缓解一下 ...
- 使用nginx lua实现网站统计中的数据收集
导读网站数据统计分析工具是各网站站长和运营人员经常使用的一种工具,常用的有 谷歌分析.百度统计和腾讯分析等等.所有这些统计分析工具的第一步都是网站访问数据的收集.目前主流的数据收集方式基本都是基于ja ...
- Nginx + Lua搭建文件上传下载服务
收录待用,修改转载已取得腾讯云授权 最新腾讯云技术公开课直播,提问腾讯W3C代表,如何从小白成为技术专家?点击了解活动详情 作者 | 庄进发 编辑 | 迷鹿 庄进发,信息安全部后台开发工程师,主要负责 ...
- nginx + Lua 实现自定义WAF
文章摘自:https://github.com/unixhot/waf wget git@github.com:unixhot/waf.git
- 使用NGINX+Openresty和unixhot_waf开源防火墙实现WAF功能
使用NGINX+Openresty实现WAF功能 一.了解WAF1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: ...
- 使用NGINX+LUA实现WAF功能 和nginx 防盗链
使用NGINX+LUA实现WAF功能 一.了解WAF 1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: WAF) ...
随机推荐
- PATA 1036. Boys vs Girls (25)
https://www.patest.cn/contests/pat-a-practise/1036 #include <bits/stdc++.h> using namespace st ...
- Java:Web Service初入门
前言 Web Service技术在我第一次接触,又没有实际使用时完全不理解这是什么.以为是一种类似Spring,Shiro的编程框架.后来渐渐理解,WS(即Web Service缩写)是一种通用的接口 ...
- CQRS之旅——旅程8(后记:经验教训)
旅程8:后记:经验教训 我们的地图有多好?我们走了多远?我们学到了什么?我们迷路了吗? "这片土地可能对那些愿意冒险的人有益."亨利.哈德逊 这一章总结了我们旅程中的发现.它强调了 ...
- [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
这个问题是由于data的目录下没有安装数据库表 解决方法: vi /etc/my.cnf 修改为正确的datadir=“xxxxx”即可 然后service mysqld start service ...
- white box白盒测试
逻辑覆盖法:语句覆盖,判定覆盖,条件覆盖,判定/条件覆盖,组合覆盖,路径覆盖 基本路径测试法:Control Flow Graphs, CFG.带箭头的边 条件覆盖:使每个判定中每个条件的可能值至少满 ...
- BZOJ 1085:[SCOI2005]骑士精神(A*算法)
题目链接 题意 中文题意. 思路 首先找到空白的格子,因为空白的格子可以和其他的骑士换.从空白的点开始搜索,每次和其他点交换.因为最多只有十五步,可以做16次搜索,搜索的时候,记录走过的步数和至少剩余 ...
- C++学习书籍推荐《C++ Primer 第五版 (英文)》下载
百度云及其他网盘下载地址:点我 编辑推荐 <C++ Primer(英文版)(第5版)>是全球最畅销的C++图书.这本久负盛名的C++经典教程,时隔八年之久,终迎来的重大升级.除令全球无数程 ...
- 学习4:内容# 1.列表 # 2.元祖 # 3.range
1.列表 列表 -- list -- 容器 有序,可变,支持索引 列表: 存储数据,支持的数据类型很多 字符串,数字,布尔值,列表,集合,元祖,字典, 定义一个列表 lst = ["dsb& ...
- 【Download error:TOO MANY REQUESTS】&【TypeError:excepted string or buffer】
<用python写网络爬虫>,1.4.4链接爬虫,运行时,遇到错误: Download error:TOO MANY REQUESTS Traceback(most recent call ...
- python 3.5学习笔记(第五章)
本章内容 1.什么是模块 2.模块的导入方法 3.搜索路径 4.重要标准库 一.什么是模块 1.模块本质上是一个以.py 结尾的python文件,包含了python对象定义和python语句. 2.模 ...