为什么我在 ip 而不是主机名上卷曲时需要额外的“-k”标志?

cal*_*vin 0 https curl http

我有这个代码

$ curl  "https://api.weixin.qq.com:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code"
Run Code Online (Sandbox Code Playgroud)

它可以从服务器获得如下响应

{"errcode":40013,"errmsg":"invalid appid rid: 5f96703e-5e24e4f1-691a9221"}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将主机名替换为 ip 时,我失败并显示以下消息

$ host api.weixin.qq.com
api.weixin.qq.com has address 180.97.7.108
api.weixin.qq.com has address 101.226.212.27

$ curl  "https://101.226.212.27:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code"
curl: (60) SSL: no alternative certificate subject name matches target host name '101.226.212.27'
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
Run Code Online (Sandbox Code Playgroud)

我必须添加-k以完成这项工作

$ curl  "https://101.226.212.27:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code" -k
{"errcode":40013,"errmsg":"invalid appid rid: 5f967132-1ee77e4b-2f378192"}c
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会发生这种情况,以及这里是否存在任何安全问题?如果有的话,我该如何删除这个-k标志?

Dan*_*erg 6

没有-k(或长版本--insecure),作为 TLS 握手的一部分,curl 确保您连接到的主机(在 URL 中指定)在服务器连接到它时提供的证书中也正确提及。进行检查以确保它与合法机器通话。

服务器证书包含一个主机名(或通配符)列表,curl 检查它们是否与 URL 中的主机名匹配。

当您在 URL 中指定 IP 地址时,curl 只能使用该地址与证书中的名称进行比较。服务器仍然可以列出特定的 IP 地址(https://1.1.1.1例如已知的),但非常罕见且非常特殊。

更好的修复

不要在命令行中使用 IP 地址,而是--resolve使用正确的主机名确保 curl 连接到您选择的确切 IP 地址。

警告

使用-k完全禁用 curl 的证书检查,然后您可能会受到 MITM 攻击而无法检测到。