STUN: add dissection of XOR-PEER-ADDRESS with ipv6 address

This commit is contained in:
Nardi Ivan
2024-03-20 09:39:15 +01:00
committed by Ivan Nardi
parent 58894dd788
commit ee87c4bc66
+53 -17
View File
@@ -52,6 +52,7 @@
static u_int64_t get_stun_lru_key(struct ndpi_flow_struct *flow, u_int8_t rev); static u_int64_t get_stun_lru_key(struct ndpi_flow_struct *flow, u_int8_t rev);
static u_int64_t get_stun_lru_key_raw4(u_int32_t ip, u_int16_t port); static u_int64_t get_stun_lru_key_raw4(u_int32_t ip, u_int16_t port);
static u_int64_t get_stun_lru_key_raw6(u_int8_t *ip, u_int16_t port);
static void ndpi_int_stun_add_connection(struct ndpi_detection_module_struct *ndpi_struct, static void ndpi_int_stun_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
u_int app_proto); u_int app_proto);
@@ -156,6 +157,7 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
const u_int8_t *orig_payload; const u_int8_t *orig_payload;
u_int16_t orig_payload_length; u_int16_t orig_payload_length;
u_int32_t magic_cookie; u_int32_t magic_cookie;
u_int32_t transaction_id[3];
if(payload_length < STUN_HDR_LEN) { if(payload_length < STUN_HDR_LEN) {
return 0; return 0;
@@ -180,6 +182,9 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
msg_type = ntohs(*((u_int16_t *)&payload[0])); msg_type = ntohs(*((u_int16_t *)&payload[0]));
msg_len = ntohs(*((u_int16_t *)&payload[2])); msg_len = ntohs(*((u_int16_t *)&payload[2]));
magic_cookie = ntohl(*((u_int32_t *)&payload[4])); magic_cookie = ntohl(*((u_int32_t *)&payload[4]));
transaction_id[0] = ntohl(*((u_int32_t *)&payload[8]));
transaction_id[1] = ntohl(*((u_int32_t *)&payload[12]));
transaction_id[2] = ntohl(*((u_int32_t *)&payload[16]));
/* No magic_cookie on classic-stun */ /* No magic_cookie on classic-stun */
/* Let's hope that we don't have anymore classic-stun over TCP */ /* Let's hope that we don't have anymore classic-stun over TCP */
@@ -271,32 +276,58 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
switch(attribute) { switch(attribute) {
case 0x0012: /* XOR-PEER-ADDRESS */ case 0x0012: /* XOR-PEER-ADDRESS */
if(off + 12 < payload_length && if(off + 12 < payload_length ) {
len == 8 && payload[off + 5] == 0x01) { /* TODO: ipv6 */
u_int16_t port; u_int16_t port;
u_int32_t ip;
#ifdef NDPI_ENABLE_DEBUG_MESSAGES #ifdef NDPI_ENABLE_DEBUG_MESSAGES
char buf[128]; char buf[128];
#endif #endif
port = ntohs(*((u_int16_t *)&payload[off + 6])) ^ (magic_cookie >> 16); if(len == 8 && payload[off + 5] == 0x01) { /* ipv4 */
ip = *((u_int32_t *)&payload[off + 8]) ^ htonl(magic_cookie); u_int32_t ip;
NDPI_LOG_DBG(ndpi_struct, "Peer %s:%d [proto %d]\n", port = ntohs(*((u_int16_t *)&payload[off + 6])) ^ (magic_cookie >> 16);
inet_ntop(AF_INET, &ip, buf, sizeof(buf)), port, ip = *((u_int32_t *)&payload[off + 8]) ^ htonl(magic_cookie);
flow->detected_protocol_stack[0]);
if(1 /* TODO: enable/disable */ && NDPI_LOG_DBG(ndpi_struct, "Peer %s:%d [proto %d]\n",
ndpi_struct->stun_cache) { inet_ntop(AF_INET, &ip, buf, sizeof(buf)), port,
u_int64_t key = get_stun_lru_key_raw4(ip, port); flow->detected_protocol_stack[0]);
ndpi_lru_add_to_cache(ndpi_struct->stun_cache, key, if(1 /* TODO: enable/disable */ &&
flow->detected_protocol_stack[0], ndpi_struct->stun_cache) {
ndpi_get_current_time(flow)); u_int64_t key = get_stun_lru_key_raw4(ip, port);
ndpi_lru_add_to_cache(ndpi_struct->stun_cache, key,
flow->detected_protocol_stack[0],
ndpi_get_current_time(flow));
#ifdef DEBUG_LRU #ifdef DEBUG_LRU
printf("[LRU] Add peer 0x%llx %d\n", (long long unsigned int)key, flow->detected_protocol_stack[0]); printf("[LRU] Add peer 0x%llx %d\n", (long long unsigned int)key, flow->detected_protocol_stack[0]);
#endif #endif
} }
} else if(len == 20 && payload[off + 5] == 0x02 && off + 24 < payload_length) { /* ipv6 */
u_int32_t ip[4];
port = ntohs(*((u_int16_t *)&payload[off + 6])) ^ (magic_cookie >> 16);
ip[0] = *((u_int32_t *)&payload[off + 8]) ^ htonl(magic_cookie);
ip[1] = *((u_int32_t *)&payload[off + 12]) ^ htonl(transaction_id[0]);
ip[2] = *((u_int32_t *)&payload[off + 16]) ^ htonl(transaction_id[1]);
ip[3] = *((u_int32_t *)&payload[off + 20]) ^ htonl(transaction_id[2]);
NDPI_LOG_DBG(ndpi_struct, "Peer %s:%d [proto %d]\n",
inet_ntop(AF_INET6, &ip, buf, sizeof(buf)), port,
flow->detected_protocol_stack[0]);
if(1 /* TODO: enable/disable */ &&
ndpi_struct->stun_cache) {
u_int64_t key = get_stun_lru_key_raw6((u_int8_t *)ip, port);
ndpi_lru_add_to_cache(ndpi_struct->stun_cache, key,
flow->detected_protocol_stack[0],
ndpi_get_current_time(flow));
#ifdef DEBUG_LRU
printf("[LRU] Add peer 0x%llx %d\n", (long long unsigned int)key, flow->detected_protocol_stack[0]);
#endif
}
}
} }
break; break;
@@ -360,7 +391,6 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
case 0x0013: case 0x0013:
NDPI_LOG_DBG(ndpi_struct, "DATA attribute (%d/%d)\n", NDPI_LOG_DBG(ndpi_struct, "DATA attribute (%d/%d)\n",
real_len, payload_length - off - 4); real_len, payload_length - off - 4);
if(real_len <= payload_length - off - 4) { if(real_len <= payload_length - off - 4) {
orig_payload = packet->payload; orig_payload = packet->payload;
orig_payload_length = packet->payload_packet_len; orig_payload_length = packet->payload_packet_len;
@@ -592,6 +622,12 @@ static u_int64_t get_stun_lru_key_raw4(u_int32_t ip, u_int16_t port_host_order)
/* ************************************************************ */ /* ************************************************************ */
static u_int64_t get_stun_lru_key_raw6(u_int8_t *ip, u_int16_t port_host_order) {
return ((u_int64_t)ndpi_quick_hash(ip, 16) << 32) | htons(port_host_order);
}
/* ************************************************************ */
int stun_search_into_zoom_cache(struct ndpi_detection_module_struct *ndpi_struct, int stun_search_into_zoom_cache(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) struct ndpi_flow_struct *flow)
{ {