当前位置: 首页 > news >正文

nginx添加modsecurity插件

文章目录

    • 准备工作
      • 安装依赖
    • 下载ModSecurity并安装
    • 安装nginx的插件/安装nginx
    • 安装规则
    • 测试
    • 遇到的问题

ModSecurity的git地址

准备工作

安装依赖

  1. yum install -y gcc make pcre-devel libxml2 libxml2-devel curl-devel httpd-devel libtool
    

    如果安装过程中还有其他依赖百度安装即可

  2. 查看g++的版本:版本需要大于等于7.3,否则不支持C++17标准

    [root@192 rules]# g++ --version
    g++ (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

如果g++版本过低,则升级
升级g++方式

下载ModSecurity并安装

[root@localhost source]# git clone --recursive https://github.com/owasp-modsecurity/ModSecurity ModSecurity

完成后进入源码目录:

[root@localhost modsecurity]# cd ModSecurity/

安装

[root@localhost ModSecurity]# git submodule init
[root@localhost ModSecurity]# git submodule update
[root@localhost ModSecurity]# ./build.sh
[root@localhost ModSecurity]# ./configure
[root@localhost ModSecurity]# make
[root@localhost ModSecurity]# make install

安装nginx的插件/安装nginx

  1. ModSecurity-nginx的代码站地址,它是nginx和modsecurity之间联动的桥梁

  2. 下载ModSecurity-nginx

    [root@localhost modsecurity]# git clone https://github.com/owasp-modsecurity/ModSecurity-nginx.git
    
  3. 安装nginx
    在执行配置文件的时候添加上ModSecurity-nginx插件

    ./configure --with-http_stub_status_module --with-http_ssl_module  --add-module=/soft/ModSecurity-nginx
    # 在nginx的目录中执行,/soft/ModSecurity-nginx为ModSecurity-nginx的下载目录
    

安装规则

规则的代码站

  1. 下载规则

    [root@localhost modsecurity]#  git clone https://github.com/coreruleset/coreruleset.git
    
  2. 进入coreruleset下载目录复制规则的配置文件

    cp crs-setup.conf.example crs-setup.conf
    
  3. 生成ModSecurity的配置文件并修改

cp /soft/ModSecurity/modsecurity.conf-recommended /soft/ModSecurity/modsecurity.conf
vi modsecurity.conf
# 打开引擎
  1. 配置modsecurity

    vi /soft/ModSecurity/modsecurity.conf
    # 打开引擎
    SecRuleEngine On# 在modsecurity.conf末尾处添加
    include /soft/coreruleset/crs-setup.conf   # 规则的配置文件
    include /soft/coreruleset/rules/*.conf  # 各项规则的配置文件
    
  2. 修改nginx配置文件
    在nginx下的server文件中增加两行

    modsecurity on;
    modsecurity_rules_file /soft/ModSecurity/modsecurity.conf;
    
  3. 启动nginx,或者重启nginx

测试

访问http://192.168.96.128:81/%E4%BA%8C%E7%BA%A7%E8%8F%9C%E5%8D%95.html是可以的

在路径后面添加上?param=后访问http://192.168.96.128:81/%E4%BA%8C%E7%BA%A7%E8%8F%9C%E5%8D%95.html?param=%3Cscript%3Ealert(1);%3C/script%3E
返回403

遇到的问题

  1. 需要配置阿里的镜像地址,否则下载依赖时报错或者很慢
  2. 我是先安装的nginx,在安装的modsecurity,则需要重新配置nginx和重新编译,重新编译后会生成新的nginx启动文件,由于我把先安装的nginx启动文件复制到的PATH目录下,为了在任何目录执行,所以即使重新编译了nginx,重启nginx后通过nginx -V查看nginx的编译参数还是没有新的插件,因为是用的旧的nginx启动文件启动的,将新的启动文件复制到PATH目录下覆盖掉之前的即可,nginx源码安装的默认生成路径是/usr/local/nginx/sbin下

参考: https://www.cnblogs.com/architectforest/p/18396169

http://www.dtcms.com/a/394121.html

相关文章:

  • 代码上传Github:SSH法
  • 【iOS】AFNetworking初步了解及使用
  • JVM实战-G1参数调优
  • 超简单的视频分割脚本
  • 基于51单片机电子钟闹钟12/24小时制LCD显示( proteus仿真+程序+设计报告+讲解视频)
  • 在 Windows 系统上安装官方 Codex CLI 教程
  • Redis 配置与优化全攻略
  • 二分查找左右边界写法
  • Oracle体系结构-Large Pool详解
  • Elasticsearch面试精讲 Day 19:磁盘IO与存储优化
  • 【AI智能体】Dify 搭建数据分析应用实战操作详解
  • Nginx localtion / 、/a、/a/ 的区别
  • 【C++】string的使用与模拟实现
  • 新手向 算法 希尔排序-yang
  • 如何用RAG增强的动态能力与大模型结合打造企业AI产品?
  • 黑马头条_SpringCloud项目阶段五:openFeign服务接入以及接入腾讯云内容安全服务实现文章提交违规信息自动审核
  • Spring、SpringBoot框架核心流程详解
  • 195. Java 异常 - finally 块:Java 中的“兜底侠”
  • C语言底层学习(2.指针与数组的关系与应用)(超详细)
  • 008 Rust注释
  • ubuntu防火墙开放端口
  • ​MySQL 8.0.29 RPM 安装教程(CentOS 7 / RHEL 7 详细步骤)​附安装包
  • AIPPT:AI一键生成高质量PPT
  • [已更新]2025华为杯E题数学建模研赛E题研究生数学建模思路代码文章成品:高速列车轴承智能故障诊断问题
  • 从零到一:Vue3 + Spring Boot + MySQL 全栈项目部署到阿里云服务器完整教程
  • 微服务基础2-网关路由
  • ubuntu创建新用户
  • 黑豹X2(Panther-x2)armbian 驱动NPU/VPU的驱动下载安装
  • 50.Mysql主从复制与读写分离
  • 软件设计师,经典计算题