SSH code cleanup

This commit is contained in:
Luca Deri
2020-07-25 16:43:54 +02:00
parent b26539d65a
commit 1c405e382a
3 changed files with 80 additions and 83 deletions
+2 -2
View File
@@ -1525,10 +1525,10 @@ const char* ndpi_risk2str(ndpi_risk_enum risk) {
return("Malformed packet"); return("Malformed packet");
case NDPI_SSH_OBSOLETE_CLIENT_VERSION_OR_CIPHER: case NDPI_SSH_OBSOLETE_CLIENT_VERSION_OR_CIPHER:
return("SSH Obsolete client version/cipher"); return("SSH Obsolete Client Version/Cipher");
case NDPI_SSH_OBSOLETE_SERVER_VERSION_OR_CIPHER: case NDPI_SSH_OBSOLETE_SERVER_VERSION_OR_CIPHER:
return("SSH Obsolete server version/cipher"); return("SSH Obsolete Server Version/Cipher");
default: default:
snprintf(buf, sizeof(buf), "%d", (int)risk); snprintf(buf, sizeof(buf), "%d", (int)risk);
+77 -80
View File
@@ -1,8 +1,8 @@
/* /*
* ssh.c * ssh.c
* *
* Copyright (C) 2009-2011 by ipoque GmbH
* Copyright (C) 2011-20 - ntop.org * Copyright (C) 2011-20 - ntop.org
* Copyright (C) 2009-2011 by ipoque GmbH
* *
* This file is part of nDPI, an open source deep packet inspection * This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH * library based on the OpenDPI and PACE technology by ipoque GmbH
@@ -25,9 +25,6 @@
#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 MAJOR_CUTOFF 7
#define MINOR_CUTOFF 0
#define PATCH_CUTOFF 0
#include "ndpi_api.h" #include "ndpi_api.h"
#include "ndpi_md5.h" #include "ndpi_md5.h"
@@ -59,10 +56,15 @@
that usually is packet 14 that usually is packet 14
*/ */
/* #define SSH_DEBUG 1 */ // #define SSH_DEBUG 1
static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
typedef struct {
const char *signature;
u_int16_t major, minor, patch;
} ssh_pattern;
/* ************************************************************************ */ /* ************************************************************************ */
static void ssh_analyze_signature_version(struct ndpi_detection_module_struct *ndpi_struct, static void ssh_analyze_signature_version(struct ndpi_detection_module_struct *ndpi_struct,
@@ -70,64 +72,51 @@ static void ssh_analyze_signature_version(struct ndpi_detection_module_struct *n
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;
int i; u_int i;
int matches; u_int8_t obsolete_ssh_version = 0;
int major = 0; const ssh_pattern ssh_servers_strings[] =
int minor = 0; {
int patch = 0; { (const char*)"SSH-%*f-OpenSSH_%d.%d.%d", 7, 0, 0 }, /* OpenSSH */
u_int8_t version_match = 0; { (const char*)"SSH-%*f-APACHE-SSHD-%d.%d.%d", 2, 5, 1 }, /* Apache MINA SSHD */
u_int8_t obsolete_ssh_version = 0; { (const char*)"SSH-%*f-FileZilla_%d.%d.%d", 3, 40, 0 }, /* FileZilla SSH*/
{ (const char*)"SSH-%*f-paramiko_%d.%d.%d", 2, 4, 0 }, /* Paramiko SSH */
const char *ssh_servers_strings[] = { { (const char*)"SSH-%*f-dropbear_%d.%d", 2020, 0, 0 }, /* Dropbear SSH */
"SSH-%*f-OpenSSH_%d.%d.%d", /* OpenSSH */ { NULL, 0, 0, 0 }
"SSH-%*f-APACHE-SSHD-%d.%d.%d", /* Apache MINA SSHD */ };
"SSH-%*f-FileZilla_%d.%d.%d", /* FileZilla SSH*/
"SSH-%*f-paramiko_%d.%d.%d", /* Paramiko SSH */
"SSH-%*f-dropbear_%d.%d", /* Dropbear SSH */
NULL,
};
int versions_cutoff[][3] = { for(i = 0; ssh_servers_strings[i].signature != NULL; i++) {
/* maj,min,patch */ int matches;
int major = 0;
int minor = 0;
int patch = 0;
matches = sscanf(str_to_check, ssh_servers_strings[i].signature, &major, &minor, &patch);
{7,0,0}, /* OpenSSH */ if(matches == 3 || matches == 2) {
{2,5,1}, /* Apache MINA SSHD */ /* checking if is an old version */
{3,40,0}, /* FileZilla SSH */ if(major < ssh_servers_strings[i].major)
{2,4,0}, /* Paramiko SSH */ obsolete_ssh_version = 1;
{2020,0,0} /* Dropbear SSH (leave patch field as 0)*/ else if(major == ssh_servers_strings[i].major) {
if(minor < ssh_servers_strings[i].minor)
obsolete_ssh_version = 1;
else if(minor == ssh_servers_strings[i].minor)
if(patch < ssh_servers_strings[i].patch)
obsolete_ssh_version = 1;
}
}; #ifdef SSH_DEBUG
printf("[SSH] [SSH Version: %d.%d.%d]\n", major, minor, patch);
for (i = 0; ssh_servers_strings[i]; i++) { #endif
matches = sscanf(str_to_check, ssh_servers_strings[i], &major, &minor, &patch);
if (matches == 3 || matches == 2) {
version_match = 1;
break; break;
} }
} }
if (!version_match) return;
/* checking if is an old version */
if (major < versions_cutoff[i][0]) obsolete_ssh_version = 1;
else if (major == versions_cutoff[i][0]) {
if (minor < versions_cutoff[i][1]) obsolete_ssh_version = 1;
else if (minor == versions_cutoff[i][1])
if (patch < versions_cutoff[i][2]) obsolete_ssh_version = 1;
}
if (obsolete_ssh_version) { if(obsolete_ssh_version)
#ifdef SSH_DEBUG NDPI_SET_BIT(flow->risk,
printf("[SSH] [SSH Version: %d.%d.%d]\n", major, minor, patch); is_client_signature ? NDPI_SSH_OBSOLETE_CLIENT_VERSION_OR_CIPHER :
#endif NDPI_SSH_OBSOLETE_SERVER_VERSION_OR_CIPHER);
NDPI_SET_BIT(flow->risk, (is_client_signature ? NDPI_SSH_OBSOLETE_CLIENT_VERSION_OR_CIPHER : NDPI_SSH_OBSOLETE_SERVER_VERSION_OR_CIPHER));
}
} }
/* ************************************************************************ */ /* ************************************************************************ */
@@ -140,32 +129,44 @@ static void ssh_analyse_cipher(struct ndpi_detection_module_struct *ndpi_struct,
char *rem; char *rem;
char *cipher; char *cipher;
u_int8_t found_obsolete_cipher = 0; u_int8_t found_obsolete_cipher = 0;
char *cipher_copy;
/*
List of obsolete ciphers can be found at
https://www.linuxminion.com/deprecated-ssh-cryptographic-settings/
*/
const char *obsolete_ciphers[] = { const char *obsolete_ciphers[] = {
"arcfour256", "arcfour256",
"arcfour128", "arcfour128",
"3des-cbc", "3des-cbc",
"blowfish-cbc", "blowfish-cbc",
"cast128-cbc", "cast128-cbc",
"arcfour", "arcfour",
NULL, NULL,
}; };
char *copy = (char*)ndpi_calloc(cipher_len, sizeof(char)); if((cipher_copy = (char*)ndpi_malloc(cipher_len+1)) == NULL) {
if (copy == NULL) { #ifdef SSH_DEBUG
printf("[SSH] Nout enough memory\n");
#endif
return; return;
} }
if (strncpy(copy, ciphers, cipher_len) == NULL) strncpy(cipher_copy, ciphers, cipher_len);
return; cipher_copy[cipher_len] = '\0';
cipher = strtok_r(copy, ",", &rem); cipher = strtok_r(cipher_copy, ",", &rem);
while (cipher && !found_obsolete_cipher) { while(cipher && !found_obsolete_cipher) {
u_int i;
for (int i = 0; obsolete_ciphers[i]; i++) {
if (strcmp(cipher, obsolete_ciphers[i]) == 0) { for(i = 0; obsolete_ciphers[i]; i++) {
if(strcmp(cipher, obsolete_ciphers[i]) == 0) {
found_obsolete_cipher = 1; found_obsolete_cipher = 1;
#ifdef SSH_DEBUG
printf("[SSH] [SSH obsolete %s cipher][%s]\n",
is_client_signature ? "client" : "server",
obsolete_ciphers[i]);
#endif
break; break;
} }
} }
@@ -173,15 +174,11 @@ static void ssh_analyse_cipher(struct ndpi_detection_module_struct *ndpi_struct,
cipher = strtok_r(NULL, ",", &rem); cipher = strtok_r(NULL, ",", &rem);
} }
if (found_obsolete_cipher) { if(found_obsolete_cipher) {
#ifdef SSH_DEBUG
printf("[SSH] [SSH obsolete cipher]\n");
#endif
NDPI_SET_BIT(flow->risk, (is_client_signature ? NDPI_SSH_OBSOLETE_CLIENT_VERSION_OR_CIPHER : NDPI_SSH_OBSOLETE_SERVER_VERSION_OR_CIPHER)); NDPI_SET_BIT(flow->risk, (is_client_signature ? NDPI_SSH_OBSOLETE_CLIENT_VERSION_OR_CIPHER : NDPI_SSH_OBSOLETE_SERVER_VERSION_OR_CIPHER));
} }
ndpi_free(copy); ndpi_free(cipher_copy);
} }
/* ************************************************************************ */ /* ************************************************************************ */
@@ -411,7 +408,7 @@ static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct
if(flow->l4.tcp.ssh_stage == 0) { if(flow->l4.tcp.ssh_stage == 0) {
if(packet->payload_packet_len > 7 && packet->payload_packet_len < 100 if(packet->payload_packet_len > 7 && packet->payload_packet_len < 100
&& memcmp(packet->payload, "SSH-", 4) == 0) { && memcmp(packet->payload, "SSH-", 4) == 0) {
int len = ndpi_min(sizeof(flow->protos.ssh.client_signature)-1, packet->payload_packet_len); int len = ndpi_min(sizeof(flow->protos.ssh.client_signature)-1, packet->payload_packet_len);
strncpy(flow->protos.ssh.client_signature, (const char *)packet->payload, len); strncpy(flow->protos.ssh.client_signature, (const char *)packet->payload, len);
@@ -431,7 +428,7 @@ static void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct
} }
} else if(flow->l4.tcp.ssh_stage == (2 - packet->packet_direction)) { } else if(flow->l4.tcp.ssh_stage == (2 - packet->packet_direction)) {
if(packet->payload_packet_len > 7 && packet->payload_packet_len < 500 if(packet->payload_packet_len > 7 && packet->payload_packet_len < 500
&& memcmp(packet->payload, "SSH-", 4) == 0) { && memcmp(packet->payload, "SSH-", 4) == 0) {
int len = ndpi_min(sizeof(flow->protos.ssh.server_signature)-1, packet->payload_packet_len); int len = ndpi_min(sizeof(flow->protos.ssh.server_signature)-1, packet->payload_packet_len);
strncpy(flow->protos.ssh.server_signature, (const char *)packet->payload, len); strncpy(flow->protos.ssh.server_signature, (const char *)packet->payload, len);
+1 -1
View File
@@ -1,3 +1,3 @@
SSH 258 35546 1 SSH 258 35546 1
1 TCP 172.16.238.1:58395 <-> 172.16.238.168:22 [proto: 92/SSH][cat: RemoteAccess/12][159 pkts/15615 bytes <-> 99 pkts/19931 bytes][Goodput ratio: 33/67][248.48 sec][bytes ratio: -0.121 (Mixed)][IAT c2s/s2c min/avg/max/stddev: 0/0 1846/2934 166223/166224 14794/19692][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 98/201 970/1346 83/283][Client: SSH-2.0-OpenSSH_5.3][HASSH-C: 21B457A327CE7A2D4FCE5EF2C42400BD][Server: SSH-2.0-OpenSSH_5.6][HASSH-S: B1C6C0D56317555B85C7005A3DE29325][Plen Bins: 2,76,12,2,3,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0] 1 TCP 172.16.238.1:58395 <-> 172.16.238.168:22 [proto: 92/SSH][cat: RemoteAccess/12][159 pkts/15615 bytes <-> 99 pkts/19931 bytes][Goodput ratio: 33/67][248.48 sec][bytes ratio: -0.121 (Mixed)][IAT c2s/s2c min/avg/max/stddev: 0/0 1846/2934 166223/166224 14794/19692][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 98/201 970/1346 83/283][Risk: ** SSH Obsolete Client Version/Cipher **** SSH Obsolete Server Version/Cipher **][Client: SSH-2.0-OpenSSH_5.3][HASSH-C: 21B457A327CE7A2D4FCE5EF2C42400BD][Server: SSH-2.0-OpenSSH_5.6][HASSH-S: B1C6C0D56317555B85C7005A3DE29325][Plen Bins: 2,76,12,2,3,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0]