2012-04-20 15:31:15 +00:00
|
|
|
/* packet-websocket.c
|
|
|
|
* Routines for WebSocket dissection
|
|
|
|
* Copyright 2012, Alexis La Goutte <alexis.lagoutte@gmail.com>
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2012-09-20 02:03:38 +00:00
|
|
|
#include "config.h"
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <epan/packet.h>
|
|
|
|
#include <epan/expert.h>
|
2013-03-12 19:16:35 +00:00
|
|
|
#include <epan/prefs.h>
|
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The information used comes from:
|
|
|
|
* RFC6455: The WebSocket Protocol
|
|
|
|
* http://www.iana.org/assignments/websocket (last updated 2012-04-12)
|
|
|
|
*/
|
|
|
|
|
2013-03-12 19:16:35 +00:00
|
|
|
void proto_reg_handoff_websocket(void);
|
|
|
|
|
2013-08-05 20:15:06 +00:00
|
|
|
static dissector_handle_t text_lines_handle;
|
|
|
|
static dissector_handle_t json_handle;
|
2013-03-12 19:16:35 +00:00
|
|
|
|
|
|
|
#define WEBSOCKET_NONE 0
|
|
|
|
#define WEBSOCKET_TEXT 1
|
|
|
|
#define WEBSOCKET_JSON 2
|
|
|
|
|
|
|
|
static gint pref_text_type = WEBSOCKET_NONE;
|
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
/* Initialize the protocol and registered fields */
|
|
|
|
static int proto_websocket = -1;
|
|
|
|
static int hf_ws_fin = -1;
|
|
|
|
static int hf_ws_reserved = -1;
|
|
|
|
static int hf_ws_opcode = -1;
|
|
|
|
static int hf_ws_mask = -1;
|
|
|
|
static int hf_ws_payload_length = -1;
|
|
|
|
static int hf_ws_payload_length_ext_16 = -1;
|
|
|
|
static int hf_ws_payload_length_ext_64 = -1;
|
|
|
|
static int hf_ws_masking_key = -1;
|
|
|
|
static int hf_ws_payload = -1;
|
|
|
|
static int hf_ws_payload_unmask = -1;
|
|
|
|
static int hf_ws_payload_continue = -1;
|
|
|
|
static int hf_ws_payload_text = -1;
|
|
|
|
static int hf_ws_payload_text_mask = -1;
|
|
|
|
static int hf_ws_payload_text_unmask = -1;
|
|
|
|
static int hf_ws_payload_binary = -1;
|
|
|
|
static int hf_ws_payload_binary_mask = -1;
|
|
|
|
static int hf_ws_payload_binary_unmask = -1;
|
|
|
|
static int hf_ws_payload_close = -1;
|
|
|
|
static int hf_ws_payload_close_mask = -1;
|
|
|
|
static int hf_ws_payload_close_unmask = -1;
|
|
|
|
static int hf_ws_payload_close_status_code = -1;
|
|
|
|
static int hf_ws_payload_close_reason = -1;
|
|
|
|
static int hf_ws_payload_ping = -1;
|
|
|
|
static int hf_ws_payload_ping_mask = -1;
|
|
|
|
static int hf_ws_payload_ping_unmask = -1;
|
|
|
|
static int hf_ws_payload_pong = -1;
|
|
|
|
static int hf_ws_payload_pong_mask = -1;
|
|
|
|
static int hf_ws_payload_pong_unmask = -1;
|
|
|
|
static int hf_ws_payload_unknown = -1;
|
|
|
|
|
|
|
|
static gint ett_ws = -1;
|
|
|
|
static gint ett_ws_pl = -1;
|
|
|
|
static gint ett_ws_mask = -1;
|
|
|
|
|
2013-05-28 03:11:44 +00:00
|
|
|
static expert_field ei_ws_payload_unknown = EI_INIT;
|
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
#define WS_CONTINUE 0x0
|
|
|
|
#define WS_TEXT 0x1
|
|
|
|
#define WS_BINARY 0x2
|
|
|
|
#define WS_CLOSE 0x8
|
|
|
|
#define WS_PING 0x9
|
|
|
|
#define WS_PONG 0xA
|
|
|
|
|
|
|
|
static const value_string ws_opcode_vals[] = {
|
|
|
|
{ WS_CONTINUE, "Continuation" },
|
|
|
|
{ WS_TEXT, "Text" },
|
|
|
|
{ WS_BINARY, "Binary" },
|
|
|
|
{ WS_CLOSE, "Connection Close" },
|
|
|
|
{ WS_PING, "Ping" },
|
|
|
|
{ WS_PONG, "Pong" },
|
2012-11-02 20:35:24 +00:00
|
|
|
{ 0, NULL}
|
2012-04-20 15:31:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MASK_WS_FIN 0x80
|
|
|
|
#define MASK_WS_RSV 0x70
|
2012-11-02 20:35:24 +00:00
|
|
|
#define MASK_WS_OPCODE 0x0F
|
|
|
|
#define MASK_WS_MASK 0x80
|
2012-04-20 15:31:15 +00:00
|
|
|
#define MASK_WS_PAYLOAD_LEN 0x7F
|
|
|
|
|
|
|
|
static const value_string ws_close_status_code_vals[] = {
|
|
|
|
{ 1000, "Normal Closure" },
|
|
|
|
{ 1001, "Going Away" },
|
|
|
|
{ 1002, "Protocol error" },
|
|
|
|
{ 1003, "Unsupported Data" },
|
|
|
|
{ 1004, "---Reserved----" },
|
|
|
|
{ 1005, "No Status Rcvd" },
|
|
|
|
{ 1006, "Abnormal Closure" },
|
|
|
|
{ 1007, "Invalid frame payload data" },
|
|
|
|
{ 1008, "Policy Violation" },
|
|
|
|
{ 1009, "Message Too Big" },
|
|
|
|
{ 1010, "Mandatory Ext." },
|
|
|
|
{ 1011, "Internal Server" },
|
|
|
|
{ 1015, "TLS handshake" },
|
2012-11-02 20:35:24 +00:00
|
|
|
{ 0, NULL}
|
2012-04-20 15:31:15 +00:00
|
|
|
};
|
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
static dissector_table_t port_subdissector_table;
|
|
|
|
static heur_dissector_list_t heur_subdissector_list;
|
|
|
|
|
2012-04-20 18:13:02 +00:00
|
|
|
#define MAX_UNMASKED_LEN (1024 * 64)
|
2012-04-20 15:31:15 +00:00
|
|
|
tvbuff_t *
|
2013-02-17 14:39:16 +00:00
|
|
|
tvb_unmasked(tvbuff_t *tvb, const guint offset, guint payload_length, const guint8 *masking_key)
|
2012-04-20 15:31:15 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
gchar *data_unmask;
|
2012-11-02 20:35:24 +00:00
|
|
|
tvbuff_t *tvb_unmask = NULL;
|
2013-02-17 14:39:16 +00:00
|
|
|
guint i;
|
2012-04-20 15:31:15 +00:00
|
|
|
const guint8 *data_mask;
|
2013-02-17 14:39:16 +00:00
|
|
|
guint unmasked_length = payload_length > MAX_UNMASKED_LEN ? MAX_UNMASKED_LEN : payload_length;
|
2012-04-20 15:31:15 +00:00
|
|
|
|
2013-03-04 07:25:57 +00:00
|
|
|
data_unmask = (gchar *)g_malloc(unmasked_length);
|
2012-04-20 18:13:02 +00:00
|
|
|
data_mask = tvb_get_ptr(tvb, offset, unmasked_length);
|
2012-04-20 15:31:15 +00:00
|
|
|
/* Unmasked(XOR) Data... */
|
2012-04-20 18:13:02 +00:00
|
|
|
for(i=0; i < unmasked_length; i++){
|
2012-04-20 15:31:15 +00:00
|
|
|
data_unmask[i] = data_mask[i] ^ masking_key[i%4];
|
|
|
|
}
|
|
|
|
|
2012-04-20 18:13:02 +00:00
|
|
|
tvb_unmask = tvb_new_real_data(data_unmask, unmasked_length, unmasked_length);
|
2012-04-20 15:31:15 +00:00
|
|
|
tvb_set_free_cb(tvb_unmask, g_free);
|
|
|
|
return tvb_unmask;
|
|
|
|
}
|
|
|
|
|
2012-04-20 18:10:15 +00:00
|
|
|
static int
|
2013-02-17 14:39:16 +00:00
|
|
|
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)
|
2012-04-20 15:31:15 +00:00
|
|
|
{
|
2013-02-17 14:39:16 +00:00
|
|
|
guint offset = 0;
|
2012-04-20 15:31:15 +00:00
|
|
|
proto_item *ti_unmask, *ti;
|
2012-11-02 20:35:24 +00:00
|
|
|
dissector_handle_t handle;
|
2012-04-20 15:31:15 +00:00
|
|
|
proto_tree *pl_tree, *mask_tree = NULL;
|
2012-11-02 20:35:24 +00:00
|
|
|
tvbuff_t *payload_tvb = NULL;
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
/* Payload */
|
2012-11-02 20:35:24 +00:00
|
|
|
ti = proto_tree_add_item(ws_tree, hf_ws_payload, tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
pl_tree = proto_item_add_subtree(ti, ett_ws_pl);
|
|
|
|
if(mask){
|
2012-11-02 20:35:24 +00:00
|
|
|
payload_tvb = tvb_unmasked(tvb, offset, payload_length, masking_key);
|
|
|
|
tvb_set_child_real_data_tvbuff(tvb, payload_tvb);
|
2013-02-17 14:39:16 +00:00
|
|
|
add_new_data_source(pinfo, payload_tvb, payload_length > tvb_length(payload_tvb) ? "Unmasked Data (truncated)" : "Unmasked Data");
|
2012-11-02 20:35:24 +00:00
|
|
|
ti = proto_tree_add_item(ws_tree, hf_ws_payload_unmask, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
mask_tree = proto_item_add_subtree(ti, ett_ws_mask);
|
2012-11-02 20:35:24 +00:00
|
|
|
}else{
|
|
|
|
payload_tvb = tvb_new_subset(tvb, offset, payload_length, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = dissector_get_uint_handle(port_subdissector_table, pinfo->match_uint);
|
|
|
|
if(handle != NULL){
|
|
|
|
call_dissector_only(handle, payload_tvb, pinfo, tree, NULL);
|
|
|
|
}else{
|
|
|
|
dissector_try_heuristic(heur_subdissector_list, payload_tvb, pinfo, tree, NULL);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extension Data */
|
|
|
|
/* TODO: Add dissector of Extension (not extension available for the moment...) */
|
|
|
|
|
|
|
|
/* Application Data */
|
|
|
|
switch(opcode){
|
|
|
|
|
|
|
|
case WS_CONTINUE: /* Continue */
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_continue, tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
/* TODO: Add Fragmentation support... */
|
|
|
|
break;
|
|
|
|
|
2012-04-20 18:10:15 +00:00
|
|
|
case WS_TEXT: /* Text */
|
2012-04-20 15:31:15 +00:00
|
|
|
if(mask){
|
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_text_mask, tvb, offset, payload_length, ENC_NA);
|
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_text_unmask, payload_tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_text, payload_tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_HIDDEN(ti_unmask);
|
|
|
|
}else{
|
2013-03-12 19:16:35 +00:00
|
|
|
const gchar *saved_match_string = pinfo->match_string;
|
|
|
|
void *save_private_data = pinfo->private_data;
|
|
|
|
|
|
|
|
pinfo->match_string = NULL;
|
|
|
|
pinfo->private_data = NULL;
|
|
|
|
switch(pref_text_type){
|
|
|
|
case WEBSOCKET_TEXT:
|
|
|
|
call_dissector(text_lines_handle, payload_tvb, pinfo, pl_tree);
|
|
|
|
break;
|
|
|
|
case WEBSOCKET_JSON:
|
|
|
|
call_dissector(json_handle, payload_tvb, pinfo, pl_tree);
|
|
|
|
break;
|
|
|
|
case WEBSOCKET_NONE:
|
|
|
|
/* falltrough */
|
|
|
|
default:
|
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_text, tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pinfo->match_string = saved_match_string;
|
|
|
|
pinfo->private_data = save_private_data;
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
offset += payload_length;
|
2012-04-20 15:31:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WS_BINARY: /* Binary */
|
|
|
|
if(mask){
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_binary_mask, tvb, offset, payload_length, ENC_NA);
|
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_binary_unmask, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_binary, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_HIDDEN(ti_unmask);
|
|
|
|
}else{
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_binary, tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
offset += payload_length;
|
2012-04-20 15:31:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WS_CLOSE: /* Close */
|
|
|
|
if(mask){
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_close_mask, tvb, offset, payload_length, ENC_NA);
|
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_unmask, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_HIDDEN(ti_unmask);
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_status_code, payload_tvb, offset, 2, ENC_BIG_ENDIAN);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
if(payload_length > 2){
|
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_reason, payload_tvb, offset+2, payload_length-2, ENC_ASCII|ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
|
|
|
}
|
|
|
|
}else{
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_close, tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_close_status_code, tvb, offset, 2, ENC_BIG_ENDIAN);
|
2012-11-02 20:35:24 +00:00
|
|
|
if(payload_length > 2){
|
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_close_reason, tvb, offset+2, payload_length-2, ENC_ASCII|ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
offset += payload_length;
|
2012-04-20 15:31:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WS_PING: /* Ping */
|
|
|
|
if(mask){
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_ping_mask, tvb, offset, payload_length, ENC_NA);
|
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_ping_unmask, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_ping, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_HIDDEN(ti_unmask);
|
|
|
|
}else{
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_ping, tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
offset += payload_length;
|
2012-04-20 15:31:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WS_PONG: /* Pong */
|
|
|
|
if(mask){
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_pong_mask, tvb, offset, payload_length, ENC_NA);
|
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_pong_unmask, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_GENERATED(ti_unmask);
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_pong, payload_tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
PROTO_ITEM_SET_HIDDEN(ti_unmask);
|
|
|
|
}else{
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(pl_tree, hf_ws_payload_pong, tvb, offset, payload_length, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
offset += payload_length;
|
2012-04-20 15:31:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Unknown */
|
2012-11-02 20:35:24 +00:00
|
|
|
ti = proto_tree_add_item(pl_tree, hf_ws_payload_unknown, tvb, offset, payload_length, ENC_NA);
|
2013-09-09 00:44:09 +00:00
|
|
|
expert_add_info_format(pinfo, ti, &ei_ws_payload_unknown, "Dissector for Websocket Opcode (%d)"
|
2012-11-02 20:35:24 +00:00
|
|
|
" code not implemented, Contact Wireshark developers"
|
|
|
|
" if you want this supported", opcode);
|
2012-04-20 15:31:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
|
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
static int
|
2012-09-10 21:40:21 +00:00
|
|
|
dissect_websocket(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
|
2012-04-20 15:31:15 +00:00
|
|
|
{
|
|
|
|
proto_item *ti, *ti_len;
|
|
|
|
guint8 fin, opcode, mask;
|
2013-02-17 14:39:16 +00:00
|
|
|
guint length, short_length, payload_length, recurse_length;
|
|
|
|
guint payload_offset, mask_offset, recurse_offset;
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree *ws_tree = NULL;
|
2012-04-20 15:31:15 +00:00
|
|
|
const guint8 *masking_key = NULL;
|
2012-11-02 20:35:24 +00:00
|
|
|
tvbuff_t *tvb_payload = NULL;
|
|
|
|
|
|
|
|
length = tvb_length(tvb);
|
|
|
|
if(length<2){
|
|
|
|
pinfo->desegment_len = 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
short_length = tvb_get_guint8(tvb, 1) & MASK_WS_PAYLOAD_LEN;
|
|
|
|
if(short_length==126){
|
|
|
|
if(length < 2+2){
|
|
|
|
pinfo->desegment_len = 2+2;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-03 15:36:09 +00:00
|
|
|
payload_length = tvb_get_ntohs(tvb, 2);
|
2012-11-02 20:35:24 +00:00
|
|
|
mask_offset = 2+2;
|
|
|
|
}
|
|
|
|
else if(short_length==127){
|
|
|
|
if(length < 2+8){
|
|
|
|
pinfo->desegment_len = 2+8;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-02-17 14:39:16 +00:00
|
|
|
/* warning C4244: '=' : conversion from 'guint64' to 'guint ', possible loss of data */
|
|
|
|
payload_length = (guint)tvb_get_ntoh64(tvb, 2);
|
2012-11-02 20:35:24 +00:00
|
|
|
mask_offset = 2+8;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
payload_length = short_length;
|
|
|
|
mask_offset = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mask */
|
|
|
|
mask = (tvb_get_guint8(tvb, 1) & MASK_WS_MASK) >> 4;
|
|
|
|
payload_offset = mask_offset + (mask ? 4 : 0);
|
|
|
|
|
2013-03-16 02:56:17 +00:00
|
|
|
if (payload_offset + payload_length < payload_length) {
|
|
|
|
/* Integer overflow, which means the packet contains a ridiculous
|
|
|
|
* payload length. Just take what we've got available.
|
|
|
|
* See bug https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8448 */
|
|
|
|
payload_length = tvb_reported_length_remaining(tvb, payload_offset);
|
|
|
|
}
|
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
if(length < payload_offset + payload_length){
|
2012-11-03 15:36:09 +00:00
|
|
|
/* XXXX Warning desegment_len is 32 bits */
|
2012-11-02 20:35:24 +00:00
|
|
|
pinfo->desegment_len = payload_offset + payload_length - length;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've got the entire message! */
|
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_PROTOCOL, "WebSocket");
|
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "WebSocket");
|
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
if(tree){
|
|
|
|
ti = proto_tree_add_item(tree, proto_websocket, tvb, 0, payload_offset, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
ws_tree = proto_item_add_subtree(ti, ett_ws);
|
|
|
|
}
|
2012-11-02 20:35:24 +00:00
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
/* Flags */
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(ws_tree, hf_ws_fin, tvb, 0, 1, ENC_NA);
|
|
|
|
fin = (tvb_get_guint8(tvb, 0) & MASK_WS_FIN) >> 4;
|
|
|
|
proto_tree_add_item(ws_tree, hf_ws_reserved, tvb, 0, 1, ENC_NA);
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
/* Opcode */
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(ws_tree, hf_ws_opcode, tvb, 0, 1, ENC_NA);
|
|
|
|
opcode = tvb_get_guint8(tvb, 0) & MASK_WS_OPCODE;
|
2012-04-20 15:31:15 +00:00
|
|
|
col_append_fstr(pinfo->cinfo, COL_INFO, " %s", val_to_str_const(opcode, ws_opcode_vals, "Unknown Opcode"));
|
2013-10-29 14:09:20 +00:00
|
|
|
col_append_str(pinfo->cinfo, COL_INFO, fin ? " [FIN]" : " ");
|
2012-04-20 15:31:15 +00:00
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
/* Add Mask bit to the tree */
|
|
|
|
proto_tree_add_item(ws_tree, hf_ws_mask, tvb, 1, 1, ENC_NA);
|
2013-10-29 14:09:20 +00:00
|
|
|
col_append_str(pinfo->cinfo, COL_INFO, mask ? " [MASKED]" : " ");
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
/* (Extended) Payload Length */
|
2012-11-02 20:35:24 +00:00
|
|
|
ti_len = proto_tree_add_item(ws_tree, hf_ws_payload_length, tvb, 1, 1, ENC_NA);
|
|
|
|
if(short_length==126){
|
|
|
|
proto_item_append_text(ti_len, " Extended Payload Length (16 bits)");
|
|
|
|
proto_tree_add_item(ws_tree, hf_ws_payload_length_ext_16, tvb, 2, 2, ENC_BIG_ENDIAN);
|
|
|
|
}
|
|
|
|
else if(short_length==127){
|
|
|
|
proto_item_append_text(ti_len, " Extended Payload Length (64 bits)");
|
|
|
|
proto_tree_add_item(ws_tree, hf_ws_payload_length_ext_64, tvb, 2, 8, ENC_BIG_ENDIAN);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Masking-key */
|
|
|
|
if(mask){
|
2012-11-02 20:35:24 +00:00
|
|
|
proto_tree_add_item(ws_tree, hf_ws_masking_key, tvb, mask_offset, 4, ENC_NA);
|
|
|
|
masking_key = tvb_get_ptr(tvb, mask_offset, 4);
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
tvb_payload = tvb_new_subset_remaining(tvb, payload_offset);
|
|
|
|
dissect_websocket_payload(tvb_payload, pinfo, tree, ws_tree, opcode, payload_length, mask, masking_key);
|
2012-04-20 15:31:15 +00:00
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
/* Call this function recursively, to see if we have enough data to parse another websocket message */
|
|
|
|
|
|
|
|
recurse_offset = payload_offset + payload_length;
|
|
|
|
if(length > recurse_offset){
|
|
|
|
recurse_length = dissect_websocket(tvb_new_subset_remaining(tvb, payload_offset+payload_length), pinfo, tree, data);
|
|
|
|
if(pinfo->desegment_len) pinfo->desegment_offset += recurse_offset;
|
|
|
|
return recurse_offset + recurse_length;
|
|
|
|
}
|
|
|
|
return recurse_offset;
|
2012-04-20 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_register_websocket(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
static hf_register_info hf[] = {
|
|
|
|
{ &hf_ws_fin,
|
|
|
|
{ "Fin", "websocket.fin",
|
|
|
|
FT_BOOLEAN, 8, NULL, MASK_WS_FIN,
|
|
|
|
"Indicates that this is the final fragment in a message", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_reserved,
|
|
|
|
{ "Reserved", "websocket.rsv",
|
|
|
|
FT_UINT8, BASE_HEX, NULL, MASK_WS_RSV,
|
|
|
|
"Must be zero", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_opcode,
|
|
|
|
{ "Opcode", "websocket.opcode",
|
|
|
|
FT_UINT8, BASE_DEC, VALS(ws_opcode_vals), MASK_WS_OPCODE,
|
|
|
|
"Defines the interpretation of the Payload data", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_mask,
|
|
|
|
{ "Mask", "websocket.mask",
|
|
|
|
FT_BOOLEAN, 8, NULL, MASK_WS_MASK,
|
|
|
|
"Defines whether the Payload data is masked", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_length,
|
|
|
|
{ "Payload length", "websocket.payload_length",
|
|
|
|
FT_UINT8, BASE_DEC, NULL, MASK_WS_PAYLOAD_LEN,
|
|
|
|
"The length of the Payload data", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_length_ext_16,
|
|
|
|
{ "Extended Payload length (16 bits)", "websocket.payload_length_ext_16",
|
|
|
|
FT_UINT16, BASE_DEC, NULL, 0x0,
|
|
|
|
"The length (16 bits) of the Payload data", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_length_ext_64,
|
2013-02-17 14:39:16 +00:00
|
|
|
{ "Extended Payload length (64 bits)", "websocket.payload_length_ext_64",
|
2012-04-20 15:31:15 +00:00
|
|
|
FT_UINT64, BASE_DEC, NULL, 0x0,
|
|
|
|
"The length (64 bits) of the Payload data", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_masking_key,
|
|
|
|
{ "Masking-Key", "websocket.masking_key",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
"All frames sent from the client to the server are masked by a 32-bit value that is contained within the frame", HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload,
|
|
|
|
{ "Payload", "websocket.payload",
|
|
|
|
FT_NONE, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_unmask,
|
|
|
|
{ "Unmask Payload", "websocket.payload.unmask",
|
|
|
|
FT_NONE, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_continue,
|
|
|
|
{ "Continue", "websocket.payload.continue",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_text,
|
|
|
|
{ "Text", "websocket.payload.text",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_text_mask,
|
|
|
|
{ "Text", "websocket.payload.text_mask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_text_unmask,
|
|
|
|
{ "Text unmask", "websocket.payload.text_unmask",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_binary,
|
|
|
|
{ "Binary", "websocket.payload.binary",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_binary_mask,
|
|
|
|
{ "Binary", "websocket.payload.binary_mask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_binary_unmask,
|
|
|
|
{ "Binary", "websocket.payload.binary_unmask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_close,
|
|
|
|
{ "Close", "websocket.payload.close",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_close_mask,
|
|
|
|
{ "Close", "websocket.payload.close_mask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_close_unmask,
|
|
|
|
{ "Unmask Close", "websocket.payload.close_unmask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_close_status_code,
|
|
|
|
{ "Close", "websocket.payload.close.status_code",
|
|
|
|
FT_UINT16, BASE_DEC, VALS(ws_close_status_code_vals), 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_close_reason,
|
|
|
|
{ "Reason", "websocket.payload.close.reason",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_ping,
|
|
|
|
{ "Ping", "websocket.payload.ping",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_ping_mask,
|
|
|
|
{ "Ping", "websocket.payload.ping_mask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_ping_unmask,
|
|
|
|
{ "Ping", "websocket.payload.ping_unmask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_pong,
|
|
|
|
{ "Pong", "websocket.payload.pong",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_pong_mask,
|
|
|
|
{ "Pong", "websocket.payload.pong_mask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_pong_unmask,
|
|
|
|
{ "Pong", "websocket.payload.pong_unmask",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_ws_payload_unknown,
|
|
|
|
{ "Unknown", "websocket.payload.unknown",
|
|
|
|
FT_BYTES, BASE_NONE, NULL, 0x0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_ws,
|
|
|
|
&ett_ws_pl,
|
|
|
|
&ett_ws_mask
|
|
|
|
};
|
|
|
|
|
2013-05-28 03:11:44 +00:00
|
|
|
static ei_register_info ei[] = {
|
|
|
|
{ &ei_ws_payload_unknown, { "websocket.payload.unknown.expert", PI_UNDECODED, PI_NOTE, "Dissector for Websocket Opcode", EXPFILL }},
|
|
|
|
};
|
|
|
|
|
2013-03-12 19:16:35 +00:00
|
|
|
static const enum_val_t text_types[] = {
|
|
|
|
{"None", "No subdissection", WEBSOCKET_NONE},
|
|
|
|
{"Line based text", "Line based text", WEBSOCKET_TEXT},
|
|
|
|
{"As JSON", "As json", WEBSOCKET_JSON},
|
|
|
|
{NULL, NULL, -1}
|
|
|
|
};
|
|
|
|
|
|
|
|
module_t *websocket_module;
|
2013-05-28 03:11:44 +00:00
|
|
|
expert_module_t* expert_websocket;
|
2013-03-12 19:16:35 +00:00
|
|
|
|
2012-04-20 15:31:15 +00:00
|
|
|
proto_websocket = proto_register_protocol("WebSocket",
|
|
|
|
"WebSocket", "websocket");
|
2013-10-13 19:56:52 +00:00
|
|
|
|
2012-11-02 20:35:24 +00:00
|
|
|
/*
|
|
|
|
* Heuristic dissectors SHOULD register themselves in
|
|
|
|
* this table using the standard heur_dissector_add()
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
register_heur_dissector_list("ws", &heur_subdissector_list);
|
|
|
|
|
|
|
|
port_subdissector_table = register_dissector_table("ws.port",
|
|
|
|
"TCP port for protocols using WebSocket", FT_UINT16, BASE_DEC);
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
proto_register_field_array(proto_websocket, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
2013-05-28 03:11:44 +00:00
|
|
|
expert_websocket = expert_register_protocol(proto_websocket);
|
|
|
|
expert_register_field_array(expert_websocket, ei, array_length(ei));
|
2012-04-20 15:31:15 +00:00
|
|
|
|
|
|
|
new_register_dissector("websocket", dissect_websocket, proto_websocket);
|
|
|
|
|
2013-03-12 19:16:35 +00:00
|
|
|
websocket_module = prefs_register_protocol(proto_websocket, proto_reg_handoff_websocket);
|
|
|
|
|
|
|
|
prefs_register_enum_preference(websocket_module, "text_type",
|
|
|
|
"Dissect websocket text as",
|
|
|
|
"Select dissector for websocket text",
|
|
|
|
&pref_text_type, text_types, WEBSOCKET_NONE);
|
2012-04-20 15:31:15 +00:00
|
|
|
|
2013-03-12 19:16:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_reg_handoff_websocket(void)
|
|
|
|
{
|
|
|
|
text_lines_handle = find_dissector("data-text-lines");
|
|
|
|
json_handle = find_dissector("json");
|
|
|
|
}
|
2012-04-20 15:31:15 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 2
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=2 tabstop=8 expandtab:
|
|
|
|
* :indentSize=2:tabSize=8:noTabs=true:
|
|
|
|
*/
|