1 /******************************************************************************************************/
3 * Routines for BT-DHT dissection
4 * Copyright 2011, Xiao Xiangquan <xiaoxiangquan@gmail.com>
8 * A plugin for BT-DHT packet:
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1999 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <epan/packet.h>
34 #include <epan/conversation.h>
35 #include <epan/prefs.h>
36 #include <epan/wmem/wmem.h>
38 void proto_register_bt_dht(void);
39 void proto_reg_handoff_bt_dht(void);
41 /* Specifications: BEP-0005
42 * http://www.bittorrent.org/beps/bep_0005.html
45 static int proto_bt_dht = -1;
46 static dissector_handle_t bt_dht_handle;
48 static gboolean bt_dht_enable_heuristic_dissection = FALSE; /* disabled by default since heuristic is weak */
51 static int hf_bencoded_int = -1;
52 static int hf_bencoded_string = -1;
53 static int hf_bencoded_list = -1;
54 static int hf_bencoded_dict = -1;
55 static int hf_bencoded_dict_entry = -1;
57 static int hf_bt_dht_error = -1;
58 static int hf_bt_dht_peers = -1;
59 static int hf_bt_dht_peer = -1;
60 static int hf_bt_dht_nodes = -1;
61 static int hf_bt_dht_node = -1;
62 static int hf_bt_dht_id = -1;
64 static int hf_ip = -1;
65 static int hf_port = -1;
66 static int hf_truncated_data = -1;
69 static gint ett_bt_dht = -1;
70 static gint ett_bencoded_list = -1;
71 static gint ett_bencoded_dict = -1;
72 static gint ett_bencoded_dict_entry = -1;
73 static gint ett_bt_dht_error = -1;
74 static gint ett_bt_dht_peers = -1;
75 static gint ett_bt_dht_nodes = -1;
77 /* some keys use short name in packet */
78 static const value_string short_key_name_value_string[] = {
79 { 'y', "Message type" },
80 { 'q', "Request type" },
82 { 't', "Transaction ID" },
84 { 'a', "Request arguments" },
85 { 'r', "Response values" },
89 /* some values use short name in packet */
90 static const value_string short_val_name_value_string[] = {
97 static const char dict_str[] = "Dictionary...";
98 static const char list_str[] = "List...";
102 bencoded_string_length(tvbuff_t *tvb, guint *offset_ptr)
104 guint offset, start, len;
106 offset = *offset_ptr;
109 while(tvb_get_guint8(tvb, offset) != ':')
112 len = atoi(tvb_get_string(wmem_packet_scope(), tvb, start, offset-start));
113 ++offset; /* skip the ':' */
115 *offset_ptr = offset;
121 * dissect a bencoded string from tvb, start at offset. it's like "5:abcde"
122 * *result will be the decoded value
126 dissect_bencoded_string(tvbuff_t *tvb, packet_info _U_*pinfo, proto_tree *tree, guint offset, char **result, gboolean tohex, const char *label )
129 string_len = bencoded_string_length(tvb, &offset);
131 /* fill the return data */
133 *result = tvb_bytes_to_str(tvb, offset, string_len );
135 *result = tvb_get_string( wmem_packet_scope(), tvb, offset, string_len );
137 proto_tree_add_string_format( tree, hf_bencoded_string, tvb, offset, string_len, *result, "%s: %s", label, *result );
138 offset += string_len;
143 * dissect a bencoded integer from tvb, start at offset. it's like "i5673e"
144 * *result will be the decoded value
147 dissect_bencoded_int(tvbuff_t *tvb, packet_info _U_*pinfo, proto_tree *tree, guint offset, char **result, const char *label )
151 /* we have confirmed that the first byte is 'i' */
153 start_offset = offset;
155 while( tvb_get_guint8(tvb,offset)!='e' )
158 *result = tvb_get_string( wmem_packet_scope(), tvb, start_offset, offset-start_offset);
159 proto_tree_add_string_format( tree, hf_bencoded_int, tvb, start_offset, offset-start_offset, *result,
160 "%s: %s", label, *result );
166 /* pre definition of dissect_bencoded_dict(), which is needed by dissect_bencoded_list() */
167 static int dissect_bencoded_dict(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, const char *label );
169 /* dissect a bencoded list from tvb, start at offset. it's like "lXXXe", "X" is any bencoded thing */
171 dissect_bencoded_list(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, const char *label )
174 proto_tree *sub_tree;
178 ti = proto_tree_add_none_format( tree, hf_bencoded_list, tvb, offset, 0, "%s: list...", label );
179 sub_tree = proto_item_add_subtree( ti, ett_bencoded_list);
184 while( (one_byte=tvb_get_guint8(tvb,offset)) != 'e' )
190 offset = dissect_bencoded_int( tvb, pinfo, sub_tree, offset, &result, "Integer" );
194 offset = dissect_bencoded_list( tvb, pinfo, sub_tree, offset, "Sub-list" );
198 offset = dissect_bencoded_dict( tvb, pinfo, sub_tree, offset, "Sub-dict" );
202 offset = dissect_bencoded_string( tvb, pinfo, sub_tree, offset, &result, FALSE, "String" );
210 /* dissect a bt dht error from tvb, start at offset. it's like "li201e9:error msge" */
212 dissect_bt_dht_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, char **result, const char *label )
215 proto_tree *sub_tree;
216 char *error_no, *error_msg;
221 ti = proto_tree_add_item( tree, hf_bt_dht_error, tvb, offset, 0, ENC_NA );
222 sub_tree = proto_item_add_subtree( ti, ett_bt_dht_error);
224 /* we have confirmed that the first byte is 'l' */
227 /* dissect bt-dht error number and message */
228 offset = dissect_bencoded_int( tvb, pinfo, sub_tree, offset, &error_no, "Error ID" );
229 offset = dissect_bencoded_string( tvb, pinfo, sub_tree, offset, &error_msg, FALSE, "Error Message" );
231 proto_item_set_text( ti, "%s: error %s, %s", label, error_no, error_msg );
232 col_append_fstr( pinfo->cinfo, COL_INFO, "error_no=%s error_msg=%s ", error_no, error_msg );
233 *result = wmem_strdup_printf(wmem_packet_scope(), "error %s, %s", error_no, error_msg );
238 /* dissect a bt dht values list from tvb, start at offset. it's like "l6:....6:....e" */
240 dissect_bt_dht_values(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, char **result, const char *label )
243 proto_tree *sub_tree;
244 proto_item *value_ti;
245 proto_tree *value_tree;
250 ti = proto_tree_add_item( tree, hf_bt_dht_peers, tvb, offset, 0, ENC_NA );
251 sub_tree = proto_item_add_subtree( ti, ett_bt_dht_peers);
254 /* we has confirmed that the first byte is 'l' */
257 /* dissect bt-dht values */
258 while( tvb_get_guint8(tvb,offset)!='e' )
260 string_len = bencoded_string_length(tvb, &offset);
262 /* 4 bytes ip, 2 bytes port */
263 for( ; string_len>=6; string_len-=6, offset+=6 )
267 value_ti = proto_tree_add_item( sub_tree, hf_bt_dht_peer, tvb, offset, 6, ENC_NA );
268 proto_item_append_text(value_ti, " %d", peer_index);
269 value_tree = proto_item_add_subtree( value_ti, ett_bt_dht_peers);
271 proto_tree_add_item( value_tree, hf_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
272 proto_item_append_text(value_ti, " (IP/Port: %s", tvb_ip_to_str(tvb, offset));
273 proto_tree_add_item( value_tree, hf_port, tvb, offset+4, 2, ENC_BIG_ENDIAN);
274 proto_item_append_text(value_ti, ":%u)", tvb_get_ntohs( tvb, offset+4 ));
280 proto_tree_add_item( tree, hf_truncated_data, tvb, offset, string_len, ENC_NA );
281 offset += string_len;
285 if (tvb_get_guint8(tvb,offset)=='e') /* list ending delimiter */
288 proto_item_set_text( ti, "%s: %d peers", label, peer_index );
289 col_append_fstr( pinfo->cinfo, COL_INFO, "reply=%d peers ", peer_index );
290 *result = wmem_strdup_printf(wmem_packet_scope(), "%d peers", peer_index);
296 dissect_bt_dht_nodes(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, char **result, const char *label )
299 proto_tree *sub_tree;
301 proto_tree *node_tree;
306 string_len = bencoded_string_length(tvb, &offset);
308 ti = proto_tree_add_item( tree, hf_bt_dht_nodes, tvb, offset, string_len, ENC_NA );
309 sub_tree = proto_item_add_subtree( ti, ett_bt_dht_nodes);
312 /* 20 bytes id, 4 bytes ip, 2 bytes port */
313 for( ; string_len>=26; string_len-=26, offset+=26 )
318 node_ti = proto_tree_add_item( sub_tree, hf_bt_dht_node, tvb, offset, 26, ENC_NA);
319 proto_item_append_text(node_ti, " %d", node_index);
320 node_tree = proto_item_add_subtree( node_ti, ett_bt_dht_peers);
322 proto_tree_add_item( node_tree, hf_bt_dht_id, tvb, offset, 20, ENC_NA);
323 proto_item_append_text(node_ti, " (id: %s", tvb_bytes_to_str(tvb, offset, 20));
324 proto_tree_add_item( node_tree, hf_ip, tvb, offset+20, 4, ENC_BIG_ENDIAN);
325 proto_item_append_text(node_ti, ", IP/Port: %s", tvb_ip_to_str(tvb, offset+20));
326 proto_tree_add_item( node_tree, hf_port, tvb, offset+24, 2, ENC_BIG_ENDIAN);
327 proto_item_append_text(node_ti, ":%u)", tvb_get_ntohs( tvb, offset+24 ));
331 proto_tree_add_item( tree, hf_truncated_data, tvb, offset, string_len, ENC_NA );
332 offset += string_len;
334 proto_item_set_text( ti, "%s: %d nodes", label, node_index );
335 col_append_fstr( pinfo->cinfo, COL_INFO, "reply=%d nodes ", node_index );
336 *result = wmem_strdup_printf(wmem_packet_scope(), "%d", node_index);
342 dissect_bencoded_dict_entry(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset )
345 proto_tree *sub_tree;
348 guint orig_offset = offset;
353 ti = proto_tree_add_item( tree, hf_bencoded_dict_entry, tvb, offset, 0, ENC_NA );
354 sub_tree = proto_item_add_subtree( ti, ett_bencoded_dict_entry);
356 /* dissect the key, it must be a string */
357 offset = dissect_bencoded_string( tvb, pinfo, sub_tree, offset, &key, FALSE, "Key" );
359 /* If it is a dict, then just do recursion */
360 switch( tvb_get_guint8(tvb,offset) )
363 offset = dissect_bencoded_dict( tvb, pinfo, sub_tree, offset, "Value" );
364 val = (char*)dict_str;
367 if( strcmp(key,"e")==0 )
368 offset = dissect_bt_dht_error( tvb, pinfo, sub_tree, offset, &val, "Value" );
369 else if( strcmp(key,"values")==0 )
370 offset = dissect_bt_dht_values( tvb, pinfo, sub_tree, offset, &val, "Value" );
371 /* other unfamiliar lists */
374 offset = dissect_bencoded_list( tvb, pinfo, sub_tree, offset, "Value" );
375 val = (char*)list_str;
379 offset = dissect_bencoded_int( tvb, pinfo, sub_tree, offset, &val, "Value" );
383 /* special process */
384 if( strcmp(key,"nodes")==0 )
386 offset = dissect_bt_dht_nodes( tvb, pinfo, sub_tree, offset, &val, "Value" );
388 else if( strcmp(key,"ip")==0 )
391 * Not found in BEP 0005 but explained by
392 * http://www.rasterbar.com/products/libtorrent/dht_sec.html
397 len = bencoded_string_length(tvb, &offset);
400 proto_tree_add_item(sub_tree, hf_ip, tvb, offset, len, ENC_BIG_ENDIAN);
401 val = (char*)tvb_ip_to_str(tvb, offset);
405 offset = dissect_bencoded_string( tvb, pinfo, sub_tree, old_offset, &val, TRUE, "Value" );
410 /* some need to return hex string */
411 tohex = strcmp(key,"id")==0 || strcmp(key,"target")==0
412 || strcmp(key,"info_hash")==0 || strcmp(key,"t")==0
413 || strcmp(key,"v")==0 || strcmp(key,"token")==0;
414 offset = dissect_bencoded_string( tvb, pinfo, sub_tree, offset, &val, tohex, "Value" );
419 key = (char*)val_to_str_const( key[0], short_key_name_value_string, key );
421 val = (char*)val_to_str_const( val[0], short_val_name_value_string, val );
423 proto_item_set_text( ti, "%s: %s", key, val );
424 proto_item_set_len( ti, offset-orig_offset );
426 if( strcmp(key,"message_type")==0 || strcmp(key,"request_type")==0 )
427 col_append_fstr(pinfo->cinfo, COL_INFO, "%s=%s ", key, val);
434 dissect_bencoded_dict(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, const char *label )
437 proto_tree *sub_tree;
438 guint orig_offset = offset;
442 ti = proto_tree_add_item(tree, proto_bt_dht, tvb, 0, -1, ENC_NA);
443 sub_tree = proto_item_add_subtree(ti, ett_bt_dht);
447 ti = proto_tree_add_none_format( tree, hf_bencoded_dict, tvb, offset, -1, "%s: Dictionary...", label );
448 sub_tree = proto_item_add_subtree( ti, ett_bencoded_dict);
451 /* skip the first char('d') */
454 while( tvb_get_guint8(tvb,offset)!='e' )
455 offset = dissect_bencoded_dict_entry( tvb, pinfo, sub_tree, offset );
458 proto_item_set_len( ti, offset-orig_offset );
464 dissect_bt_dht(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
466 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BT-DHT");
467 col_clear(pinfo->cinfo, COL_INFO);
469 return dissect_bencoded_dict(tvb, pinfo, tree, 0, "BitTorrent DHT Protocol");
473 gboolean dissect_bt_dht_heur (tvbuff_t *tvb, packet_info *pinfo,
474 proto_tree *tree, void *data _U_)
477 /* Assume dictionary (d) is followed by a one char long (1:) key string. */
478 if(tvb_memeql(tvb, 0, "d1:", 3) == 0)
481 guint8 key = tvb_get_guint8(tvb, 3);
483 /* Iterate through possible keys to improve heuristics. */
484 for(i=0; short_key_name_value_string[i].value != 0; i++)
486 if(short_key_name_value_string[i].value == key)
488 conversation_t *conversation;
490 conversation = find_or_create_conversation(pinfo);
491 conversation_set_dissector(conversation, bt_dht_handle);
493 dissect_bt_dht(tvb, pinfo, tree, NULL);
502 proto_register_bt_dht(void)
504 static hf_register_info hf[] = {
505 { &hf_bencoded_string,
506 { "String", "bt-dht.bencoded.string",
507 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
510 { "List", "bt-dht.bencoded.list",
511 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
514 { "Int", "bt-dht.bencoded.int",
515 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
518 { "Dictionary", "bt-dht.bencoded.dict",
519 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
521 { &hf_bencoded_dict_entry,
522 { "Dictionary Entry", "bt-dht.bencoded.dict_entry",
523 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
526 { "Error", "bt-dht.error",
527 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
530 { "Peer", "bt-dht.peer",
531 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
534 { "Peers", "bt-dht.peers",
535 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
538 { "Node", "bt-dht.node",
539 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
542 { "Nodes", "bt-dht.nodes",
543 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
547 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
551 FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }
554 { "Port", "bt-dht.port",
555 FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }
557 { &hf_truncated_data,
558 { "Truncated data", "bt-dht.truncated_data",
559 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
563 /* Setup protocol subtree array */
564 static gint *ett[] = {
571 &ett_bencoded_dict_entry
574 module_t *bt_dht_module;
576 proto_bt_dht = proto_register_protocol (
577 "BitTorrent DHT Protocol", /* name */
578 "BT-DHT", /* short name */
579 "bt-dht" /* abbrev */
582 bt_dht_module = prefs_register_protocol(proto_bt_dht, proto_reg_handoff_bt_dht);
583 prefs_register_bool_preference(bt_dht_module, "enable", "Enable BT-DHT heuristic dissection",
584 "Enable BT-DHT heuristic dissection (default is disabled)",
585 &bt_dht_enable_heuristic_dissection);
587 proto_register_field_array(proto_bt_dht, hf, array_length(hf));
588 proto_register_subtree_array(ett, array_length(ett));
592 proto_reg_handoff_bt_dht(void)
594 static gboolean prefs_initialized = FALSE;
596 /* "Decode As" is always available;
597 * Heuristic dissection in disabled by default since the heuristic is quite weak.
598 * XXX - Still too weak?
600 if (!prefs_initialized) {
601 heur_dissector_add("udp", dissect_bt_dht_heur, proto_bt_dht);
603 bt_dht_handle = new_create_dissector_handle(dissect_bt_dht, proto_bt_dht);
604 dissector_add_handle("udp.port", bt_dht_handle); /* for "decode_as" */
606 prefs_initialized = TRUE;
609 heur_dissector_set_enabled("udp", dissect_bt_dht_heur, proto_bt_dht, bt_dht_enable_heuristic_dissection);
618 * indent-tabs-mode: nil
621 * ex: set shiftwidth=2 tabstop=8 expandtab:
622 * :indentSize=2:tabSize=8:noTabs=true: