Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4257,3 +4257,10 @@ References: `Official site <https://www.espn.com/>`_
Akamai Technologies, Inc. is an American company specialized in content delivery network (CDN), cybersecurity, DDoS mitigation, and cloud services.

References: `Official site <https://www.akamai.com>`_


.. _Proto_468:

`NDPI_PROTOCOL_JSON`
====================
JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text.
1 change: 1 addition & 0 deletions src/include/ndpi_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ void init_mudfish_dissector(struct ndpi_detection_module_struct *ndpi_struct);
void init_tristation_dissector(struct ndpi_detection_module_struct *ndpi_struct);
void init_samsung_sdp_dissector(struct ndpi_detection_module_struct *ndpi_struct);
void init_matter_dissector(struct ndpi_detection_module_struct *ndpi_struct);
void init_json_dissector(struct ndpi_detection_module_struct *ndpi_struct);

#ifdef CUSTOM_NDPI_PROTOCOLS
#include "../../../nDPI-custom/custom_ndpi_private.h"
Expand Down
1 change: 1 addition & 0 deletions src/include/ndpi_protocol_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ typedef enum {
NDPI_PROTOCOL_AWS_DYNAMODB = 465,
NDPI_PROTOCOL_ESPN = 466,
NDPI_PROTOCOL_AKAMAI = 467,
NDPI_PROTOCOL_JSON = 468,

/* If you add a new protocol, please update the documentation at doc/protocols.rst, too! */

Expand Down
9 changes: 8 additions & 1 deletion src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2962,12 +2962,16 @@ static void init_protocol_defaults(struct ndpi_detection_module_struct *ndpi_str
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */ ,
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */,
0);

ndpi_set_proto_defaults(ndpi_str, 1 , 1 , NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_AKAMAI,
"Akamai", NDPI_PROTOCOL_CATEGORY_DATABASE, NDPI_PROTOCOL_QOE_CATEGORY_UNSPECIFIED,
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */ ,
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */,
0);
ndpi_set_proto_defaults(ndpi_str, 1 /* cleartext */, 1 /* app proto */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_JSON,
"JSON", NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_QOE_CATEGORY_UNSPECIFIED,
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */,
0);

#ifdef CUSTOM_NDPI_PROTOCOLS
#include "../../../nDPI-custom/custom_ndpi_main.c"
Expand Down Expand Up @@ -7453,6 +7457,9 @@ static int dissectors_init(struct ndpi_detection_module_struct *ndpi_str) {
/* MATTER */
init_matter_dissector(ndpi_str);

/* JSON */
init_json_dissector(ndpi_str);

#ifdef CUSTOM_NDPI_PROTOCOLS
#include "../../../nDPI-custom/custom_ndpi_main_init.c"
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/lib/protocols/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "ndpi_api.h"
#include "ndpi_private.h"

extern void ndpi_search_json(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow);

static const char* binary_exec_file_mimes_e[] = { "exe", NULL };
static const char* binary_exec_file_mimes_j[] = { "java-vm", NULL };
static const char* binary_exec_file_mimes_v[] = { "vnd.ms-cab-compressed", "vnd.microsoft.portable-executable", NULL };
Expand Down Expand Up @@ -164,6 +167,10 @@ static int ndpi_search_http_tcp_again(struct ndpi_detection_module_struct *ndpi_
return(0); /* We are good now */
}

if (flow->detected_protocol_stack[1] == NDPI_PROTOCOL_UNKNOWN) {
ndpi_search_json(ndpi_struct, flow);
}

/* Possibly more processing */
return(1);
}
Expand Down
123 changes: 123 additions & 0 deletions src/lib/protocols/json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* json.c
*
* Copyright (C) 2011-25 - 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_JSON

#include "ndpi_api.h"
#include "ndpi_private.h"

#define JSON_MAX_BYTES_TO_CHECK 16

static void ndpi_int_json_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
struct ndpi_flow_struct * const flow)
{
NDPI_LOG_INFO(ndpi_struct, "found JSON\n");
if (flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) {
ndpi_set_detected_protocol_keeping_master(ndpi_struct, flow, NDPI_PROTOCOL_JSON, NDPI_CONFIDENCE_DPI);
} else {
ndpi_set_detected_protocol(ndpi_struct, flow,
NDPI_PROTOCOL_JSON,
NDPI_PROTOCOL_UNKNOWN,
NDPI_CONFIDENCE_DPI);
}
}

