Improve BT-uTP protocol dissection

This commit is contained in:
Luca Deri
2016-02-25 09:58:23 +01:00
parent e025d696f6
commit 80a35226c5
+9 -2
View File
@@ -378,6 +378,7 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
{ {
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
int no_bittorrent = 0; int no_bittorrent = 0;
char *bt_proto = NULL;
/* This is broadcast */ /* This is broadcast */
if(packet->iph if(packet->iph
@@ -412,7 +413,7 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
if(packet->payload_packet_len >= 23 /* min header size */) { if(packet->payload_packet_len >= 23 /* min header size */) {
if(strncmp((const char*)packet->payload, bt_search, strlen(bt_search)) == 0) { if(strncmp((const char*)packet->payload, bt_search, strlen(bt_search)) == 0) {
ndpi_add_connection_as_bittorrent(ndpi_struct, flow, -1, ndpi_add_connection_as_bittorrent(ndpi_struct, flow, -1,
NDPI_PROTOCOL_SAFE_DETECTION, NDPI_PROTOCOL_PLAIN_DETECTION/* , */ NDPI_PROTOCOL_SAFE_DETECTION, NDPI_PROTOCOL_PLAIN_DETECTION /* , */
/* NDPI_REAL_PROTOCOL */); /* NDPI_REAL_PROTOCOL */);
return; return;
} else { } else {
@@ -431,12 +432,14 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
&& (packet->payload[3]== 0x0) && (packet->payload[3]== 0x0)
&& (packet->payload[4]== 0x0)) { && (packet->payload[4]== 0x0)) {
/* Heuristic */ /* Heuristic */
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], "BitTorrent protocol", packet->payload_packet_len-20);
goto bittorrent_found; goto bittorrent_found;
} else if(((v1_version & 0x0f) == 1) } else if(((v1_version & 0x0f) == 1)
&& ((v1_version >> 4) < 5 /* ST_NUM_STATES */) && ((v1_version >> 4) < 5 /* ST_NUM_STATES */)
&& (v1_extension < 3 /* EXT_NUM_EXT */) && (v1_extension < 3 /* EXT_NUM_EXT */)
&& (v1_window_size < 32768 /* 32k */) && (v1_window_size < 32768 /* 32k */)
) { ) {
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], "BitTorrent protocol", packet->payload_packet_len-20);
goto bittorrent_found; goto bittorrent_found;
} else if((v0_flags < 6 /* ST_NUM_STATES */) } else if((v0_flags < 6 /* ST_NUM_STATES */)
&& (v0_extension < 3 /* EXT_NUM_EXT */)) { && (v0_extension < 3 /* EXT_NUM_EXT */)) {
@@ -446,6 +449,7 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
now = (u_int32_t)time(NULL); now = (u_int32_t)time(NULL);
if((ts < (now+86400)) && (ts > (now-86400))) { if((ts < (now+86400)) && (ts > (now-86400))) {
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], "BitTorrent protocol", packet->payload_packet_len-20);
goto bittorrent_found; goto bittorrent_found;
} }
} }
@@ -462,9 +466,12 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
|| ndpi_strnstr((const char *)packet->payload, ":info_hash20:", packet->payload_packet_len) || ndpi_strnstr((const char *)packet->payload, ":info_hash20:", packet->payload_packet_len)
|| ndpi_strnstr((const char *)packet->payload, ":filter64", packet->payload_packet_len) || ndpi_strnstr((const char *)packet->payload, ":filter64", packet->payload_packet_len)
|| ndpi_strnstr((const char *)packet->payload, "d1:rd2:id20:", packet->payload_packet_len) || ndpi_strnstr((const char *)packet->payload, "d1:rd2:id20:", packet->payload_packet_len)
|| ndpi_strnstr((const char *)packet->payload, "BitTorrent protocol", packet->payload_packet_len) || (bt_proto = ndpi_strnstr((const char *)packet->payload, "BitTorrent protocol", packet->payload_packet_len))
) { ) {
bittorrent_found: bittorrent_found:
if(bt_proto && (packet->payload_packet_len > 47))
memcpy(flow->bittorent_hash, &bt_proto[27], 20);
NDPI_LOG(NDPI_PROTOCOL_BITTORRENT, NDPI_LOG(NDPI_PROTOCOL_BITTORRENT,
ndpi_struct, NDPI_LOG_TRACE, "BT: plain BitTorrent protocol detected\n"); ndpi_struct, NDPI_LOG_TRACE, "BT: plain BitTorrent protocol detected\n");
ndpi_add_connection_as_bittorrent(ndpi_struct, flow, -1, ndpi_add_connection_as_bittorrent(ndpi_struct, flow, -1,