aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-fortinet-sso.c
blob: 22d61124ef9e166ddee724b9ac59b651f7ec4729 (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
/* packet-fortinet-sso.c
 * Routines for Fortinet Single Sign-On
 * Copyright 2020, Alexis La Goutte <alexis.lagoutte at gmail dot com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * No spec/doc is available based on reverse/analysis of protocol...
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/addr_resolv.h>

#define UDP_FSSO        8002

void proto_register_fortinet_sso(void);
void proto_reg_handoff_fortinet_sso(void);

static int proto_fortinet_sso = -1;
static gint ett_fortinet_sso  = -1;

static int hf_fsso_length = -1;
static int hf_fsso_timestamp = -1;
static int hf_fsso_client_ip = -1;
static int hf_fsso_payload_length = -1;
static int hf_fsso_string = -1;
static int hf_fsso_domain = -1;
static int hf_fsso_user = -1;
static int hf_fsso_host = -1;
static int hf_fsso_version = -1;
static int hf_fsso_tsagent_number_port_range = -1;
static int hf_fsso_tsagent_port_range_min = -1;
static int hf_fsso_tsagent_port_range_max = -1;
static int hf_fsso_unknown = -1;
static int hf_fsso_unknown_ipv4 = -1;

static dissector_handle_t fortinet_sso_handle;

static int
dissect_fortinet_sso(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    proto_tree *ti;
    proto_tree *fsso_tree;
    guint32 payload_length, client_ip;
    gint string_length = -1;
    const gchar *string;
    gint32 len;
    int offset = 0;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "FSSO");
    col_clear(pinfo->cinfo, COL_INFO);
    col_add_fstr(pinfo->cinfo, COL_INFO, "Fortinet Single Sign-On");

    ti = proto_tree_add_item(tree, proto_fortinet_sso, tvb, 0, -1, ENC_NA);
    fsso_tree = proto_item_add_subtree(ti, ett_fortinet_sso);

    proto_tree_add_item(fsso_tree, hf_fsso_length, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    proto_tree_add_item(fsso_tree, hf_fsso_timestamp, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;

    proto_tree_add_item(fsso_tree, hf_fsso_client_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
    client_ip = tvb_get_ipv4(tvb, offset);
    offset += 4;

    proto_tree_add_item_ret_uint(fsso_tree, hf_fsso_payload_length, tvb, offset, 2, ENC_BIG_ENDIAN, &payload_length);
    offset += 2;

    string = tvb_get_stringz_enc(pinfo->pool, tvb, offset, &string_length, ENC_ASCII);
    proto_tree_add_item(fsso_tree, hf_fsso_string, tvb, offset, string_length, ENC_ASCII);
    col_set_str(pinfo->cinfo, COL_INFO, string);

    if(client_ip == 0xFFFFFFFF) { //if client_ip equal 255.255.255.255 (0xFFFFFFFF) is KeepAlive packet
        /* Domain / KeepAlive (User) / Version */
        len = tvb_find_guint8(tvb, offset, string_length, '/') - offset;
        proto_tree_add_item(fsso_tree, hf_fsso_domain, tvb, offset, len, ENC_ASCII);
        offset += (len + 1);
        string_length -= (len + 1);

        len = tvb_find_guint8(tvb, offset, string_length, '/') - offset;
        proto_tree_add_item(fsso_tree, hf_fsso_user, tvb, offset, len, ENC_ASCII);
        offset += (len + 1);
        string_length -= (len + 1);

        proto_tree_add_item(fsso_tree, hf_fsso_version, tvb, offset, string_length, ENC_ASCII);
        offset += (string_length);

    } else {
        /* Host / Domain / User */
        len = tvb_find_guint8(tvb, offset, string_length, '/') - offset;
        proto_tree_add_item(fsso_tree, hf_fsso_host, tvb, offset, len, ENC_ASCII);
        offset += (len + 1);
        string_length -= (len + 1);

        len = tvb_find_guint8(tvb, offset, string_length, '/') - offset;
        proto_tree_add_item(fsso_tree, hf_fsso_domain, tvb, offset, len, ENC_ASCII);
        offset += (len + 1);
        string_length -= (len + 1);

        proto_tree_add_item(fsso_tree, hf_fsso_user, tvb, offset, string_length, ENC_ASCII);
        offset += (string_length);
    }

    if(tvb_reported_length_remaining(tvb, offset) == 4) {

        /* There is some packet with extra IPv4 address... */
        proto_tree_add_item(fsso_tree, hf_fsso_unknown_ipv4, tvb, offset, 4, ENC_NA);
        offset += 4;

    } else {

        if(tvb_reported_length_remaining(tvb, offset)) {
            guint16 value;
            guint32 number_port_range;
            value = tvb_get_ntohs(tvb, offset);

            if(value == 0x2002) { /* Not a TS Agent additionnal Data */
                proto_tree_add_item(fsso_tree, hf_fsso_unknown, tvb, offset, 2, ENC_NA);
                offset += 2;

                proto_tree_add_item(fsso_tree, hf_fsso_unknown_ipv4, tvb, offset, 4, ENC_NA);
                offset += 4;

                proto_tree_add_item(fsso_tree, hf_fsso_unknown, tvb, offset, 6, ENC_NA);
                offset += 6;

                proto_tree_add_item(fsso_tree, hf_fsso_unknown_ipv4, tvb, offset, 4, ENC_NA);
                offset += 4;

                proto_tree_add_item(fsso_tree, hf_fsso_unknown, tvb, offset, 1, ENC_NA);
                offset += 1;
            } else {
                proto_tree_add_item(fsso_tree, hf_fsso_unknown, tvb, offset, 15, ENC_NA);
                offset += 15;

                proto_tree_add_item(fsso_tree, hf_fsso_unknown, tvb, offset, 5, ENC_NA);
                offset += 5;

                proto_tree_add_item(fsso_tree, hf_fsso_unknown, tvb, offset, 6, ENC_NA);
                offset += 6;

                /* Port Range assigned to user for TS Agent (RDP/Citrix) */
                proto_tree_add_item_ret_uint(fsso_tree, hf_fsso_tsagent_number_port_range, tvb, offset, 2, ENC_BIG_ENDIAN, &number_port_range);
                offset += 2;

                while (number_port_range) {

                    proto_tree_add_item(fsso_tree, hf_fsso_tsagent_port_range_min, tvb, offset, 2, ENC_BIG_ENDIAN);
                    offset += 2;

                    proto_tree_add_item(fsso_tree, hf_fsso_tsagent_port_range_max, tvb, offset, 2, ENC_BIG_ENDIAN);
                    offset += 2;

                    number_port_range --;
                }
            }
        }

    }

    return offset;
}

