improved performance by removing linear scan

This commit is contained in:
MrRadix
2020-07-22 10:54:55 +02:00
parent f66cd5aabc
commit 847eb7b180
+36 -78
View File
@@ -25,6 +25,7 @@
#include "ndpi_protocol_ids.h" #include "ndpi_protocol_ids.h"
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_SSH #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_SSH
#define VERSION_CUTOFF 7
#include "ndpi_api.h" #include "ndpi_api.h"
#include "ndpi_md5.h" #include "ndpi_md5.h"
@@ -62,102 +63,59 @@ static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct
/* ************************************************************************ */ /* ************************************************************************ */
static int ssh_has_old_signature(char *signature) { static void ssh_analyze_signature_version(struct ndpi_detection_module_struct *ndpi_struct,
int is_old = 0;
int i = 0;
char *old_versions[46] = {
"OpenSSH_1.2.2",
"OpenSSH_2.5.1",
"OpenSSH_2.9.9",
"OpenSSH_3.0",
"OpenSSH_3.4",
"OpenSSH_3.5",
"OpenSSH_3.6",
"OpenSSH_3.6.1",
"OpenSSH_3.7",
"OpenSSH_3.7.1",
"OpenSSH_3.8",
"OpenSSH_3.9",
"OpenSSH_4.0",
"OpenSSH_4.1",
"OpenSSH_4.2",
"OpenSSH_4.3",
"OpenSSH_4.4",
"OpenSSH_4.5",
"OpenSSH_4.6",
"OpenSSH_4.7",
"OpenSSH_4.9",
"OpenSSH_5.0",
"OpenSSH_5.1",
"OpenSSH_5.2",
"OpenSSH_5.3",
"OpenSSH_5.4",
"OpenSSH_5.5",
"OpenSSH_5.6",
"OpenSSH_5.7",
"OpenSSH_5.8",
"OpenSSH_5.9",
"OpenSSH_6.0",
"OpenSSH_6.1",
"OpenSSH_6.2",
"OpenSSH_6.3",
"OpenSSH_6.4",
"OpenSSH_6.5",
"OpenSSH_6.6",
"OpenSSH_6.7",
"OpenSSH_6.8",
"OpenSSH_6.9",
"OpenSSH_7.0",
"OpenSSH_7.1",
"OpenSSH_7.3",
"OpenSSH_7.4",
"OpenSSH_7.5"
};
while (i < 46 && !is_old) {
if (strstr(old_versions[i], signature) != NULL)
is_old = 1;
i++;
}
return is_old;
}
/* ************************************************************************ */
static void ssh_analyse_signature_version(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
char *str_to_check, char *str_to_check,
u_int8_t is_client_signature) { u_int8_t is_client_signature) {
if (str_to_check == NULL) return; if (str_to_check == NULL) return;
char *copy = (char*)malloc(sizeof(char)*strlen(str_to_check)); char *rem;
char *rest;
char *signature; char *signature;
int obsolete_ssh_version; char *version;
int major_number;
char *copy = (char*)ndpi_malloc(sizeof(char)*(strlen(str_to_check)+1));
int obsolete_ssh_version = 0;
/*
string example: SSH-2.0-OpenSSH_5.3
*/
strcpy(copy, str_to_check); strcpy(copy, str_to_check);
strtok_r(copy, "-", &rest); // SSH /* SSH */
strtok_r(NULL, "-", &rest); // 2.0 strtok_r(copy, "-", &rem);
// OpenSSH_X.X /* 2.0 */
signature = strtok_r(NULL, "-", &rest); strtok_r(NULL, "-", &rem);
if (signature == NULL) return; /* signature = OpenSSH_5.3 */
signature = strtok_r(NULL, "-", &rem);
obsolete_ssh_version = ssh_has_old_signature(signature); /* OpenSSH */
strtok_r(signature, "_", &rem);
/* version = 5.3 */
version = strtok_r(NULL, "_", &rem);
if (version == NULL) return;
/* major_number = 5 */
major_number = atoi(strtok_r(version, ".", &rem));
if (major_number < VERSION_CUTOFF) {
obsolete_ssh_version = 1;
}
#ifdef SSH_DEBUG #ifdef SSH_DEBUG
if(obsolete_ssh_version) if(obsolete_ssh_version)
printf("[SSH] %s: obsolete signature\n", signature); printf("[SSH] Obsolete signature\n");
#endif #endif
if(obsolete_ssh_version) if(obsolete_ssh_version)
NDPI_SET_BIT(flow->risk, is_client_signature ? NDPI_SSH_OBSOLETE_CLIENT_SIGNATURE : NDPI_SSH_OBSOLETE_SERVER_SIGNATURE); NDPI_SET_BIT(flow->risk, is_client_signature ? NDPI_SSH_OBSOLETE_CLIENT_SIGNATURE : NDPI_SSH_OBSOLETE_SERVER_SIGNATURE);
ndpi_free(copy);
} }
/* ************************************************************************ */ /* ************************************************************************ */
@@ -390,7 +348,7 @@ static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct
flow->protos.ssh.client_signature[len] = '\0'; flow->protos.ssh.client_signature[len] = '\0';
ndpi_ssh_zap_cr(flow->protos.ssh.client_signature, len); ndpi_ssh_zap_cr(flow->protos.ssh.client_signature, len);
ssh_analyse_signature_version(ndpi_struct, flow, flow->protos.ssh.client_signature, 1); ssh_analyze_signature_version(ndpi_struct, flow, flow->protos.ssh.client_signature, 1);
#ifdef SSH_DEBUG #ifdef SSH_DEBUG
printf("[SSH] [client_signature: %s]\n", flow->protos.ssh.client_signature); printf("[SSH] [client_signature: %s]\n", flow->protos.ssh.client_signature);
@@ -410,7 +368,7 @@ static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct
flow->protos.ssh.server_signature[len] = '\0'; flow->protos.ssh.server_signature[len] = '\0';
ndpi_ssh_zap_cr(flow->protos.ssh.server_signature, len); ndpi_ssh_zap_cr(flow->protos.ssh.server_signature, len);
ssh_analyse_signature_version(ndpi_struct, flow, flow->protos.ssh.server_signature, 0); ssh_analyze_signature_version(ndpi_struct, flow, flow->protos.ssh.server_signature, 0);
#ifdef SSH_DEBUG #ifdef SSH_DEBUG
printf("[SSH] [server_signature: %s]\n", flow->protos.ssh.server_signature); printf("[SSH] [server_signature: %s]\n", flow->protos.ssh.server_signature);