不求甚解

此博客为个人学习之用,如与其他作品雷同,纯属巧合。

导航

Windows 系统上 Nginx 服务,出现SSL/TLS:证书已过期 漏洞解决方案

 

一、漏洞说明

 

  • 漏洞名称:SSL/TLS:证书已过期(Certificate Expired)
  • 风险等级:中高危
  • 影响:
    • 浏览器提示“您的连接不是私密连接”
    • 客户端(如 App、API 调用)可能中断连接
    • 存在中间人攻击(MITM)风险
    • 不符合安全合规要求(如等保、ISO27001)

二、根本原因

Nginx 配置中使用的 SSL 证书已超过其有效期(通常为 1 年或更短),未及时更新。

三、解决方案(适用于 Windows 内网/外网环境)

步骤 1:确认证书已过期

打开 PowerShell 或 CMD,运行:

echo Q | openssl s_client -connect 127.0.0.1:443 -servername localhost 2>$null | openssl x509 -noout -dates

输出示例:

notBefore=Jan  1 00:00:00 2023 GMT
notAfter=Dec 31 23:59:59 2023 GMT   ← 已过期

如果 notAfter 时间早于当前时间,说明证书已过期。


步骤 2:生成新的 SSL 证书(适用于内网/测试环境)

由于是 Windows 环境,推荐使用 OpenSSL 生成自签名证书(长期有效)。

1. 确保已安装 OpenSSL

2. 生成新证书(有效期 10 年)

# 进入 Nginx 配置目录
cd C:\nginx\conf
mkdir ssl
cd ssl

# 生成私钥和证书
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 ^
  -keyout server.key ^
  -out server.crt ^
  -subj "/C=CN/ST=Beijing/L=Beijing/O=Company/OU=IT/CN=your-domain-or-ip" ^
  -addext "subjectAltName=DNS:localhost,DNS:your-domain.com,IP:192.168.1.100,IP:127.0.0.1"

替换 CNsubjectAltName 为你的实际域名或 IP。


步骤 3:更新 Nginx 配置

编辑 C:\nginx\conf\nginx.conf,找到 HTTPS server 块:

server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate      conf/ssl/server.crt;
    ssl_certificate_key  conf/ssl/server.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers off;

    location / {
        proxy_pass http://localhost:8080;
        # 其他代理配置...
    }
}

步骤 4:测试并重启 Nginx

# 测试配置
C:\nginx> nginx -t

# 重新加载
C:\nginx> nginx -s reload

如果失败,先停止再启动:

nginx -s stop
start nginx

 步骤 5:验证新证书

再次运行:

echo Q | openssl s_client -connect 127.0.0.1:443 -servername localhost 2>$null | openssl x509 -noout -dates

确认 notAfter 是未来时间(如 2035 年)。


步骤 6:让客户端信任证书(关键!)

由于是自签名证书,所有访问客户端必须手动信任,否则仍会提示警告。

导入证书到“受信任的根证书颁发机构”:

  1. 将 C:\nginx\conf\ssl\server.crt 复制到客户端电脑。
  2. 双击 .crt 文件 → “安装证书” → “本地计算机” → “受信任的根证书颁发机构” → 完成。
  3. 重启浏览器即可正常访问。

🏢 企业环境建议通过 组策略(GPO) 批量部署。

六、漏洞修复验证
  1. 浏览器访问 https://your-domain,确认无“证书过期”提示。
  2. 使用 SSL Labs 测试 验证安全性。
  3. 安全扫描工具(如 Nessus、Xray)应不再报此漏洞。

总结

步骤操作
1 确认证书已过期
2 使用 OpenSSL 生成新证书(有效期 10 年)
3 更新 Nginx 配置,指向新证书
4 nginx -t 测试并 reload
5 将证书导入所有客户端的“受信任的根证书颁发机构”
6 建立维护机制,防止再次过期