Added SMBv1 over NetBIOS detection

This commit is contained in:
Luca Deri
2020-03-01 11:32:09 +01:00
parent 34ad06fef5
commit 7fd135a8c9
+39 -22
View File
@@ -1,8 +1,8 @@
/* /*
* netbios.c * netbios.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
@@ -29,11 +29,14 @@
#include "ndpi_api.h" #include "ndpi_api.h"
/* ****************************************************************** */
struct netbios_header { struct netbios_header {
u_int16_t transaction_id, flags, questions, answer_rrs, authority_rrs, additional_rrs; u_int16_t transaction_id, flags, questions, answer_rrs, authority_rrs, additional_rrs;
}; };
/* ****************************************************************** */
/* The function below has been inherited by tcpdump */ /* The function below has been inherited by tcpdump */
int ndpi_netbios_name_interpret(char *in, char *out, u_int out_len) { int ndpi_netbios_name_interpret(char *in, char *out, u_int out_len) {
int ret = 0, len; int ret = 0, len;
@@ -69,18 +72,24 @@ int ndpi_netbios_name_interpret(char *in, char *out, u_int out_len) {
return(ret); return(ret);
} }
/* ****************************************************************** */
static void ndpi_int_netbios_add_connection(struct ndpi_detection_module_struct static void ndpi_int_netbios_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
*ndpi_struct, struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow,
u_int16_t sub_protocol) {
char name[64]; char name[64];
u_int off = flow->packet.payload[12] == 0x20 ? 12 : 14; u_int off = flow->packet.payload[12] == 0x20 ? 12 : 14;
if(ndpi_netbios_name_interpret((char*)&flow->packet.payload[off], name, sizeof(name)) > 0) if(ndpi_netbios_name_interpret((char*)&flow->packet.payload[off], name, sizeof(name)) > 0)
snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name); snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name);
if(sub_protocol == NDPI_PROTOCOL_UNKNOWN)
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_NETBIOS, NDPI_PROTOCOL_UNKNOWN); ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_NETBIOS, NDPI_PROTOCOL_UNKNOWN);
else
ndpi_set_detected_protocol(ndpi_struct, flow, sub_protocol, NDPI_PROTOCOL_NETBIOS);
} }
/* ****************************************************************** */
void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct, void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow) {
@@ -88,6 +97,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
u_int16_t dport; u_int16_t dport;
NDPI_LOG_DBG(ndpi_struct, "search netbios\n"); NDPI_LOG_DBG(ndpi_struct, "search netbios\n");
if(packet->udp != NULL) { if(packet->udp != NULL) {
dport = ntohs(packet->udp->dest); dport = ntohs(packet->udp->dest);
@@ -109,7 +119,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 \n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -120,7 +130,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 and broadcast \n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 and broadcast \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -131,7 +141,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers, authority, additional = 0 \n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers, authority, additional = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -142,7 +152,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 \n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -154,7 +164,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios with flag 8400 questions = 0 and answers = 1, authority, additional = 0 \n"); "found netbios with flag 8400 questions = 0 and answers = 1, authority, additional = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -166,7 +176,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios with flag 8500 questions = 0 and answers = 1, authority, additional = 0 \n"); "found netbios with flag 8500 questions = 0 and answers = 1, authority, additional = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -178,7 +188,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios with flag 2910, questions = 1 and answers, authority=0, additional = 1 \n"); "found netbios with flag 2910, questions = 1 and answers, authority=0, additional = 1 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -190,7 +200,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios with flag ad86 questions = 0 and answers = 1, authority, additional = 0 \n"); "found netbios with flag ad86 questions = 0 and answers = 1, authority, additional = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -202,7 +212,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios with flag 0110 questions = 1 and answers = 0, authority, additional = 0 \n"); "found netbios with flag 0110 questions = 1 and answers = 0, authority, additional = 0 \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
@@ -231,7 +241,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios name query request\n"); "found netbios name query request\n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
} }
@@ -262,7 +272,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios name query response\n"); "found netbios name query response\n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
} }
@@ -288,7 +298,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios name query response\n"); "found netbios name query response\n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
} }
@@ -314,7 +324,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios name query response\n"); "found netbios name query response\n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
} }
@@ -327,24 +337,29 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
/* netbios header token from http://www.protocolbase.net/protocols/protocol_NBDGM.php */ /* netbios header token from http://www.protocolbase.net/protocols/protocol_NBDGM.php */
if((dport == 138) && if((dport == 138) && (packet->payload_packet_len >= 14)) {
packet->payload_packet_len >= 14 && u_int16_t netbios_len = ntohs(get_u_int16_t(packet->payload, 10));
ntohs(get_u_int16_t(packet->payload, 10)) == packet->payload_packet_len - 14) {
if(netbios_len == packet->payload_packet_len - 14) {
NDPI_LOG_DBG2(ndpi_struct, "found netbios port 138 and payload length >= 112 \n"); NDPI_LOG_DBG2(ndpi_struct, "found netbios port 138 and payload length >= 112 \n");
if(packet->payload[0] >= 0x10 && packet->payload[0] <= 0x16) { if(packet->payload[0] >= 0x10 && packet->payload[0] <= 0x16) {
u_int32_t source_ip = ntohl(get_u_int32_t(packet->payload, 4));
NDPI_LOG_DBG2(ndpi_struct, "found netbios with MSG-type 0x10,0x11,0x12,0x13,0x14,0x15 or 0x16\n"); NDPI_LOG_DBG2(ndpi_struct, "found netbios with MSG-type 0x10,0x11,0x12,0x13,0x14,0x15 or 0x16\n");
if(ntohl(get_u_int32_t(packet->payload, 4)) == ntohl(packet->iph->saddr)) { if(source_ip == ntohl(packet->iph->saddr)) {
int16_t leftover = netbios_len - 82; /* NetBIOS len */
NDPI_LOG_INFO(ndpi_struct, "found netbios with checked ip-address\n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with checked ip-address\n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, (leftover > 0) ? NDPI_PROTOCOL_SMBV1 : NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
} }
} }
} }
}
if(packet->tcp != NULL) { if(packet->tcp != NULL) {
dport = ntohs(packet->tcp->dest); dport = ntohs(packet->tcp->dest);
@@ -361,7 +376,7 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_LOG_INFO(ndpi_struct, NDPI_LOG_INFO(ndpi_struct,
"found netbios with session request = 81, flags=0 and length od following bytes = 68. \n"); "found netbios with session request = 81, flags=0 and length od following bytes = 68. \n");
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN);
return; return;
} }
} }
@@ -372,6 +387,8 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct,
NDPI_EXCLUDE_PROTO(ndpi_struct, flow); NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
} }
/* ****************************************************************** */
void init_netbios_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) void init_netbios_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask)
{ {
ndpi_set_bitmask_protocol_detection("NETBIOS", ndpi_struct, detection_bitmask, *id, ndpi_set_bitmask_protocol_detection("NETBIOS", ndpi_struct, detection_bitmask, *id,