summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-kpasswd.c
blob: da65c1542820598fd30b4423cd004d7bbd04bf24 (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
/* packet-kpasswd.c
 * Routines for kpasswd packet dissection
 *    Ronnie Sahlberg 2003
 *
 * See RFC 3244
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/asn1.h>
#include "packet-tcp.h"
#include "packet-kerberos.h"
#include <epan/prefs.h>

void proto_register_kpasswd(void);
void proto_reg_handoff_kpasswd(void);

static dissector_handle_t kpasswd_handle_udp;
static dissector_handle_t kpasswd_handle_tcp;

/* Desegment Kerberos over TCP messages */
static bool kpasswd_desegment = true;

static int proto_kpasswd;
static int hf_kpasswd_message_len;
static int hf_kpasswd_version;
static int hf_kpasswd_result;
static int hf_kpasswd_result_string;
static int hf_kpasswd_ap_req_len;
static int hf_kpasswd_ap_req_data;
static int hf_kpasswd_krb_priv_message;
static int hf_kpasswd_ChangePasswdData;

static int ett_kpasswd;
static int ett_ap_req_data;
static int ett_krb_priv_message;


#define UDP_PORT_KPASSWD        464
#define TCP_PORT_KPASSWD        464


static const value_string vers_vals[] = {
    { 0x0001, "Reply" },
    { 0xff80, "Request" },
    { 0,      NULL },
};


/** Dissects AP-REQ or AP-REP part of password change. */
static void
dissect_kpasswd_ap_req_data(packet_info *pinfo _U_, tvbuff_t *tvb, proto_tree *parent_tree)
{
    proto_item *it;
    proto_tree *tree;

    it=proto_tree_add_item(parent_tree, hf_kpasswd_ap_req_data, tvb, 0, -1, ENC_NA);
    tree=proto_item_add_subtree(it, ett_ap_req_data);
    dissect_kerberos_main(tvb, pinfo, tree, false, NULL);
}

static int
dissect_kpasswd_user_data_request(packet_info *pinfo, tvbuff_t *tvb, proto_tree *tree)
{
    int offset=0;
    asn1_ctx_t asn1_ctx;
    asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, true, pinfo);

    offset=dissect_kerberos_ChangePasswdData (false, tvb, offset, &asn1_ctx, tree, hf_kpasswd_ChangePasswdData);
    return offset;
}

static kerberos_callbacks cb_req[] = {
    { KRB_CBTAG_PRIV_USER_DATA, dissect_kpasswd_user_data_request },
    { 0, NULL }
};

#define KRB5_KPASSWD_SUCCESS             0
#define KRB5_KPASSWD_MALFORMED           1
#define KRB5_KPASSWD_HARDERROR           2
#define KRB5_KPASSWD_AUTHERROR           3
#define KRB5_KPASSWD_SOFTERROR           4
#define KRB5_KPASSWD_ACCESSDENIED        5
#define KRB5_KPASSWD_BAD_VERSION         6
#define KRB5_KPASSWD_INITIAL_FLAG_NEEDED 7
static const value_string kpasswd_result_types[] = {
    { KRB5_KPASSWD_SUCCESS, "Success" },
    { KRB5_KPASSWD_MALFORMED, "Malformed" },
    { KRB5_KPASSWD_HARDERROR, "HardError" },
    { KRB5_KPASSWD_AUTHERROR, "AuthError" },
    { KRB5_KPASSWD_SOFTERROR, "SoftError" },
    { KRB5_KPASSWD_ACCESSDENIED, "AccessDenied" },
    { KRB5_KPASSWD_BAD_VERSION, "BadVersion" },
    { KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "InitialFlagNeeded" },
    { 0, NULL }
};

static int
dissect_kpasswd_user_data_reply(packet_info *pinfo, tvbuff_t *tvb, proto_tree *tree)
{
    int offset=0;
    uint16_t result;

    /* result */
    result = tvb_get_ntohs(tvb, offset);
    proto_tree_add_uint(tree, hf_kpasswd_result, tvb, offset, 2, result);
    offset+=2;
    col_add_str(pinfo->cinfo, COL_INFO,
        val_to_str(result, kpasswd_result_types, "Result: %u"));


    /* optional result string */
    if(tvb_reported_length_remaining(tvb, offset) > 0){
        proto_tree_add_item(tree, hf_kpasswd_result_string, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_ASCII);
        offset = tvb_reported_length(tvb);
    }

    return offset;
}