static gboolean
dissect_fortinet_fsso_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    guint32 length_remaining, length;

    if (tvb_captured_length(tvb) < 2) {
        return FALSE;
    }

    length_remaining = tvb_reported_length_remaining(tvb, 0);
    //first bytes is the length of payload
    length = tvb_get_ntohs(tvb, 0);
    if(length_remaining != length)
    {
        return FALSE;
    }

    //always send with UDP Destination Port 80002
    if(pinfo->destport != UDP_FSSO)
    {
        return FALSE;
    }

    dissect_fortinet_sso(tvb, pinfo, tree, data);
    return TRUE;
}

void
proto_register_fortinet_sso(void)
{
    static hf_register_info hf[] = {
        { &hf_fsso_length,
        { "Length", "fortinet_sso.length", FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_timestamp,
        { "Timestamp", "fortinet_sso.timestamp", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_client_ip,
        { "Client IP", "fortinet_sso.client_ip", FT_IPv4, BASE_NONE, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_payload_length,
        { "Payload Length", "fortinet_sso.payload_length", FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_string,
        { "String", "fortinet_sso.string", FT_STRING, BASE_NONE, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_user,
        { "User", "fortinet_sso.user", FT_STRING, BASE_NONE, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_domain,
        { "Domain", "fortinet_sso.domain", FT_STRING, BASE_NONE, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_host,
        { "Host", "fortinet_sso.host", FT_STRING, BASE_NONE, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_version,
        { "Version", "fortinet_sso.version", FT_STRING, BASE_NONE, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_tsagent_number_port_range,
        { "Number of Port Range", "fortinet_sso.tsagent.port_range.number", FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_tsagent_port_range_min,
        { "Port Range (Min)", "fortinet_sso.tsagent.port_range.min", FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_tsagent_port_range_max,
        { "Port Range (Max)", "fortinet_sso.tsagent.port_range.max", FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},

        { &hf_fsso_unknown,
        { "Unknown", "fortinet_sso.unknown", FT_BYTES, BASE_NONE, NULL, 0x0,
        "Unknown Data...", HFILL}},

        { &hf_fsso_unknown_ipv4,
        { "Unknown IPv4", "fortinet_sso.unknown.ipv4", FT_IPv4, BASE_NONE, NULL, 0x0,
        "Unknown Data...", HFILL}},

    };

    static gint *ett[] = {
        &ett_fortinet_sso,
    };

    proto_fortinet_sso = proto_register_protocol("Fortinet Single Sign On", "fortinet_sso", "fortinet_sso");
    fortinet_sso_handle = register_dissector("fortinet_sso", dissect_fortinet_sso, proto_fortinet_sso);

    proto_register_field_array(proto_fortinet_sso, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}


void
proto_reg_handoff_fortinet_sso(void)
{
    dissector_add_uint_with_preference("udp.port", 0, fortinet_sso_handle);
    heur_dissector_add("udp", dissect_fortinet_fsso_heur, "Fortinet SSO over UDP", "fortinet_sso", proto_fortinet_sso, HEURISTIC_ENABLE);
}

/*
 * 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:
 */