Http 为什么我需要额外的`-k`标志,而不是主机名?

Http 为什么我需要额外的`-k`标志,而不是主机名?,http,curl,https,Http,Curl,Https,我有这个密码 $ curl "https://api.weixin.qq.com:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code" 它可以从服务器获得如下响应 {"errcode":40013,"errmsg":"invalid appid rid: 5f96703e-5e24e4f1-6

我有这个密码

$ curl  "https://api.weixin.qq.com:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code"
它可以从服务器获得如下响应

{"errcode":40013,"errmsg":"invalid appid rid: 5f96703e-5e24e4f1-691a9221"}
但是,当我尝试将主机名替换为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.
我必须添加
-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
我想知道为什么会发生这种情况,以及这里是否存在任何安全问题?如果有,如何删除此
-k
标志?

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

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

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

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

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

因此,当我试图将
api.weixin.qq.com
解析为
101.226.212.27
时,可能会发生攻击,因为我们面临dns劫持的风险。如果我能确保
101.226.212.27
是我想要的确切IP,那么就不会有安全问题?不会。这将完全禁用任何证书检查。我建议您查看curl的--resolve选项。同时,我认为如果我决定对一个IP进行curl,并且conenction顺利建立,我总能得到这个警告,除了几个IP?顺便说一下,-k是--unsecure的别名