static kerberos_callbacks cb_rep[] = {
    { KRB_CBTAG_PRIV_USER_DATA, dissect_kpasswd_user_data_reply },
    { 0, NULL }
};

static int
dissect_kpasswd_krb_priv_message(packet_info *pinfo _U_, tvbuff_t *tvb, proto_tree *parent_tree, bool isrequest)
{
    proto_item *it;
    proto_tree *tree=NULL;
    int offset;

    if(parent_tree){
        it=proto_tree_add_item(parent_tree, hf_kpasswd_krb_priv_message, tvb, 0, -1, ENC_NA);
        tree=proto_item_add_subtree(it, ett_krb_priv_message);
    }
    if(isrequest){
        offset = dissect_kerberos_main(tvb, pinfo, tree, false, cb_req);
    } else {
        offset = dissect_kerberos_main(tvb, pinfo, tree, false, cb_rep);
    }

    /* offset is bytes consumed in child tvb given to us */
    return offset;
}


static int
dissect_kpasswd_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, bool have_rm)
{
    proto_item *kpasswd_item=NULL;
    proto_tree *kpasswd_tree=NULL;
    int offset = 0;
    uint16_t message_len, version, ap_req_len;
    tvbuff_t *next_tvb;

    /* TCP record mark and length */
    uint32_t krb_rm = 0;
    int krb_reclen = 0;
    int krb_rm_size = 0;    /* bytes consumed by record mark: 0 or 4 */

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "KPASSWD");
    col_clear(pinfo->cinfo, COL_INFO);

    /* can't pass have_rm to dissect_kerberos_main, so strip rm first */
    if (have_rm) {
        krb_rm = tvb_get_ntohl(tvb, offset);
        krb_reclen = kerberos_rm_to_reclen(krb_rm);
        krb_rm_size = 4;
        /*
         * What is a reasonable size limit?
         */
        if (krb_reclen > 10 * 1024 * 1024) {
            return (-1);
        }
        offset += krb_rm_size;
    }

    /* it might be a KERBEROS ERROR */
    if(tvb_get_uint8(tvb, offset)==0x7e){
        /* TCP record mark, if any, not displayed.  But hopefully
         * KRB-ERROR dissection will proceed correctly. */
        next_tvb=tvb_new_subset_remaining(tvb, offset);
        return dissect_kerberos_main(next_tvb, pinfo, tree, false, NULL);
    }

    message_len=tvb_get_ntohs(tvb, offset);
    version=tvb_get_ntohs(tvb, offset+2);
    ap_req_len=tvb_get_ntohs(tvb, offset+4);
    if(tree){
        kpasswd_item=proto_tree_add_item(tree, proto_kpasswd, tvb, offset-krb_rm_size, message_len+krb_rm_size, ENC_NA);
        kpasswd_tree=proto_item_add_subtree(kpasswd_item, ett_kpasswd);
        if (have_rm) {
            show_krb_recordmark(kpasswd_tree, tvb, offset-krb_rm_size, krb_rm);
        }
    }

    proto_tree_add_uint(kpasswd_tree, hf_kpasswd_message_len, tvb, offset, 2, message_len);
    proto_tree_add_uint(kpasswd_tree, hf_kpasswd_version, tvb, offset+2, 2, version);
    col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(version, vers_vals, "Unknown command"));
    proto_tree_add_uint(kpasswd_tree, hf_kpasswd_ap_req_len, tvb, offset+4, 2, ap_req_len);
    offset+=6;

    /* AP-REQ / AP-REP data */
    next_tvb=tvb_new_subset_length(tvb, offset, ap_req_len);
    dissect_kpasswd_ap_req_data(pinfo, next_tvb, kpasswd_tree);
    offset+=ap_req_len;

    /* KRB-PRIV message */
    next_tvb=tvb_new_subset_remaining(tvb, offset);
    offset += dissect_kpasswd_krb_priv_message(pinfo, next_tvb, kpasswd_tree, (version==0xff80));

    proto_item_set_len(kpasswd_item, offset);
    return offset;

}

