基于 Nginx + ModSecurity V3 实现对 web 流量的安全访问控制
off999 2025-01-06 14:49 17 浏览 0 评论
ModSecurity
- ModSecurity 是一个开源的、跨平台的 Web 应用防火墙,它可以通过检查 Web 服务器收发的数据来对网站流量进行安全防护
- 最初设计 ModSecurity 项目时,它只是一个 Apache 模块。随着时间的推移,该项目已经扩展到支持其他平台, 如 Nginx; 为了满足对额外平台支持不断增长的需求,有必要删除该项目底层的 Apache 依赖项,使其更加独立于平台
- 当前 ModSecurity v3 由 Libmodsecurity(对 ModSecurity 平台的完全重写) 和 对应 web 服务器的连接器(模块)组成
- 请求处理阶段 Request Headers、Request Body、Response Headers、Response Body、Logging
组件版本
- nginx v1.20.1
- libmodsecurity v3.05
- ModSecurity-nginx v1.0.2
- coreruleset v3.3.2
- CentOS Linux release 7.6.1810 (Core)
libmodsecurity
基于 SecRules 的 web 流量处理引擎, 提供了加载/解释与 ModSecurity SecRules 格式编写的规则的能力
1、安装 libmodsecurity 所需依赖库
# 安装依赖
yum install -y epel-release git gcc gcc-c++ autoconf libtool pcre-devel libxml2-devel curl-devel yajl-devel flex-devel lua-devel lmdb-devel ssdeep-devel
# 安装依赖项 libmaxminddb
wget -c https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz
./configure
make && make install
2、下载编译安装 libmodsecurity v3.05
git clone https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
# 下载 libInjection
git submodule init
git submodule update
# 开始构建, 默认安装位置 /usr/local/modsecurity/
./build.sh
./configure && make && make install
# 备注: ./build.sh 执行中提示 `fatal: No names found, cannot describe anything.` 暂时忽略
modsecurity-nginx
nginx 和 libmodsecurity 之间的连接器, 其实就是一个第三方 Nginx 模块, Nginx 可以通过静态或动态方式加载该模块
1、Nginx 的编译安装
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar xzf nginx-1.20.1.tar.gz && cd nginx-1.20.1
yum install openssl-devel
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
./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-dynamic-module=../ModSecurity-nginx
make -j4
mkdir /var/cache/nginx/ && useradd -r nginx && mkdir /etc/nginx/modules
cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules
# nginx 配置文件首行添加如下配置, 全局加载该模块
load_module modules/ngx_http_modsecurity_module.so;
2、添加 modsecurity 相关配置文件 modsecurity.conf, main.conf
# 创建配置文件 modsecurity.conf
mkdir /etc/nginx/modsec && cd /etc/nginx/modsec
wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended
mv modsecurity.conf-recommended modsecurity.conf
# 修改 modsecurity.conf 中的对应配置项
vim /etc/nginx/modsec/modsecurity.conf
SecRuleEngine On # 默认 DetectionOnly(开启规则匹配,但不执行任何拦截操作), On(开启规则匹配并进行相应的拦截)
#SecUnicodeMapFile unicode.mapping 20127 # 默认启用, 现变更为注释该行
# 创建主配置 main.conf 及自定义规则
cat > /etc/nginx/modsec/main.conf << EOF
Include /etc/nginx/modsec/modsecurity.conf
# 自定义安全规则, 参数 x 值中包含 test 字符串, 则返回 403 状态
SecRule ARGS:x "@contains test" "id:1234,deny,log,status:403"
SecUploadFileLimit 15 # 配置在multipart POST中处理的最大文件上传数量
EOF
# 在 Nginx 的 server 上下文中添加如下配置
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
3、完整的 Nginx 配置文件如下
cat /etc/nginx/nginx.conf
load_module modules/ngx_http_modsecurity_module.so;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
location / {
root html;
index index.html index.htm;
}
}
}
OWASP
OWASP ModSecurity 核心规则集 (CRS) 是一组通用攻击检测规则, 用于 ModSecurity 或兼容的 Web 应用程序防火墙; CRS 旨在保护 Web 应用程序免受包括 OWASP 前十名在内的各种攻击, 同时将错误警报降至最低
1、在 Modsecurity 中启用 OWASP 核心规则集
# 下载部署 crs
wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.2.tar.gz
tar xzf v3.3.2.tar.gz && cd cd coreruleset-3.3.2/
cp crs-setup.conf.example crs-setup.conf && cd ..
mv coreruleset-3.3.2/ /etc/nginx/modsec/crs3
# 在主配置文件 main.conf 中引用 modsecurity 及 crs 配置
cat > /etc/nginx/modsec/main.conf << EOF
# modsecurity 基本配置
Include /etc/nginx/modsec/modsecurity.conf
# OWASP CRS v3 rules
Include /etc/nginx/modsec/crs3/crs-setup.conf
Include /etc/nginx/modsec/crs3/rules/*.conf
# 自定义规则集
SecRule ARGS:x "@contains test" "id:1234,deny,log,status:403"
EOF
安全验证
1、请求参数 x 不包含字符串 test 的请求, 返回状态 200 http://192.168.31.66/?x=wyun
2、请求参数 x 包含非法字符串 test 的请求, 返回状态 403 http://192.168.31.66/?x=wyuntest
# 在 /var/log/nginx/error.log 中可以看到拦截的详细日志
2021/09/13 19:28:06 [error] 24702#24702: *7 [client 192.168.31.220] ModSecurity: Access denied with code 403 (phase 1). Matched "Operator `Contains' with parameter `test' against variable `ARGS:x' (Value: `test11' ) [file "/etc/nginx/modsec/main.conf"] [line "3"] [id "1234"] [rev ""] [msg ""] [data ""] [severity "0"] [ver ""] [maturity "0"] [accuracy "0"] [hostname "192.168.31.66"] [uri "/"] [unique_id "1631532486"] [ref "o0,4v8,6"], client: 192.168.31.220, server: localhost, request: "GET /?x=test11 HTTP/1.1", host: "192.168.31.66"
- 上一篇:我的Python_WEB服务器简单配置
- 下一篇:Nginx反向代理
相关推荐
- 工程师必备!DeepSeek自动化运维全攻略
-
每天省出3小时,故障自修复+智能监控实战指南导语“总在深夜被报警短信吵醒?教你搭建智能运维体系,让DeepSeek自己管自己!”正文技能1:自动化故障诊断配置智能诊断规则:yaml复制alert_ru...
- Spug - 轻量级自动化运维平台(自动化运维平台 devops)
-
对于中小型企业而言,进行主机和应用的管理是比较麻烦的,应用部署往往需要直接连接服务器,再进行手动的环境配置、代码拉取、应用构建和部署发布等工作,容易出错,且耗时费力。一个好的自动化运维平台,往往能大大...
- 轻量级无 Agent 的一个好用的“小麻雀”自动化运维平台工具!-Spug
-
对于中小型企业而言,进行主机和应用的管理是比较麻烦的,应用部署往往需要直接连接服务器,再进行手动的环境配置、代码拉取、应用构建和部署发布等工作,容易出错,且耗时费力。一个好的自动化运维平台,往往能大大...
- 运维自动化之实用python代码汇总(python自动化运维常用模块)
-
本文总结了运维工作中经常用到的一些实用代码块,方便在需要的时候直接搬过来使用即可1.执行系统命令,获取返回结果fromsubprocessimportPopen,PIPE,STDOUTcp...
- 从代码小白到自动化大师:Python 编程实战
-
昨天我聊了一下关于线性代数、概率统计、微积分核心概念的学习,也花了一些时间恢复一下大学时候学这些的记忆,确实来说数学很有趣也很考验人,兴趣是最好的老师对吧,既然对AI感兴趣,总要认真的学一学,接下来我...
- 锐捷:基于Python TextFSM模块的网络设备自动化运维方法
-
网络设备自动化运维,首先要实现网络设备与自动化运维平台对接,即通过代码实现登录网络设备并获取信息。邮政业科技创新战略联盟单位锐捷自主研发的数据中心交换机产品已全面支持NETCONF协议,可适用于和SD...
- 基于Python+vue的自动化运维、完全开源的云管理平台
-
真正的大师,永远都怀着一颗学徒的心!一、项目简介今天说的这个软件是一款基于Python+vue的自动化运维、完全开源的云管理平台。二、实现功能基于RBAC权限系统录像回放DNS管理配置中心强大的作业调...
- 编程与数学:在Python里怎么用turtle库函数填色?
-
这里只给出一个示例,一个最简单的示例。看懂这个示例,你就能在自己的代码里需要填色的地方填色。首先,与前面发的Python绘画程序一样,先要装入turtle库。然后在代码中,下面需要填色时,先写一个填色...
- Python UV 环境下的 PyKDL 运动学库安装
-
视频讲解:PythonUV环境下的PyKDL运动学库安装_哔哩哔哩_bilibilimujoco-learning这个仓库,改成uv管理环境依赖后,原来的一些包有些缺失,比如之前安装的PyKD...
- python最新版3.11正式发布,有哪些新特色?(3/5)
-
异步任务的语法更完美python编程语言对异步编程的支持一直在改进,比如python2.0版开始就增加了生成器(generator),在3.4版开始增加了asyncio库,随后在3.5版中...
- 清华北大都在用!Python王者归来(全彩版)
-
纸上得来终觉浅,绝知此事要躬行。今天给大家带来一份由清华大学出版的《python王者归来》。在当下全民互联网,大数据的时代,Python已然成为了学习大数据、人工智能时代的首选编程语言,Python...
- 第六章:Python模块与包(python模块与包与类的关系区别)
-
6.1模块基础6.1.1理论知识模块是一个包含Python定义和语句的文件,其扩展名为.py。模块可以将代码组织成逻辑单元,提高代码的可维护性和复用性。通过将相关的函数、类和变量放在同一个模块中...
- 语言教育项目实战之一:Ubuntu下安装Python环境
-
如下项目,运行在#ubuntu#上,使用#pytho#,从最初环境开始,逐渐深入。此项目以语言学习为主要目的,实现听写、跟读、对话的服务,面向中小学生、大学生、涉外交流人员等。计划通过pyenv管...
- openai-python v1.79.0重磅发布!全新Evals API升级,音频转录终极
-
2025年5月17日,OpenAI官方在GitHub上发布了openai-python库的最新版本——v1.79.0。本次版本重点围绕Evals评估API进行了多项功能完善,同时修复了音频转录接口的重...
- 你真的用对了吗?7个常被误用的Python内置函数及最佳实践
-
你是否曾经在使用多年的工具中突然发现一个新功能,然后感叹:“我怎么一直没发现这个?”没错,今天我们就来体验一把“Python函数版”的这种乐趣。这些函数很可能已经是你日常代码的一部分,但我敢打赌,你并...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 工程师必备!DeepSeek自动化运维全攻略
- Spug - 轻量级自动化运维平台(自动化运维平台 devops)
- 轻量级无 Agent 的一个好用的“小麻雀”自动化运维平台工具!-Spug
- 运维自动化之实用python代码汇总(python自动化运维常用模块)
- 从代码小白到自动化大师:Python 编程实战
- 锐捷:基于Python TextFSM模块的网络设备自动化运维方法
- 基于Python+vue的自动化运维、完全开源的云管理平台
- 编程与数学:在Python里怎么用turtle库函数填色?
- Python UV 环境下的 PyKDL 运动学库安装
- python最新版3.11正式发布,有哪些新特色?(3/5)
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python人脸识别 (54)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- centos7安装python (53)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)