summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-rdp_multitransport.c
blob: 6c6bf645b6d2ec59bbf586fad3b86dbb7f267a29 (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
/* packet-rdpudp.c
 * Routines for RDP multi transport packet dissection
 * Copyright 2021, David Fort
 *
 * 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/prefs.h>
#include <epan/expert.h>
#include <epan/conversation.h>

#include "packet-rdp.h"
#include "packet-rdpudp.h"

#define PNAME  "Remote Desktop Protocol Multi-transport"
#define PSNAME "RDPMT"
#define PFNAME "rdpmt"

void proto_register_rdpmt(void);
void proto_reg_handoff_rdpmt(void);

static dissector_handle_t rdpmt_handle;

static int proto_rdpmt = -1;

static int pf_mt_action = -1;
static int pf_mt_flags = -1;
static int pf_mt_payload_len = -1;
static int pf_mt_header_len = -1;
static int pf_mt_subheader_len = -1;
static int pf_mt_subheader_type = -1;
static int pf_mt_createreq_reqId = -1;
static int pf_mt_createreq_reserved = -1;
static int pf_mt_createreq_cookie = -1;
static int pf_mt_createresp_hrResponse = -1;

static int ett_rdpmt = -1;
static int ett_rdpudp_subheaders = -1;
static int ett_rdpmt_create_req = -1;
static int ett_rdpmt_create_resp = -1;
static int ett_rdpmt_data = -1;

static dissector_handle_t drdynvcDissector;

static const value_string rdpmt_action_vals[] = {
	{ 0x00, "CreateRequest"},
	{ 0x01, "CreateResponse"},
	{ 0x02, "Data"},
	{ 0x00, NULL}
};

static const value_string rdpmt_subheader_type_vals[] = {
	{ 0x0, "auto detect request" },
	{ 0x1, "auto detect response" },
	{ 0x0, NULL}
};

enum {
	RDPMT_TUNNEL_CREATE_REQ = 0,
	RDPMT_TUNNEL_CREATE_RESP = 1,
	RDPMT_TUNNEL_DATA = 2,
};

static int
dissect_rdpmt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void *data _U_)
{
	proto_item *item;
	proto_tree *tree, *subtree;
	guint8 action, subheader_len;
	guint16 payload_len;
	int offset = 0;

	item = proto_tree_add_item(parent_tree, proto_rdpmt, tvb, 0, -1, ENC_NA);
	tree = proto_item_add_subtree(item, ett_rdpmt);

	action = tvb_get_guint8(tvb, offset) & 0x0f;
	proto_tree_add_item(tree, pf_mt_action, tvb, offset, 1, ENC_NA);
	proto_tree_add_item(tree, pf_mt_flags, tvb, offset, 1, ENC_NA);
	offset++;

	payload_len	= tvb_get_guint16(tvb, offset, ENC_LITTLE_ENDIAN);
	proto_tree_add_item(tree, pf_mt_payload_len, tvb, offset, 2, ENC_LITTLE_ENDIAN);
	offset += 2;

	subheader_len = tvb_get_guint8(tvb, offset);
	proto_tree_add_item(tree, pf_mt_header_len, tvb, offset, 1, ENC_NA);
	offset += 1;

	if (subheader_len > 4) {
		tvbuff_t *subheaders = tvb_new_subset_length(tvb,  offset, subheader_len-4);
		proto_tree *subheaders_tree = proto_tree_add_subtree(tree, tvb, offset, -1, ett_rdpudp_subheaders, NULL, "SubHeaders");
		dissect_rdp_bandwidth_req(subheaders, 0, pinfo, subheaders_tree, !!rdp_isServerAddressTarget(pinfo));
	}


	offset += subheader_len - 4;

	switch (action) {
	case RDPMT_TUNNEL_CREATE_REQ: {
		guint8 cookie[16];
		guint32 reqId;
		conversation_t *conv = find_or_create_conversation(pinfo);

		col_set_str(pinfo->cinfo, COL_INFO, "TunnelCreateRequest");

		subtree = proto_tree_add_subtree(tree, tvb, offset, payload_len, ett_rdpmt_create_req, NULL, "TunnelCreateRequest");
		proto_tree_add_item(subtree, pf_mt_createreq_reqId, tvb, offset, 4, ENC_LITTLE_ENDIAN);
		reqId = tvb_get_guint32(tvb, offset, ENC_LITTLE_ENDIAN);
		offset += 4;

		proto_tree_add_item(subtree, pf_mt_createreq_reserved, tvb, offset, 4, ENC_LITTLE_ENDIAN);
		offset += 4;

		proto_tree_add_item(subtree, pf_mt_createreq_cookie, tvb, offset, 16, ENC_NA);
		tvb_memcpy(tvb, cookie, offset, 16);
		offset += 4;

		rdp_transport_set_udp_conversation(&pinfo->dst, pinfo->destport, rdpudp_is_reliable_transport(pinfo), reqId, cookie, conv);
		break;
	}
	case RDPMT_TUNNEL_CREATE_RESP:
		col_set_str(pinfo->cinfo, COL_INFO, "TunnelCreateResponse");
		subtree = proto_tree_add_subtree(tree, tvb, offset, payload_len, ett_rdpmt_create_resp, NULL, "TunnelCreateResponse");
		proto_tree_add_item(subtree, pf_mt_createresp_hrResponse, tvb, offset, 4, ENC_LITTLE_ENDIAN);
		break;

	case RDPMT_TUNNEL_DATA:
		if (payload_len) {
			tvbuff_t *payload = tvb_new_subset_length(tvb, offset, payload_len);
			subtree = proto_tree_add_subtree(tree, tvb, offset, -1, ett_rdpmt_data, NULL, "Data");
			call_dissector(drdynvcDissector, payload, pinfo, subtree);
		}
		break;
	}

	return offset;
}

void
proto_register_rdpmt(void) {
	/* List of fields */
	static hf_register_info hf[] = {

	  { &pf_mt_action,
		{"Action", "rdpmt.action", FT_UINT8, BASE_HEX, VALS(rdpmt_action_vals), 0x0F, NULL, HFILL}
	  },
	  {&pf_mt_flags,
		{"Flags", "rdpmt.flags", FT_UINT8, BASE_HEX, NULL, 0xF0, NULL, HFILL}
	  },
	  {&pf_mt_payload_len,
		{"Payload length", "rdpmt.payloadlen", FT_UINT16, BASE_DEC, NULL, 0, NULL, HFILL}
	  },
	  {&pf_mt_header_len,
		{"Header length", "rdpmt.headerlen", FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL}
	  },
	  {&pf_mt_subheader_len,
		{"Sub header length", "rdpmt.subheaderlen", FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL}
	  },
	  {&pf_mt_subheader_type,
		{"Sub header type", "rdpmt.subheadertype", FT_UINT8, BASE_HEX, VALS(rdpmt_subheader_type_vals), 0, NULL, HFILL}
	  },
	  {&pf_mt_createreq_reqId,
		{"RequestID", "rdpmt.createrequest.requestid", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL}
	  },
	  {&pf_mt_createreq_reserved,
		{"Reserved", "rdpmt.createrequest.reserved", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL}
	  },
	  {&pf_mt_createreq_cookie,
		{"Security cookie", "rdpmt.createrequest.cookie", FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL}
	  },
	  {&pf_mt_createresp_hrResponse,
		{"hrResponse", "rdpmt.createresponse.hrresponse", FT_INT32, BASE_DEC, NULL, 0, NULL, HFILL}
	  }
	};

	/* List of subtrees */
	static gint *ett[] = {
		&ett_rdpmt,
		&ett_rdpudp_subheaders,
		&ett_rdpmt_create_req,
		&ett_rdpmt_create_resp,
		&ett_rdpmt_data
	};

	/* Register protocol */
	proto_rdpmt = proto_register_protocol(PNAME, PSNAME, PFNAME);
	/* Register fields and subtrees */
	proto_register_field_array(proto_rdpmt, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	rdpmt_handle = register_dissector("rdpmt", dissect_rdpmt, proto_rdpmt);
}

static gboolean
rdpmt_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
	guint8 action, header_len;
	guint16 payload_len;

	if (tvb_reported_length(tvb) <= 4)
		return FALSE;

	action = tvb_get_guint8(tvb, 0);
	if (action > 2)
		return FALSE;

	payload_len = tvb_get_guint16(tvb, 1, ENC_LITTLE_ENDIAN);
	header_len = tvb_get_guint8(tvb, 3);

	if ((header_len < 4UL) || (tvb_reported_length_remaining(tvb, header_len) < payload_len))
		return FALSE;

	if (header_len > 4) {
		guint8 subheader_len, subheader_type;

		if(header_len < 6)
			return FALSE;

		subheader_len = tvb_get_guint8(tvb, 4);
		if ((subheader_len < 2) || (subheader_len > header_len-4))
			return FALSE;

		subheader_type = tvb_get_guint8(tvb, 5);
		if (subheader_type > 1) /* AUTODETECT_REQUEST or AUTODETECT_RESPONSE */
			return FALSE;
	}

	return dissect_rdpmt(tvb, pinfo, tree, data) > 0;
}

void
proto_reg_handoff_rdpmt(void)
{
	drdynvcDissector = find_dissector("rdp_drdynvc");

	heur_dissector_add("tls", rdpmt_heur, "RDP MultiTransport", "rdpmt_tls_", proto_rdpmt, TRUE);
	heur_dissector_add("dtls", rdpmt_heur, "RDP MultiTransport", "rdpmt_dtls", proto_rdpmt, TRUE);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */