1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/* packet-hpfeeds.c
* Routines for Honeypot Protocol Feeds packet disassembly
* Copyright 2013, Sebastiano DI PAOLA - <sebastiano.dipaola@gmail.com>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/*
* Additional information regarding hpfeeds protocol can be found here
* https://redmine.honeynet.org/projects/hpfeeds/wiki
*/
#include "config.h"
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/tap.h>
#include <epan/stats_tree.h>
#include <epan/wmem_scopes.h>
#include "packet-tcp.h"
struct HpfeedsTap {
guint payload_size;
guint8* channel;
guint8 opcode;
};
static int hpfeeds_tap = -1;
static const gchar* st_str_channels_payload = "Payload size per channel";
static const gchar* st_str_opcodes = "Opcodes";
static int st_node_channels_payload = -1;
static int st_node_opcodes = -1;
static wmem_list_t* channels_list;
struct channel_node {
guint8* channel;
guint st_node_channel_payload;
};
void proto_register_hpfeeds(void);
void proto_reg_handoff_hpfeeds(void);
static dissector_handle_t hpfeeds_handle;
static heur_dissector_list_t heur_subdissector_list;
/* Preferences */
static gboolean hpfeeds_desegment = TRUE;
static gboolean try_heuristic = TRUE;
static int proto_hpfeeds = -1;
static int hf_hpfeeds_opcode = -1;
static int hf_hpfeeds_msg_length = -1;
static int hf_hpfeeds_nonce = -1;
static int hf_hpfeeds_secret = -1;
static int hf_hpfeeds_payload = -1;
static int hf_hpfeeds_server_len = -1;
static int hf_hpfeeds_server = -1;
static int hf_hpfeeds_ident_len = -1;
static int hf_hpfeeds_ident = -1;
static int hf_hpfeeds_channel = -1;
static int hf_hpfeeds_chan_len = -1;
static int hf_hpfeeds_errmsg = -1;
static gint ett_hpfeeds = -1;
static expert_field ei_hpfeeds_opcode_unknown = EI_INIT;
/* OPCODE */
#define OP_ERROR 0 /* error message*/
#define OP_INFO 1 /* server name, nonce */
#define OP_AUTH 2 /* client id, sha1(nonce+authkey) */
#define OP_PUBLISH 3 /* client id, channelname, payload */
#define OP_SUBSCRIBE 4 /* client id, channelname*/
/* OFFSET FOR HEADER */
#define HPFEEDS_HDR_LEN 5
static const value_string opcode_vals[] = {
{ OP_ERROR, "Error" },
{ OP_INFO, "Info" },
{ OP_AUTH, "Auth" },
{ OP_PUBLISH, "Publish" },
{ OP_SUBSCRIBE, "Subscribe" },
{ 0, NULL },
};
static void
dissect_hpfeeds_error_pdu(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
proto_tree_add_item(tree, hf_hpfeeds_errmsg, tvb, offset, -1, ENC_ASCII);
}
static void
dissect_hpfeeds_info_pdu(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
guint8 len = 0;
proto_tree *data_subtree;
guint8 *strptr = NULL;
len = tvb_get_guint8(tvb, offset);
/* don't move the offset yet as we need to get data after this operation */
strptr = tvb_get_string_enc(wmem_packet_scope(), tvb, offset + 1, len, ENC_ASCII);
data_subtree = proto_tree_add_subtree_format(tree, tvb, offset, -1, ett_hpfeeds, NULL, "Broker: %s", strptr);
proto_tree_add_item(data_subtree, hf_hpfeeds_server_len, tvb, offset, 1,
ENC_BIG_ENDIAN);
offset += 1;
proto_tree_add_item(data_subtree, hf_hpfeeds_server, tvb, offset, len,
ENC_ASCII);
offset += len;
proto_tree_add_item(data_subtree, hf_hpfeeds_nonce, tvb, offset, -1,
ENC_NA);
}
static void
dissect_hpfeeds_auth_pdu(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
guint8 len = 0;
len = tvb_get_guint8(tvb, offset);
proto_tree_add_item(tree, hf_hpfeeds_ident_len, tvb,
offset, 1, ENC_BIG_ENDIAN);
offset += 1;
proto_tree_add_item(tree, hf_hpfeeds_ident, tvb,
offset, len, ENC_ASCII);
offset += len;
proto_tree_add_item(tree, hf_hpfeeds_secret, tvb,
offset, -1, ENC_NA);
}
static guint8*
hpfeeds_get_channel_name(tvbuff_t* tvb, guint offset)
{
guint8 len = tvb_get_guint8(tvb, offset);
offset += len + 1;
len = tvb_get_guint8(tvb, offset);
offset += 1;
return tvb_get_string_enc(wmem_file_scope(), tvb, offset, len, ENC_ASCII);
}
static guint
hpfeeds_get_payload_size(tvbuff_t* tvb, guint offset)
{
guint message_len = tvb_get_ntohl(tvb, offset);
guint ident_len = tvb_get_guint8(tvb, offset + 5);
guint channel_len = tvb_get_guint8(tvb, offset + 6 + ident_len);
return (message_len - 2 - ident_len - 1 - channel_len);
}
static void
dissect_hpfeeds_publish_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
guint offset)
{
guint8 len = 0;
heur_dtbl_entry_t *hdtbl_entry;
tvbuff_t *next_tvb;
const guint8 *channelname = NULL;
const char* save_match_string = NULL;
len = tvb_get_guint8(tvb, offset);
proto_tree_add_item(tree, hf_hpfeeds_ident_len, tvb, offset, 1, ENC_BIG_ENDIAN);
offset += 1;
proto_tree_add_item(tree, hf_hpfeeds_ident, tvb, offset, len, ENC_ASCII);
offset += len;
len = tvb_get_guint8(tvb, offset);
proto_tree_add_item(tree, hf_hpfeeds_chan_len, tvb, offset, 1, ENC_BIG_ENDIAN);
offset += 1;
/* get the channel name as ephemeral string to pass it to the heuristic decoders */
proto_tree_add_item_ret_string(tree, hf_hpfeeds_channel, tvb, offset, len, ENC_ASCII|ENC_NA,
wmem_packet_scope(), &channelname);
offset += len;
/* try the heuristic dissectors */
if (try_heuristic) {
/* save the current match_string before calling the subdissectors */
if (pinfo->match_string)
save_match_string = pinfo->match_string;
pinfo->match_string = (const char*)channelname;
next_tvb = tvb_new_subset_remaining(tvb, offset);
if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree, &hdtbl_entry, NULL)) {
return;
}
pinfo->match_string = save_match_string;
}
/* heuristic failed. Print remaining bytes as flat payload */
proto_tree_add_item(tree, hf_hpfeeds_payload, tvb, offset, -1, ENC_NA);
}
static void hpfeeds_stats_tree_init(stats_tree* st)
{
st_node_channels_payload = stats_tree_create_node(st, st_str_channels_payload, 0, STAT_DT_INT, TRUE);
st_node_opcodes = stats_tree_create_pivot(st, st_str_opcodes, 0);
channels_list = wmem_list_new(wmem_epan_scope());
}
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_)
{
const struct HpfeedsTap *pi = (const struct HpfeedsTap *)p;
wmem_list_frame_t* head = wmem_list_head(channels_list);
wmem_list_frame_t* cur = head;
struct channel_node* ch_node;
if (pi->opcode == OP_PUBLISH) {
/* search an existing channel node and create it if it does not */
while(cur != NULL) {
ch_node = (struct channel_node*)wmem_list_frame_data(cur);
if (strncmp((gchar*)ch_node->channel, (gchar*)pi->channel, strlen((gchar*)pi->channel)) == 0) {
break;
}
cur = wmem_list_frame_next(cur);
}
if (cur == NULL) {
ch_node = wmem_new0(wmem_file_scope(), struct channel_node);
ch_node->channel = (guchar*)wmem_strdup(wmem_file_scope(), (gchar*)pi->channel);
ch_node->st_node_channel_payload = stats_tree_create_node(st, (gchar*)ch_node->channel,
st_node_channels_payload, STAT_DT_INT, FALSE);
wmem_list_append(channels_list, ch_node);
}
avg_stat_node_add_value_int(st, st_str_channels_payload, 0, FALSE, pi->payload_size);
avg_stat_node_add_value_int(st, (gchar*)ch_node->channel, 0, FALSE, pi->payload_size);
}
stats_tree_tick_pivot(st, st_node_opcodes,
val_to_str(pi->opcode, opcode_vals, "Unknown opcode (%d)"));
return TAP_PACKET_REDRAW;
}
static void
dissect_hpfeeds_subscribe_pdu(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
guint8 len = 0;
/* get length of ident field */
len = tvb_get_guint8(tvb, offset);
proto_tree_add_item(tree, hf_hpfeeds_ident_len, tvb, offset, 1,
ENC_BIG_ENDIAN);
offset += 1;
proto_tree_add_item(tree, hf_hpfeeds_ident, tvb, offset, len,
ENC_ASCII);
/* move forward inside data */
offset += len;
proto_tree_add_item(tree, hf_hpfeeds_channel, tvb, offset, -1,
ENC_ASCII);
}
/*
* Get the length of the HPFEED message, including header
* This is a trivial function, but it's mandatory as it is used as a callback
* by the routine to re-assemble the protocol spread on multiple TCP packets
*/
static guint
get_hpfeeds_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
{
return tvb_get_ntohl(tvb, offset + 0);
}
static int
dissect_hpfeeds_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
struct HpfeedsTap *hpfeeds_stats;
/* We have already parsed msg length we need to skip to opcode offset */
guint offset = 0;
guint8 opcode;
proto_item *ti;
proto_tree *hpfeeds_tree, *data_subtree;
col_set_str(pinfo->cinfo, COL_PROTOCOL, "HPFEEDS");
ti = proto_tree_add_item(tree, proto_hpfeeds, tvb, 0, -1, ENC_NA);
hpfeeds_tree = proto_item_add_subtree(ti, ett_hpfeeds);
proto_tree_add_item(hpfeeds_tree, hf_hpfeeds_msg_length, tvb, offset,
4, ENC_BIG_ENDIAN);
offset += 4;
/* Get opcode and write it */
opcode = tvb_get_guint8(tvb, offset);
/* Clear out stuff in the info column */
col_add_fstr(pinfo->cinfo, COL_INFO, "Type %s",
val_to_str(opcode, opcode_vals, "Unknown (0x%02x)"));
ti = proto_tree_add_item(hpfeeds_tree, hf_hpfeeds_opcode, tvb, offset,
1, ENC_BIG_ENDIAN);
data_subtree = proto_item_add_subtree(ti, ett_hpfeeds);
offset += 1;
if (opcode >= array_length(opcode_vals) - 1) {
expert_add_info_format(pinfo, ti, &ei_hpfeeds_opcode_unknown,
"Unknown value %02x for opcode field", opcode);
}
if (tree) { /* we are being asked for details */
switch (opcode) {
case OP_ERROR:
dissect_hpfeeds_error_pdu(tvb, data_subtree, offset);
break;
case OP_INFO:
dissect_hpfeeds_info_pdu(tvb, data_subtree, offset);
break;
case OP_AUTH:
dissect_hpfeeds_auth_pdu(tvb, data_subtree, offset);
break;
case OP_PUBLISH:
dissect_hpfeeds_publish_pdu(tvb, pinfo, data_subtree, offset);
break;
case OP_SUBSCRIBE:
dissect_hpfeeds_subscribe_pdu(tvb, data_subtree, offset);
break;
/* No need for a default, we check that outside the if(tree)
* block earlier */
}
}
/* In publish, generate stats every packet, even not in tree */
hpfeeds_stats = wmem_new0(wmem_file_scope(), struct HpfeedsTap);
if (opcode == OP_PUBLISH) {
hpfeeds_stats->channel = hpfeeds_get_channel_name(tvb, offset);
hpfeeds_stats->payload_size = hpfeeds_get_payload_size(tvb, 0);
}
hpfeeds_stats->opcode = opcode;
tap_queue_packet(hpfeeds_tap, pinfo, hpfeeds_stats);
return tvb_captured_length(tvb);
}
static int
dissect_hpfeeds(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
tcp_dissect_pdus(tvb, pinfo, tree, hpfeeds_desegment, HPFEEDS_HDR_LEN,
get_hpfeeds_pdu_len, dissect_hpfeeds_pdu, data);
return tvb_captured_length(tvb);
}
void
proto_register_hpfeeds(void)
{
static hf_register_info hf[] = {
{ &hf_hpfeeds_opcode,
{ "Opcode", "hpfeeds.opcode",
FT_UINT8, BASE_DEC_HEX,
VALS(opcode_vals), 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_msg_length,
{ "Message Length", "hpfeeds.msglen",
FT_UINT32, BASE_DEC_HEX,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_nonce,
{ "Nonce", "hpfeeds.nonce",
FT_BYTES, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_secret,
{ "Secret", "hpfeeds.secret",
FT_BYTES, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_payload,
{ "Payload", "hpfeeds.payload",
FT_BYTES, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_server,
{ "Server", "hpfeeds.server",
FT_STRING, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_ident,
{ "Ident", "hpfeeds.ident",
FT_STRING, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_channel,
{ "Channel", "hpfeeds.channel",
FT_STRING, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_chan_len,
{ "Channel length", "hpfeeds.channel_len",
FT_UINT8, BASE_DEC,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_ident_len,
{ "Ident length", "hpfeeds.ident_len",
FT_UINT8, BASE_DEC,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_errmsg,
{ "Error message", "hpfeeds.errmsg",
FT_STRING, BASE_NONE,
NULL, 0x0,
NULL, HFILL }
},
{ &hf_hpfeeds_server_len,
{ "Server length", "hpfeeds.server_len",
FT_UINT8, BASE_DEC,
NULL, 0x0,
NULL, HFILL }
},
};
/* Setup protocol subtree array */
static gint *ett[] = {
&ett_hpfeeds
};
static ei_register_info ei[] = {
{ &ei_hpfeeds_opcode_unknown, { "hpfeeds.opcode.unknown", PI_PROTOCOL, PI_WARN, "Unknown value for opcode field", EXPFILL }},
};
module_t *hpfeeds_module;
expert_module_t* expert_hpfeeds;
proto_hpfeeds = proto_register_protocol (
"HPFEEDS HoneyPot Feeds Protocol", /* name */
"HPFEEDS", /* short name */
"hpfeeds" /* abbrev */
);
heur_subdissector_list = register_heur_dissector_list("hpfeeds", proto_hpfeeds);
proto_register_field_array(proto_hpfeeds, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
expert_hpfeeds = expert_register_protocol(proto_hpfeeds);
expert_register_field_array(expert_hpfeeds, ei, array_length(ei));
hpfeeds_handle = register_dissector("hpfeeds", dissect_hpfeeds, proto_hpfeeds);
hpfeeds_module = prefs_register_protocol(proto_hpfeeds, NULL);
prefs_register_bool_preference(hpfeeds_module, "desegment_hpfeeds_messages",
"Reassemble HPFEEDS messages spanning multiple TCP segments",
"Whether the HPFEEDS dissector should reassemble messages spanning "
"multiple TCP segments. "
"To use this option, you must also enable \"Allow subdissectors to "
"reassemble TCP streams\" in the TCP protocol settings.",
&hpfeeds_desegment);
prefs_register_bool_preference(hpfeeds_module, "try_heuristic",
"Try heuristic sub-dissectors",
"Try to decode the payload using an heuristic sub-dissector",
&try_heuristic);
hpfeeds_tap = register_tap("hpfeeds");
}
void
proto_reg_handoff_hpfeeds(void)
{
stats_tree_register("hpfeeds", "hpfeeds", "HPFEEDS", 0, hpfeeds_stats_tree_packet, hpfeeds_stats_tree_init, NULL);
dissector_add_for_decode_as_with_preference("tcp.port", hpfeeds_handle);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|