void ndpi_search_json(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow)
{
struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
size_t offset = 0;
size_t bytes_checked = 0;

NDPI_LOG_DBG(ndpi_struct, "search JSON\n");

if (packet->payload_packet_len < 2) {
NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
return;
}

do {
if (offset >= packet->payload_packet_len) {
break;
}
if (packet->payload[offset] == '{' ||
packet->payload[offset] == '[')
{
break;
}
if (packet->payload[offset] != ' ' &&
packet->payload[offset] != '\t' &&
packet->payload[offset] != '\r' &&
packet->payload[offset] != '\n' &&
ndpi_isalnum(packet->payload[offset]) == 0 &&
offset >= 8)
{
NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
return;
}
} while (++offset < JSON_MAX_BYTES_TO_CHECK);

for (size_t i = offset; i < ndpi_min(JSON_MAX_BYTES_TO_CHECK, packet->payload_packet_len); ++i) {
if (ndpi_isprint(packet->payload[i]) == 0 &&
packet->payload[i] != '\t' &&
packet->payload[i] != '\r' &&
packet->payload[i] != '\n')
{
NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
return;
}
}

if (offset == JSON_MAX_BYTES_TO_CHECK || offset >= packet->payload_packet_len) {
NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
return;
}

offset = packet->payload_packet_len;

do {
if (packet->payload[offset - 1] == '}' ||
packet->payload[offset - 1] == ']')
{
break;
}
if (packet->payload[offset - 1] != ' ' &&
packet->payload[offset - 1] != '\t' &&
packet->payload[offset - 1] != '\r' &&
packet->payload[offset - 1] != '\n' &&
ndpi_isalnum(packet->payload[offset - 1]) == 0)
{
NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
return;
}
} while (--offset > 0 && ++bytes_checked < JSON_MAX_BYTES_TO_CHECK);

ndpi_int_json_add_connection(ndpi_struct, flow);
}

