|
| 1 | +/* |
| 2 | + * json.c |
| 3 | + * |
| 4 | + * Copyright (C) 2025 - Toni Uhlig <[email protected]> |
| 5 | + * |
| 6 | + * This file is part of nDPI, an open source deep packet inspection |
| 7 | + * library based on the OpenDPI and PACE technology by ipoque GmbH |
| 8 | + * |
| 9 | + * nDPI is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * nDPI is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include "ndpi_protocol_ids.h" |
| 25 | + |
| 26 | +#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_JSON |
| 27 | + |
| 28 | +#include "ndpi_api.h" |
| 29 | +#include "ndpi_private.h" |
| 30 | + |
| 31 | +static void ndpi_int_json_add_connection(struct ndpi_detection_module_struct * const ndpi_struct, |
| 32 | + struct ndpi_flow_struct * const flow) |
| 33 | +{ |
| 34 | + NDPI_LOG_INFO(ndpi_struct, "found JSON\n"); |
| 35 | + ndpi_set_detected_protocol(ndpi_struct, flow, |
| 36 | + NDPI_PROTOCOL_JSON, |
| 37 | + NDPI_PROTOCOL_UNKNOWN, |
| 38 | + NDPI_CONFIDENCE_DPI); |
| 39 | +} |
| 40 | + |
| 41 | +static void ndpi_search_json(struct ndpi_detection_module_struct *ndpi_struct, |
| 42 | + struct ndpi_flow_struct *flow) |
| 43 | +{ |
| 44 | + struct ndpi_packet_struct const * const packet = &ndpi_struct->packet; |
| 45 | + size_t offset = 0; |
| 46 | + size_t bytes_checked = 0; |
| 47 | + const size_t max_bytes_to_check = 16; |
| 48 | + |
| 49 | + NDPI_LOG_DBG(ndpi_struct, "search JSON\n"); |
| 50 | + |
| 51 | + if (packet->payload_packet_len < 2) { |
| 52 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + do { |
| 57 | + if (offset >= packet->payload_packet_len) { |
| 58 | + break; |
| 59 | + } |
| 60 | + if (packet->payload[offset] == '{') { |
| 61 | + break; |
| 62 | + } |
| 63 | + if (packet->payload[offset] != ' ' && |
| 64 | + packet->payload[offset] != '\t' && |
| 65 | + packet->payload[offset] != '\r' && |
| 66 | + packet->payload[offset] != '\n' && |
| 67 | + isalnum(packet->payload[offset]) == 0 && |
| 68 | + offset >= 8) |
| 69 | + { |
| 70 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 71 | + return; |
| 72 | + } |
| 73 | + } while (++offset < max_bytes_to_check); |
| 74 | + |
| 75 | + if (offset == max_bytes_to_check || offset >= packet->payload_packet_len) { |
| 76 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + offset = packet->payload_packet_len; |
| 81 | + |
| 82 | + do { |
| 83 | + if (packet->payload[offset - 1] == '}') { |
| 84 | + break; |
| 85 | + } |
| 86 | + if (packet->payload[offset - 1] != ' ' && |
| 87 | + packet->payload[offset - 1] != '\t' && |
| 88 | + packet->payload[offset - 1] != '\r' && |
| 89 | + packet->payload[offset - 1] != '\n' && |
| 90 | + isalnum(packet->payload[offset - 1]) == 0) |
| 91 | + { |
| 92 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 93 | + return; |
| 94 | + } |
| 95 | + } while (--offset > 0 && ++bytes_checked < max_bytes_to_check); |
| 96 | + |
| 97 | + if (bytes_checked == max_bytes_to_check) { |
| 98 | + NDPI_EXCLUDE_DISSECTOR(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) |
| 106 | +{ |
| 107 | + register_dissector("JSON", ndpi_struct, |
| 108 | + ndpi_search_json, |
| 109 | + NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
| 110 | + 1, NDPI_PROTOCOL_JSON); |
| 111 | +} |
0 commit comments