1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* collectd.c
*
* Copyright (C) 2022-23 - 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_COLLECTD
#include "ndpi_api.h"
#include "ndpi_private.h"
#define COLLECTD_MIN_BLOCKS_REQUIRED 3
#define COLLECTD_MAX_BLOCKS_TO_DISSECT 5
#define COLLECTD_ENCR_AES256_MIN_BLOCK_SIZE 6
#define COLLECTD_ENCR_AES256_IV_SIZE 16
enum collectd_type {
COLLECTD_TYPE_HOST = 0x0000,
COLLECTD_TYPE_TIME = 0x0001,
COLLECTD_TYPE_TIME_HR = 0x0008,
COLLECTD_TYPE_PLUGIN = 0x0002,
COLLECTD_TYPE_PLUGIN_INSTANCE = 0x0003,
COLLECTD_TYPE_TYPE = 0x0004,
COLLECTD_TYPE_TYPE_INSTANCE = 0x0005,
COLLECTD_TYPE_VALUES = 0x0006,
COLLECTD_TYPE_INTERVAL = 0x0007,
COLLECTD_TYPE_INTERVAL_HR = 0x0009,
COLLECTD_TYPE_MESSAGE = 0x0100,
COLLECTD_TYPE_SEVERITY = 0x0101,
COLLECTD_TYPE_SIGN_SHA256 = 0x0200,
COLELCTD_TYPE_ENCR_AES256 = 0x0210,
};
static u_int16_t const collectd_types[] = {
COLLECTD_TYPE_HOST, COLLECTD_TYPE_TIME, COLLECTD_TYPE_TIME_HR, COLLECTD_TYPE_PLUGIN,
COLLECTD_TYPE_PLUGIN_INSTANCE, COLLECTD_TYPE_TYPE, COLLECTD_TYPE_TYPE_INSTANCE,
COLLECTD_TYPE_VALUES, COLLECTD_TYPE_INTERVAL, COLLECTD_TYPE_INTERVAL_HR,
COLLECTD_TYPE_MESSAGE, COLLECTD_TYPE_SEVERITY, COLLECTD_TYPE_SIGN_SHA256,
COLELCTD_TYPE_ENCR_AES256
};
static const size_t collectd_types_length = NDPI_ARRAY_LENGTH(collectd_types);
static void ndpi_int_collectd_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
struct ndpi_flow_struct * const flow)
{
NDPI_LOG_INFO(ndpi_struct, "found collectd\n");
ndpi_set_detected_protocol(ndpi_struct, flow,
NDPI_PROTOCOL_COLLECTD,
NDPI_PROTOCOL_UNKNOWN,
NDPI_CONFIDENCE_DPI);
}
static u_int16_t npdi_int_collectd_block_size(struct ndpi_packet_struct const * const packet,
u_int16_t const block_offset)
{
if (block_offset + 4 > packet->payload_packet_len)
{
return 0;
}
u_int16_t next_block = ntohs(get_u_int16_t(packet->payload, block_offset + 2));
if (block_offset + next_block > packet->payload_packet_len ||
(u_int16_t)(block_offset + next_block) <= block_offset /* possible overflow or next_block is zero */)
{
return 0;
}
return next_block;
}
static int ndpi_int_collectd_check_type(u_int16_t block_type)
{
size_t i;
for (i = 0; i < collectd_types_length; ++i)
{
if (block_type == collectd_types[i])
{
return 0;
}
}
return 1;
}
static int ndpi_int_collectd_dissect_hostname(struct ndpi_flow_struct * const flow,
struct ndpi_packet_struct const * const packet,
u_int16_t block_length)
{
return (ndpi_hostname_sni_set(flow, &packet->payload[4], block_length, NDPI_HOSTNAME_NORM_ALL) == NULL);
}
static int ndpi_int_collectd_dissect_username(struct ndpi_flow_struct * const flow,
struct ndpi_packet_struct const * const packet)
{
u_int16_t username_length = ntohs(get_u_int16_t(packet->payload, 4));
if(username_length > packet->payload_packet_len -
COLLECTD_ENCR_AES256_MIN_BLOCK_SIZE -
COLLECTD_ENCR_AES256_IV_SIZE)
{
return 1;
}
size_t sz_len = ndpi_min(sizeof(flow->protos.collectd.client_username) - 1, username_length);
memcpy(flow->protos.collectd.client_username, &packet->payload[6], sz_len);
flow->protos.collectd.client_username[sz_len] = '\0';
return 0;
}
static void ndpi_search_collectd(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow)
{
struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
u_int16_t num_blocks;
u_int16_t block_offset = 0, block_type, block_length;
u_int16_t hostname_length = 0;
NDPI_LOG_DBG(ndpi_struct, "search collectd\n");
for (num_blocks = 0; num_blocks < COLLECTD_MAX_BLOCKS_TO_DISSECT;
++num_blocks, block_offset += block_length)
{
block_length = npdi_int_collectd_block_size(packet, block_offset);
if (block_length == 0)
{
break;
}
block_type = ntohs(get_u_int16_t(packet->payload, block_offset));
if (ndpi_int_collectd_check_type(block_type) != 0)
{
break;
} else {
if (block_type == COLLECTD_TYPE_HOST)
{
/*
* Dissect the hostname later, when we are sure that it is
* the collectd protocol.
*/
if(block_length > 4)
hostname_length = block_length - 4; /* Ignore type and length fields */
} else if (block_type == COLELCTD_TYPE_ENCR_AES256) {
/*
* The encrypted data block is a special case.
* It is the only dissectable block as everything else in it
* is encrypted.
*/
if (block_length != packet->payload_packet_len ||
block_length < COLLECTD_ENCR_AES256_MIN_BLOCK_SIZE ||
ndpi_int_collectd_dissect_username(flow, packet) != 0)
{
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
} else {
ndpi_int_collectd_add_connection(ndpi_struct, flow);
}
return;
}
}
}
if (num_blocks < COLLECTD_MIN_BLOCKS_REQUIRED)
{
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return;
}
if (hostname_length > 0 &&
ndpi_int_collectd_dissect_hostname(flow, packet, hostname_length) != 0)
{
ndpi_set_risk(flow, NDPI_MALFORMED_PACKET, "Invalid collectd Header");
}
ndpi_int_collectd_add_connection(ndpi_struct, flow);
}
void init_collectd_dissector(struct ndpi_detection_module_struct *ndpi_struct,
u_int32_t *id)
{
ndpi_set_bitmask_protocol_detection("collectd", ndpi_struct, *id,
NDPI_PROTOCOL_COLLECTD,
ndpi_search_collectd,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD,
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK
);
*id += 1;
}
|