static int
dissect_kpasswd_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    dissect_kpasswd_common(tvb, pinfo, tree, false);
    return tvb_captured_length(tvb);
}

static int
dissect_kpasswd_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    pinfo->fragmented = true;
    if (dissect_kpasswd_common(tvb, pinfo, tree, true) < 0) {
        /*
         * The dissector failed to recognize this as a valid
         * Kerberos message.  Mark it as a continuation packet.
         */
        col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
    }
    return tvb_captured_length(tvb);
}

static int
dissect_kpasswd_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "KPASSWD");
    col_clear(pinfo->cinfo, COL_INFO);

    tcp_dissect_pdus(tvb, pinfo, tree, kpasswd_desegment, 4, get_krb_pdu_len, dissect_kpasswd_tcp_pdu, data);
    return tvb_captured_length(tvb);
}

void
proto_register_kpasswd(void)
{
    static hf_register_info hf[] = {
    { &hf_kpasswd_message_len,
        { "Message Length", "kpasswd.message_len", FT_UINT16, BASE_DEC,
        NULL, 0, NULL, HFILL }},
    { &hf_kpasswd_ap_req_len,
        { "AP_REQ Length", "kpasswd.ap_req_len", FT_UINT16, BASE_DEC,
        NULL, 0, "Length of AP_REQ data", HFILL }},
    { &hf_kpasswd_version,
        { "Version", "kpasswd.version", FT_UINT16, BASE_HEX,
        VALS(vers_vals), 0, NULL, HFILL }},
    { &hf_kpasswd_result,
        { "Result", "kpasswd.result", FT_UINT16, BASE_DEC,
        VALS(kpasswd_result_types), 0, NULL, HFILL }},
    { &hf_kpasswd_result_string,
        { "Result String", "kpasswd.result_string", FT_STRING, BASE_NONE,
        NULL, 0, NULL, HFILL }},
    { &hf_kpasswd_ap_req_data,
        { "AP_REQ", "kpasswd.ap_req", FT_NONE, BASE_NONE,
        NULL, 0, "AP_REQ structure", HFILL }},
    { &hf_kpasswd_krb_priv_message,
        { "KRB-PRIV", "kpasswd.krb_priv", FT_NONE, BASE_NONE,
        NULL, 0, "KRB-PRIV message", HFILL }},
    { &hf_kpasswd_ChangePasswdData, {
        "ChangePasswdData", "kpasswd.ChangePasswdData", FT_NONE, BASE_NONE,
        NULL, 0, "Change Password Data structure", HFILL }},
    };

    static int *ett[] = {
        &ett_kpasswd,
        &ett_ap_req_data,
        &ett_krb_priv_message,
    };
        module_t *kpasswd_module;

    proto_kpasswd = proto_register_protocol("MS Kpasswd", "Kpasswd", "kpasswd");
    proto_register_field_array(proto_kpasswd, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    /* Register preferences */
    kpasswd_module = prefs_register_protocol(proto_kpasswd, NULL);
    prefs_register_bool_preference(kpasswd_module, "desegment",
        "Reassemble Kpasswd over TCP messages spanning multiple TCP segments",
        "Whether the Kpasswd 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.",
        &kpasswd_desegment);

    /* Register dissectors */
    kpasswd_handle_udp = register_dissector("kpasswd.udp", dissect_kpasswd_udp, proto_kpasswd);
    kpasswd_handle_tcp = register_dissector("kpasswd.tcp", dissect_kpasswd_tcp, proto_kpasswd);
}

void
proto_reg_handoff_kpasswd(void)
{
    dissector_add_uint_with_preference("udp.port", UDP_PORT_KPASSWD, kpasswd_handle_udp);
    dissector_add_uint_with_preference("tcp.port", TCP_PORT_KPASSWD, kpasswd_handle_tcp);
}

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