|
|
@@ -35,6 +35,8 @@
|
|
|
|
#define NDPI_PROTOCOL_PLAIN_DETECTION 0
|
|
|
|
#define NDPI_PROTOCOL_PLAIN_DETECTION 0
|
|
|
|
#define NDPI_PROTOCOL_WEBSEED_DETECTION 2
|
|
|
|
#define NDPI_PROTOCOL_WEBSEED_DETECTION 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BITTORRENT_PROTO_STRING "BitTorrent protocol"
|
|
|
|
|
|
|
|
|
|
|
|
//#define CACHE_DEBUG 1
|
|
|
|
//#define CACHE_DEBUG 1
|
|
|
|
|
|
|
|
|
|
|
|
struct ndpi_utp_hdr {
|
|
|
|
struct ndpi_utp_hdr {
|
|
|
@@ -44,10 +46,31 @@ struct ndpi_utp_hdr {
|
|
|
|
u_int16_t sequence_nr, ack_nr;
|
|
|
|
u_int16_t sequence_nr, ack_nr;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
|
|
|
|
static void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
|
|
|
|
struct ndpi_flow_struct *flow);
|
|
|
|
|
|
|
|
static void ndpi_search_bittorrent_hash(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
|
|
|
|
struct ndpi_flow_struct *flow, int bt_offset);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************************************** */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int search_bittorrent_again(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
|
|
|
|
struct ndpi_flow_struct *flow) {
|
|
|
|
|
|
|
|
ndpi_search_bittorrent(ndpi_struct, flow);
|
|
|
|
|
|
|
|
ndpi_search_bittorrent_hash(ndpi_struct, flow, -1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Possibly more processing */
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************************************** */
|
|
|
|
|
|
|
|
|
|
|
|
static inline u_int32_t bt_hash_funct(u_int32_t ip, u_int16_t port) {
|
|
|
|
static inline u_int32_t bt_hash_funct(u_int32_t ip, u_int16_t port) {
|
|
|
|
return(ip + 3 * port);
|
|
|
|
return(ip + 3 * port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************************************** */
|
|
|
|
|
|
|
|
|
|
|
|
static u_int8_t is_utpv1_pkt(const u_int8_t *payload, u_int payload_len) {
|
|
|
|
static u_int8_t is_utpv1_pkt(const u_int8_t *payload, u_int payload_len) {
|
|
|
|
struct ndpi_utp_hdr *h = (struct ndpi_utp_hdr*)payload;
|
|
|
|
struct ndpi_utp_hdr *h = (struct ndpi_utp_hdr*)payload;
|
|
|
|
|
|
|
|
|
|
|
@@ -63,31 +86,53 @@ static u_int8_t is_utpv1_pkt(const u_int8_t *payload, u_int payload_len) {
|
|
|
|
return(1);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ndpi_add_connection_as_bittorrent(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
/* *********************************************** */
|
|
|
|
struct ndpi_flow_struct *flow,
|
|
|
|
|
|
|
|
int bt_offset, int check_hash,
|
|
|
|
|
|
|
|
const u_int8_t save_detection, const u_int8_t encrypted_connection)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(check_hash) {
|
|
|
|
static void ndpi_search_bittorrent_hash(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
|
|
|
|
struct ndpi_flow_struct *flow, int bt_offset) {
|
|
|
|
const char *bt_hash = NULL; /* 20 bytes long */
|
|
|
|
const char *bt_hash = NULL; /* 20 bytes long */
|
|
|
|
|
|
|
|
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
|
|
|
|
|
|
|
|
|
|
|
if(bt_offset == -1) {
|
|
|
|
if(bt_offset == -1) {
|
|
|
|
const char *bt_magic = ndpi_strnstr((const char *)packet->payload,
|
|
|
|
const char *bt_magic = ndpi_strnstr((const char *)packet->payload,
|
|
|
|
"BitTorrent protocol", packet->payload_packet_len);
|
|
|
|
BITTORRENT_PROTO_STRING, packet->payload_packet_len);
|
|
|
|
|
|
|
|
|
|
|
|
if(bt_magic)
|
|
|
|
if(bt_magic) {
|
|
|
|
|
|
|
|
if(bt_magic == (const char*)&packet->payload[1])
|
|
|
|
|
|
|
|
bt_hash = (const char*)&packet->payload[28];
|
|
|
|
|
|
|
|
else
|
|
|
|
bt_hash = &bt_magic[19];
|
|
|
|
bt_hash = &bt_magic[19];
|
|
|
|
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
} else
|
|
|
|
bt_hash = (const char*)&packet->payload[28];
|
|
|
|
bt_hash = (const char*)&packet->payload[28];
|
|
|
|
|
|
|
|
|
|
|
|
if(bt_hash && (packet->payload_packet_len >= (20 + (bt_hash-(const char*)packet->payload))))
|
|
|
|
if(bt_hash && (packet->payload_packet_len >= (20 + (bt_hash-(const char*)packet->payload))))
|
|
|
|
memcpy(flow->protos.bittorrent.hash, bt_hash, 20);
|
|
|
|
memcpy(flow->protos.bittorrent.hash, bt_hash, 20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************************************** */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ndpi_add_connection_as_bittorrent(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
|
|
|
|
struct ndpi_flow_struct *flow,
|
|
|
|
|
|
|
|
int bt_offset, int check_hash,
|
|
|
|
|
|
|
|
const u_int8_t save_detection,
|
|
|
|
|
|
|
|
const u_int8_t encrypted_connection) {
|
|
|
|
|
|
|
|
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(check_hash)
|
|
|
|
|
|
|
|
ndpi_search_bittorrent_hash(ndpi_struct, flow, bt_offset);
|
|
|
|
|
|
|
|
|
|
|
|
ndpi_int_change_protocol(ndpi_struct, flow, NDPI_PROTOCOL_BITTORRENT, NDPI_PROTOCOL_UNKNOWN);
|
|
|
|
ndpi_int_change_protocol(ndpi_struct, flow, NDPI_PROTOCOL_BITTORRENT, NDPI_PROTOCOL_UNKNOWN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(flow->protos.bittorrent.hash[0] == '\0') {
|
|
|
|
|
|
|
|
/* This is necessary to inform the core to call this dissector again */
|
|
|
|
|
|
|
|
flow->check_extra_packets = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Don't use just 1 as in TCP DNS more packets could be returned (e.g. ACK). */
|
|
|
|
|
|
|
|
flow->max_extra_packets_to_check = 3;
|
|
|
|
|
|
|
|
flow->extra_packets_func = search_bittorrent_again;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(ndpi_struct->bittorrent_cache == NULL)
|
|
|
|
if(ndpi_struct->bittorrent_cache == NULL)
|
|
|
|
ndpi_struct->bittorrent_cache = ndpi_lru_cache_init(32768);
|
|
|
|
ndpi_struct->bittorrent_cache = ndpi_lru_cache_init(32768);
|
|
|
|
|
|
|
|
|
|
|
@@ -124,7 +169,7 @@ static u_int8_t ndpi_int_search_bittorrent_tcp_zero(struct ndpi_detection_module
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(flow->packet_counter == 2 && packet->payload_packet_len > 20) {
|
|
|
|
if(flow->packet_counter == 2 && packet->payload_packet_len > 20) {
|
|
|
|
if(memcmp(&packet->payload[0], "BitTorrent protocol", 19) == 0) {
|
|
|
|
if(memcmp(&packet->payload[0], BITTORRENT_PROTO_STRING, 19) == 0) {
|
|
|
|
NDPI_LOG_INFO(ndpi_struct, "found BT: plain\n");
|
|
|
|
NDPI_LOG_INFO(ndpi_struct, "found BT: plain\n");
|
|
|
|
ndpi_add_connection_as_bittorrent(ndpi_struct, flow, 19, 1,
|
|
|
|
ndpi_add_connection_as_bittorrent(ndpi_struct, flow, 19, 1,
|
|
|
|
NDPI_PROTOCOL_SAFE_DETECTION, NDPI_PROTOCOL_PLAIN_DETECTION);
|
|
|
|
NDPI_PROTOCOL_SAFE_DETECTION, NDPI_PROTOCOL_PLAIN_DETECTION);
|
|
|
@@ -133,9 +178,9 @@ static u_int8_t ndpi_int_search_bittorrent_tcp_zero(struct ndpi_detection_module
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(packet->payload_packet_len > 20) {
|
|
|
|
if(packet->payload_packet_len > 20) {
|
|
|
|
/* test for match 0x13+"BitTorrent protocol" */
|
|
|
|
/* test for match 0x13+BITTORRENT_PROTO_STRING */
|
|
|
|
if(packet->payload[0] == 0x13) {
|
|
|
|
if(packet->payload[0] == 0x13) {
|
|
|
|
if(memcmp(&packet->payload[1], "BitTorrent protocol", 19) == 0) {
|
|
|
|
if(memcmp(&packet->payload[1], BITTORRENT_PROTO_STRING, 19) == 0) {
|
|
|
|
NDPI_LOG_INFO(ndpi_struct, "found BT: plain\n");
|
|
|
|
NDPI_LOG_INFO(ndpi_struct, "found BT: plain\n");
|
|
|
|
ndpi_add_connection_as_bittorrent(ndpi_struct, flow, 20, 1,
|
|
|
|
ndpi_add_connection_as_bittorrent(ndpi_struct, flow, 20, 1,
|
|
|
|
NDPI_PROTOCOL_SAFE_DETECTION, NDPI_PROTOCOL_PLAIN_DETECTION);
|
|
|
|
NDPI_PROTOCOL_SAFE_DETECTION, NDPI_PROTOCOL_PLAIN_DETECTION);
|
|
|
@@ -410,8 +455,8 @@ static u_int8_t is_port(u_int16_t a, u_int16_t b, u_int16_t what) {
|
|
|
|
return(((what == a) || (what == b)) ? 1 : 0);
|
|
|
|
return(((what == a) || (what == b)) ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
|
|
|
|
static void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct,
|
|
|
|
{
|
|
|
|
struct ndpi_flow_struct *flow) {
|
|
|
|
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
|
|
|
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
|
|
|
char *bt_proto = NULL;
|
|
|
|
char *bt_proto = NULL;
|
|
|
|
|
|
|
|
|
|
|
@@ -460,9 +505,11 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
|
|
|
|
ntohs(packet->tcp->source), ntohs(packet->tcp->dest), key1, key2, found, flow->packet_counter);
|
|
|
|
ntohs(packet->tcp->source), ntohs(packet->tcp->dest), key1, key2, found, flow->packet_counter);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if(found)
|
|
|
|
if(found) {
|
|
|
|
|
|
|
|
ndpi_search_bittorrent_hash(ndpi_struct, flow, -1);
|
|
|
|
goto bittorrent_found;
|
|
|
|
goto bittorrent_found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(packet->tcp != NULL) {
|
|
|
|
if(packet->tcp != NULL) {
|
|
|
|
ndpi_int_search_bittorrent_tcp(ndpi_struct, flow);
|
|
|
|
ndpi_int_search_bittorrent_tcp(ndpi_struct, flow);
|
|
|
@@ -497,7 +544,7 @@ void ndpi_search_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, st
|
|
|
|
u_int8_t v0_flags = packet->payload[18];
|
|
|
|
u_int8_t v0_flags = packet->payload[18];
|
|
|
|
|
|
|
|
|
|
|
|
if(is_utpv1_pkt(packet->payload, packet->payload_packet_len)) {
|
|
|
|
if(is_utpv1_pkt(packet->payload, packet->payload_packet_len)) {
|
|
|
|
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], "BitTorrent protocol", packet->payload_packet_len-20);
|
|
|
|
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], BITTORRENT_PROTO_STRING, packet->payload_packet_len-20);
|
|
|
|
goto bittorrent_found;
|
|
|
|
goto bittorrent_found;
|
|
|
|
} else if((packet->payload[0]== 0x60)
|
|
|
|
} else if((packet->payload[0]== 0x60)
|
|
|
|
&& (packet->payload[1]== 0x0)
|
|
|
|
&& (packet->payload[1]== 0x0)
|
|
|
@@ -505,7 +552,7 @@ 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);
|
|
|
|
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], BITTORRENT_PROTO_STRING, packet->payload_packet_len-20);
|
|
|
|
goto bittorrent_found;
|
|
|
|
goto bittorrent_found;
|
|
|
|
/* CSGO/DOTA conflict */
|
|
|
|
/* CSGO/DOTA conflict */
|
|
|
|
} else if((v0_flags < 6 /* ST_NUM_STATES */) && (v0_extension < 3 /* EXT_NUM_EXT */)) {
|
|
|
|
} else if((v0_flags < 6 /* ST_NUM_STATES */) && (v0_extension < 3 /* EXT_NUM_EXT */)) {
|
|
|
@@ -515,10 +562,10 @@ 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);
|
|
|
|
bt_proto = ndpi_strnstr((const char *)&packet->payload[20], BITTORRENT_PROTO_STRING, packet->payload_packet_len-20);
|
|
|
|
goto bittorrent_found;
|
|
|
|
goto bittorrent_found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(ndpi_strnstr((const char *)&packet->payload[20], "BitTorrent protocol", packet->payload_packet_len-20)
|
|
|
|
} else if(ndpi_strnstr((const char *)&packet->payload[20], BITTORRENT_PROTO_STRING, packet->payload_packet_len-20)
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
goto bittorrent_found;
|
|
|
|
goto bittorrent_found;
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -538,12 +585,13 @@ 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)
|
|
|
|
|| (bt_proto = ndpi_strnstr((const char *)packet->payload, "BitTorrent protocol", packet->payload_packet_len))
|
|
|
|
|| (bt_proto = ndpi_strnstr((const char *)packet->payload, BITTORRENT_PROTO_STRING, packet->payload_packet_len))
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
bittorrent_found:
|
|
|
|
bittorrent_found:
|
|
|
|
if(bt_proto != NULL && ((u_int8_t *)&bt_proto[27] - packet->payload +
|
|
|
|
if(bt_proto != NULL && ((u_int8_t *)&bt_proto[27] - packet->payload +
|
|
|
|
sizeof(flow->protos.bittorrent.hash)) < packet->payload_packet_len) {
|
|
|
|
sizeof(flow->protos.bittorrent.hash)) < packet->payload_packet_len) {
|
|
|
|
memcpy(flow->protos.bittorrent.hash, &bt_proto[27], sizeof(flow->protos.bittorrent.hash));
|
|
|
|
memcpy(flow->protos.bittorrent.hash, &bt_proto[27], sizeof(flow->protos.bittorrent.hash));
|
|
|
|
|
|
|
|
flow->extra_packets_func = NULL; /* Nothing else to do */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NDPI_LOG_INFO(ndpi_struct, "found BT: plain\n");
|
|
|
|
NDPI_LOG_INFO(ndpi_struct, "found BT: plain\n");
|
|
|
|