diff options
author | Luca Deri <lucaderi@users.noreply.github.com> | 2024-11-14 23:34:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-14 23:34:31 +0100 |
commit | 3ce8d0e5087f6e3f2a3e1f65d39d78c228054a5d (patch) | |
tree | 3f74262980cd6b464f85bc08b17774378503f201 /src/lib | |
parent | dfc3168caf289c6de9ccad6401e819d710455ba2 (diff) |
Implemented Mikrotik discovery protocol dissection and metadata extraction (#2618)
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/ndpi_main.c | 7 | ||||
-rw-r--r-- | src/lib/protocols/ajp.c | 16 | ||||
-rw-r--r-- | src/lib/protocols/mikrotik.c | 120 |
3 files changed, 135 insertions, 8 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index e086d6f01..4bfc34ce0 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1634,6 +1634,10 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp "MPEG_TS", NDPI_PROTOCOL_CATEGORY_MEDIA, ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); + ndpi_set_proto_defaults(ndpi_str, 1 /* cleartext */, 0 /* nw proto */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_MIKROTIK, + "Mikrotik", NDPI_PROTOCOL_CATEGORY_NETWORK, + ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, + ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); /* http://en.wikipedia.org/wiki/Link-local_Multicast_Name_Resolution */ ndpi_set_proto_defaults(ndpi_str, 1 /* cleartext */, 0 /* nw proto */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_LLMNR, "LLMNR", NDPI_PROTOCOL_CATEGORY_NETWORK, @@ -5950,6 +5954,9 @@ static int ndpi_callback_init(struct ndpi_detection_module_struct *ndpi_str) { /* KAKAOTALK_VOICE */ init_kakaotalk_voice_dissector(ndpi_str, &a); + /* MIKROTIK */ + init_mikrotik_dissector(ndpi_str, &a); + /* MPEGTS */ init_mpegts_dissector(ndpi_str, &a); diff --git a/src/lib/protocols/ajp.c b/src/lib/protocols/ajp.c index 3d2317fb2..4d8768890 100644 --- a/src/lib/protocols/ajp.c +++ b/src/lib/protocols/ajp.c @@ -35,7 +35,7 @@ enum ajp_direction { enum ajp_packet_type { AJP_UNKNOWN = 0, - /* packet types */ + /* packet types */ AJP_FORWARD_REQUEST = 2, AJP_SEND_BODY_CHUNK = 3, AJP_SEND_HEADERS = 4, @@ -56,10 +56,10 @@ struct ajp_header { } PACK_OFF; static void set_ajp_detected(struct ndpi_detection_module_struct *ndpi_struct, - struct ndpi_flow_struct *flow) { + struct ndpi_flow_struct *flow) { if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) { - ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_AJP, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); + ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_AJP, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); } } @@ -121,13 +121,13 @@ static void ndpi_search_ajp(struct ndpi_detection_module_struct *ndpi_struct, void init_ajp_dissector(struct ndpi_detection_module_struct *ndpi_struct, - u_int32_t *id) + u_int32_t *id) { ndpi_set_bitmask_protocol_detection("AJP", ndpi_struct, - *id, NDPI_PROTOCOL_AJP, ndpi_search_ajp, - NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, - SAVE_DETECTION_BITMASK_AS_UNKNOWN, - ADD_TO_DETECTION_BITMASK); + *id, NDPI_PROTOCOL_AJP, ndpi_search_ajp, + NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, + SAVE_DETECTION_BITMASK_AS_UNKNOWN, + ADD_TO_DETECTION_BITMASK); *id += 1; } diff --git a/src/lib/protocols/mikrotik.c b/src/lib/protocols/mikrotik.c new file mode 100644 index 000000000..b1d315581 --- /dev/null +++ b/src/lib/protocols/mikrotik.c @@ -0,0 +1,120 @@ +/* + * mikrotik.c + * + * Copyright (C) 2012-24 - ntop.org + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "ndpi_protocol_ids.h" + +#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_MIKROTIK + +#include "ndpi_api.h" +#include "ndpi_private.h" + +/* ********************************* */ + +static void ndpi_search_mikrotik(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow) { + struct ndpi_packet_struct *packet = &ndpi_struct->packet; + + NDPI_LOG_DBG(ndpi_struct, "search MIKROTIK\n"); + + if((packet->iph && (packet->iph->daddr== 0xFFFFFFFF)) + || (packet->iphv6 && (ntohl(packet->iphv6->ip6_dst.u6_addr.u6_addr32[0]) == 0xFF020000 /* ff02:: */)) + ) { + if(ntohs(packet->udp->dest) == 5678) { + const u_int8_t *payload; + u_int16_t offset; + + if (packet->payload_packet_len < 8) { + NDPI_EXCLUDE_PROTO(ndpi_struct, flow); + return; + } else { + offset = 4; + payload = packet->payload; + } + + while((offset+4) < packet->payload_packet_len) { + u_int16_t m_type = ((u_int16_t)payload[offset] << 8) + payload[offset+1]; + u_int16_t m_len = ((u_int16_t)payload[offset+2] << 8) + payload[offset+3]; + + // printf("%d\n", m_type); + + if((m_len+offset) < packet->payload_packet_len) { + switch(m_type) { + case 1 /* MAC Address */: + if(m_len == 6) + memcpy(flow->protos.mikrotik.mac_addr, &payload[offset+4], m_len); + break; + case 5 /* Identity */: + snprintf(flow->protos.mikrotik.identity, sizeof(flow->protos.mikrotik.identity), + "%.*s", m_len, &payload[offset+4]); + break; + case 7 /* Version */: + snprintf(flow->protos.mikrotik.version, sizeof(flow->protos.mikrotik.version), + "%.*s", m_len, &payload[offset+4]); + break; + case 10: /* Uptime */ + if(m_len == 4) + flow->protos.mikrotik.uptime = ntohl(*((u_int32_t*)&payload[offset+4])); + break; + case 11: /* Software-ID */ + snprintf(flow->protos.mikrotik.sw_id, sizeof(flow->protos.mikrotik.sw_id), + "%.*s", m_len, &payload[offset+4]); + break; + case 12: /* Board */ + snprintf(flow->protos.mikrotik.board, sizeof(flow->protos.mikrotik.board), + "%.*s", m_len, &payload[offset+4]); + break; + case 15: /* IPv6 */ + if(m_len == 16) + memcpy(&flow->protos.mikrotik.ipv6_addr, &payload[offset+4], m_len); + break; + case 16: /* Interface Name */ + snprintf(flow->protos.mikrotik.iface_name, sizeof(flow->protos.mikrotik.iface_name), + "%.*s", m_len, &payload[offset+4]); + break; + case 14: /* IPv4 */ + if(m_len == 4) + flow->protos.mikrotik.ipv4_addr = ntohl(*((u_int32_t*)&payload[offset+4])); + break; + } + + offset += 4 + m_len; + } else + break; + } /* while */ + + ndpi_set_detected_protocol(ndpi_struct, flow, + NDPI_PROTOCOL_MIKROTIK, + NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); + } + } else + NDPI_EXCLUDE_PROTO(ndpi_struct, flow); +} + +/* ********************************* */ + +void init_mikrotik_dissector(struct ndpi_detection_module_struct *ndpi_struct, + u_int32_t *id) { + ndpi_set_bitmask_protocol_detection("MIKROTIK", ndpi_struct, + *id, NDPI_PROTOCOL_MIKROTIK, ndpi_search_mikrotik, + NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD, + SAVE_DETECTION_BITMASK_AS_UNKNOWN, + ADD_TO_DETECTION_BITMASK); + + *id += 1; +} |