/src/ndpi/src/lib/protocols/kafka.c
Line | Count | Source |
1 | | /* |
2 | | * kafka.c |
3 | | * |
4 | | * Copyright (C) 2023 - ntop.org |
5 | | * Copyright (C) 2023 - V.G <v.gavrilov@securitycode.ru> |
6 | | * |
7 | | * This file is part of nDPI, an open source deep packet inspection |
8 | | * library based on the OpenDPI and PACE technology by ipoque GmbH |
9 | | * |
10 | | * nDPI is free software: you can redistribute it and/or modify |
11 | | * it under the terms of the GNU Lesser General Public License as published by |
12 | | * the Free Software Foundation, either version 3 of the License, or |
13 | | * (at your option) any later version. |
14 | | * |
15 | | * nDPI is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public License |
21 | | * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
22 | | * |
23 | | */ |
24 | | |
25 | | #include "ndpi_protocol_ids.h" |
26 | | |
27 | | #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_APACHE_KAFKA |
28 | | |
29 | | #include "ndpi_api.h" |
30 | | #include "ndpi_private.h" |
31 | | |
32 | | static void ndpi_int_kafka_add_connection(struct ndpi_detection_module_struct *ndpi_struct, |
33 | | struct ndpi_flow_struct *flow) |
34 | 0 | { |
35 | 0 | NDPI_LOG_INFO(ndpi_struct, "found Apache Kafka\n"); |
36 | 0 | ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_APACHE_KAFKA, |
37 | 0 | NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); |
38 | 0 | } |
39 | | |
40 | | static void ndpi_search_kafka(struct ndpi_detection_module_struct *ndpi_struct, |
41 | | struct ndpi_flow_struct *flow) |
42 | 0 | { |
43 | 0 | struct ndpi_packet_struct const * const packet = &ndpi_struct->packet; |
44 | |
|
45 | 0 | NDPI_LOG_DBG(ndpi_struct, "search Apache Kafka\n"); |
46 | | |
47 | | /* All Kafka stuff start with 4 bytes containing the payload length |
48 | | * minus 4 bytes. |
49 | | * API keys: https://kafka.apache.org/protocol.html#protocol_api_keys |
50 | | * API versions: https://cwiki.apache.org/confluence/display/KAFKA/Kafka+APIs |
51 | | */ |
52 | 0 | if (packet->payload_packet_len < 8 /* min. required packet length */ || |
53 | 0 | ntohl(get_u_int32_t(packet->payload, 0)) != (uint32_t)(packet->payload_packet_len - 4)) |
54 | 0 | { |
55 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
56 | 0 | return; |
57 | 0 | } |
58 | | |
59 | | /* Request */ |
60 | 0 | if (ntohs(get_u_int16_t(packet->payload, 4)) < 75 && /* API key */ |
61 | 0 | ntohs(get_u_int16_t(packet->payload, 6)) < 16 /* API version */) |
62 | 0 | { |
63 | 0 | if (packet->payload_packet_len < 14) |
64 | 0 | { |
65 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
66 | 0 | return; |
67 | 0 | } |
68 | | |
69 | 0 | const uint16_t client_id_len = ntohs(get_u_int16_t(packet->payload, 12)); |
70 | 0 | if (client_id_len + 12 + 2 > packet->payload_packet_len) |
71 | 0 | { |
72 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
73 | 0 | return; |
74 | 0 | } |
75 | 0 | if (ndpi_is_printable_buffer(&packet->payload[14], client_id_len) == 0) |
76 | 0 | { |
77 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
78 | 0 | return; |
79 | 0 | } |
80 | | |
81 | 0 | ndpi_int_kafka_add_connection(ndpi_struct, flow); |
82 | 0 | return; |
83 | 0 | } |
84 | | |
85 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
86 | 0 | } |
87 | | |
88 | | void init_kafka_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
89 | 1 | { |
90 | 1 | register_dissector("Kafka", ndpi_struct, |
91 | 1 | ndpi_search_kafka, |
92 | 1 | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
93 | 1 | 1, NDPI_PROTOCOL_APACHE_KAFKA); |
94 | 1 | } |