void init_json_dissector(struct ndpi_detection_module_struct *ndpi_struct)
{
register_dissector("JSON", ndpi_struct,
ndpi_search_json,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
1, NDPI_PROTOCOL_JSON);
}
2 changes: 1 addition & 1 deletion tests/cfgs/caches_cfg/result/ookla.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Guessed flow protos: 1
DPI Packets (TCP): 40 (6.67 pkts/flow)
Confidence Match by port : 1 (flows)
Confidence DPI : 5 (flows)
Num dissector calls: 572 (95.33 diss/flow)
Num dissector calls: 575 (95.83 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/caches_cfg/result/teams.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DPI Packets (other): 1 (1.00 pkts/flow)
Confidence Unknown : 1 (flows)
Confidence Match by port : 2 (flows)
Confidence DPI : 80 (flows)
Num dissector calls: 521 (6.28 diss/flow)
Num dissector calls: 523 (6.30 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/9/0 (insert/search/found)
LRU cache stun: 30/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/caches_global/result/lru_ipv6_caches.pcapng.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ DPI Packets (TCP): 9 (3.00 pkts/flow)
DPI Packets (UDP): 35 (3.89 pkts/flow)
Confidence DPI (cache) : 4 (flows)
Confidence DPI : 8 (flows)
Num dissector calls: 337 (28.08 diss/flow)
Num dissector calls: 339 (28.25 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 25/4/2 (insert/search/found)
LRU cache stun: 6/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/caches_global/result/ookla.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DPI Packets (TCP): 40 (6.67 pkts/flow)
Confidence DPI (partial cache): 1 (flows)
Confidence DPI : 4 (flows)
Confidence DPI (aggressive) : 1 (flows)
Num dissector calls: 572 (95.33 diss/flow)
Num dissector calls: 575 (95.83 diss/flow)
LRU cache ookla: 4/2/2 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/caches_global/result/teams.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Confidence Unknown : 1 (flows)
Confidence Match by port : 2 (flows)
Confidence DPI (partial) : 4 (flows)
Confidence DPI : 76 (flows)
Num dissector calls: 521 (6.28 diss/flow)
Num dissector calls: 523 (6.30 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/9/0 (insert/search/found)
LRU cache stun: 30/0/0 (insert/search/found)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (TCP): 10 (10.00 pkts/flow)
Confidence DPI : 1 (flows)
Num dissector calls: 227 (227.00 diss/flow)
Num dissector calls: 228 (228.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 5/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/classification_only/result/ookla.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DPI Packets (TCP): 38 (6.33 pkts/flow)
Confidence DPI (partial cache): 1 (flows)
Confidence DPI : 4 (flows)
Confidence DPI (aggressive) : 1 (flows)
Num dissector calls: 572 (95.33 diss/flow)
Num dissector calls: 575 (95.83 diss/flow)
LRU cache ookla: 4/2/2 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/classification_only/result/sip.pcap.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DPI Packets (UDP): 6 (1.50 pkts/flow)
Confidence Unknown : 1 (flows)
Confidence DPI : 3 (flows)
Num dissector calls: 320 (80.00 diss/flow)
Num dissector calls: 322 (80.50 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/classification_only/result/teams.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Confidence Unknown : 1 (flows)
Confidence Match by port : 2 (flows)
Confidence DPI (partial) : 6 (flows)
Confidence DPI : 74 (flows)
Num dissector calls: 521 (6.28 diss/flow)
Num dissector calls: 523 (6.30 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/9/0 (insert/search/found)
LRU cache stun: 24/0/0 (insert/search/found)
Expand Down
Binary file added tests/cfgs/default/pcap/json.pcapng
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/1kxun.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DPI Packets (UDP): 120 (1.21 pkts/flow)
Confidence Unknown : 9 (flows)
Confidence Match by port : 6 (flows)
Confidence DPI : 182 (flows)
Num dissector calls: 4485 (22.77 diss/flow)
Num dissector calls: 4497 (22.83 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/45/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/443-chrome.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Guessed flow protos: 1

DPI Packets (TCP): 1 (1.00 pkts/flow)
Confidence Match by port : 1 (flows)
Num dissector calls: 159 (159.00 diss/flow)
Num dissector calls: 160 (160.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/443-opvn.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (TCP): 6 (6.00 pkts/flow)
Confidence DPI : 1 (flows)
Num dissector calls: 160 (160.00 diss/flow)
Num dissector calls: 161 (161.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/4in4tunnel.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (UDP): 5 (5.00 pkts/flow)
Confidence Unknown : 1 (flows)
Num dissector calls: 192 (192.00 diss/flow)
Num dissector calls: 193 (193.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/6in6tunnel.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (UDP): 2 (2.00 pkts/flow)
Confidence Unknown : 1 (flows)
Num dissector calls: 159 (159.00 diss/flow)
Num dissector calls: 160 (160.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/EAQ.pcap.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DPI Packets (TCP): 12 (6.00 pkts/flow)
DPI Packets (UDP): 116 (4.00 pkts/flow)
Confidence DPI : 31 (flows)
Num dissector calls: 5209 (168.03 diss/flow)
Num dissector calls: 5238 (168.97 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (UDP): 28 (5.60 pkts/flow)
Confidence DPI : 5 (flows)
Num dissector calls: 169 (33.80 diss/flow)
Num dissector calls: 170 (34.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/KakaoTalk_chat.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DPI Packets (UDP): 36 (2.00 pkts/flow)
DPI Packets (other): 1 (1.00 pkts/flow)
Confidence Match by port : 5 (flows)
Confidence DPI : 33 (flows)
Num dissector calls: 527 (13.87 diss/flow)
Num dissector calls: 529 (13.92 diss/flow)
LRU cache ookla: 0/1/0 (insert/search/found)
LRU cache bittorrent: 0/15/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/KakaoTalk_talk.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DPI Packets (UDP): 17 (3.40 pkts/flow)
Confidence Match by port : 8 (flows)
Confidence DPI : 11 (flows)
Confidence Match by IP : 1 (flows)
Num dissector calls: 1229 (61.45 diss/flow)
Num dissector calls: 1235 (61.75 diss/flow)
LRU cache ookla: 0/2/0 (insert/search/found)
LRU cache bittorrent: 0/27/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/Oscar.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Guessed flow protos: 1

DPI Packets (TCP): 19 (19.00 pkts/flow)
Confidence Match by port : 1 (flows)
Num dissector calls: 243 (243.00 diss/flow)
Num dissector calls: 244 (244.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/3/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/alexa-app.pcapng.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DPI Packets (other): 6 (1.00 pkts/flow)
Confidence Match by port : 14 (flows)
Confidence DPI : 144 (flows)
Confidence Match by custom rule: 2 (flows)
Num dissector calls: 535 (3.34 diss/flow)
Num dissector calls: 536 (3.35 diss/flow)
LRU cache ookla: 0/5/0 (insert/search/found)
LRU cache bittorrent: 0/42/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/amqp.pcap.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (TCP): 9 (3.00 pkts/flow)
Confidence DPI : 3 (flows)
Num dissector calls: 371 (123.67 diss/flow)
Num dissector calls: 372 (124.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/anyconnect-vpn.pcap.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DPI Packets (other): 10 (1.00 pkts/flow)
Confidence Unknown : 2 (flows)
Confidence Match by port : 6 (flows)
Confidence DPI : 61 (flows)
Num dissector calls: 805 (11.67 diss/flow)
Num dissector calls: 807 (11.70 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/24/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgs/default/result/bittorrent_tcp_miss.pcapng.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DPI Packets (TCP): 10 (10.00 pkts/flow)
Confidence DPI : 1 (flows)
Num dissector calls: 227 (227.00 diss/flow)
Num dissector calls: 228 (228.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 5/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
Expand Down
Loading
Loading