57

draft-ietf-hybi-thewebsocketprotocol-17 的1.3 节“打开握手”中,描述Sec-WebSocket-Key如下:

为了证明握手被接收,服务器必须获取两条信息并将它们组合起来形成响应。第一条信息来自|Sec-WebSocket-Key| 客户端握手中的头字段:

Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

对于这个头域,服务器必须取值(在头域中,例如 base64 编码的 [RFC4648] 版本减去任何前导和尾随空格),并将其与全局唯一标识符(GUID,[RFC4122 ]) 字符串形式的“258EAFA5-E914-47DA-95CA-C5AB0DC85B11”,不太可能被不理解 WebSocket 协议的网络端点使用。然后在服务器的握手 [FIPS.180-2.2002] 中返回此连接的 SHA-1 哈希(160 位)、base64 编码(参见 [RFC4648] 的第 4 节)。

这是我无法理解的事情:为什么不简单地返回代码 101?如果正确使用Sec-WebSocket-Key是为了安全,或者为了证明它们可以处理 websocket 请求,那么任何服务器都可以根据需要返回预期的密钥,并假装它们是 WebSocket 服务器。

4

6 回答 6

44

根据 RFC 6455 Websocket 标准

第一部分

.. the server has to prove to the client that it received the
client's WebSocket handshake, so that the server doesn't accept
connections that are not WebSocket connections.  This prevents an
attacker from tricking a WebSocket server by sending it carefully
crafted packets using XMLHttpRequest [XMLHttpRequest] or a form
submission.

...
For this header field, the server has to take the value (as present
in the header field, e.g., the base64-encoded [RFC4648] version minus
any leading and trailing whitespace) and concatenate this with the
Globally Unique Identifier (GUID, [RFC4122]) "258EAFA5-E914-47DA-
95CA-C5AB0DC85B11" in string form, which is unlikely to be used by
network endpoints that do not understand the WebSocket Protocol.

第二部分

The |Sec-WebSocket-Key| header field is used in the WebSocket opening
handshake.  It is sent from the client to the server to provide part
of the information used by the server to prove that it received a
valid WebSocket opening handshake.  This helps ensure that the server
does not accept connections from non-WebSocket clients (e.g., HTTP
clients) that are being abused to send data to unsuspecting WebSocket
servers.

因此,由于标准中指定了 GUID 的值,因此不知道 Websockets 的服务器不太可能(可能,以非常小的概率)使用它。它不提供任何安全性(安全 websockets - wss:// - 提供),它只是确保服务器理解 websockets 协议。

真的,正如您所提到的,如果您知道 websockets(这是要检查的),您可以通过发送正确的响应来假装是 websocket 服务器。但是,如果您的行为不正确(例如正确地形成帧),它将被视为违反协议。其实你可以写一个不正确的websocket服务器,但是用处不大。

另一个目的是防止客户端意外请求 websockets 升级而不是期望它(例如,通过手动添加相应的标头然后期望其他)。Sec-WebSocket-Key 等相关头信息禁止setRequestHeader在浏览器中使用方法设置。

于 2013-08-16T07:27:34.767 回答
19

主要用于缓存破坏。

想象一个透明的反向代理服务器监视 HTTP 流量。如果它不理解 WS,它可能会错误地缓存 WS 握手并以无用的 101 回复下一个客户端。

使用高熵密钥并要求对 WS 进行相当特定的基本质询-响应可确保服务器真正理解这是 WS 握手,并反过来告诉客户端服务器确实会在端口上侦听。缓存反向代理永远不会“错误地”实现该散列逻辑。

于 2016-09-19T22:35:58.950 回答
10

我倾向于同意。

如果客户端忽略 Sec-WebSocket-Accept 标头的值,任何重要的事情都不会改变。

为什么?因为服务器没有通过做这个计算来证明任何东西(除了它有代码来做计算)。它唯一排除的只是一个简单地回复预设响应的服务器。

标头的交换(例如,具有固定的 'key' 和 'accept' 值)已经足以排除与至少不是试图成为 WebSocket 服务器的东西的任何意外连接;如果它正在尝试,那么它进行这种计算的要求几乎不会成为它成功的障碍。

RFC 声称:

“.. 服务器必须向客户端证明它收到了客户端的 WebSocket 握手,以便服务器不接受不是 WebSocket 连接的连接。”

和:

“这有助于确保服务器不接受来自非 WebSocket 客户端的连接..”

这些说法都没有任何意义。服务器永远不会拒绝连接,因为它是计算哈希的服务器,而不是检查它的服务器。

如果魔法 GUID 不是固定的,而是客户端和服务器之间的共享秘密,这种交换将是有意义的。在这种情况下,交换将允许服务器向客户端证明它拥有共享秘密而不泄露它。

于 2016-05-06T14:02:47.027 回答
2

RFC 6455 规范显示了服务器需要响应客户端(浏览器)的(最少)4 行。最难的部分是确认您的 Websocket 服务器 C 代码正在执行正确的计算。这是一个简短的 PHP 脚本(PHP 很容易在所有操作系统上安装),它将正确计算要回复的密钥。将您从客户端(浏览器)获得的密钥硬编码到下面的第二行:

<?php
    $client_websocket_key = "IRhw449z7G0Mov9CahJ+Ow==";
    $concat = $client_websocket_key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    $ascii_sha1 = sha1( $concat );  // print this one for debugging, not used for real value.
    $sha1 = sha1( $concat, true );
    echo base64_encode( $sha1 );
?>
于 2019-06-07T20:29:30.500 回答
1

RFC 不清楚的是,来自客户端的“Sec-WebSocket-Key”标头在每个请求上应该是随机的。这意味着来自代理的任何缓存结果都将包含无效的“Sec-WebSocket-Accept”回复标头,因此 websocket 连接将失败,而不是无意中读取缓存数据。

于 2019-05-23T08:58:20.217 回答
0

服务器说:未定义的索引 Sec-WebSocket-Key。当我从手机访问该网站时。从同一台电脑(本地主机)没有这样的问题解决方案是什么:

function doHandshake($received_header,$client_socket_resource, $host_name, $port) {
        $headers = array();
        $lines = preg_split("/\r\n/", $received_header);
        foreach($lines as $line)
        {
            $line = chop($line);
            if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
            {
                $headers[$matches[1]] = $matches[2];
            }
        }

        $secKey = $headers['Sec-WebSocket-Key'];
        $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
        $buffer  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
        "Upgrade: websocket\r\n" .
        "Connection: Upgrade\r\n" .
        "WebSocket-Origin: $host_name\r\n" .
        "WebSocket-Location: ws://$host_name:$port/demo/shout.php\r\n".
        "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
        socket_write($client_socket_resource,$buffer,strlen($buffer));
    }
于 2021-03-30T03:34:54.197 回答