我正试图使用javascript从本地test.dev
页面向代表test.com
运行ip123.123.123.123
的服务器发出websocket请求。请求通过,但123.123.123.123
服务器在websocket请求中看到Origin: test.dev
标头,并拒绝连接,因为它希望看到Origin: test.com
。
以下是用于连接套接字的javascript代码:
ws = new WebSocket("123.123.123.123");
如何使用javascript启动具有Origin: test.com
的不诚实Origin
标头的websocket连接?
我希望这样的东西能起作用,但我找不到这样的东西:
ws = new WebSocket("123.123.123.123", "test.com");
简单的解决方案是在hosts
文件中创建一个条目,将test.com
映射到123.123.123.123
。稍后,当您想要连接"真正的"test.com
时,您需要删除此条目。
一个不那么棘手的解决方案需要使用一个代理,它可以随时为您重写标头。考虑在您的系统上安装nginx
,然后将请求代理到123.123.123.123
,除了Origin标头的之外,保持所有内容都相同。以下是您在nginx配置文件中需要的条目:
server {
server_name test.dev;
location / {
proxy_pass http://123.123.123.123;
proxy_set_header Origin test.com;
# the following 3 are required to proxy WebSocket connections.
# See more here: http://nginx.com/blog/websocket-nginx/
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
如何使用javascript启动具有
Origin: test.com
的不诚实Origin
标头的websocket连接?
如果我们可以在JavaScript中伪造请求的来源,那么同源策略将不能很好地保证我们的安全。它的存在只是为了保护我们免受这种和其他潜在攻击媒介的攻击。
由于这看起来像是开发工作,您是否考虑过使用Fiddler(免费)或Charles(付费)等网络调试代理?有了这些,您可以为自己的机器或通过调试器代理的任何测试机器修改WebSocket的初始握手请求或响应。
一个好的解决方案是用另一个WebSocket库覆盖WebSocket调用,比如https://github.com/websockets/ws.要在浏览器中使用这个node.js库,只需使用http://browserify.org/.
如果真的必须从javascript伪造一个假的"origin"头值,有一种方法。这不是你在公认的原则手册中可以找到的东西,但它在这里:
创建一个调用套接字的php文件,其中包含一个伪原始值。现在使用ajax从javascript调用php文件。
这可能不优雅、不道德或不可接受,但永远不要接受任何人告诉你这是不可能的。
"send.php"是使用javascript 中的ajax调用的
send.php内容
require "websocket_client.php";
$client = new Client("IP_ADDR:PORT", $_GET[mobile] ."|".$_GET[login]);
$client->send('1112223333|sms');
$client->send(json_encode(array('login'=>$user,'msg'=>$msg)));
echo $client->receive();`enter code here`
websocket_client.php是一个类文件,包含基本的websocket函数(包括自定义的原始值
/**
* Perform WebSocket handshake
*/
protected function connect() {
$url_parts = parse_url($this->socket_uri);
$scheme = $url_parts['scheme'];
$host = $url_parts['host'];
$user = isset($url_parts['user']) ? $url_parts['user'] : '';
$pass = isset($url_parts['pass']) ? $url_parts['pass'] : '';
$port = isset($url_parts['port']) ? $url_parts['port'] : ($scheme === 'wss' ? 443 : 80);
$path = isset($url_parts['path']) ? $url_parts['path'] : '/';
$query = isset($url_parts['query']) ? $url_parts['query'] : '';
$fragment = isset($url_parts['fragment']) ? $url_parts['fragment'] : '';
$path_with_query = $path;
if (!empty($query)) $path_with_query .= '?' . $query;
if (!empty($fragment)) $path_with_query .= '#' . $fragment;
if (!in_array($scheme, array('ws', 'wss'))) {
throw new BadUriException(
"Url should have scheme ws or wss, not '$scheme' from URI '$this->socket_uri' ."
);
}
$host_uri = ($scheme === 'wss' ? 'ssl' : 'tcp') . '://' . $host;
// Open the socket. @ is there to supress warning that we will catch in check below instead.
$this->socket = @fsockopen($host_uri, $port, $errno, $errstr, $this->options['timeout']);
if ($this->socket === false) {
throw new ConnectionException(
"Could not open socket to "$host:$port": $errstr ($errno)."
);
}
// Set timeout on the stream as well.
stream_set_timeout($this->socket, $this->options['timeout']);
// Generate the WebSocket key.
$key = self::generateKey();
// Default headers (using lowercase for simpler array_merge below).
$headers = array(
'host' => $host . ":" . $port,
'user-agent' => 'websocket-client-php',
'connection' => 'Upgrade',
'upgrade' => 'websocket',
'origin' => $MY_CUSTOM_SHADY_VALUE,
'sec-websocket-key' => $key,
'sec-websocket-version' => '13',
);
// Handle basic authentication.
if ($user || $pass) {
$headers['authorization'] = 'Basic ' . base64_encode($user . ':' . $pass) . "rn";
}
// Deprecated way of adding origin (use headers instead).
if (isset($this->options['origin'])) $headers['origin'] = $this->options['origin'];
// Add and override with headers from options.
if (isset($this->options['headers'])) {
$headers = array_merge($headers, array_change_key_case($this->options['headers']));
}
$header =
"GET " . $path_with_query . " HTTP/1.1rn"
. implode(
"rn", array_map(
function($key, $value) { return "$key: $value"; }, array_keys($headers), $headers
)
)
. "rnrn";
// Send headers.
$this->write($header);
// Get server response.
$response = '';
do {
$buffer = stream_get_line($this->socket, 1024, "rn");
$response .= $buffer . "n";
$metadata = stream_get_meta_data($this->socket);
} while (!feof($this->socket) && $metadata['unread_bytes'] > 0);
/// @todo Handle version switching
// Validate response.
if (!preg_match('#Sec-WebSocket-Accept:s(.*)$#mUi', $response, $matches)) {
$address = $scheme . '://' . $host . $path_with_query;
throw new ConnectionException(
"Connection to '{$address}' failed: Server sent invalid upgrade response:n"
. $response
);
}
$keyAccept = trim($matches[1]);
$expectedResonse
= base64_encode(pack('H*', sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
if ($keyAccept !== $expectedResonse) {
throw new ConnectionException('Server sent bad upgrade response.');
}
$this->is_connected = true;
}