【问题标题】:Why I need extra `-k` flag when I curl over ip rather than hostname?为什么当我在 ip 而不是主机名上卷曲时需要额外的 `-k` 标志?
【发布时间】:2021-02-08 10:35:15
【问题描述】:

我有这个代码

$ 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 标志?

【问题讨论】:

    标签: http curl https


    【解决方案1】:

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

    服务器证书包含一个主机名(或通配符)列表,curl 检查它们是否与 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,并且连接顺利建立,我总是可以得到这个警告,除了几个IP?
    • 顺便说一下,-k 是 --insecure 的别名
    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2016-10-05
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多