Add support to opportunistic TLS
A lot of protocols provide the feature to upgrade their plain text
connections to an encrypted one, via some kind of "STARTTLS" command.
Add generic code to support this extension, and allow dissection of the
entire TLS handshake.
As examples, SMTP, POP, IMAP and FTP dissectors have been updated.
Since this feature requires to process more packets per flow, add the
possibility to disable it.
Fix some log messages.
Slight improvement on TCP sequence number tracking.
As a side effect, this commit fix also a memory leak found by
oss-fuzzer
```
==108966==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 22 byte(s) in 1 object(s) allocated from:
#0 0x55f8b367a0be in malloc (/home/ivan/svnrepos/nDPI/fuzz/fuzz_ndpi_reader_with_main+0x5480be) (BuildId: 94debacb4a6784c30420ab748c8bf3cc59621063)
#1 0x55f8b36e1345 in ndpi_malloc_wrapper /home/ivan/svnrepos/nDPI/example/reader_util.c:321:10
#2 0x55f8b379c7d2 in ndpi_malloc /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:212:25
#3 0x55f8b379cb18 in ndpi_strdup /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:279:13
#4 0x55f8b386ce46 in processClientServerHello /home/ivan/svnrepos/nDPI/src/lib/protocols/tls.c:2153:34
#5 0x55f8b385ebf7 in processTLSBlock /home/ivan/svnrepos/nDPI/src/lib/protocols/tls.c:867:5
#6 0x55f8b39e708c in ndpi_extra_search_mail_smtp_tcp /home/ivan/svnrepos/nDPI/src/lib/protocols/mail_smtp.c:422:9
#7 0x55f8b37e636c in ndpi_process_extra_packet /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:5884:9
#8 0x55f8b37edc05 in ndpi_detection_process_packet /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:6276:5
#9 0x55f8b3701ffc in packet_processing /home/ivan/svnrepos/nDPI/example/reader_util.c:1619:31
#10 0x55f8b36faf14 in ndpi_workflow_process_packet /home/ivan/svnrepos/nDPI/example/reader_util.c:2189:10
#11 0x55f8b36b6a50 in LLVMFuzzerTestOneInput /home/ivan/svnrepos/nDPI/fuzz/fuzz_ndpi_reader.c:107:7
```
See: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=50765
This commit is contained in:
@@ -43,12 +43,16 @@
|
||||
#define POP_BIT_STLS 0x0400
|
||||
|
||||
|
||||
extern void switch_extra_dissection_to_tls(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow);
|
||||
|
||||
static void ndpi_int_mail_pop_add_connection(struct ndpi_detection_module_struct
|
||||
*ndpi_struct, struct ndpi_flow_struct *flow) {
|
||||
*ndpi_struct, struct ndpi_flow_struct *flow,
|
||||
u_int16_t protocol) {
|
||||
|
||||
NDPI_LOG_INFO(ndpi_struct, "mail_pop identified\n");
|
||||
flow->guessed_protocol_id = NDPI_PROTOCOL_UNKNOWN; /* Avoid POP3S to be used s sub-protocol */
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_MAIL_POP, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, protocol, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
|
||||
}
|
||||
|
||||
/* **************************************** */
|
||||
@@ -142,6 +146,7 @@ static int ndpi_int_mail_pop_check_for_client_commands(struct ndpi_detection_mod
|
||||
&& (packet->payload[2] == 'L' || packet->payload[2] == 'l')
|
||||
&& (packet->payload[3] == 'S' || packet->payload[3] == 's')) {
|
||||
flow->l4.tcp.pop_command_bitmask |= POP_BIT_STLS;
|
||||
flow->l4.tcp.mail_imap_starttls = 1;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -168,6 +173,19 @@ void ndpi_search_mail_pop_tcp(struct ndpi_detection_module_struct
|
||||
&& (packet->payload[3] == 'R' || packet->payload[3] == 'r')))) {
|
||||
// +OK or -ERR seen
|
||||
flow->l4.tcp.mail_pop_stage += 1;
|
||||
if(packet->payload[0] == '+' && flow->l4.tcp.mail_imap_starttls == 1) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "starttls detected\n");
|
||||
ndpi_int_mail_pop_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_MAIL_POPS);
|
||||
if(ndpi_struct->opportunistic_tls_pop_enabled) {
|
||||
NDPI_LOG_DBG(ndpi_struct, "Switching to [%d/%d]\n",
|
||||
flow->detected_protocol_stack[0], flow->detected_protocol_stack[1]);
|
||||
/* We are done (in POP dissector): delegating TLS... */
|
||||
switch_extra_dissection_to_tls(ndpi_struct, flow);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(packet->payload[0] == '-' && flow->l4.tcp.mail_imap_starttls == 1)
|
||||
flow->l4.tcp.mail_imap_starttls = 0;
|
||||
} else if(!ndpi_int_mail_pop_check_for_client_commands(ndpi_struct, flow)) {
|
||||
goto maybe_split_pop;
|
||||
}
|
||||
@@ -189,7 +207,7 @@ void ndpi_search_mail_pop_tcp(struct ndpi_detection_module_struct
|
||||
|
||||
if((flow->l4.tcp.ftp_imap_pop_smtp.password[0] != '\0')
|
||||
|| (flow->l4.tcp.mail_pop_stage > 3)) {
|
||||
ndpi_int_mail_pop_add_connection(ndpi_struct, flow);
|
||||
ndpi_int_mail_pop_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_MAIL_POP);
|
||||
if(flow->l4.tcp.ftp_imap_pop_smtp.password[0] == '\0')
|
||||
popInitExtraPacketProcessing(flow);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user