nginx waf防火墙之Modsecurity
创始人
2024-06-03 10:04:08
0

waf防火墙可以拦截一些非法请求,毕竟谁都不能保证自己的代码没有bug.

参考文档:Compilation recipes for v3.x · SpiderLabs/ModSecurity Wiki · GitHub

本次安装环境:Centos8 Stream

本次使用为nginx  1.22.1版本

本次安装方式为动态库安装及加载

主要实现功能:仅允许境内访问,拦截自动跳转,频率访问限制

1.配置powertools源,并安装依赖环境

 a.新增repo文件 /etc/yum.repos.d/powertools.repo   内容如下

vim /etc/yum.repos.d/powertools.repo
[powertools1]
name=Extra Packages for Enterprise Linux $releasever - $basearch
baseurl=https://mirrors.aliyun.com/centos/8-stream/PowerTools/x86_64/os/
enabled=1
gpgcheck=0

b.安装依赖环境

dnf install -y gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel lmdb lmdb-devel libxml2 libxml2-devel ssdeep ssdeep-devel lua lua-devel

2.安装ModSecurity

cd /opt/
git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
git submodule init
git submodule update
./build.sh
./configure  --with-lmdb # ip地域访问模块
make -j 4
make install

3.下载 ModSecurity-nginx 连接器

cd /opt/
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

 4.安装NGINX服务

centos8 stream 默认的nginx 是1.14版本,需要重置一下版本,安装目前最新版

dnf module reset nginx -y 
dnf module enable nginx:1.22 -y 
dnf remove nginx -y   # 如果有老版本nginx,建议卸载安装最新版本
dnf install nginx -y 

5.编译waf动态库 

cd /opt/
wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1
sudo ./configure --with-compat --add-dynamic-module=../ModSecurity-nginx
sudo make modules
mkdir  -pv /etc/nginx/modules
\cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/

6.配置NGINX

a.在nginx.conf 添加如下配置 

load_module "/etc/nginx/modules/ngx_http_modsecurity_module.so";

 b.配置规则

mkdir /etc/nginx/conf.d/modsecurity
cp /opt/ModSecurity/modsecurity.conf-recommended /etc/nginx/conf.d/modsecurity/modsecurity.conf
cp /opt/ModSecurity/unicode.mapping /etc/nginx/conf.d/modsecurity/unicode.mapping
cd /opt/
wget https://github.com/coreruleset/coreruleset/archive/v3.3.4.tar.gz
tar xvf v3.3.4.tar.gz 
cd coreruleset-3.3.4/
cp -a rules /etc/nginx/conf.d/modsecurity/
cp crs-setup.conf.example /etc/nginx/conf.d/modsecurity/crs-setup.conf

c.启用waf拦截,在nginx.conf配置文件 http 块内加入配置【在http节点添加表示全局配置,在server节点添加表示为指定网站配置】

modsecurity on;
modsecurity_rules_file /etc/nginx/conf.d/modsecurity/modsecurity.conf;

d.编辑 modsecurity.conf

vim /etc/nginx/conf.d/modsecurity/modsecurity.conf

将下面字段修改如下

SecRuleEngine DetectionOnly 改为 SecRuleEngine On

同时添加以下内容:

Include /etc/nginx/conf.d/modsecurity/crs-setup.conf
Include /etc/nginx/conf.d/modsecurity/rules/*.conf

e.自定义规则

#可将自己写的规则放置于此两个文件中
cd /etc/nginx/conf.d/modsecurity/rules/
mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

7.拦截指定区域ip

 a.该地理位置拦截需要用到 geoip 库,下载该ip库:https://download.csdn.net/download/ly1358152944/87575813

或者自己去官方下载: https://www.maxmind.com/en/accounts/387514/geoip/downloads

 

mkdir -pv /etc/nginx/conf.d/modsecurity/geoip/# 并将下载好的文件解压,将文件 GeoLite2-Country.mmdb 放到该目录中

b.修改配置文件  /etc/nginx/conf.d/modsecurity/crs-setup.conf 在 713行左右,添加如下内容

SecGeoLookupDB /etc/nginx/conf.d/modsecurity/geoip/GeoLite2-Country.mmdb

c.配置自定义规则,修改文件  /etc/nginx/conf.d/modsecurity/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf

#将以下规则,复制到文件中,规则id如与自己编写的规则ID冲突,直接更改即可:
SecRule REMOTE_ADDR "@geoLookup" "chain,id:22,deny,phase:1,log,msg:'Non-China IP address'"
SecRule GEO:COUNTRY_CODE "!@rx CN|HK|TW|MO"


上规则表示,仅允许中国的IP地址进行访问,其他国家的IP地址若访问网站,则直接阻断。

之所以同时使用CN、HK、TW、MO,是因为在ISO 3166-1标准中,CN仅代表中国内地,不包含港澳台,因此在此直接将港澳台的代码添加其中,如果只希望中国内地的IP地址进行访问,复制以下规则即可:

SecRule REMOTE_ADDR "@geoLookup" "chain,id:22,deny,phase:1,log,msg:'Non-CN IP address'"
SecRule GEO:COUNTRY_CODE "!@rx CN"
SecMarker "END-BEFORE-RULE-EXCLUSIONS"  # 该配置为规则结束,应位于配置最底部  

8.访问速率限制

【nginx 通过自带的limit 模块进行限制】Module ngx_http_limit_req_module

9.自动跳转页面

需要设置默认的拦截跳转规则,在 /etc/nginx/conf.d/modsecurity/crs-setup.conf 修改如下配置,大约100行左右

SecDefaultAction "phase:1,deny,log,noauditlog,status:302,redirect:http://cdn.hiheyin.    com/intercept.html?url=%{REQUEST_URI}&intercept_domain=%{request_headers.host}"SecDefaultAction "phase:2,deny,log,noauditlog,status:302,redirect:http://cdn.hiheyin.    com/intercept.html?url=%{REQUEST_URI}&intercept_domain=%{request_headers.host}"

intercept.html 内容如下:



访问被拦截





Warning:


当前的操作可能包含恶意代码,可能会对网站造成安全威胁,已被服务器防火墙拦截。



当访问带有异常参数的时候,会出现下面错误

 在服务器的 /var/log/modsec_audit.log 可以查看详细拦截日志

相关内容

热门资讯

122.(leaflet篇)l... 听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
Vue使用pdf-lib为文件... 之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vu...
PyQt5数据库开发1 4.1... 文章目录 前言 步骤/方法 1 使用windows身份登录 2 启用混合登录模式 3 允许远程连接服...
Android studio ... 解决 Android studio 出现“The emulator process for AVD ...
Linux基础命令大全(上) ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维...
再谈解决“因为文件包含病毒或潜... 前面出了一篇博文专门来解决“因为文件包含病毒或潜在的垃圾软件”的问题,其中第二种方法有...
南京邮电大学通达学院2023c... 题目展示 一.问题描述 实验题目1 定义一个学生类,其中包括如下内容: (1)私有数据成员 ①年龄 ...
PageObject 六大原则 PageObject六大原则: 1.封装服务的方法 2.不要暴露页面的细节 3.通过r...
【Linux网络编程】01:S... Socket多进程 OVERVIEWSocket多进程1.Server2.Client3.bug&...
数据结构刷题(二十五):122... 1.122. 买卖股票的最佳时机 II思路:贪心。把利润分解为每天为单位的维度,然后收...
浏览器事件循环 事件循环 浏览器的进程模型 何为进程? 程序运行需要有它自己专属的内存空间࿰...
8个免费图片/照片压缩工具帮您... 继续查看一些最好的图像压缩工具,以提升用户体验和存储空间以及网站使用支持。 无数图像压...
计算机二级Python备考(2... 目录  一、选择题 1.在Python语言中: 2.知识点 二、基本操作题 1. j...
端电压 相电压 线电压 记得刚接触矢量控制的时候,拿到板子,就赶紧去测各种波形,结...
如何使用Python检测和识别... 车牌检测与识别技术用途广泛,可以用于道路系统、无票停车场、车辆门禁等。这项技术结合了计...
带环链表详解 目录 一、什么是环形链表 二、判断是否为环形链表 2.1 具体题目 2.2 具体思路 2.3 思路的...
【C语言进阶:刨根究底字符串函... 本节重点内容: 深入理解strcpy函数的使用学会strcpy函数的模拟实现⚡strc...
Django web开发(一)... 文章目录前端开发1.快速开发网站2.标签2.1 编码2.2 title2.3 标题2.4 div和s...