2 * Routines for Honeypot Protocol Feeds packet disassembly
3 * Copyright 2013, Sebastiano DI PAOLA - <sebastiano.dipaola@gmail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
14 * Additional information regarding hpfeeds protocol can be found here
15 * https://redmine.honeynet.org/projects/hpfeeds/wiki
20 #include <epan/packet.h>
21 #include <epan/prefs.h>
22 #include <epan/expert.h>
24 #include <epan/stats_tree.h>
25 #include <epan/wmem_scopes.h>
27 #include "packet-tcp.h"
30 unsigned payload_size
;
35 static int hpfeeds_tap
;
37 static const char* st_str_channels_payload
= "Payload size per channel";
38 static const char* st_str_opcodes
= "Opcodes";
40 static int st_node_channels_payload
= -1;
41 static int st_node_opcodes
= -1;
43 static wmem_list_t
* channels_list
;
47 unsigned st_node_channel_payload
;
50 void proto_register_hpfeeds(void);
51 void proto_reg_handoff_hpfeeds(void);
53 static dissector_handle_t hpfeeds_handle
;
55 static heur_dissector_list_t heur_subdissector_list
;
58 static bool hpfeeds_desegment
= true;
59 static bool try_heuristic
= true;
61 static int proto_hpfeeds
;
63 static int hf_hpfeeds_opcode
;
64 static int hf_hpfeeds_msg_length
;
65 static int hf_hpfeeds_nonce
;
66 static int hf_hpfeeds_secret
;
67 static int hf_hpfeeds_payload
;
68 static int hf_hpfeeds_server_len
;
69 static int hf_hpfeeds_server
;
70 static int hf_hpfeeds_ident_len
;
71 static int hf_hpfeeds_ident
;
72 static int hf_hpfeeds_channel
;
73 static int hf_hpfeeds_chan_len
;
74 static int hf_hpfeeds_errmsg
;
76 static int ett_hpfeeds
;
78 static expert_field ei_hpfeeds_opcode_unknown
;
81 #define OP_ERROR 0 /* error message*/
82 #define OP_INFO 1 /* server name, nonce */
83 #define OP_AUTH 2 /* client id, sha1(nonce+authkey) */
84 #define OP_PUBLISH 3 /* client id, channelname, payload */
85 #define OP_SUBSCRIBE 4 /* client id, channelname*/
87 /* OFFSET FOR HEADER */
88 #define HPFEEDS_HDR_LEN 5
90 static const value_string opcode_vals
[] = {
91 { OP_ERROR
, "Error" },
94 { OP_PUBLISH
, "Publish" },
95 { OP_SUBSCRIBE
, "Subscribe" },
100 dissect_hpfeeds_error_pdu(tvbuff_t
*tvb
, proto_tree
*tree
, unsigned offset
)
102 proto_tree_add_item(tree
, hf_hpfeeds_errmsg
, tvb
, offset
, -1, ENC_ASCII
);
106 dissect_hpfeeds_info_pdu(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
)
109 proto_tree
*data_subtree
;
110 uint8_t *strptr
= NULL
;
112 len
= tvb_get_uint8(tvb
, offset
);
113 /* don't move the offset yet as we need to get data after this operation */
114 strptr
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
+ 1, len
, ENC_ASCII
);
115 data_subtree
= proto_tree_add_subtree_format(tree
, tvb
, offset
, -1, ett_hpfeeds
, NULL
, "Broker: %s", strptr
);
117 proto_tree_add_item(data_subtree
, hf_hpfeeds_server_len
, tvb
, offset
, 1,
121 proto_tree_add_item(data_subtree
, hf_hpfeeds_server
, tvb
, offset
, len
,
125 proto_tree_add_item(data_subtree
, hf_hpfeeds_nonce
, tvb
, offset
, -1,
130 dissect_hpfeeds_auth_pdu(tvbuff_t
*tvb
, proto_tree
*tree
, unsigned offset
)
134 len
= tvb_get_uint8(tvb
, offset
);
135 proto_tree_add_item(tree
, hf_hpfeeds_ident_len
, tvb
,
136 offset
, 1, ENC_BIG_ENDIAN
);
138 proto_tree_add_item(tree
, hf_hpfeeds_ident
, tvb
,
139 offset
, len
, ENC_ASCII
);
142 proto_tree_add_item(tree
, hf_hpfeeds_secret
, tvb
,
147 hpfeeds_get_channel_name(tvbuff_t
* tvb
, unsigned offset
)
149 uint8_t len
= tvb_get_uint8(tvb
, offset
);
151 len
= tvb_get_uint8(tvb
, offset
);
153 return tvb_get_string_enc(wmem_file_scope(), tvb
, offset
, len
, ENC_ASCII
);
157 hpfeeds_get_payload_size(tvbuff_t
* tvb
, unsigned offset
)
159 unsigned message_len
= tvb_get_ntohl(tvb
, offset
);
160 unsigned ident_len
= tvb_get_uint8(tvb
, offset
+ 5);
161 unsigned channel_len
= tvb_get_uint8(tvb
, offset
+ 6 + ident_len
);
162 return (message_len
- 2 - ident_len
- 1 - channel_len
);
166 dissect_hpfeeds_publish_pdu(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
,
170 heur_dtbl_entry_t
*hdtbl_entry
;
172 const uint8_t *channelname
= NULL
;
173 const char* save_match_string
= NULL
;
175 len
= tvb_get_uint8(tvb
, offset
);
176 proto_tree_add_item(tree
, hf_hpfeeds_ident_len
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
178 proto_tree_add_item(tree
, hf_hpfeeds_ident
, tvb
, offset
, len
, ENC_ASCII
);
180 len
= tvb_get_uint8(tvb
, offset
);
181 proto_tree_add_item(tree
, hf_hpfeeds_chan_len
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
184 /* get the channel name as ephemeral string to pass it to the heuristic decoders */
185 proto_tree_add_item_ret_string(tree
, hf_hpfeeds_channel
, tvb
, offset
, len
, ENC_ASCII
|ENC_NA
,
186 pinfo
->pool
, &channelname
);
189 /* try the heuristic dissectors */
191 /* save the current match_string before calling the subdissectors */
192 if (pinfo
->match_string
)
193 save_match_string
= pinfo
->match_string
;
194 pinfo
->match_string
= (const char*)channelname
;
196 next_tvb
= tvb_new_subset_remaining(tvb
, offset
);
198 if (dissector_try_heuristic(heur_subdissector_list
, next_tvb
, pinfo
, tree
, &hdtbl_entry
, NULL
)) {
202 pinfo
->match_string
= save_match_string
;
205 /* heuristic failed. Print remaining bytes as flat payload */
206 proto_tree_add_item(tree
, hf_hpfeeds_payload
, tvb
, offset
, -1, ENC_NA
);
209 static void hpfeeds_stats_tree_init(stats_tree
* st
)
211 st_node_channels_payload
= stats_tree_create_node(st
, st_str_channels_payload
, 0, STAT_DT_INT
, true);
212 st_node_opcodes
= stats_tree_create_pivot(st
, st_str_opcodes
, 0);
214 channels_list
= wmem_list_new(wmem_epan_scope());
217 static tap_packet_status
hpfeeds_stats_tree_packet(stats_tree
* st _U_
, packet_info
* pinfo _U_
, epan_dissect_t
* edt _U_
, const void* p
, tap_flags_t flags _U_
)
219 const struct HpfeedsTap
*pi
= (const struct HpfeedsTap
*)p
;
220 wmem_list_frame_t
* head
= wmem_list_head(channels_list
);
221 wmem_list_frame_t
* cur
= head
;
222 struct channel_node
* ch_node
;
224 if (pi
->opcode
== OP_PUBLISH
) {
225 /* search an existing channel node and create it if it does not */
227 ch_node
= (struct channel_node
*)wmem_list_frame_data(cur
);
228 if (strncmp((char*)ch_node
->channel
, (char*)pi
->channel
, strlen((char*)pi
->channel
)) == 0) {
231 cur
= wmem_list_frame_next(cur
);
235 ch_node
= wmem_new0(wmem_file_scope(), struct channel_node
);
236 ch_node
->channel
= (unsigned char*)wmem_strdup(wmem_file_scope(), (char*)pi
->channel
);
237 ch_node
->st_node_channel_payload
= stats_tree_create_node(st
, (char*)ch_node
->channel
,
238 st_node_channels_payload
, STAT_DT_INT
, false);
239 wmem_list_append(channels_list
, ch_node
);
242 avg_stat_node_add_value_int(st
, st_str_channels_payload
, 0, false, pi
->payload_size
);
243 avg_stat_node_add_value_int(st
, (char*)ch_node
->channel
, 0, false, pi
->payload_size
);
246 stats_tree_tick_pivot(st
, st_node_opcodes
,
247 val_to_str(pi
->opcode
, opcode_vals
, "Unknown opcode (%d)"));
248 return TAP_PACKET_REDRAW
;
252 dissect_hpfeeds_subscribe_pdu(tvbuff_t
*tvb
, proto_tree
*tree
, unsigned offset
)
255 /* get length of ident field */
256 len
= tvb_get_uint8(tvb
, offset
);
257 proto_tree_add_item(tree
, hf_hpfeeds_ident_len
, tvb
, offset
, 1,
261 proto_tree_add_item(tree
, hf_hpfeeds_ident
, tvb
, offset
, len
,
263 /* move forward inside data */
265 proto_tree_add_item(tree
, hf_hpfeeds_channel
, tvb
, offset
, -1,
270 * Get the length of the HPFEED message, including header
271 * This is a trivial function, but it's mandatory as it is used as a callback
272 * by the routine to re-assemble the protocol spread on multiple TCP packets
275 get_hpfeeds_pdu_len(packet_info
*pinfo _U_
, tvbuff_t
*tvb
, int offset
, void *data _U_
)
277 return tvb_get_ntohl(tvb
, offset
+ 0);
281 dissect_hpfeeds_pdu(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
283 struct HpfeedsTap
*hpfeeds_stats
;
285 /* We have already parsed msg length we need to skip to opcode offset */
290 proto_tree
*hpfeeds_tree
, *data_subtree
;
292 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "HPFEEDS");
294 ti
= proto_tree_add_item(tree
, proto_hpfeeds
, tvb
, 0, -1, ENC_NA
);
295 hpfeeds_tree
= proto_item_add_subtree(ti
, ett_hpfeeds
);
296 proto_tree_add_item(hpfeeds_tree
, hf_hpfeeds_msg_length
, tvb
, offset
,
300 /* Get opcode and write it */
301 opcode
= tvb_get_uint8(tvb
, offset
);
303 /* Clear out stuff in the info column */
304 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "Type %s",
305 val_to_str(opcode
, opcode_vals
, "Unknown (0x%02x)"));
307 ti
= proto_tree_add_item(hpfeeds_tree
, hf_hpfeeds_opcode
, tvb
, offset
,
309 data_subtree
= proto_item_add_subtree(ti
, ett_hpfeeds
);
312 if (opcode
>= array_length(opcode_vals
) - 1) {
313 expert_add_info_format(pinfo
, ti
, &ei_hpfeeds_opcode_unknown
,
314 "Unknown value %02x for opcode field", opcode
);
317 if (tree
) { /* we are being asked for details */
320 dissect_hpfeeds_error_pdu(tvb
, data_subtree
, offset
);
323 dissect_hpfeeds_info_pdu(tvb
, pinfo
, data_subtree
, offset
);
326 dissect_hpfeeds_auth_pdu(tvb
, data_subtree
, offset
);
329 dissect_hpfeeds_publish_pdu(tvb
, pinfo
, data_subtree
, offset
);
332 dissect_hpfeeds_subscribe_pdu(tvb
, data_subtree
, offset
);
334 /* No need for a default, we check that outside the if(tree)
339 /* In publish, generate stats every packet, even not in tree */
340 hpfeeds_stats
= wmem_new0(wmem_file_scope(), struct HpfeedsTap
);
341 if (opcode
== OP_PUBLISH
) {
342 hpfeeds_stats
->channel
= hpfeeds_get_channel_name(tvb
, offset
);
343 hpfeeds_stats
->payload_size
= hpfeeds_get_payload_size(tvb
, 0);
346 hpfeeds_stats
->opcode
= opcode
;
347 tap_queue_packet(hpfeeds_tap
, pinfo
, hpfeeds_stats
);
348 return tvb_captured_length(tvb
);
352 dissect_hpfeeds(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data
)
354 tcp_dissect_pdus(tvb
, pinfo
, tree
, hpfeeds_desegment
, HPFEEDS_HDR_LEN
,
355 get_hpfeeds_pdu_len
, dissect_hpfeeds_pdu
, data
);
356 return tvb_captured_length(tvb
);
360 proto_register_hpfeeds(void)
362 static hf_register_info hf
[] = {
364 { &hf_hpfeeds_opcode
,
365 { "Opcode", "hpfeeds.opcode",
366 FT_UINT8
, BASE_DEC_HEX
,
367 VALS(opcode_vals
), 0x0,
370 { &hf_hpfeeds_msg_length
,
371 { "Message Length", "hpfeeds.msglen",
372 FT_UINT32
, BASE_DEC_HEX
,
377 { "Nonce", "hpfeeds.nonce",
382 { &hf_hpfeeds_secret
,
383 { "Secret", "hpfeeds.secret",
388 { &hf_hpfeeds_payload
,
389 { "Payload", "hpfeeds.payload",
394 { &hf_hpfeeds_server
,
395 { "Server", "hpfeeds.server",
396 FT_STRING
, BASE_NONE
,
401 { "Ident", "hpfeeds.ident",
402 FT_STRING
, BASE_NONE
,
406 { &hf_hpfeeds_channel
,
407 { "Channel", "hpfeeds.channel",
408 FT_STRING
, BASE_NONE
,
412 { &hf_hpfeeds_chan_len
,
413 { "Channel length", "hpfeeds.channel_len",
418 { &hf_hpfeeds_ident_len
,
419 { "Ident length", "hpfeeds.ident_len",
424 { &hf_hpfeeds_errmsg
,
425 { "Error message", "hpfeeds.errmsg",
426 FT_STRING
, BASE_NONE
,
430 { &hf_hpfeeds_server_len
,
431 { "Server length", "hpfeeds.server_len",
439 /* Setup protocol subtree array */
440 static int *ett
[] = {
444 static ei_register_info ei
[] = {
445 { &ei_hpfeeds_opcode_unknown
, { "hpfeeds.opcode.unknown", PI_PROTOCOL
, PI_WARN
, "Unknown value for opcode field", EXPFILL
}},
448 module_t
*hpfeeds_module
;
449 expert_module_t
* expert_hpfeeds
;
451 proto_hpfeeds
= proto_register_protocol (
452 "HPFEEDS HoneyPot Feeds Protocol", /* name */
453 "HPFEEDS", /* short name */
454 "hpfeeds" /* abbrev */
457 heur_subdissector_list
= register_heur_dissector_list_with_description("hpfeeds", "HPFEEDS Publish payload", proto_hpfeeds
);
459 proto_register_field_array(proto_hpfeeds
, hf
, array_length(hf
));
460 proto_register_subtree_array(ett
, array_length(ett
));
461 expert_hpfeeds
= expert_register_protocol(proto_hpfeeds
);
462 expert_register_field_array(expert_hpfeeds
, ei
, array_length(ei
));
464 hpfeeds_handle
= register_dissector("hpfeeds", dissect_hpfeeds
, proto_hpfeeds
);
466 hpfeeds_module
= prefs_register_protocol(proto_hpfeeds
, NULL
);
467 prefs_register_bool_preference(hpfeeds_module
, "desegment_hpfeeds_messages",
468 "Reassemble HPFEEDS messages spanning multiple TCP segments",
469 "Whether the HPFEEDS dissector should reassemble messages spanning "
470 "multiple TCP segments. "
471 "To use this option, you must also enable \"Allow subdissectors to "
472 "reassemble TCP streams\" in the TCP protocol settings.",
475 prefs_register_bool_preference(hpfeeds_module
, "try_heuristic",
476 "Try heuristic sub-dissectors",
477 "Try to decode the payload using an heuristic sub-dissector",
480 hpfeeds_tap
= register_tap("hpfeeds");
484 proto_reg_handoff_hpfeeds(void)
486 stats_tree_register("hpfeeds", "hpfeeds", "HPFEEDS", 0, hpfeeds_stats_tree_packet
, hpfeeds_stats_tree_init
, NULL
);
488 dissector_add_for_decode_as_with_preference("tcp.port", hpfeeds_handle
);
492 * Editor modelines - https://www.wireshark.org/tools/modelines.html
497 * indent-tabs-mode: nil
500 * vi: set shiftwidth=4 tabstop=8 expandtab:
501 * :indentSize=4:tabSize=8:noTabs=true: