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