Skip to content

Commit 1de7e7d

Browse files
committed
Add (Generic) JSON dissector
Signed-off-by: Toni Uhlig <[email protected]>
1 parent dab8d30 commit 1de7e7d

File tree

360 files changed

+3116
-3133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

360 files changed

+3116
-3133
lines changed

src/include/ndpi_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ void init_cod_mobile_dissector(struct ndpi_detection_module_struct *ndpi_struct,
918918
void init_zug_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
919919
void init_jrmi_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
920920
void init_ripe_atlas_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
921+
void init_json_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
921922

922923
#endif
923924

src/include/ndpi_protocol_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ typedef enum {
446446
NDPI_PROTOCOL_ZUG = 415,
447447
NDPI_PROTOCOL_JRMI = 416,
448448
NDPI_PROTOCOL_RIPE_ATLAS = 417,
449+
NDPI_PROTOCOL_JSON = 418,
449450

450451
#ifdef CUSTOM_NDPI_PROTOCOLS
451452
#include "../../../nDPI-custom/custom_ndpi_protocol_ids.h"

src/lib/ndpi_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,10 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
22822282
"RipeAtlas", NDPI_PROTOCOL_CATEGORY_NETWORK,
22832283
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
22842284
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
2285+
ndpi_set_proto_defaults(ndpi_str, 1 /* cleartext */, 0 /* nw proto */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_JSON,
2286+
"JSON", NDPI_PROTOCOL_CATEGORY_NETWORK,
2287+
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
2288+
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
22852289

22862290
#ifdef CUSTOM_NDPI_PROTOCOLS
22872291
#include "../../../nDPI-custom/custom_ndpi_main.c"
@@ -6181,6 +6185,9 @@ static int ndpi_callback_init(struct ndpi_detection_module_struct *ndpi_str) {
61816185
/* (Magellan) Ripe Atlas */
61826186
init_ripe_atlas_dissector(ndpi_str, &a);
61836187

6188+
/* Generic JSON */
6189+
init_json_dissector(ndpi_str, &a);
6190+
61846191
#ifdef CUSTOM_NDPI_PROTOCOLS
61856192
#include "../../../nDPI-custom/custom_ndpi_main_init.c"
61866193
#endif

src/lib/protocols/json.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* json-rpc.c
3+
*
4+
* Copyright (C) 2024 - ntop.org
5+
* Copyright (C) 2024 - Toni Uhlig <[email protected]>
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_JSON
28+
29+
#include "ndpi_api.h"
30+
#include "ndpi_private.h"
31+
32+
static void ndpi_int_json_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
33+
struct ndpi_flow_struct * const flow)
34+
{
35+
NDPI_LOG_INFO(ndpi_struct, "found (Generic) JSON\n");
36+
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_JSON,
37+
NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
38+
}
39+
40+
static void ndpi_search_json(struct ndpi_detection_module_struct *ndpi_struct,
41+
struct ndpi_flow_struct *flow)
42+
{
43+
struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
44+
size_t offset = 0;
45+
size_t bytes_checked = 0;
46+
const size_t max_bytes_to_check = 16;
47+
48+
NDPI_LOG_DBG(ndpi_struct, "search (Generic) JSON\n");
49+
50+
if (packet->payload_packet_len < 2) {
51+
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
52+
return;
53+
}
54+
55+
do {
56+
if (offset <= packet->payload_packet_len) {
57+
break;
58+
}
59+
if (packet->payload[offset] == '{') {
60+
break;
61+
}
62+
if (packet->payload[offset] != ' ' &&
63+
packet->payload[offset] != '\t' &&
64+
packet->payload[offset] != '\r' &&
65+
packet->payload[offset] != '\n')
66+
{
67+
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
68+
return;
69+
}
70+
offset++;
71+
} while (++bytes_checked < max_bytes_to_check);
72+
73+
if (bytes_checked == max_bytes_to_check) {
74+
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
75+
return;
76+
}
77+
bytes_checked = 0;
78+
79+
do {
80+
if (offset <= packet->payload_packet_len) {
81+
break;
82+
}
83+
if (packet->payload[offset] == '"') {
84+
break;
85+
}
86+
if (packet->payload[offset] != ' ' &&
87+
packet->payload[offset] != '\t' &&
88+
packet->payload[offset] != '\r' &&
89+
packet->payload[offset] != '\n')
90+
{
91+
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
92+
return;
93+
}
94+
offset++;
95+
} while (++bytes_checked < max_bytes_to_check);
96+
97+
if (bytes_checked == max_bytes_to_check) {
98+
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
99+
return;
100+
}
101+
102+
ndpi_int_json_add_connection(ndpi_struct, flow);
103+
}
104+
105+
void init_json_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id)
106+
{
107+
ndpi_set_bitmask_protocol_detection("JSON", ndpi_struct, *id,
108+
NDPI_PROTOCOL_JSON,
109+
ndpi_search_json,
110+
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITHOUT_RETRANSMISSION,
111+
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
112+
ADD_TO_DETECTION_BITMASK);
113+
*id += 1;
114+
}

tests/cfgs/caches_cfg/result/ookla.pcap.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Guessed flow protos: 1
33
DPI Packets (TCP): 40 (6.67 pkts/flow)
44
Confidence Match by port : 1 (flows)
55
Confidence DPI : 5 (flows)
6-
Num dissector calls: 571 (95.17 diss/flow)
6+
Num dissector calls: 577 (96.17 diss/flow)
77
LRU cache ookla: 0/0/0 (insert/search/found)
88
LRU cache bittorrent: 0/3/0 (insert/search/found)
99
LRU cache stun: 0/0/0 (insert/search/found)

0 commit comments

Comments
 (0)