Implemented IMAP data extraction

This commit is contained in:
Luca Deri
2019-10-20 22:46:08 +02:00
parent 1a5c7daaf6
commit 437e8bd706
4 changed files with 114 additions and 49 deletions
+8
View File
@@ -949,10 +949,18 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
} }
/* FTP */ /* FTP */
else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_FTP_CONTROL) { else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_FTP_CONTROL) {
if(flow->ndpi_flow->protos.ftp.username[0] != '\0')
snprintf(flow->info, sizeof(flow->info), "User: %s][Pwd: %s", snprintf(flow->info, sizeof(flow->info), "User: %s][Pwd: %s",
flow->ndpi_flow->protos.ftp.username, flow->ndpi_flow->protos.ftp.username,
flow->ndpi_flow->protos.ftp.password); flow->ndpi_flow->protos.ftp.password);
} }
/* IMAP */
else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_MAIL_IMAP) {
if(flow->ndpi_flow->protos.imap.username[0] != '\0')
snprintf(flow->info, sizeof(flow->info), "User: %s][Pwd: %s",
flow->ndpi_flow->protos.imap.username,
flow->ndpi_flow->protos.imap.password);
}
/* KERBEROS */ /* KERBEROS */
else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_KERBEROS) { else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_KERBEROS) {
if(flow->ndpi_flow->protos.kerberos.cname[0] != '\0') { if(flow->ndpi_flow->protos.kerberos.cname[0] != '\0') {
+4
View File
@@ -1238,6 +1238,10 @@ struct ndpi_flow_struct {
char username[16], password[16]; char username[16], password[16];
} ftp; } ftp;
struct {
char username[16], password[16];
} imap;
struct { struct {
/* Bittorrent hash */ /* Bittorrent hash */
u_char hash[20]; u_char hash[20];
+7 -1
View File
@@ -628,7 +628,6 @@ static int ndpi_is_valid_char(char c) {
/* ******************************************************************** */ /* ******************************************************************** */
static int ndpi_find_non_eng_bigrams(struct ndpi_detection_module_struct *ndpi_struct, static int ndpi_find_non_eng_bigrams(struct ndpi_detection_module_struct *ndpi_struct,
char *str) { char *str) {
char s[3]; char s[3];
@@ -881,6 +880,13 @@ int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_serialize_end_of_block(serializer); ndpi_serialize_end_of_block(serializer);
break; break;
case NDPI_PROTOCOL_MAIL_IMAP:
ndpi_serialize_start_of_block(serializer, "imap");
ndpi_serialize_string_string(serializer, "user", flow->protos.imap.username);
ndpi_serialize_string_string(serializer, "password", flow->protos.imap.password);
ndpi_serialize_end_of_block(serializer);
break;
case NDPI_PROTOCOL_FTP_CONTROL: case NDPI_PROTOCOL_FTP_CONTROL:
ndpi_serialize_start_of_block(serializer, "ftp"); ndpi_serialize_start_of_block(serializer, "ftp");
ndpi_serialize_string_string(serializer, "user", flow->protos.ftp.username); ndpi_serialize_string_string(serializer, "user", flow->protos.ftp.username);
+48 -1
View File
@@ -28,6 +28,7 @@
#include "ndpi_api.h" #include "ndpi_api.h"
/* #define IMAP_DEBUG 1*/
static void ndpi_int_mail_imap_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) static void ndpi_int_mail_imap_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
{ {
@@ -45,6 +46,10 @@ void ndpi_search_mail_imap_tcp(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_DBG(ndpi_struct, "search IMAP_IMAP\n"); NDPI_LOG_DBG(ndpi_struct, "search IMAP_IMAP\n");
#ifdef IMAP_DEBUG
printf("%s() [%s]\n", __FUNCTION__, packet->payload);
#endif
if(flow->l4.tcp.mail_imap_starttls == 2) { if(flow->l4.tcp.mail_imap_starttls == 2) {
NDPI_LOG_DBG2(ndpi_struct, "starttls detected\n"); NDPI_LOG_DBG2(ndpi_struct, "starttls detected\n");
NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_MAIL_IMAP); NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_MAIL_IMAP);
@@ -154,6 +159,41 @@ void ndpi_search_mail_imap_tcp(struct ndpi_detection_module_struct *ndpi_struct,
&& (packet->payload[command_start + 2] == 'G' || packet->payload[command_start + 2] == 'g') && (packet->payload[command_start + 2] == 'G' || packet->payload[command_start + 2] == 'g')
&& (packet->payload[command_start + 3] == 'I' || packet->payload[command_start + 3] == 'i') && (packet->payload[command_start + 3] == 'I' || packet->payload[command_start + 3] == 'i')
&& (packet->payload[command_start + 4] == 'N' || packet->payload[command_start + 4] == 'n')) { && (packet->payload[command_start + 4] == 'N' || packet->payload[command_start + 4] == 'n')) {
/* xxxx LOGIN "username" "password" */
char str[256], *item;
u_int len = packet->payload_packet_len > sizeof(str) ? sizeof(str) : packet->payload_packet_len;
strncpy(str, (const char*)packet->payload, len);
str[len] = '\0';
item = strchr(str, '"');
if(item) {
char *column;
item++;
column = strchr(item, '"');
if(column) {
column[0] = '\0';
snprintf(flow->protos.imap.username,
sizeof(flow->protos.imap.username),
"%s", item);
column = strchr(&column[1], '"');
if(column) {
item = &column[1];
column = strchr(item, '"');
if(column) {
column[0] = '\0';
snprintf(flow->protos.imap.password,
sizeof(flow->protos.imap.password),
"%s", item);
}
}
}
}
flow->l4.tcp.mail_imap_stage += 1; flow->l4.tcp.mail_imap_stage += 1;
saw_command = 1; saw_command = 1;
} else if((packet->payload[command_start] == 'F' || packet->payload[command_start] == 'f') } else if((packet->payload[command_start] == 'F' || packet->payload[command_start] == 'f')
@@ -275,9 +315,16 @@ void ndpi_search_mail_imap_tcp(struct ndpi_detection_module_struct *ndpi_struct,
} }
if(saw_command == 1) { if(saw_command == 1) {
if (flow->l4.tcp.mail_imap_stage == 3 || flow->l4.tcp.mail_imap_stage == 5) { if((flow->l4.tcp.mail_imap_stage == 3)
|| (flow->l4.tcp.mail_imap_stage == 5)
|| (flow->l4.tcp.mail_imap_stage == 7)
) {
if((flow->protos.imap.username[0] != '\0')
|| (flow->l4.tcp.mail_imap_stage >= 7)) {
NDPI_LOG_INFO(ndpi_struct, "found MAIL_IMAP\n"); NDPI_LOG_INFO(ndpi_struct, "found MAIL_IMAP\n");
ndpi_int_mail_imap_add_connection(ndpi_struct, flow); ndpi_int_mail_imap_add_connection(ndpi_struct, flow);
}
return; return;
} }
} }