aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-hpfeeds.c
blob: cb23d5e83d0c7d33c66c64dd54d9375ae40b885d (plain)
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* 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
 *
 * 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.
 */


/*
 * 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/wmem_list.h>

#include "packet-tcp.h"

struct HpfeedsTap {
    guint payload_size;
    guint8* channel;
    guint8 opcode;
};

static int hpfeeds_tap = -1;

static const guint8* st_str_channels_payload = "Payload size per channel";
static const guint8* 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 heur_dissector_list_t heur_subdissector_list;

/* Preferences */
static guint hpfeeds_port_pref = 0;
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|ENC_NA);
}

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|ENC_NA);
    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|ENC_NA);
    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;
    guint8 *strptr = NULL;
    tvbuff_t *next_tvb;

    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|ENC_NA);
    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 */
    strptr = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, len, ENC_ASCII);
    proto_tree_add_item(tree, hf_hpfeeds_channel, tvb, offset, len, ENC_ASCII|ENC_NA);
    offset += len;

    next_tvb = tvb_new_subset_remaining(tvb, offset);

    /* try the heuristic dissectors */
    if (try_heuristic) {
        if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree, &hdtbl_entry, strptr)) {
            return;
        }
    }

    /* 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, TRUE);
    st_node_opcodes = stats_tree_create_pivot(st, st_str_opcodes, 0);

    channels_list = wmem_list_new(wmem_epan_scope());
}

static int hpfeeds_stats_tree_packet(stats_tree* st _U_, packet_info* pinfo _U_, epan_dissect_t* edt _U_, const void* p)
{
    struct HpfeedsTap *pi = (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(ch_node->channel, pi->channel, strlen(pi->channel)) == 0) {
                break;
            }
            cur = wmem_list_frame_next(cur);
        }

        if (cur == NULL) {
            ch_node = (struct channel_node*)wmem_alloc0(wmem_file_scope(), sizeof(struct channel_node));
            ch_node->channel = wmem_strdup(wmem_file_scope(), pi->channel);
            ch_node->st_node_channel_payload = stats_tree_create_node(st, ch_node->channel,
                st_node_channels_payload, FALSE);
            wmem_list_append(channels_list, ch_node);
        }

        avg_stat_node_add_value(st, st_str_channels_payload, 0, FALSE, pi->payload_size);
        avg_stat_node_add_value(st, 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 1;
}

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|ENC_NA);
    /* move forward inside data */
    offset += len;
    proto_tree_add_item(tree, hf_hpfeeds_channel, tvb, offset, -1,
        ENC_ASCII|ENC_NA);
}

/*
 * 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)
{
    /* At lease header is needed */
    if (tvb_reported_length(tvb) < HPFEEDS_HDR_LEN)
        return 0;

    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_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_module = prefs_register_protocol(proto_hpfeeds, proto_reg_handoff_hpfeeds);
    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_uint_preference(hpfeeds_module,
        "dissector_port",
        "Dissector TCP port",
        "Set the TCP port for HPFEEDS messages",
        10, &hpfeeds_port_pref);

    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)
{
    static dissector_handle_t hpfeeds_handle;
    static gboolean hpfeeds_prefs_initialized = FALSE;
    static gint16 hpfeeds_dissector_port;

    if (!hpfeeds_prefs_initialized) {
        hpfeeds_handle = new_create_dissector_handle(dissect_hpfeeds, proto_hpfeeds);
        stats_tree_register("hpfeeds", "hpfeeds", "HPFEEDS", 0, hpfeeds_stats_tree_packet, hpfeeds_stats_tree_init, NULL);
        hpfeeds_prefs_initialized = TRUE;
    }
    else {
        dissector_delete_uint("tcp.port",hpfeeds_dissector_port , hpfeeds_handle);
    }

    hpfeeds_dissector_port = hpfeeds_port_pref;

    dissector_add_uint("tcp.port", hpfeeds_dissector_port,  hpfeeds_handle);
}

/*
 * Editor modelines  -  http://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:
 */