Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to a websocket manually, with netcat/socat/telnet?

I am trying to connect to the reference websocket echo server "manually", in order to learn how the protocol works (I am using socat for that). However, the server invariably closes the connection without providing an answer. Any idea why?

Here is what I do:

socat - TCP:echo.websocket.org:80

Then, I paste the following text in the terminal:

GET /?encoding=text HTTP/1.1
Origin: http://www.websocket.org
Connection: Upgrade
Host: echo.websocket.org
Sec-WebSocket-Key: P7Kp2hTLNRPFMGLxPV47eQ==
Upgrade: websocket
Sec-WebSocket-Version: 13

I sniffed the parameters of the connection with the developer tools, in firefox, on the same machine, where this works flawlessly: therefore, I would assume they are correct. However after that, the server closes the connection immediately, without providing an answer. Why? How can I implement the protocol "manually"?

I would like type test in my terminal and get the server to reply with what I typed (It works in a web browser).

like image 412
user48678 Avatar asked Oct 22 '14 12:10

user48678


People also ask

How do I connect to a WebSocket?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

What is netcat and Socat?

Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party. It can act like a simple ad-hoc chat program. Socat can talk to Netcat and Netcat can talk to Socat.

How to use netcat as a web host?

Printf – Netcat can actually operate as a simplified web host. This command will let you save HTML code and publish it through your local server. All Netcat commands must start with the “netcat” identifier or “nc” as a shorter option. By default, the Netcat tool will assume you want to perform a port scan unless you indicate otherwise.

What is the difference between SOCAT and Netcat?

This tool is regarded as the advanced version of netcat. They do similar things, but socat has more additional functionality, such as permitting multiple clients to listen on a port, or reusing connections. Why do we need socat?

How do I test a netcat application?

Once you have a Netcat application set up on your Windows or Linux server, you can start running basic commands to test its functionality. Here are a few to get started with: nc -help – This command will print a list of all of the available commands you can use in Netcat.

How do I send and receive files using netcat?

To accomplish this, you need to run Netcat from two locations: one that will act as a server to send the file and one that will act as the client to receive it. Run this Netcat command on the server instance to send the file over port 1499: Then run this command on the client to accept, receive, and close the connection:


3 Answers

I think you want to modify the socket stream to translate \n (line feed) to CRLF (Carriage return & line feed). Doing info socat produces detailed information which includes this modifier:

crnl   Converts the default line termination character NL ('\n',  0x0a)
       to/from CRNL ("\r\n", 0x0d0a) when writing/reading on this chan-
       nel (example).  Note: socat simply strips all CR characters.

So I think you should be able to do this:

socat - TCP:echo.websocket.org:80,crnl
like image 173
Michael Petch Avatar answered Oct 22 '22 02:10

Michael Petch


I'd like to add that my WebSocket tool websocat can help in debugging the WebSocket protocol, especially when combined with socat:

$ websocat - ws-c:sh-c:"socat -v -x - tcp:echo.websocket.org:80" --ws-c-uri ws://echo.websocket.org
> 2018/07/03 16:30:06.021658  length=157 from=0 to=156
 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a  GET / HTTP/1.1..
 48 6f 73 74 3a 20 65 63 68 6f 2e 77 65 62 73 6f  Host: echo.webso
 63 6b 65 74 2e 6f 72 67 0d 0a                    cket.org..
 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72  Connection: Upgr
 61 64 65 0d 0a                                   ade..
 55 70 67 72 61 64 65 3a 20 77 65 62 73 6f 63 6b  Upgrade: websock
 65 74 0d 0a                                      et..
 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 56 65  Sec-WebSocket-Ve
 72 73 69 6f 6e 3a 20 31 33 0d 0a                 rsion: 13..
 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 4b 65  Sec-WebSocket-Ke
 79 3a 20 59 76 36 32 44 31 57 6d 7a 79 79 31 65  y: Yv62D1Wmzyy1e
 69 6d 62 47 6d 68 69 61 67 3d 3d 0d 0a           imbGmhiag==..
 0d 0a                                            ..
--
< 2018/07/03 16:30:06.164057  length=201 from=0 to=200
 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62  HTTP/1.1 101 Web
 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c   Socket Protocol
 20 48 61 6e 64 73 68 61 6b 65 0d 0a               Handshake..
 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72  Connection: Upgr
 61 64 65 0d 0a                                   ade..
 44 61 74 65 3a 20 54 75 65 2c 20 30 33 20 4a 75  Date: Tue, 03 Ju
 6c 20 32 30 31 38 20 31 33 3a 31 35 3a 30 30 20  l 2018 13:15:00 
 47 4d 54 0d 0a                                   GMT..
 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 41 63  Sec-WebSocket-Ac
 63 65 70 74 3a 20 55 56 6a 32 74 35 50 43 7a 62  cept: UVj2t5PCzb
 58 49 32 52 4e 51 75 70 2f 71 48 31 63 5a 44 6e  XI2RNQup/qH1cZDn
 38 3d 0d 0a                                      8=..
 53 65 72 76 65 72 3a 20 4b 61 61 7a 69 6e 67 20  Server: Kaazing 
 47 61 74 65 77 61 79 0d 0a                       Gateway..
 55 70 67 72 61 64 65 3a 20 77 65 62 73 6f 63 6b  Upgrade: websock
 65 74 0d 0a                                      et..
 0d 0a                                            ..
--
ABCDEF
> 2018/07/03 16:30:12.707919  length=13 from=157 to=169
 82 87 40 57 f5 88 01 15 b6 cc 05 11 ff           ..@W.........
--
< 2018/07/03 16:30:12.848398  length=9 from=201 to=209
 82 07 41 42 43 44 45 46 0a                       ..ABCDEF.
--
ABCDEF
> 2018/07/03 16:30:14.528333  length=6 from=170 to=175
 88 80 18 ec 05 a8                                ......
--
< 2018/07/03 16:30:14.671629  length=2 from=210 to=211
 88 00                                            ..
--

In case of failures with manually driven socat -v -x - TCP:echo.websocket.org:80,crnl (mentioned in the other answer), you can compare it with WebSocat-driven socat like in session depicted above.

Reverse (server) example with socat debug dump:

socat -v -x tcp-l:1234,fork,reuseaddr exec:'websocat -t ws-u\:stdio\: mirror\:'
like image 40
Vi. Avatar answered Oct 22 '22 02:10

Vi.


Alternatively, here is a way to connect and read the stream from a wss secure websocket stream from the command line using solely core php.

php -r '$sock=stream_socket_client("tls://echo.websocket.org:443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));if(!$sock){echo"[$n]$e".PHP_EOL;}else{fwrite($sock,"GET / HTTP/1.1\r\nHost: echo.websocket.org\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");while(!feof($sock)){var_dump(fgets($sock,2048));}}'

Other similar example, pulling from another wss server: (Do not get rekt)

php -r '$sock=stream_socket_client("tls://stream.binance.com:9443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));if(!$sock){echo"[$n]$e".PHP_EOL;}else{fwrite($sock,"GET /stream?streams=btcusdt@kline_1m HTTP/1.1\r\nHost: stream.binance.com:9443\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");while(!feof($sock)){var_dump(explode(",",fgets($sock,512)));}}'
like image 2
NVRM Avatar answered Oct 22 '22 03:10

NVRM