2014-02-03 00:36:50 +00:00
|
|
|
/* packet-bencode.c
|
|
|
|
* Routines for bencode dissection
|
|
|
|
* Copyright (C) 2004,2013 Jelmer Vernooij <jelmer@samba.org>
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* Copied from packet-pop.c
|
|
|
|
*
|
2018-02-12 11:23:27 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2014-02-03 00:36:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <epan/packet.h>
|
2014-06-01 18:38:24 +00:00
|
|
|
#include <epan/expert.h>
|
2014-02-03 00:36:50 +00:00
|
|
|
#include <epan/strutil.h>
|
|
|
|
|
2014-04-14 12:47:30 +00:00
|
|
|
void proto_register_bencode(void);
|
|
|
|
|
2014-02-03 00:36:50 +00:00
|
|
|
static int proto_bencode = -1;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
static gint hf_bencode_str_length = -1;
|
|
|
|
static gint hf_bencode_str = -1;
|
|
|
|
static gint hf_bencode_int = -1;
|
|
|
|
static gint hf_bencode_dict = -1;
|
|
|
|
static gint hf_bencode_dict_entry = -1;
|
|
|
|
static gint hf_bencode_list = -1;
|
2014-06-01 18:38:24 +00:00
|
|
|
static gint hf_bencode_truncated_data = -1;
|
2014-02-03 00:36:50 +00:00
|
|
|
|
|
|
|
static gint ett_bencode_dict = -1;
|
|
|
|
static gint ett_bencode_dict_entry = -1;
|
|
|
|
static gint ett_bencode_list = -1;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
static expert_field ei_bencode_str = EI_INIT;
|
2014-06-01 18:38:24 +00:00
|
|
|
static expert_field ei_bencode_str_length = EI_INIT;
|
2014-08-05 16:35:36 +00:00
|
|
|
static expert_field ei_bencode_int = EI_INIT;
|
|
|
|
static expert_field ei_bencode_nest = EI_INIT;
|
|
|
|
static expert_field ei_bencode_dict_key = EI_INIT;
|
2014-06-01 18:38:24 +00:00
|
|
|
static expert_field ei_bencode_dict_value = EI_INIT;
|
2014-08-05 16:35:36 +00:00
|
|
|
static expert_field ei_bencode_invalid = EI_INIT;
|
2014-06-01 18:38:24 +00:00
|
|
|
|
|
|
|
static int dissect_bencoding_str(tvbuff_t *tvb, packet_info *pinfo,
|
2014-02-03 00:36:50 +00:00
|
|
|
int offset, int length, proto_tree *tree, proto_item *ti, int treeadd)
|
|
|
|
{
|
|
|
|
guint8 ch;
|
|
|
|
int stringlen = 0, nextstringlen;
|
|
|
|
int used;
|
|
|
|
int izero = 0;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if (length < 2) {
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_str, tvb, offset, length);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
used = 0;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
while (length >= 1) {
|
|
|
|
ch = tvb_get_guint8(tvb, offset + used);
|
2014-02-03 00:36:50 +00:00
|
|
|
length--;
|
|
|
|
used++;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if ((ch == ':') && (used > 1)) {
|
|
|
|
if ((stringlen > length) || (stringlen < 0)) {
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_str_length, tvb, offset, length);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (tree) {
|
|
|
|
proto_tree_add_uint(tree, hf_bencode_str_length, tvb, offset, used, stringlen);
|
2014-08-05 16:35:36 +00:00
|
|
|
proto_tree_add_item(tree, hf_bencode_str, tvb, offset + used, stringlen, ENC_ASCII|ENC_NA);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if (treeadd == 1) {
|
|
|
|
proto_item_append_text(ti, " Key: %s",
|
2017-01-30 02:53:49 +00:00
|
|
|
tvb_format_text(tvb, offset + used, stringlen));
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
2014-08-05 16:35:36 +00:00
|
|
|
if (treeadd == 2) {
|
|
|
|
proto_item_append_text(ti, " Value: %s",
|
2017-01-30 02:53:49 +00:00
|
|
|
tvb_format_text(tvb, offset + used, stringlen));
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 16:35:36 +00:00
|
|
|
return used + stringlen;
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if (!izero && (ch >= '0') && (ch <= '9')) {
|
|
|
|
if ((ch == '0') && (used == 1)) {
|
2014-02-03 00:36:50 +00:00
|
|
|
izero = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextstringlen = (stringlen * 10) + (ch - '0');
|
2014-08-05 16:35:36 +00:00
|
|
|
if (nextstringlen >= stringlen) {
|
2014-02-03 00:36:50 +00:00
|
|
|
stringlen = nextstringlen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_str, tvb, offset, length);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_item(tree, hf_bencode_truncated_data, tvb, offset, length, ENC_NA);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:02:17 +00:00
|
|
|
static int dissect_bencoding_int(tvbuff_t *tvb, packet_info *pinfo,
|
2014-02-03 00:36:50 +00:00
|
|
|
int offset, int length, proto_tree *tree, proto_item *ti, int treeadd)
|
|
|
|
{
|
2014-08-05 16:35:36 +00:00
|
|
|
gint32 ival = 0;
|
|
|
|
int neg = 0;
|
|
|
|
int izero = 0;
|
|
|
|
int used;
|
2014-02-03 00:36:50 +00:00
|
|
|
guint8 ch;
|
|
|
|
|
|
|
|
if (length<3) {
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_int, tvb, offset, length);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
length--;
|
|
|
|
used = 1;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
while (length >= 1) {
|
|
|
|
ch = tvb_get_guint8(tvb, offset + used);
|
2014-02-03 00:36:50 +00:00
|
|
|
length--;
|
|
|
|
used++;
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case 'e':
|
|
|
|
if (tree) {
|
|
|
|
if (neg) ival = -ival;
|
|
|
|
proto_tree_add_int(tree, hf_bencode_int, tvb, offset, used, ival);
|
2014-08-05 16:35:36 +00:00
|
|
|
if (treeadd == 2) {
|
2014-02-03 00:36:50 +00:00
|
|
|
proto_item_append_text(ti, " Value: %d", ival);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return used;
|
|
|
|
|
|
|
|
case '-':
|
2014-08-05 16:35:36 +00:00
|
|
|
if (used == 2) {
|
2014-02-03 00:36:50 +00:00
|
|
|
neg = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
default:
|
2014-08-05 16:35:36 +00:00
|
|
|
if (!((ch == '0') && (used == 3) && neg)) { /* -0 is invalid */
|
|
|
|
if ((ch == '0') && (used == 2)) { /* as is 0[0-9]+ */
|
2014-02-03 00:36:50 +00:00
|
|
|
izero = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-05 16:35:36 +00:00
|
|
|
if (!izero && (ch >= '0') && (ch <= '9')) {
|
2014-02-03 00:36:50 +00:00
|
|
|
ival = (ival * 10) + (ch - '0');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_int, tvb, offset, length);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_item(tree, hf_bencode_truncated_data, tvb, offset, length, ENC_NA);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:02:17 +00:00
|
|
|
static int dissect_bencoding_rec(tvbuff_t *tvb, packet_info *pinfo,
|
2014-02-03 00:36:50 +00:00
|
|
|
int offset, int length, proto_tree *tree, int level, proto_item *treei, int treeadd)
|
|
|
|
{
|
|
|
|
guint8 op;
|
|
|
|
int oplen = 0, op1len, op2len;
|
|
|
|
int used;
|
|
|
|
|
|
|
|
proto_item *ti = NULL, *td = NULL;
|
|
|
|
proto_tree *itree = NULL, *dtree = NULL;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if (level > 10) {
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_nest, tvb, offset, -1);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-08-05 16:35:36 +00:00
|
|
|
if (length < 1) {
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_item(tree, hf_bencode_truncated_data, tvb, offset, -1, ENC_NA);
|
2014-02-03 00:36:50 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
op = tvb_get_guint8(tvb, offset);
|
2014-08-05 16:35:36 +00:00
|
|
|
oplen = dissect_bencoding_rec(tvb, pinfo, offset, length, NULL, level + 1, NULL, 0);
|
|
|
|
if (oplen < 0)
|
2014-06-01 18:38:24 +00:00
|
|
|
oplen = length;
|
2014-02-03 00:36:50 +00:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case 'd':
|
2014-06-01 18:38:24 +00:00
|
|
|
td = proto_tree_add_item(tree, hf_bencode_dict, tvb, offset, oplen, ENC_NA);
|
|
|
|
dtree = proto_item_add_subtree(td, ett_bencode_dict);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
|
|
|
used = 1;
|
|
|
|
length--;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
while (length >= 1) {
|
|
|
|
op = tvb_get_guint8(tvb, offset + used);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if (op == 'e') {
|
|
|
|
return used + 1;
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
op1len = dissect_bencoding_str(tvb, pinfo, offset + used, length, NULL, NULL, 0);
|
|
|
|
if (op1len < 0) {
|
|
|
|
proto_tree_add_expert(dtree, pinfo, &ei_bencode_dict_key, tvb, offset + used, -1);
|
2014-02-03 00:36:50 +00:00
|
|
|
return op1len;
|
|
|
|
}
|
|
|
|
|
|
|
|
op2len = -1;
|
2014-08-05 16:35:36 +00:00
|
|
|
if ((length - op1len) > 2)
|
|
|
|
op2len = dissect_bencoding_rec(tvb, pinfo, offset + used + op1len, length - op1len, NULL, level + 1, NULL, 0);
|
|
|
|
if (op2len < 0) {
|
|
|
|
proto_tree_add_expert(dtree, pinfo, &ei_bencode_dict_value, tvb, offset + used + op1len, -1);
|
2014-02-03 00:36:50 +00:00
|
|
|
return op2len;
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
ti = proto_tree_add_item(dtree, hf_bencode_dict_entry, tvb, offset + used, op1len + op2len, ENC_NA);
|
2014-06-01 18:38:24 +00:00
|
|
|
itree = proto_item_add_subtree(ti, ett_bencode_dict_entry);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
dissect_bencoding_str(tvb, pinfo, offset + used, length, itree, ti, 1);
|
|
|
|
dissect_bencoding_rec(tvb, pinfo, offset + used + op1len, length - op1len, itree, level + 1, ti, 2);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
used += op1len + op2len;
|
|
|
|
length -= op1len + op2len;
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
2014-06-01 18:38:24 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
proto_tree_add_item(dtree, hf_bencode_truncated_data, tvb, offset + used, -1, ENC_NA);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 'l':
|
2014-06-01 18:38:24 +00:00
|
|
|
ti = proto_tree_add_item(tree, hf_bencode_list, tvb, offset, oplen, ENC_NA);
|
|
|
|
itree = proto_item_add_subtree(ti, ett_bencode_list);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
|
|
|
used = 1;
|
|
|
|
length--;
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
while (length >= 1) {
|
|
|
|
op = tvb_get_guint8(tvb, offset + used);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
if (op == 'e') {
|
|
|
|
return used + 1;
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
oplen = dissect_bencoding_rec(tvb, pinfo, offset + used, length, itree, level + 1, ti, 0);
|
|
|
|
if (oplen < 1) return oplen;
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
used += oplen;
|
2014-02-03 00:36:50 +00:00
|
|
|
length -= oplen;
|
|
|
|
}
|
2014-06-01 18:38:24 +00:00
|
|
|
|
2014-08-05 16:35:36 +00:00
|
|
|
proto_tree_add_item(itree, hf_bencode_truncated_data, tvb, offset + used, -1, ENC_NA);
|
2014-02-03 00:36:50 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
return dissect_bencoding_int(tvb, pinfo, offset, length, tree, treei, treeadd);
|
|
|
|
|
|
|
|
default:
|
2014-08-05 16:35:36 +00:00
|
|
|
if ((op >= '1') && (op <= '9')) {
|
2014-02-03 00:36:50 +00:00
|
|
|
return dissect_bencoding_str(tvb, pinfo, offset, length, tree, treei, treeadd);
|
|
|
|
}
|
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_expert(tree, pinfo, &ei_bencode_invalid, tvb, offset, -1);
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-10 04:01:28 +00:00
|
|
|
static int dissect_bencoding(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
|
2014-02-03 00:36:50 +00:00
|
|
|
{
|
2014-08-05 16:35:36 +00:00
|
|
|
dissect_bencoding_rec(tvb, pinfo, 0, tvb_reported_length(tvb), tree, 0, NULL, 0);
|
2015-11-10 04:01:28 +00:00
|
|
|
return tvb_captured_length(tvb);
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_register_bencode(void)
|
|
|
|
{
|
|
|
|
static hf_register_info hf[] = {
|
|
|
|
{ &hf_bencode_str_length,
|
|
|
|
{ "String Length", "bencode.str.length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_bencode_str,
|
|
|
|
{ "String", "bencode.str", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_bencode_int,
|
|
|
|
{ "Integer", "bencode.int", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_bencode_dict,
|
|
|
|
{ "Dictionary", "bencode.dict", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_bencode_dict_entry,
|
|
|
|
{ "Entry", "bencode.dict.entry", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_bencode_list,
|
|
|
|
{ "List", "bencode.list", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
2014-06-01 18:38:24 +00:00
|
|
|
{ &hf_bencode_truncated_data,
|
|
|
|
{ "Truncated Data", "bencode.truncated_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
|
|
|
|
},
|
2014-02-03 00:36:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_bencode_dict,
|
|
|
|
&ett_bencode_dict_entry,
|
|
|
|
&ett_bencode_list,
|
|
|
|
};
|
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
static ei_register_info ei[] = {
|
|
|
|
{ &ei_bencode_str, { "bencode.str.invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid String", EXPFILL }},
|
|
|
|
{ &ei_bencode_str_length, { "bencode.str.length.invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid String Length", EXPFILL }},
|
|
|
|
{ &ei_bencode_int, { "bencode.int.invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid Integer", EXPFILL }},
|
|
|
|
{ &ei_bencode_nest, { "bencode.nest", PI_MALFORMED, PI_ERROR, "Decode Aborted: Nested Too Deep", EXPFILL }},
|
|
|
|
{ &ei_bencode_dict_key, { "bencode.dict.key_invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid Dictionary Key", EXPFILL }},
|
|
|
|
{ &ei_bencode_dict_value, { "bencode.dict.value_invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid Dictionary Value", EXPFILL }},
|
|
|
|
{ &ei_bencode_invalid, { "bencode.invalid", PI_MALFORMED, PI_ERROR, "Invalid Bencoding", EXPFILL }},
|
|
|
|
};
|
|
|
|
|
|
|
|
expert_module_t* expert_bencode;
|
|
|
|
|
2014-02-03 00:36:50 +00:00
|
|
|
proto_bencode = proto_register_protocol("Bencode", "Bencode", "bencode");
|
2015-12-09 04:04:01 +00:00
|
|
|
register_dissector("bencode", dissect_bencoding, proto_bencode);
|
2014-02-03 00:36:50 +00:00
|
|
|
proto_register_field_array(proto_bencode, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
2014-06-01 18:38:24 +00:00
|
|
|
expert_bencode = expert_register_protocol(proto_bencode);
|
|
|
|
expert_register_field_array(expert_bencode, ei, array_length(ei));
|
2014-02-03 00:36:50 +00:00
|
|
|
}
|
2014-08-05 16:35:36 +00:00
|
|
|
|
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2014-08-05 16:35:36 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 3
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=3 tabstop=8 expandtab:
|
|
|
|
* :indentSize=3:tabSize=8:noTabs=true:
|
|
|
|
*/
|