Add more length checks in HASSH
This to prevent possible crashes on invalid packets
This commit is contained in:
+106
-67
@@ -64,93 +64,132 @@ static u_int16_t concat_hash_string(struct ndpi_packet_struct *packet,
|
|||||||
char *buf, u_int8_t client_hash) {
|
char *buf, u_int8_t client_hash) {
|
||||||
u_int16_t offset = 22, buf_out_len = 0;
|
u_int16_t offset = 22, buf_out_len = 0;
|
||||||
u_int32_t len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
u_int32_t len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
if(len < (packet->payload_packet_len-offset)) {
|
/* -1 for ';' */
|
||||||
/* ssh.kex_algorithms [C/S] */
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
|
goto invalid_payload;
|
||||||
|
|
||||||
|
/* ssh.kex_algorithms [C/S] */
|
||||||
|
strncpy(buf, (const char *)&packet->payload[offset], buf_out_len = len);
|
||||||
|
buf[buf_out_len++] = ';';
|
||||||
|
offset += len;
|
||||||
|
|
||||||
|
/* ssh.server_host_key_algorithms [None] */
|
||||||
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
offset += 4 + len;
|
||||||
|
|
||||||
|
/* ssh.encryption_algorithms_client_to_server [C] */
|
||||||
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
|
||||||
|
if(client_hash) {
|
||||||
offset += 4;
|
offset += 4;
|
||||||
strncpy(buf, (const char *)&packet->payload[offset], buf_out_len = len);
|
|
||||||
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
|
goto invalid_payload;
|
||||||
|
|
||||||
|
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
||||||
|
buf_out_len += len;
|
||||||
buf[buf_out_len++] = ';';
|
buf[buf_out_len++] = ';';
|
||||||
offset += len;
|
offset += len;
|
||||||
|
} else
|
||||||
/* ssh.server_host_key_algorithms [None] */
|
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
|
||||||
offset += 4 + len;
|
offset += 4 + len;
|
||||||
|
|
||||||
/* ssh.encryption_algorithms_client_to_server [C] */
|
/* ssh.encryption_algorithms_server_to_client [S] */
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
|
||||||
if(client_hash) {
|
if(!client_hash) {
|
||||||
offset += 4;
|
offset += 4;
|
||||||
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
|
||||||
buf_out_len += len;
|
|
||||||
buf[buf_out_len++] = ';';
|
|
||||||
offset += len;
|
|
||||||
} else
|
|
||||||
offset += 4 + len;
|
|
||||||
|
|
||||||
/* ssh.encryption_algorithms_server_to_client [S] */
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
goto invalid_payload;
|
||||||
if(!client_hash) {
|
|
||||||
offset += 4;
|
|
||||||
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
|
||||||
buf_out_len += len;
|
|
||||||
buf[buf_out_len++] = ';';
|
|
||||||
offset += len;
|
|
||||||
} else
|
|
||||||
offset += 4 + len;
|
|
||||||
|
|
||||||
/* ssh.mac_algorithms_client_to_server [C] */
|
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
buf_out_len += len;
|
||||||
if(client_hash) {
|
buf[buf_out_len++] = ';';
|
||||||
offset += 4;
|
offset += len;
|
||||||
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
} else
|
||||||
buf_out_len += len;
|
offset += 4 + len;
|
||||||
buf[buf_out_len++] = ';';
|
|
||||||
offset += len;
|
|
||||||
} else
|
|
||||||
offset += 4 + len;
|
|
||||||
|
|
||||||
/* ssh.mac_algorithms_server_to_client [S] */
|
/* ssh.mac_algorithms_client_to_server [C] */
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
if(!client_hash) {
|
|
||||||
offset += 4;
|
|
||||||
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
|
||||||
buf_out_len += len;
|
|
||||||
buf[buf_out_len++] = ';';
|
|
||||||
offset += len;
|
|
||||||
} else
|
|
||||||
offset += 4 + len;
|
|
||||||
|
|
||||||
/* ssh.compression_algorithms_client_to_server [C] */
|
if(client_hash) {
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
offset += 4;
|
||||||
if(client_hash) {
|
|
||||||
offset += 4;
|
|
||||||
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
|
||||||
buf_out_len += len;
|
|
||||||
offset += len;
|
|
||||||
} else
|
|
||||||
offset += 4 + len;
|
|
||||||
|
|
||||||
/* ssh.compression_algorithms_server_to_client [S] */
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
goto invalid_payload;
|
||||||
if(!client_hash) {
|
|
||||||
offset += 4;
|
|
||||||
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
|
||||||
buf_out_len += len;
|
|
||||||
offset += len;
|
|
||||||
} else
|
|
||||||
offset += 4 + len;
|
|
||||||
|
|
||||||
/* ssh.languages_client_to_server [None] */
|
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
||||||
|
buf_out_len += len;
|
||||||
|
buf[buf_out_len++] = ';';
|
||||||
|
offset += len;
|
||||||
|
} else
|
||||||
|
offset += 4 + len;
|
||||||
|
|
||||||
/* ssh.languages_server_to_client [None] */
|
/* ssh.mac_algorithms_server_to_client [S] */
|
||||||
}
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
|
||||||
|
if(!client_hash) {
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
|
goto invalid_payload;
|
||||||
|
|
||||||
|
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
||||||
|
buf_out_len += len;
|
||||||
|
buf[buf_out_len++] = ';';
|
||||||
|
offset += len;
|
||||||
|
} else
|
||||||
|
offset += 4 + len;
|
||||||
|
|
||||||
|
/* ssh.compression_algorithms_client_to_server [C] */
|
||||||
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
|
||||||
|
if(client_hash) {
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
|
goto invalid_payload;
|
||||||
|
|
||||||
|
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
||||||
|
buf_out_len += len;
|
||||||
|
offset += len;
|
||||||
|
} else
|
||||||
|
offset += 4 + len;
|
||||||
|
|
||||||
|
/* ssh.compression_algorithms_server_to_client [S] */
|
||||||
|
len = ntohl(*(u_int32_t*)&packet->payload[offset]);
|
||||||
|
|
||||||
|
if(!client_hash) {
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
|
if((offset >= packet->payload_packet_len) || (len >= packet->payload_packet_len-offset-1))
|
||||||
|
goto invalid_payload;
|
||||||
|
|
||||||
|
strncpy(&buf[buf_out_len], (const char *)&packet->payload[offset], len);
|
||||||
|
buf_out_len += len;
|
||||||
|
offset += len;
|
||||||
|
} else
|
||||||
|
offset += 4 + len;
|
||||||
|
|
||||||
|
/* ssh.languages_client_to_server [None] */
|
||||||
|
|
||||||
|
/* ssh.languages_server_to_client [None] */
|
||||||
|
|
||||||
#ifdef SSH_DEBUG
|
#ifdef SSH_DEBUG
|
||||||
printf("\n[SSH] %s\n", buf);
|
printf("\n[SSH] %s\n", buf);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return(buf_out_len);
|
return(buf_out_len);
|
||||||
|
|
||||||
|
invalid_payload:
|
||||||
|
|
||||||
|
#ifdef SSH_DEBUG
|
||||||
|
printf("\n[SSH] Invalid packet payload\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|||||||
Reference in New Issue
Block a user