2 * Routines for imap packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * Copied from packet-tftp.c
11 * SPDX-License-Identifier: GPL-2.0-or-later
17 #include <epan/packet.h>
18 #include <epan/strutil.h>
19 #include "packet-ssl.h"
20 #include "packet-ssl-utils.h"
22 void proto_register_imap(void);
23 void proto_reg_handoff_imap(void);
25 static int proto_imap = -1;
26 static int hf_imap_isrequest = -1;
27 static int hf_imap_line = -1;
28 static int hf_imap_request = -1;
29 static int hf_imap_request_tag = -1;
30 static int hf_imap_response = -1;
31 static int hf_imap_response_tag = -1;
32 static int hf_imap_request_command = -1;
33 static int hf_imap_response_status = -1;
34 static int hf_imap_request_folder = -1;
35 static int hf_imap_request_uid = -1;
37 static gint ett_imap = -1;
38 static gint ett_imap_reqresp = -1;
40 static dissector_handle_t imap_handle;
41 static dissector_handle_t tls_handle;
43 static gboolean imap_ssl_heuristic = TRUE;
45 #define TCP_PORT_IMAP 143
46 #define TCP_PORT_SSL_IMAP 993
47 #define MAX_BUFFER 1024
48 #define IMAP_HEUR_LEN 5
50 typedef struct imap_state {
51 gboolean ssl_requested;
52 gint ssl_heur_tries_left;
55 /* Heuristic to detect plaintext or TLS ciphertext IMAP */
57 check_imap_heur(tvbuff_t *tvb)
62 if (!tvb_bytes_exist(tvb, 0, IMAP_HEUR_LEN)) {
66 s = (const gchar *)tvb_get_ptr(tvb, 0, IMAP_HEUR_LEN);
67 for (i = 0; i < IMAP_HEUR_LEN; i++) {
68 if (!g_ascii_isprint(s[i])) {
76 dissect_imap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
79 proto_tree *imap_tree, *reqresp_tree;
80 proto_item *ti, *hidden_item;
83 gint folder_offset = 0;
85 const guchar *lineend;
86 const guchar *uid_line;
87 const guchar *folder_line;
93 const guchar *next_token;
94 const guchar *uid_next_token;
95 const guchar *folder_next_token;
97 guchar *command_token;
100 conversation_t *conversation;
101 imap_state_t *session_state;
103 conversation = find_or_create_conversation(pinfo);
104 session_state = (imap_state_t *)conversation_get_proto_data(conversation, proto_imap);
105 if (!session_state) {
106 session_state = wmem_new0(wmem_file_scope(), imap_state_t);
107 session_state->ssl_requested = FALSE;
108 if (imap_ssl_heuristic)
109 session_state->ssl_heur_tries_left = 2;
111 session_state->ssl_heur_tries_left = -1; /* Disabled */
112 conversation_add_proto_data(conversation, proto_imap, session_state);
115 if (imap_ssl_heuristic && session_state->ssl_heur_tries_left < 0) {
116 /* Preference changed to enabled */
117 session_state->ssl_heur_tries_left = 2;
119 else if (!imap_ssl_heuristic && session_state->ssl_heur_tries_left >= 0) {
120 /* Preference changed to disabled */
121 session_state->ssl_heur_tries_left = -1;
125 * It is possible the IMAP session is already running over TLS and the
126 * STARTTLS request/response happened before the capture began. Don't assume
127 * we have plaintext without performing some heuristic checks first.
128 * We have three cases:
129 * 1. capture includes STARTTLS command: no need for heuristics
130 * 2. capture starts with STARTTLS OK response: next frame will be TLS (need to retry heuristic)
131 * 3. capture start after STARTTLS negotiation: current frame is TLS
133 if (session_state->ssl_heur_tries_left > 0) {
134 session_state->ssl_heur_tries_left--;
135 if (!check_imap_heur(tvb)) {
136 ssl_starttls_post_ack(tls_handle, pinfo, imap_handle);
137 session_state->ssl_heur_tries_left = 0;
138 return call_dissector(tls_handle, tvb, pinfo, tree);
142 tokenbuf = (guchar *)wmem_alloc0(wmem_packet_scope(), MAX_BUFFER);
143 command_token = (guchar *)wmem_alloc0(wmem_packet_scope(), MAX_BUFFER);
148 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IMAP");
151 if (pinfo->match_uint == pinfo->destport)
157 * Put the first line from the buffer into the summary
158 * (but leave out the line terminator).
160 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
161 line = tvb_get_ptr(tvb, offset, linelen);
163 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s", is_request ? "Request" : "Response", format_text(wmem_packet_scope(), line, linelen));
166 ti = proto_tree_add_item(tree, proto_imap, tvb, offset, -1, ENC_NA);
167 imap_tree = proto_item_add_subtree(ti, ett_imap);
169 hidden_item = proto_tree_add_boolean(imap_tree, hf_imap_isrequest, tvb, 0, 0, is_request);
170 PROTO_ITEM_SET_HIDDEN(hidden_item);
172 while(tvb_offset_exists(tvb, offset)) {
175 * Find the end of each line
177 * Note that "tvb_find_line_end()" will return a value that is
178 * not longer than what's in the buffer, so the "tvb_get_ptr()"
179 * call won't throw an exception.
181 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
182 line = tvb_get_ptr(tvb, offset, linelen);
183 lineend = (line + linelen);
186 * Put the line into the protocol tree.
188 ti = proto_tree_add_item(imap_tree, hf_imap_line, tvb, offset, next_offset - offset, ENC_ASCII|ENC_NA);
190 reqresp_tree = proto_item_add_subtree(ti, ett_imap_reqresp);
193 * Check that the line doesn't begin with '*', because that's a continuation line.
194 * Otherwise if a tag is present then extract tokens.
196 if ( (line) && ((line[0] != '*') || (TRUE == is_request)) ) {
198 * Show each line as tags + requests or replies.
202 * Extract the first token, and, if there is a first
203 * token, add it as the request or reply tag.
205 tokenlen = get_token_len(line, lineend, &next_token);
207 proto_tree_add_item(reqresp_tree, (is_request) ? hf_imap_request_tag : hf_imap_response_tag, tvb, offset, tokenlen, ENC_ASCII|ENC_NA);
209 offset += (gint) (next_token - line);
210 linelen -= (int) (next_token - line);
215 * Extract second token, and, if there is a second
216 * token, and it's not uid, add it as the request or reply command.
218 tokenlen = get_token_len(line, lineend, &next_token);
220 for (iter = 0; iter < tokenlen && iter < MAX_BUFFER-1; iter++) {
221 tokenbuf[iter] = g_ascii_tolower(line[iter]);
223 if (tree && is_request && strncmp(tokenbuf, "uid", tokenlen) == 0) {
224 proto_tree_add_item(reqresp_tree, hf_imap_request_uid, tvb, offset, tokenlen, ENC_ASCII|ENC_NA);
226 * UID is a precursor to a command, if following the tag,
227 * so move to next token to grab the actual command.
230 uid_offset += (gint) (next_token - line);
231 uid_line = next_token;
232 uid_tokenlen = get_token_len(uid_line, lineend, &uid_next_token);
234 proto_tree_add_item(reqresp_tree, hf_imap_request_command, tvb, uid_offset, uid_tokenlen, ENC_ASCII|ENC_NA);
237 * Save command string to do specialized processing.
239 for (iter = 0; iter < uid_tokenlen && iter < MAX_BUFFER-1; iter++) {
240 command_token[iter] = g_ascii_tolower(uid_line[iter]);
242 commandlen = uid_tokenlen;
244 folder_offset = uid_offset;
245 folder_offset += (gint) (uid_next_token - uid_line);
246 folder_line = uid_next_token;
247 folder_tokenlen = get_token_len(folder_line, lineend, &folder_next_token);
251 * Not a UID request so perform normal parsing.
253 proto_tree_add_item(reqresp_tree, (is_request) ? hf_imap_request_command : hf_imap_response_status, tvb, offset, tokenlen, ENC_ASCII|ENC_NA);
257 * Save command string to do specialized processing.
259 for (iter = 0; iter < tokenlen && iter < 256; iter++) {
260 command_token[iter] = g_ascii_tolower(line[iter]);
262 commandlen = tokenlen;
264 folder_offset = offset;
265 folder_offset += (gint) (next_token - line);
266 folder_line = next_token;
267 folder_tokenlen = get_token_len(folder_line, lineend, &folder_next_token);
271 if (tree && commandlen > 0 && (
272 strncmp(command_token, "select", commandlen) == 0 ||
273 strncmp(command_token, "examine", commandlen) == 0 ||
274 strncmp(command_token, "create", commandlen) == 0 ||
275 strncmp(command_token, "delete", commandlen) == 0 ||
276 strncmp(command_token, "rename", commandlen) == 0 ||
277 strncmp(command_token, "subscribe", commandlen) == 0 ||
278 strncmp(command_token, "unsubscribe", commandlen) == 0 ||
279 strncmp(command_token, "status", commandlen) == 0 ||
280 strncmp(command_token, "append", commandlen) == 0 ||
281 strncmp(command_token, "search", commandlen) == 0)) {
283 * These commands support folder as an argument,
284 * so parse out the folder name.
286 if (folder_tokenlen != 0)
287 proto_tree_add_item(reqresp_tree, hf_imap_request_folder, tvb, folder_offset, folder_tokenlen, ENC_ASCII|ENC_NA);
290 if (tree && is_request && (NULL != folder_line) && strncmp(command_token, "copy", commandlen) == 0) {
292 * Handle the copy command separately since folder
293 * is the second argument for this command.
295 folder_offset += (gint) (folder_next_token - folder_line);
296 folder_line = folder_next_token;
297 folder_tokenlen = get_token_len(folder_line, lineend, &folder_next_token);
299 if (folder_tokenlen != 0)
300 proto_tree_add_item(reqresp_tree, hf_imap_request_folder, tvb, folder_offset, folder_tokenlen, ENC_ASCII|ENC_NA);
303 /* If not yet switched to TLS, check for STARTTLS. */
304 if (session_state->ssl_requested) {
305 if (!is_request && strncmp(tokenbuf, "ok", tokenlen) == 0) {
306 /* STARTTLS accepted, next reply will be TLS. */
307 ssl_starttls_ack(tls_handle, pinfo, imap_handle);
308 if (session_state->ssl_heur_tries_left > 0) {
309 session_state->ssl_heur_tries_left = 0;
312 session_state->ssl_requested = FALSE;
314 if (is_request && commandlen > 0 &&
315 strncmp(command_token, "starttls", commandlen) == 0) {
316 /* If next response is OK, then TLS should be commenced. */
317 session_state->ssl_requested = TRUE;
322 * Add the rest of the line as request or reply data.
325 proto_tree_add_item(reqresp_tree, (is_request) ? hf_imap_request : hf_imap_response, tvb, offset, linelen, ENC_ASCII|ENC_NA);
330 offset = next_offset; /* Skip over last line and \r\n at the end of it */
334 return tvb_captured_length(tvb);
338 proto_register_imap(void)
340 static hf_register_info hf[] = {
342 { &hf_imap_isrequest,
343 { "Request", "imap.isrequest",
344 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
345 "TRUE if IMAP request, FALSE otherwise", HFILL }
348 { "Line", "imap.line",
349 FT_STRINGZ, BASE_NONE, NULL, 0x0,
350 "A line of an IMAP message", HFILL }
353 { "Request", "imap.request",
354 FT_STRINGZ, BASE_NONE, NULL, 0x0,
355 "Remainder of request line", HFILL }
357 { &hf_imap_request_tag,
358 { "Request Tag", "imap.request_tag",
359 FT_STRINGZ, BASE_NONE, NULL, 0x0,
360 "First token of request line", HFILL }
363 { "Response", "imap.response",
364 FT_STRINGZ, BASE_NONE, NULL, 0x0,
365 "Remainder of response line", HFILL }
367 { &hf_imap_response_tag,
368 { "Response Tag", "imap.response_tag",
369 FT_STRINGZ, BASE_NONE, NULL, 0x0,
370 "First token of response line", HFILL }
372 { &hf_imap_request_command,
373 { "Request Command", "imap.request.command",
374 FT_STRINGZ, BASE_NONE, NULL, 0x0,
375 "Request command name", HFILL }
377 { &hf_imap_response_status,
378 { "Response Status", "imap.response.status",
379 FT_STRINGZ, BASE_NONE, NULL, 0x0,
380 "Response status code", HFILL }
382 { &hf_imap_request_folder,
383 { "Request Folder", "imap.request.folder",
384 FT_STRINGZ, BASE_NONE, NULL, 0x0,
385 "Request command folder", HFILL }
387 { &hf_imap_request_uid,
388 { "Request isUID", "imap.request.command.uid",
389 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
390 "Request command uid", HFILL }
394 static gint *ett[] = {
399 module_t *imap_module;
401 proto_imap = proto_register_protocol("Internet Message Access Protocol", "IMAP", "imap");
403 imap_handle = register_dissector("imap", dissect_imap, proto_imap);
405 proto_register_field_array(proto_imap, hf, array_length(hf));
406 proto_register_subtree_array(ett, array_length(ett));
408 imap_module = prefs_register_protocol(proto_imap, NULL);
409 prefs_register_bool_preference(imap_module, "ssl_heuristic",
410 "Use heuristic detection for TLS",
411 "Whether to use heuristics for post-STARTTLS detection of encrypted IMAP conversations",
412 &imap_ssl_heuristic);
416 proto_reg_handoff_imap(void)
418 dissector_add_uint_with_preference("tcp.port", TCP_PORT_IMAP, imap_handle);
419 ssl_dissector_add(TCP_PORT_SSL_IMAP, imap_handle);
420 tls_handle = find_dissector("tls");
423 * Editor modelines - http://www.wireshark.org/tools/modelines.html
428 * indent-tabs-mode: nil
431 * vi: set shiftwidth=2 tabstop=8 expandtab:
432 * :indentSize=2:tabSize=8:noTabs=true