如何配置Nginx的安全SSL/TLS?

Nginx

1 基础知识

1.1 TLS版本现状

– 目前TLS 1.2和TLS 1.3以外的所有协议均被视为不安全
– 显示是许多Web服务器平台默认仍启用TLS 1.0和TLS 1.1
– 实际情况是当前所有Web浏览器都与TLS 1.2兼容

1.2 确认服务器支持的协议

nmap --script ssl-enum-ciphers -p <PORT> <DOMAIN NAME>  

1.3 测试服务器的协议

openssl s_client -connect <DOMAIN NAME>:<PORT>

2 最佳实践

2.1 操作的环境

如何使用yum安装Nginx?

2.2 确认站点支持的SSL/TLS版本

2.2.1 安装工具

dnf install -y nmap

2.2.2 扫描站点域名

nmap --script ssl-enum-ciphers -p 443 www.cmdschool.org

可见如下显示,

Starting Nmap 7.70 ( https://nmap.org ) at 2020-12-08 20:48 EST
Nmap scan report for www.cmdschool.org (47.90.98.232)
Host is up (0.012s latency).

PORT    STATE SERVICE
443/tcp open  https
| ssl-enum-ciphers:
|   TLSv1.0:
|     ciphers:
|       TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA (secp256r1) - A
|       TLS_RSA_WITH_AES_256_CBC_SHA (rsa 2048) - A
|       TLS_RSA_WITH_CAMELLIA_256_CBC_SHA (rsa 2048) - A
|       TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (secp256r1) - A
|       TLS_RSA_WITH_AES_128_CBC_SHA (rsa 2048) - A
|       TLS_RSA_WITH_CAMELLIA_128_CBC_SHA (rsa 2048) - A
|     compressors:
|       NULL
|     cipher preference: server
|   TLSv1.1:
|     ciphers:
|       TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA (secp256r1) - A
|       TLS_RSA_WITH_AES_256_CBC_SHA (rsa 2048) - A
|       TLS_RSA_WITH_CAMELLIA_256_CBC_SHA (rsa 2048) - A
|       TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (secp256r1) - A
|       TLS_RSA_WITH_AES_128_CBC_SHA (rsa 2048) - A
|       TLS_RSA_WITH_CAMELLIA_128_CBC_SHA (rsa 2048) - A
|     compressors:
|       NULL
|     cipher preference: server
|   TLSv1.2:
|     ciphers:
|       TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (secp256r1) - A
|       TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (secp256r1) - A
|       TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 (secp256r1) - A
|       TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA (secp256r1) - A
|       TLS_RSA_WITH_AES_256_GCM_SHA384 (rsa 2048) - A
|       TLS_RSA_WITH_AES_256_CBC_SHA256 (rsa 2048) - A
|       TLS_RSA_WITH_AES_256_CBC_SHA (rsa 2048) - A
|       TLS_RSA_WITH_CAMELLIA_256_CBC_SHA (rsa 2048) - A
|       TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 (secp256r1) - A
|       TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (secp256r1) - A
|       TLS_RSA_WITH_AES_128_GCM_SHA256 (rsa 2048) - A
|       TLS_RSA_WITH_AES_128_CBC_SHA256 (rsa 2048) - A
|       TLS_RSA_WITH_AES_128_CBC_SHA (rsa 2048) - A
|       TLS_RSA_WITH_CAMELLIA_128_CBC_SHA (rsa 2048) - A
|     compressors:
|       NULL
|     cipher preference: server
|_  least strength: A

Nmap done: 1 IP address (1 host up) scanned in 1.80 seconds

注:以上可见当前服务器开启的TLS协议版本包括TLSv1.0、TLSv1.1和TLSv1.2

2.3 禁用非安全的协议

vim /etc/nginx/conf.d/www.cmdschool.org_443.conf

参数修改如下,

server {
    #...

    ssl_certificate 1_www.cmdschool.org_bundle.crt;
    ssl_certificate_key 2_www.cmdschool.org.key;
    ssl_session_timeout 5m;
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;

    #...
}

配置参数解析如下,
– “ssl_protocols”参数定义仅开启当前服务器支持的“TLSv1.2”
– “ssl_ciphers”参数定义安全的加密集合
另外,基于性能考虑,官方建议使用如下命令添加参数,

vim /etc/nginx/nginx.conf

加入的参数如下,

http {
    #...

    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    #...
}

详细的,以上参数意义如下,
– “ssl_session_cache”指令缓存工作进程之间的SSL会话(一兆字节大约4000个会话)。
– “ssl_session_timeout”指令定义缓存的时间,以上范例是10分钟。

2.4 重载使配置生效

systemctl reload nginx.service

2.5 使用客户端检查

openssl s_client -connect www.cmdschool.org:443 -servername www.cmdschool.org

可见如下显示,

CONNECTED(00000003)
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
verify return:1
depth=1 C = CN, O = "TrustAsia Technologies, Inc.", OU = Domain Validated SSL, CN = TrustAsia TLS RSA CA
verify return:1
depth=0 CN = www.cmdschool.org
verify return:1
---
Certificate chain
 0 s:CN = www.cmdschool.org
   i:C = CN, O = "TrustAsia Technologies, Inc.", OU = Domain Validated SSL, CN = TrustAsia TLS RSA CA
 1 s:C = CN, O = "TrustAsia Technologies, Inc.", OU = Domain Validated SSL, CN = TrustAsia TLS RSA CA
   i:C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIFsTCCBJmgAwIBAgIQD4jGnSVQAuYPvo055VM4ZDANBgkqhkiG9w0BAQsFADBy
MQswCQYDVQQGEwJDTjElMCMGA1UEChMcVHJ1c3RBc2lhIFRlY2hub2xvZ2llcywg
SW5jLjEdMBsGA1UECxMURG9tYWluIFZhbGlkYXRlZCBTU0wxHTAbBgNVBAMTFFRy
dXN0QXNpYSBUTFMgUlNBIENBMB4XDTIwMDUwMTAwMDAwMFoXDTI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
lFaO400TGTO/3wwvIAvMTvFk4wAAAXHQnkmAAAAEAwBIMEYCIQDqmicfouvvfcTZ
ebGuns16M2JFKYLBRXmltezL90lbBAIhAPlaTNoJlP9uoxt4u+8tXhHH+AcJt+Qa
wCKRX4f9R7nUAHYAXNxDkv7mq0VEsV6a1FbmEDf71fpH3KFzlLJe5vbHDsoAAAFx
0J5JvAAABAMARzBFAiAQ5/oZfOQ/rz6/9xWpVV1qb+rEjaDv7ycqkQCiNAJXuQIh
APbASKOtYEeoQoFbzMFRjIHNs2k3zzzC7sAvpsqZVa8QMA0GCSqGSIb3DQEBCwUA
A4IBAQA8eE3UOJw6U75cCNs/aw5obNeaPe/9Yxcq7Enu360PJQZunc+se1P/IbsN
So4Mhc6n35p3MsOUS0D/EMxltWxxLTV38t40EA1hccMa1fva1By1jMcnYd6duqDu
SbNw3lh94pN1em6QO8xvP8326obnqWMio71l5BSaqx1mB+aPjZRr8eHelb5ExXu9
OKDXsoItWlt3iLt2fG8x6T01OU9uHpv/9PjdujINDFAbzvZsiipduZlKUODIqoqg
VDFMclBanQ8NIr+LfdaSz5jxMAA4jvOkz+smDwdGBua89HsvUkZTQjw8lRfGL8Dc
CoF1HkG+RSSouh5FzMQMkFR9icmq
-----END CERTIFICATE-----
subject=CN = www.cmdschool.org

issuer=C = CN, O = "TrustAsia Technologies, Inc.", OU = Domain Validated SSL, CN = TrustAsia TLS RSA CA

---
No client certificate CA names sent
Peer signing digest: SHA512
Peer signature type: RSA
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 3356 bytes and written 451 bytes
Verification: OK
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: 81C8A7C5199F9026DEED47B900E19C92085E221473CE05A665F6FE0A0A5F4C2F
    Session-ID-ctx:
    Master-Key: C5244AEEC69D3D828B3350C1E763EFD95BB58588E2CB53862AF1AB782C86DC4EC9E0F88DDFB2C261070C6532774A2444
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket lifetime hint: 300 (seconds)
    TLS session ticket:
    0000 - 53 10 bb 7c c4 5a 85 71-71 d8 39 5e 2f 49 d7 d4   S..|.Z.qq.9^/I..
    0010 - 01 0c df ed a3 6c d4 1b-ad 38 ca 2d 7c 8e 6f be   .....l...8.-|.o.
    0020 - e2 d9 8b 04 d0 47 7b 0d-36 eb 66 8f 44 cd 08 50   .....G{.6.f.D..P
    0030 - 6e 3d c6 08 c8 04 19 08-e3 fe c3 c6 1c 50 0e 45   n=...........P.E
    0040 - 82 bf 54 6f da 29 17 67-f4 0e 18 03 a6 47 be 45   ..To.).g.....G.E
    0050 - 31 05 b3 95 5e 89 ce db-6b f5 57 9c 8b 45 bb 90   1...^...k.W..E..
    0060 - 5b e4 51 7d 35 dc 0e 27-2f 0f b6 11 42 40 57 a0   [.Q}5..'/...B@W.
    0070 - 92 56 79 f3 d9 65 bd 0c-08 14 e6 91 44 e9 7c 08   .Vy..e......D.|.
    0080 - 49 f2 47 e4 85 8b 86 17-8a 68 77 a4 5c cd 02 22   I.G......hw.\.."
    0090 - 2b ad 03 e8 e0 a3 ac e9-33 b5 e8 bc 0d e7 ab 08   +.......3.......
    00a0 - 8f 9b 21 b3 3f 44 ae 9d-0b 5d 6c 45 86 c3 f3 84   ..!.?D...]lE....
    00b0 - 8b 4a 3b 8e e4 48 4a 28-8e 08 cc 97 51 c0 49 a6   .J;..HJ(....Q.I.

    Start Time: 1607484000
    Timeout   : 7200 (sec)
    Verify return code: 0 (ok)
    Extended master secret: no
---
closed

如果你尝试用低版本协议链接,可尝试如下命令,

openssl s_client -connect www.cmdschool.org:443 -servername www.cmdschool.org -tls1

会收到如下拒绝信息,

CONNECTED(00000003)
write:errno=104
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 130 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1
    Cipher    : 0000
    Session-ID:
    Session-ID-ctx:
    Master-Key:
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1607564153
    Timeout   : 7200 (sec)
    Verify return code: 0 (ok)
    Extended master secret: no
---

另外,也可以之前的命令测试,

nmap --script ssl-enum-ciphers -p 443 www.cmdschool.org

可见如下显示,

Starting Nmap 7.70 ( https://nmap.org ) at 2020-12-09 03:50 EST
Nmap scan report for www.cmdschool.org (47.90.98.232)
Host is up (0.011s latency).

PORT    STATE SERVICE
443/tcp open  https
| ssl-enum-ciphers:
|   TLSv1.2:
|     ciphers:
|       TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (secp256r1) - A
|       TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 (secp256r1) - A
|     compressors:
|       NULL
|     cipher preference: server
|_  least strength: A

Nmap done: 1 IP address (1 host up) scanned in 0.99 seconds

参阅文档
====================
https://www.ssl.com/guide/disable-tls-1-0-and-1-1-apache-nginx/

https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/

http://nginx.org/en/docs/http/ngx_http_ssl_module.html

https://httpd.apache.org/docs/2.4/mod/mod_ssl.html

没有评论

发表评论

Nginx
如何设置Nginx HTTP-only Cookie?

1 基础知识 1.1 HTTP cookies 1.1.1 HTTP cookies的概念 &#82 …

Nginx
如何附加Nginx Cookie标签模块?

1 基础知识 “nginx_cookie_flag_module”为上游响应标头中的cookie设置 …