2 * Routines for WebSocket dissection
3 * Copyright 2012, Alexis La Goutte <alexis.lagoutte@gmail.com>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <epan/packet.h>
31 #include <epan/expert.h>
32 #include <epan/prefs.h>
36 * The information used comes from:
37 * RFC6455: The WebSocket Protocol
38 * http://www.iana.org/assignments/websocket (last updated 2012-04-12)
41 void proto_reg_handoff_websocket(void);
43 dissector_handle_t text_lines_handle;
44 dissector_handle_t json_handle;
46 #define WEBSOCKET_NONE 0
47 #define WEBSOCKET_TEXT 1
48 #define WEBSOCKET_JSON 2
50 static gint pref_text_type = WEBSOCKET_NONE;
52 /* Initialize the protocol and registered fields */
53 static int proto_websocket = -1;
54 static int hf_ws_fin = -1;
55 static int hf_ws_reserved = -1;
56 static int hf_ws_opcode = -1;
57 static int hf_ws_mask = -1;
58 static int hf_ws_payload_length = -1;
59 static int hf_ws_payload_length_ext_16 = -1;
60 static int hf_ws_payload_length_ext_64 = -1;
61 static int hf_ws_masking_key = -1;
62 static int hf_ws_payload = -1;
63 static int hf_ws_payload_unmask = -1;
64 static int hf_ws_payload_continue = -1;
65 static int hf_ws_payload_text = -1;
66 static int hf_ws_payload_text_mask = -1;
67 static int hf_ws_payload_text_unmask = -1;
68 static int hf_ws_payload_binary = -1;
69 static int hf_ws_payload_binary_mask = -1;
70 static int hf_ws_payload_binary_unmask = -1;
71 static int hf_ws_payload_close = -1;
72 static int hf_ws_payload_close_mask = -1;
73 static int hf_ws_payload_close_unmask = -1;
74 static int hf_ws_payload_close_status_code = -1;
75 static int hf_ws_payload_close_reason = -1;
76 static int hf_ws_payload_ping = -1;
77 static int hf_ws_payload_ping_mask = -1;
78 static int hf_ws_payload_ping_unmask = -1;
79 static int hf_ws_payload_pong = -1;
80 static int hf_ws_payload_pong_mask = -1;
81 static int hf_ws_payload_pong_unmask = -1;
82 static int hf_ws_payload_unknown = -1;
84 static gint ett_ws = -1;
85 static gint ett_ws_pl = -1;
86 static gint ett_ws_mask = -1;
88 #define WS_CONTINUE 0x0
95 static const value_string ws_opcode_vals[] = {
96 { WS_CONTINUE, "Continuation" },
98 { WS_BINARY, "Binary" },
99 { WS_CLOSE, "Connection Close" },
105 #define MASK_WS_FIN 0x80
106 #define MASK_WS_RSV 0x70
107 #define MASK_WS_OPCODE 0x0F
108 #define MASK_WS_MASK 0x80
109 #define MASK_WS_PAYLOAD_LEN 0x7F
111 static const value_string ws_close_status_code_vals[] = {
112 { 1000, "Normal Closure" },
113 { 1001, "Going Away" },
114 { 1002, "Protocol error" },
115 { 1003, "Unsupported Data" },
116 { 1004, "---Reserved----" },
117 { 1005, "No Status Rcvd" },
118 { 1006, "Abnormal Closure" },
119 { 1007, "Invalid frame payload data" },
120 { 1008, "Policy Violation" },
121 { 1009, "Message Too Big" },
122 { 1010, "Mandatory Ext." },
123 { 1011, "Internal Server" },
124 { 1015, "TLS handshake" },
128 static dissector_table_t port_subdissector_table;
129 static heur_dissector_list_t heur_subdissector_list;
131 #define MAX_UNMASKED_LEN (1024 * 64)
133 tvb_unmasked(tvbuff_t *tvb, const guint offset, guint payload_length, const guint8 *masking_key)
137 tvbuff_t *tvb_unmask = NULL;
139 const guint8 *data_mask;
140 guint unmasked_length = payload_length > MAX_UNMASKED_LEN ? MAX_UNMASKED_LEN : payload_length;
142 data_unmask = (gchar *)g_malloc(unmasked_length);
143 data_mask = tvb_get_ptr(tvb, offset, unmasked_length);
144 /* Unmasked(XOR) Data... */
145 for(i=0; i < unmasked_length; i++){
146 data_unmask[i] = data_mask[i] ^ masking_key[i%4];
149 tvb_unmask = tvb_new_real_data(data_unmask, unmasked_length, unmasked_length);
150 tvb_set_free_cb(tvb_unmask, g_free);
155 dissect_websocket_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, proto_tree *ws_tree, guint8 opcode, guint payload_length, guint8 mask, const guint8* masking_key)
158 proto_item *ti_unmask, *ti;
159 dissector_handle_t handle;
160 proto_tree *pl_tree, *mask_tree = NULL;
161 tvbuff_t *payload_tvb = NULL;
164 ti = proto_tree_add_item(ws_tree, hf_ws_payload, tvb, offset, payload_length, ENC_NA);
165 pl_tree = proto_item_add_subtree(ti, ett_ws_pl);
167 payload_tvb = tvb_unmasked(tvb, offset, payload_length, masking_key);
168 tvb_set_child_real_data_tvbuff(tvb, payload_tvb);
169 add_new_data_source(pinfo, payload_tvb, payload_length > tvb_length(payload_tvb) ? "Unmasked Data (truncated)" : "Unmasked Data");
170 ti = proto_tree_add_item(ws_tree, hf_ws_payload_unmask, payload_tvb, offset, payload_length, ENC_NA);
171 mask_tree = proto_item_add_subtree(ti, ett_ws_mask);
173 payload_tvb = tvb_new_subset(tvb, offset, payload_length, -1);
176 handle = dissector_get_uint_handle(port_subdissector_table, pinfo->match_uint);
178 call_dissector_only(handle, payload_tvb, pinfo, tree, NULL);
180 dissector_try_heuristic(heur_subdissector_list, payload_tvb, pinfo, tree, NULL);
184 /* TODO: Add dissector of Extension (not extension available for the moment...) */
186 /* Application Data */
189 case WS_CONTINUE: /* Continue */
190 proto_tree_add_item(pl_tree, hf_ws_payload_continue, tvb, offset, payload_length, ENC_NA);
191 /* TODO: Add Fragmentation support... */
194 case WS_TEXT: /* Text */
197 proto_tree_add_item(pl_tree, hf_ws_payload_text_mask, tvb, offset, payload_length, ENC_NA);
198 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_text_unmask, payload_tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
199 PROTO_ITEM_SET_GENERATED(ti_unmask);
200 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_text, payload_tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
201 PROTO_ITEM_SET_HIDDEN(ti_unmask);
203 const gchar *saved_match_string = pinfo->match_string;
204 void *save_private_data = pinfo->private_data;
206 pinfo->match_string = NULL;
207 pinfo->private_data = NULL;
208 switch(pref_text_type){
210 call_dissector(text_lines_handle, payload_tvb, pinfo, pl_tree);
213 call_dissector(json_handle, payload_tvb, pinfo, pl_tree);
218 proto_tree_add_item(pl_tree, hf_ws_payload_text, tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
221 pinfo->match_string = saved_match_string;
222 pinfo->private_data = save_private_data;
224 offset += payload_length;
227 case WS_BINARY: /* Binary */
229 proto_tree_add_item(pl_tree, hf_ws_payload_binary_mask, tvb, offset, payload_length, ENC_NA);
230 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_binary_unmask, payload_tvb, offset, payload_length, ENC_NA);
231 PROTO_ITEM_SET_GENERATED(ti_unmask);
232 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_binary, payload_tvb, offset, payload_length, ENC_NA);
233 PROTO_ITEM_SET_HIDDEN(ti_unmask);
235 proto_tree_add_item(pl_tree, hf_ws_payload_binary, tvb, offset, payload_length, ENC_NA);
237 offset += payload_length;
240 case WS_CLOSE: /* Close */
242 proto_tree_add_item(pl_tree, hf_ws_payload_close_mask, tvb, offset, payload_length, ENC_NA);
243 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_unmask, payload_tvb, offset, payload_length, ENC_NA);
244 PROTO_ITEM_SET_GENERATED(ti_unmask);
245 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close, payload_tvb, offset, payload_length, ENC_NA);
246 PROTO_ITEM_SET_HIDDEN(ti_unmask);
247 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_status_code, payload_tvb, offset, 2, ENC_BIG_ENDIAN);
248 PROTO_ITEM_SET_GENERATED(ti_unmask);
250 if(payload_length > 2){
251 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_reason, payload_tvb, offset+2, payload_length-2, ENC_ASCII|ENC_NA);
252 PROTO_ITEM_SET_GENERATED(ti_unmask);
255 proto_tree_add_item(pl_tree, hf_ws_payload_close, tvb, offset, payload_length, ENC_NA);
256 proto_tree_add_item(pl_tree, hf_ws_payload_close_status_code, tvb, offset, 2, ENC_BIG_ENDIAN);
257 if(payload_length > 2){
258 proto_tree_add_item(pl_tree, hf_ws_payload_close_reason, tvb, offset+2, payload_length-2, ENC_ASCII|ENC_NA);
261 offset += payload_length;
264 case WS_PING: /* Ping */
266 proto_tree_add_item(pl_tree, hf_ws_payload_ping_mask, tvb, offset, payload_length, ENC_NA);
267 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_ping_unmask, payload_tvb, offset, payload_length, ENC_NA);
268 PROTO_ITEM_SET_GENERATED(ti_unmask);
269 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_ping, payload_tvb, offset, payload_length, ENC_NA);
270 PROTO_ITEM_SET_HIDDEN(ti_unmask);
272 proto_tree_add_item(pl_tree, hf_ws_payload_ping, tvb, offset, payload_length, ENC_NA);
274 offset += payload_length;
277 case WS_PONG: /* Pong */
279 proto_tree_add_item(pl_tree, hf_ws_payload_pong_mask, tvb, offset, payload_length, ENC_NA);
280 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_pong_unmask, payload_tvb, offset, payload_length, ENC_NA);
281 PROTO_ITEM_SET_GENERATED(ti_unmask);
282 ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_pong, payload_tvb, offset, payload_length, ENC_NA);
283 PROTO_ITEM_SET_HIDDEN(ti_unmask);
285 proto_tree_add_item(pl_tree, hf_ws_payload_pong, tvb, offset, payload_length, ENC_NA);
287 offset += payload_length;
290 default: /* Unknown */
291 ti = proto_tree_add_item(pl_tree, hf_ws_payload_unknown, tvb, offset, payload_length, ENC_NA);
292 expert_add_info_format(pinfo, ti, PI_UNDECODED, PI_NOTE, "Dissector for Websocket Opcode (%d)"
293 " code not implemented, Contact Wireshark developers"
294 " if you want this supported", opcode);
302 dissect_websocket(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
304 proto_item *ti, *ti_len;
305 guint8 fin, opcode, mask;
306 guint length, short_length, payload_length, recurse_length;
307 guint payload_offset, mask_offset, recurse_offset;
308 proto_tree *ws_tree = NULL;
309 const guint8 *masking_key = NULL;
310 tvbuff_t *tvb_payload = NULL;
312 length = tvb_length(tvb);
314 pinfo->desegment_len = 2;
318 short_length = tvb_get_guint8(tvb, 1) & MASK_WS_PAYLOAD_LEN;
319 if(short_length==126){
321 pinfo->desegment_len = 2+2;
324 payload_length = tvb_get_ntohs(tvb, 2);
327 else if(short_length==127){
329 pinfo->desegment_len = 2+8;
332 /* warning C4244: '=' : conversion from 'guint64' to 'guint ', possible loss of data */
333 payload_length = (guint)tvb_get_ntoh64(tvb, 2);
337 payload_length = short_length;
342 mask = (tvb_get_guint8(tvb, 1) & MASK_WS_MASK) >> 4;
343 payload_offset = mask_offset + (mask ? 4 : 0);
345 if(length < payload_offset + payload_length){
346 /* XXXX Warning desegment_len is 32 bits */
347 pinfo->desegment_len = payload_offset + payload_length - length;
351 /* We've got the entire message! */
353 col_set_str(pinfo->cinfo, COL_PROTOCOL, "WebSocket");
354 col_set_str(pinfo->cinfo, COL_INFO, "WebSocket");
357 ti = proto_tree_add_item(tree, proto_websocket, tvb, 0, payload_offset, ENC_NA);
358 ws_tree = proto_item_add_subtree(ti, ett_ws);
362 proto_tree_add_item(ws_tree, hf_ws_fin, tvb, 0, 1, ENC_NA);
363 fin = (tvb_get_guint8(tvb, 0) & MASK_WS_FIN) >> 4;
364 proto_tree_add_item(ws_tree, hf_ws_reserved, tvb, 0, 1, ENC_NA);
367 proto_tree_add_item(ws_tree, hf_ws_opcode, tvb, 0, 1, ENC_NA);
368 opcode = tvb_get_guint8(tvb, 0) & MASK_WS_OPCODE;
369 col_append_fstr(pinfo->cinfo, COL_INFO, " %s", val_to_str_const(opcode, ws_opcode_vals, "Unknown Opcode"));
370 col_append_fstr(pinfo->cinfo, COL_INFO, " %s", fin ? "[FIN]" : "");
372 /* Add Mask bit to the tree */
373 proto_tree_add_item(ws_tree, hf_ws_mask, tvb, 1, 1, ENC_NA);
374 col_append_fstr(pinfo->cinfo, COL_INFO, " %s", mask ? "[MASKED]" : "");
376 /* (Extended) Payload Length */
377 ti_len = proto_tree_add_item(ws_tree, hf_ws_payload_length, tvb, 1, 1, ENC_NA);
378 if(short_length==126){
379 proto_item_append_text(ti_len, " Extended Payload Length (16 bits)");
380 proto_tree_add_item(ws_tree, hf_ws_payload_length_ext_16, tvb, 2, 2, ENC_BIG_ENDIAN);
382 else if(short_length==127){
383 proto_item_append_text(ti_len, " Extended Payload Length (64 bits)");
384 proto_tree_add_item(ws_tree, hf_ws_payload_length_ext_64, tvb, 2, 8, ENC_BIG_ENDIAN);
389 proto_tree_add_item(ws_tree, hf_ws_masking_key, tvb, mask_offset, 4, ENC_NA);
390 masking_key = tvb_get_ptr(tvb, mask_offset, 4);
393 tvb_payload = tvb_new_subset_remaining(tvb, payload_offset);
394 dissect_websocket_payload(tvb_payload, pinfo, tree, ws_tree, opcode, payload_length, mask, masking_key);
396 /* Call this function recursively, to see if we have enough data to parse another websocket message */
398 recurse_offset = payload_offset + payload_length;
399 if(length > recurse_offset){
400 recurse_length = dissect_websocket(tvb_new_subset_remaining(tvb, payload_offset+payload_length), pinfo, tree, data);
401 if(pinfo->desegment_len) pinfo->desegment_offset += recurse_offset;
402 return recurse_offset + recurse_length;
404 return recurse_offset;
409 proto_register_websocket(void)
412 static hf_register_info hf[] = {
414 { "Fin", "websocket.fin",
415 FT_BOOLEAN, 8, NULL, MASK_WS_FIN,
416 "Indicates that this is the final fragment in a message", HFILL }
419 { "Reserved", "websocket.rsv",
420 FT_UINT8, BASE_HEX, NULL, MASK_WS_RSV,
421 "Must be zero", HFILL }
424 { "Opcode", "websocket.opcode",
425 FT_UINT8, BASE_DEC, VALS(ws_opcode_vals), MASK_WS_OPCODE,
426 "Defines the interpretation of the Payload data", HFILL }
429 { "Mask", "websocket.mask",
430 FT_BOOLEAN, 8, NULL, MASK_WS_MASK,
431 "Defines whether the Payload data is masked", HFILL }
433 { &hf_ws_payload_length,
434 { "Payload length", "websocket.payload_length",
435 FT_UINT8, BASE_DEC, NULL, MASK_WS_PAYLOAD_LEN,
436 "The length of the Payload data", HFILL }
438 { &hf_ws_payload_length_ext_16,
439 { "Extended Payload length (16 bits)", "websocket.payload_length_ext_16",
440 FT_UINT16, BASE_DEC, NULL, 0x0,
441 "The length (16 bits) of the Payload data", HFILL }
443 { &hf_ws_payload_length_ext_64,
444 { "Extended Payload length (64 bits)", "websocket.payload_length_ext_64",
445 FT_UINT64, BASE_DEC, NULL, 0x0,
446 "The length (64 bits) of the Payload data", HFILL }
448 { &hf_ws_masking_key,
449 { "Masking-Key", "websocket.masking_key",
450 FT_BYTES, BASE_NONE, NULL, 0x0,
451 "All frames sent from the client to the server are masked by a 32-bit value that is contained within the frame", HFILL }
454 { "Payload", "websocket.payload",
455 FT_NONE, BASE_NONE, NULL, 0x0,
458 { &hf_ws_payload_unmask,
459 { "Unmask Payload", "websocket.payload.unmask",
460 FT_NONE, BASE_NONE, NULL, 0x0,
463 { &hf_ws_payload_continue,
464 { "Continue", "websocket.payload.continue",
465 FT_BYTES, BASE_NONE, NULL, 0x0,
468 { &hf_ws_payload_text,
469 { "Text", "websocket.payload.text",
470 FT_STRING, BASE_NONE, NULL, 0x0,
473 { &hf_ws_payload_text_mask,
474 { "Text", "websocket.payload.text_mask",
475 FT_BYTES, BASE_NONE, NULL, 0x0,
478 { &hf_ws_payload_text_unmask,
479 { "Text unmask", "websocket.payload.text_unmask",
480 FT_STRING, BASE_NONE, NULL, 0x0,
483 { &hf_ws_payload_binary,
484 { "Binary", "websocket.payload.binary",
485 FT_BYTES, BASE_NONE, NULL, 0x0,
488 { &hf_ws_payload_binary_mask,
489 { "Binary", "websocket.payload.binary_mask",
490 FT_BYTES, BASE_NONE, NULL, 0x0,
493 { &hf_ws_payload_binary_unmask,
494 { "Binary", "websocket.payload.binary_unmask",
495 FT_BYTES, BASE_NONE, NULL, 0x0,
498 { &hf_ws_payload_close,
499 { "Close", "websocket.payload.close",
500 FT_BYTES, BASE_NONE, NULL, 0x0,
503 { &hf_ws_payload_close_mask,
504 { "Close", "websocket.payload.close_mask",
505 FT_BYTES, BASE_NONE, NULL, 0x0,
508 { &hf_ws_payload_close_unmask,
509 { "Unmask Close", "websocket.payload.close_unmask",
510 FT_BYTES, BASE_NONE, NULL, 0x0,
513 { &hf_ws_payload_close_status_code,
514 { "Close", "websocket.payload.close.status_code",
515 FT_UINT16, BASE_DEC, VALS(ws_close_status_code_vals), 0x0,
518 { &hf_ws_payload_close_reason,
519 { "Reason", "websocket.payload.close.reason",
520 FT_STRING, BASE_NONE, NULL, 0x0,
523 { &hf_ws_payload_ping,
524 { "Ping", "websocket.payload.ping",
525 FT_BYTES, BASE_NONE, NULL, 0x0,
528 { &hf_ws_payload_ping_mask,
529 { "Ping", "websocket.payload.ping_mask",
530 FT_BYTES, BASE_NONE, NULL, 0x0,
533 { &hf_ws_payload_ping_unmask,
534 { "Ping", "websocket.payload.ping_unmask",
535 FT_BYTES, BASE_NONE, NULL, 0x0,
538 { &hf_ws_payload_pong,
539 { "Pong", "websocket.payload.pong",
540 FT_BYTES, BASE_NONE, NULL, 0x0,
543 { &hf_ws_payload_pong_mask,
544 { "Pong", "websocket.payload.pong_mask",
545 FT_BYTES, BASE_NONE, NULL, 0x0,
548 { &hf_ws_payload_pong_unmask,
549 { "Pong", "websocket.payload.pong_unmask",
550 FT_BYTES, BASE_NONE, NULL, 0x0,
553 { &hf_ws_payload_unknown,
554 { "Unknown", "websocket.payload.unknown",
555 FT_BYTES, BASE_NONE, NULL, 0x0,
561 static gint *ett[] = {
567 static const enum_val_t text_types[] = {
568 {"None", "No subdissection", WEBSOCKET_NONE},
569 {"Line based text", "Line based text", WEBSOCKET_TEXT},
570 {"As JSON", "As json", WEBSOCKET_JSON},
574 module_t *websocket_module;
576 proto_websocket = proto_register_protocol("WebSocket",
577 "WebSocket", "websocket");
580 * Heuristic dissectors SHOULD register themselves in
581 * this table using the standard heur_dissector_add()
584 register_heur_dissector_list("ws", &heur_subdissector_list);
586 port_subdissector_table = register_dissector_table("ws.port",
587 "TCP port for protocols using WebSocket", FT_UINT16, BASE_DEC);
589 proto_register_field_array(proto_websocket, hf, array_length(hf));
590 proto_register_subtree_array(ett, array_length(ett));
592 new_register_dissector("websocket", dissect_websocket, proto_websocket);
594 websocket_module = prefs_register_protocol(proto_websocket, proto_reg_handoff_websocket);
596 prefs_register_enum_preference(websocket_module, "text_type",
597 "Dissect websocket text as",
598 "Select dissector for websocket text",
599 &pref_text_type, text_types, WEBSOCKET_NONE);
605 proto_reg_handoff_websocket(void)
607 text_lines_handle = find_dissector("data-text-lines");
608 json_handle = find_dissector("json");
611 * Editor modelines - http://www.wireshark.org/tools/modelines.html
616 * indent-tabs-mode: nil
619 * vi: set shiftwidth=2 tabstop=8 expandtab:
620 * :indentSize=2:tabSize=8:noTabs=true: