Skip to content

Commit

Permalink
Detect SMTPs w/ STARTTLS as TLS and dissect client/server hello. Fixes
Browse files Browse the repository at this point in the history
…#1630.

 * more packets need to be processed -> bad
 * FTP needs to get updated as well as it has similiar STARTTLS semantics -> follow-up

Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
  • Loading branch information
utoni committed Jul 5, 2022
1 parent 7fa8d88 commit 9e80568
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8058,8 +8058,8 @@ u_int8_t ndpi_extra_dissection_possible(struct ndpi_detection_module_struct *ndp
case NDPI_PROTOCOL_MAIL_IMAP:
case NDPI_PROTOCOL_MAIL_SMTP:
if(flow->l4.tcp.ftp_imap_pop_smtp.password[0] == '\0' &&
flow->l4.tcp.ftp_imap_pop_smtp.auth_tls == 0 &&
flow->l4.tcp.ftp_imap_pop_smtp.auth_done == 0)
(flow->l4.tcp.ftp_imap_pop_smtp.auth_tls == 0 ||
flow->l4.tcp.ftp_imap_pop_smtp.auth_done == 0))
return(1);
break;

Expand Down
47 changes: 38 additions & 9 deletions src/lib/protocols/mail_smtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@

/* #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
Expand Down Expand Up @@ -299,8 +302,8 @@ void ndpi_search_mail_smtp_tcp(struct ndpi_detection_module_struct *ndpi_struct,
}
}

if(packet->line[a].len >= 8) {
if((packet->line[a].ptr[0] == 'S' || packet->line[a].ptr[0] == 's')
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')
Expand All @@ -310,9 +313,9 @@ void ndpi_search_mail_smtp_tcp(struct ndpi_detection_module_struct *ndpi_struct,
&& (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 = 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')
Expand Down Expand Up @@ -381,12 +384,38 @@ void ndpi_search_mail_smtp_tcp(struct ndpi_detection_module_struct *ndpi_struct,
/* **************************************** */

int ndpi_extra_search_mail_smtp_tcp(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) {
int rc;
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;
}

ndpi_search_mail_smtp_tcp(ndpi_struct, flow);
packet->payload = p;
packet->payload_packet_len = l;

rc = (flow->l4.tcp.ftp_imap_pop_smtp.password[0] == '\0') ? 1 : 0;
/* 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);
Expand Down
6 changes: 4 additions & 2 deletions src/lib/protocols/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "ndpi_encryption.h"

extern char *strptime(const char *s, const char *format, struct tm *tm);
extern int processTLSBlock(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow);
extern int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, uint32_t quic_version);
extern int http_process_user_agent(struct ndpi_detection_module_struct *ndpi_struct,
Expand Down Expand Up @@ -839,8 +841,8 @@ int processCertificate(struct ndpi_detection_module_struct *ndpi_struct,

/* **************************************** */

static int processTLSBlock(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) {
int processTLSBlock(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) {
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
int ret;

Expand Down
4 changes: 2 additions & 2 deletions tests/result/ftp-start-tls.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guessed flow protos: 0
Guessed flow protos: 1

DPI Packets (TCP): 10 (10.00 pkts/flow)
DPI Packets (TCP): 51 (51.00 pkts/flow)
Confidence DPI : 1 (flows)

FTP_CONTROL 51 7510 1
Expand Down
4 changes: 2 additions & 2 deletions tests/result/ftp_failed.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guessed flow protos: 0
Guessed flow protos: 1

DPI Packets (TCP): 8 (8.00 pkts/flow)
DPI Packets (TCP): 18 (18.00 pkts/flow)
Confidence DPI : 1 (flows)

FTP_CONTROL 18 1700 1
Expand Down
11 changes: 8 additions & 3 deletions tests/result/smtp-starttls.pcap.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
Guessed flow protos: 0
Guessed flow protos: 1

DPI Packets (TCP): 9 (9.00 pkts/flow)
DPI Packets (TCP): 36 (36.00 pkts/flow)
Confidence DPI : 1 (flows)

Google 36 8403 1

1 TCP 10.0.0.1:57406 <-> 173.194.68.26:25 [proto: 3.126/SMTP.Google][ClearText][Confidence: DPI][cat: Email/3][17 pkts/2514 bytes <-> 19 pkts/5889 bytes][Goodput ratio: 55/79][0.48 sec][Hostname/SNI: mx.google.com][bytes ratio: -0.402 (Download)][IAT c2s/s2c min/avg/max/stddev: 0/0 30/24 156/103 42/26][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 148/310 752/1484 168/444][PLAIN TEXT (x.google.com ESMTP s4)][Plen Bins: 23,18,13,9,4,4,4,0,0,4,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0]
JA3 Host Stats:
IP Address # JA3C
1 10.0.0.1 1


1 TCP 10.0.0.1:57406 <-> 173.194.68.26:25 [proto: 91.126/TLS.Google][Encrypted][Confidence: DPI][cat: Email/3][17 pkts/2514 bytes <-> 19 pkts/5889 bytes][Goodput ratio: 55/79][0.48 sec][Hostname/SNI: mx.google.com][bytes ratio: -0.402 (Download)][IAT c2s/s2c min/avg/max/stddev: 0/0 30/24 156/103 42/26][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 148/310 752/1484 168/444][Risk: ** Obsolete TLS (v1.1 or older) **][Risk Score: 100][Risk Info: No client to server traffic / TLSv1][TLSv1][JA3C: fab507fe132c544e8a0eb7c394affeae][Plen Bins: 23,18,13,9,4,4,4,0,0,4,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0]
2 changes: 1 addition & 1 deletion tests/result/smtp.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guessed flow protos: 0

DPI Packets (TCP): 11 (11.00 pkts/flow)
DPI Packets (TCP): 81 (81.00 pkts/flow)
Confidence DPI : 1 (flows)

SMTP 95 23157 1
Expand Down

0 comments on commit 9e80568

Please sign in to comment.