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

网络安全入门防御与加固(二)

Web服务器加固

1 Apache/Nginx配置(禁用目录遍历、隐藏版本号)

(1)Apache安全配置

  • 禁用目录遍历

    # 修改Apache配置文件(httpd.conf或虚拟主机配置)  
    <Directory "/var/www/html">  
        Options -Indexes  # 关闭目录列表  
        AllowOverride None  
        Require all granted  
    </Directory>  

    验证方法:访问 http://example.com/uploads/,若返回403错误则生效。

  • 隐藏版本号

    # 主配置文件中添加  
    ServerTokens Prod    # 仅显示“Apache”  
    ServerSignature Off  # 隐藏底部版本信息  

    验证

    curl -I http://example.com  # 查看Server头字段  

(2) Nginx安全配置

  • 禁用目录遍历

    # 在server块中添加  
    location / {  
        autoindex off;  # 关闭目录列表  
    }  
  • 隐藏版本号

    # 主配置文件(nginx.conf)中添加  
    server_tokens off;  

    验证

    curl -I http://example.com  # Server头显示“nginx”而非具体版本  

2 使用ModSecurity防御Web攻击

(1)ModSecurity简介

  • 功能:开源的Web应用防火墙(WAF),支持SQL注入、XSS、文件包含等攻击检测。

  • 支持平台:Apache、Nginx、IIS。

(2)Apache + ModSecurity配置

  • 安装(Ubuntu/Debian)

    sudo apt install libapache2-mod-security2  
    sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf  
  • 启用核心规则集(OWASP CRS)

    # 下载规则集  
    git clone https://github.com/coreruleset/coreruleset /etc/modsecurity/crs/  
    # 修改配置文件  
    sudo vim /etc/apache2/mods-enabled/security2.conf  
    # 添加以下内容  
    IncludeOptional /etc/modsecurity/crs/crs-setup.conf  
    IncludeOptional /etc/modsecurity/crs/rules/*.conf  
  • 重启Apache

    sudo systemctl restart apache2  

(3)Nginx + ModSecurity配置

  • 编译Nginx模块(需源码编译)

    git clone https://github.com/SpiderLabs/ModSecurity-nginx.git  
    ./configure --add-module=../ModSecurity-nginx  
    make && make install  
  • 加载规则集

    # nginx.conf中配置  
    modsecurity on;  
    modsecurity_rules_file /etc/nginx/modsecurity/main.conf;  

(4)自定义规则示例(防SQL注入)

# 在modsecurity.conf中添加  
SecRule ARGS "@detectSQLi" "id:1001,phase:2,deny,status:403,msg:'SQL Injection Detected'"  

常见问题与解决方案
问题原因分析解决方案
Apache/Nginx重启失败配置语法错误使用命令检查语法:
apachectl configtest 或 nginx -t
ModSecurity误拦截合法请求规则过于严格调整规则级别(如SecRuleEngine DetectionOnly)或禁用特定规则ID。
OWASP CRS导致性能下降规则加载过多启用SecRequestBodyLimit 13107200 调整检测范围,或按需裁剪规则。
ModSecurity日志不生成文件权限不足修改日志目录权限:
sudo chown -R www-data:www-data /var/log/modsec/
Nginx编译ModSecurity失败依赖缺失或版本冲突安装依赖:sudo apt install libmodsecurity3,确保Nginx版本兼容。

动手实验:加固Web服务器
  1. 禁用目录遍历验证

    • 在Web目录创建空文件夹 /uploads,访问测试是否返回403。

  2. ModSecurity攻击拦截测试

    • 发送含SQL注入的请求:

      curl http://example.com/?id=1' OR 1=1--   
    • 检查ModSecurity日志(/var/log/modsec_audit.log)是否记录拦截事件。

  3. 自定义规则实战

    • 添加规则拦截特定User-Agent:

      SecRule REQUEST_HEADERS:User-Agent "badbot" "deny,status:403"  

总结
  • Apache/Nginx加固:最小化信息暴露(隐藏版本号)+ 限制敏感功能(目录遍历)。

  • ModSecurity:提供主动防御层,但需平衡安全性与性能,定期更新规则集。

  • 通用原则

    • 配置前备份原始文件,避免服务不可用。

    • 监控日志(tail -f /var/log/nginx/error.log),及时调整规则。

注意:所有加固操作需在测试环境验证后再部署到生产环境!


通过本节文章,可以掌握Web服务器核心防护的手段。有什么问题可以在评论区讨论。


http://www.kler.cn/a/554648.html

相关文章:

  • Windows 10操作系统上安装PHP
  • 广度优先搜索--之重生之我是蒟蒻,从入坟到入坑式讲解
  • 练习题:45
  • JavaScript系列(78)--Service Worker 深入解析
  • RAGFLOW使用flask转发的open ai接口
  • BFS 解决 FloodFill 算法(典型算法思想)—— OJ例题算法解析思路
  • 神经网络八股(2)
  • Unity 位图字体
  • 3.1 actor基本框架(c#的Akka.Actor模式)
  • 约束性委派攻击和非约束性委派攻击
  • Vue 3 工程化:从理论到实践 (上篇)
  • DeepSeek在企业中的有那些具体应用?
  • 易基因: ChIP-seq+DRIP-seq揭示AMPK通过调控H3K4me3沉积和R-loop形成以维持基因组稳定性和生殖细胞完整性|NAR
  • jvm中各个参数的理解
  • ROS 2机器人开发--第一个节点
  • HTTP SSE 实现
  • 【清华大学】DeepSeek从入门到精通完整版pdf下载
  • Could not initialize class io.netty.util.internal.Platfor...
  • nginx配置ssl
  • 《Spring实战》(第6版) 第3章 使用数据