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
|
/*
* mail_smtp.c
*
* Copyright (C) 2011-22 - ntop.org
* Copyright (C) 2009-11 - ipoque GmbH
*
* This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH
*
* nDPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nDPI 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nDPI. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ndpi_protocol_ids.h"
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_MAIL_SMTP
#include "ndpi_api.h"
/* #define SMTP_DEBUG 1 */
extern int processTLSBlock(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow);
static void ndpi_int_mail_smtp_add_connection(struct ndpi_detection_module_struct
*ndpi_struct, struct ndpi_flow_struct *flow) {
#ifdef SMTP_DEBUG
printf("**** %s()\n", __FUNCTION__);
#endif
ndpi_set_detected_protocol(ndpi_struct, flow,
NDPI_PROTOCOL_MAIL_SMTP, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
}
/* **************************************** */
static void smtpInitExtraPacketProcessing(struct ndpi_flow_struct *flow);
/* **************************************** */
static void get_credentials_auth_plain(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow,
const u_int8_t *line, u_int16_t line_len)
{
u_int8_t buf[255];
u_char *out;
size_t i, out_len;
unsigned int user_len = 0;
/* AUTH PLAIN XXXXXX */
if(line_len <= 11)
return;
line += 11;
line_len -= 11;
ndpi_user_pwd_payload_copy(buf, sizeof(buf), 0, line, line_len);
out = ndpi_base64_decode(buf, strlen((char *)buf), &out_len);
if(!out)
return;
/* No guarantee that out is null terminated:
UTF8NUL authcid UTF8NUL passwd */
for(i = 1; i < out_len; i++) {
if(out[i] == '\0')
user_len = i - 1;
}
if(user_len > 0) {
char buf[64];
user_len = ndpi_min(user_len, sizeof(flow->l4.tcp.ftp_imap_pop_smtp.username) - 1);
memcpy(flow->l4.tcp.ftp_imap_pop_smtp.username, out + 1, user_len);
flow->l4.tcp.ftp_imap_pop_smtp.username[user_len] = '\0';
snprintf(buf, sizeof(buf), "Found username (%s)",
flow->l4.tcp.ftp_imap_pop_smtp.username);
ndpi_set_risk(ndpi_struct, flow, NDPI_CLEAR_TEXT_CREDENTIALS, buf);
if(1 + user_len + 1 < out_len) {
unsigned int pwd_len;
pwd_len = ndpi_min(out_len - (1 + user_len + 1), sizeof(flow->l4.tcp.ftp_imap_pop_smtp.password) - 1);
memcpy(flow->l4.tcp.ftp_imap_pop_smtp.password, out + 1 + user_len + 1, pwd_len);
flow->l4.tcp.ftp_imap_pop_smtp.password[pwd_len] = '\0';
}
}
ndpi_free(out);
}
/* **************************************** */
void ndpi_search_mail_smtp_tcp(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) {
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
NDPI_LOG_DBG(ndpi_struct, "search mail_smtp\n");
if((packet->payload_packet_len > 2)
&& (packet->parsed_lines < NDPI_MAX_PARSE_LINES_PER_PACKET)
&& (ntohs(get_u_int16_t(packet->payload, packet->payload_packet_len - 2)) == 0x0d0a)
) {
u_int16_t a;
u_int8_t bit_count = 0;
NDPI_PARSE_PACKET_LINE_INFO(ndpi_struct, flow, packet);
for(a = 0; a < packet->parsed_lines; a++) {
// expected server responses
if(packet->line[a].len >= 3) {
if(memcmp(packet->line[a].ptr, "220", 3) == 0) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_220;
if(flow->host_server_name[0] == '\0') {
if(packet->line[a].len > 4) {
int i;
unsigned int len;
if(packet->line[a].ptr[4] != '(') {
for(i=5; (i<packet->line[a].len-1) && (packet->line[a].ptr[i] != ' '); i++)
;
if((packet->line[a].ptr[i+1] != '\r')
&& (packet->line[a].ptr[i+1] != '\n')) {
len = i-4;
/* Copy result for nDPI apps */
ndpi_hostname_sni_set(flow, &packet->line[a].ptr[4], len);
if (ndpi_match_hostname_protocol(ndpi_struct, flow, NDPI_PROTOCOL_MAIL_SMTP,
flow->host_server_name,
strlen(flow->host_server_name))) {
/* We set the protocols; we need to initialize extra dissection
to search for credentials */
NDPI_LOG_DBG(ndpi_struct, "SMTP: hostname matched\n");
smtpInitExtraPacketProcessing(flow);
}
}
}
}
}
} else if(memcmp(packet->line[a].ptr, "250", 3) == 0) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_250;
} else if(memcmp(packet->line[a].ptr, "235", 3) == 0) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_235;
} else if(memcmp(packet->line[a].ptr, "334", 3) == 0) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_334;
} else if(memcmp(packet->line[a].ptr, "354", 3) == 0) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_354;
}
}
// expected client requests
if(packet->line[a].len >= 5) {
if((((packet->line[a].ptr[0] == 'H' || packet->line[a].ptr[0] == 'h')
&& (packet->line[a].ptr[1] == 'E' || packet->line[a].ptr[1] == 'e'))
|| ((packet->line[a].ptr[0] == 'E' || packet->line[a].ptr[0] == 'e')
&& (packet->line[a].ptr[1] == 'H' || packet->line[a].ptr[1] == 'h')))
&& (packet->line[a].ptr[2] == 'L' || packet->line[a].ptr[2] == 'l')
&& (packet->line[a].ptr[3] == 'O' || packet->line[a].ptr[3] == 'o')
&& packet->line[a].ptr[4] == ' ') {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_HELO_EHLO;
flow->l4.tcp.ftp_imap_pop_smtp.auth_found = 0;
} else if((packet->line[a].ptr[0] == 'M' || packet->line[a].ptr[0] == 'm')
&& (packet->line[a].ptr[1] == 'A' || packet->line[a].ptr[1] == 'a')
&& (packet->line[a].ptr[2] == 'I' || packet->line[a].ptr[2] == 'i')
&& (packet->line[a].ptr[3] == 'L' || packet->line[a].ptr[3] == 'l')
&& packet->line[a].ptr[4] == ' ') {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_MAIL;
flow->l4.tcp.ftp_imap_pop_smtp.auth_found = 0;
/* We shouldn't be here if there are credentials */
flow->l4.tcp.ftp_imap_pop_smtp.auth_done = 1;
} else if((packet->line[a].ptr[0] == 'R' || packet->line[a].ptr[0] == 'r')
&& (packet->line[a].ptr[1] == 'C' || packet->line[a].ptr[1] == 'c')
&& (packet->line[a].ptr[2] == 'P' || packet->line[a].ptr[2] == 'p')
&& (packet->line[a].ptr[3] == 'T' || packet->line[a].ptr[3] == 't')
&& packet->line[a].ptr[4] == ' ') {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_RCPT;
flow->l4.tcp.ftp_imap_pop_smtp.auth_found = 0;
/* We shouldn't be here if there are credentials */
flow->l4.tcp.ftp_imap_pop_smtp.auth_done = 1;
} else if((packet->line[a].ptr[0] == 'A' || packet->line[a].ptr[0] == 'a')
&& (packet->line[a].ptr[1] == 'U' || packet->line[a].ptr[1] == 'u')
&& (packet->line[a].ptr[2] == 'T' || packet->line[a].ptr[2] == 't')
&& (packet->line[a].ptr[3] == 'H' || packet->line[a].ptr[3] == 'h')
&& packet->line[a].ptr[4] == ' ') {
#ifdef SMTP_DEBUG
printf("%s() AUTH [%.*s]\n", __FUNCTION__, packet->line[a].len, packet->line[a].ptr);
#endif
flow->l4.tcp.ftp_imap_pop_smtp.auth_found = 1;
if(packet->line[a].len >= 6) {
if(packet->line[a].ptr[5] == 'L' || packet->line[a].ptr[5] == 'l') {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_AUTH_LOGIN;
/* AUTH LOGIN: Username and pwd on the next messages */
} else if(packet->line[a].ptr[5] == 'P' || packet->line[a].ptr[5] == 'p') {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_AUTH_PLAIN;
/* AUTH PLAIN: username and pwd here */
get_credentials_auth_plain(ndpi_struct, flow,
packet->line[a].ptr, packet->line[a].len);
flow->l4.tcp.ftp_imap_pop_smtp.auth_done = 1;
}
}
} else {
if(packet->line[a].ptr[3] != ' ') {
#ifdef SMTP_DEBUG
printf("%s() => [%.*s]\n", __FUNCTION__, packet->line[a].len, packet->line[a].ptr);
#endif
if(flow->l4.tcp.ftp_imap_pop_smtp.auth_found &&
(flow->l4.tcp.smtp_command_bitmask & SMTP_BIT_AUTH_LOGIN)) {
if(flow->l4.tcp.ftp_imap_pop_smtp.username[0] == '\0') {
/* Username */
u_int8_t buf[48];
u_char *out;
size_t out_len;
char msg[64];
ndpi_user_pwd_payload_copy(buf, sizeof(buf), 0,
packet->line[a].ptr, packet->line[a].len);
#ifdef SMTP_DEBUG
printf("%s() => [auth: %u] (username) [%s]\n", __FUNCTION__, flow->l4.tcp.ftp_imap_pop_smtp.auth_found, buf);
#endif
out = ndpi_base64_decode((const u_char*)buf, (size_t)strlen((const char*)buf), &out_len);
if(out) {
size_t len = ndpi_min(out_len, sizeof(flow->l4.tcp.ftp_imap_pop_smtp.username) - 1);
memcpy(flow->l4.tcp.ftp_imap_pop_smtp.username, out, len);
flow->l4.tcp.ftp_imap_pop_smtp.username[len] = '\0';
ndpi_free(out);
}
snprintf(msg, sizeof(msg), "Found SMTP username (%s)",
flow->l4.tcp.ftp_imap_pop_smtp.username);
ndpi_set_risk(ndpi_struct, flow, NDPI_CLEAR_TEXT_CREDENTIALS, msg);
} else if(flow->l4.tcp.ftp_imap_pop_smtp.password[0] == '\0') {
/* Password */
u_int8_t buf[48];
u_char *out;
size_t out_len;
ndpi_user_pwd_payload_copy(buf, sizeof(buf), 0,
packet->line[a].ptr, packet->line[a].len);
#ifdef SMTP_DEBUG
printf("%s() => [auth: %u] (password) [%s]\n", __FUNCTION__, flow->l4.tcp.ftp_imap_pop_smtp.auth_found, buf);
#endif
out = ndpi_base64_decode((const u_char*)buf, (size_t)strlen((const char*)buf), &out_len);
if(out) {
size_t len = ndpi_min(out_len, sizeof(flow->l4.tcp.ftp_imap_pop_smtp.password) - 1);
memcpy(flow->l4.tcp.ftp_imap_pop_smtp.password, out, len);
flow->l4.tcp.ftp_imap_pop_smtp.password[len] = '\0';
ndpi_free(out);
}
ndpi_set_risk(ndpi_struct, flow, NDPI_CLEAR_TEXT_CREDENTIALS, "Found password");
flow->l4.tcp.ftp_imap_pop_smtp.auth_done = 1;
} else {
flow->host_server_name[0] = '\0';
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return;
}
}
}
}
}
if(packet->line[a].len >= 8) {
if((packet->line[a].ptr[0] == 'S' || packet->line[a].ptr[0] == 's')
&& (packet->line[a].ptr[1] == 'T' || packet->line[a].ptr[1] == 't')
&& (packet->line[a].ptr[2] == 'A' || packet->line[a].ptr[2] == 'a')
&& (packet->line[a].ptr[3] == 'R' || packet->line[a].ptr[3] == 'r')
&& (packet->line[a].ptr[4] == 'T' || packet->line[a].ptr[4] == 't')
&& (packet->line[a].ptr[5] == 'T' || packet->line[a].ptr[5] == 't')
&& (packet->line[a].ptr[6] == 'L' || packet->line[a].ptr[6] == 'l')
&& (packet->line[a].ptr[7] == 'S' || packet->line[a].ptr[7] == 's')) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_STARTTLS;
flow->l4.tcp.ftp_imap_pop_smtp.auth_tls = 1;
flow->l4.tcp.ftp_imap_pop_smtp.auth_done = 0;
}
}
if(packet->line[a].len >= 4) {
if((packet->line[a].ptr[0] == 'D' || packet->line[a].ptr[0] == 'd')
&& (packet->line[a].ptr[1] == 'A' || packet->line[a].ptr[1] == 'a')
&& (packet->line[a].ptr[2] == 'T' || packet->line[a].ptr[2] == 't')
&& (packet->line[a].ptr[3] == 'A' || packet->line[a].ptr[3] == 'a')) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_DATA;
} else if((packet->line[a].ptr[0] == 'N' || packet->line[a].ptr[0] == 'n')
&& (packet->line[a].ptr[1] == 'O' || packet->line[a].ptr[1] == 'o')
&& (packet->line[a].ptr[2] == 'O' || packet->line[a].ptr[2] == 'o')
&& (packet->line[a].ptr[3] == 'P' || packet->line[a].ptr[3] == 'p')) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_NOOP;
} else if((packet->line[a].ptr[0] == 'R' || packet->line[a].ptr[0] == 'r')
&& (packet->line[a].ptr[1] == 'S' || packet->line[a].ptr[1] == 's')
&& (packet->line[a].ptr[2] == 'E' || packet->line[a].ptr[2] == 'e')
&& (packet->line[a].ptr[3] == 'T' || packet->line[a].ptr[3] == 't')) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_RSET;
}
}
}
// now count the bits set in the bitmask
if(flow->l4.tcp.smtp_command_bitmask != 0) {
for(a = 0; a < 16; a++) {
bit_count += (flow->l4.tcp.smtp_command_bitmask >> a) & 0x01;
}
}
NDPI_LOG_DBG2(ndpi_struct, "seen smtp commands and responses: %u\n",
bit_count);
if(bit_count >= 3) {
NDPI_LOG_INFO(ndpi_struct, "mail smtp identified\n");
#ifdef SMTP_DEBUG
printf("%s() [bit_count: %u][%s]\n", __FUNCTION__,
bit_count, flow->l4.tcp.ftp_imap_pop_smtp.password);
#endif
/* Only if we don't have already set the protocol via hostname matching */
if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN &&
flow->detected_protocol_stack[1] == NDPI_PROTOCOL_UNKNOWN) {
ndpi_int_mail_smtp_add_connection(ndpi_struct, flow);
smtpInitExtraPacketProcessing(flow);
}
return;
}
if(bit_count >= 1 && flow->packet_counter < 12) {
return;
}
}
/* when the first or second packets are split into two packets, those packets are ignored. */
if(flow->packet_counter <= 4 &&
packet->payload_packet_len >= 4 &&
(ntohs(get_u_int16_t(packet->payload, packet->payload_packet_len - 2)) == 0x0d0a
|| memcmp(packet->payload, "220", 3) == 0 || memcmp(packet->payload, "EHLO", 4) == 0)) {
NDPI_LOG_DBG2(ndpi_struct, "maybe SMTP, need next packet\n");
return;
}
if((!flow->check_extra_packets) || (flow->packet_counter > 12))
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
}
/* **************************************** */
int ndpi_extra_search_mail_smtp_tcp(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow)
{
struct ndpi_packet_struct * const packet = &ndpi_struct->packet;
int rc = 0;
if (flow->l4.tcp.smtp_command_bitmask & SMTP_BIT_STARTTLS &&
packet->payload_packet_len > 5)
{
uint8_t const * const block = &packet->payload[5];
uint8_t const * const p = &packet->payload[0];
uint16_t const block_len = packet->payload_packet_len - 5;
uint16_t const l = packet->payload_packet_len;
packet->payload = block;
packet->payload_packet_len = block_len;
if (processTLSBlock(ndpi_struct, flow) != 0) {
rc = 1;
}
packet->payload = p;
packet->payload_packet_len = l;
/* STARTTLS may be followed by a 220 - Service ready */
if (rc == 0 && memcmp(packet->payload, "220", 3) != 0)
{
flow->l4.tcp.ftp_imap_pop_smtp.auth_done = 1;
}
} else {
ndpi_search_mail_smtp_tcp(ndpi_struct, flow);
rc = (flow->l4.tcp.ftp_imap_pop_smtp.password[0] == '\0') ? 1 : 0;
}
#ifdef SMTP_DEBUG
printf("**** %s() [rc: %d]\n", __FUNCTION__, rc);
#endif
return(rc);
}
/* **************************************** */
static void smtpInitExtraPacketProcessing(struct ndpi_flow_struct *flow) {
#ifdef SMTP_DEBUG
static u_int num = 0;
printf("**** %s(%u)\n", __FUNCTION__, ++num);
#endif
flow->check_extra_packets = 1;
/* At most 12 packets should almost always be enough */
flow->max_extra_packets_to_check = 12;
flow->extra_packets_func = ndpi_extra_search_mail_smtp_tcp;
}
/* **************************************** */
void init_mail_smtp_dissector(struct ndpi_detection_module_struct *ndpi_struct,
u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) {
ndpi_set_bitmask_protocol_detection("MAIL_SMTP", ndpi_struct, detection_bitmask, *id,
NDPI_PROTOCOL_MAIL_SMTP,
ndpi_search_mail_smtp_tcp,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK);
*id += 1;
}
|