diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-06-29 02:29:57 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-06-29 02:54:46 +0200 |
commit | 761a29d20509c97748de297b9baf4bfa4dcd7497 (patch) | |
tree | b9b99412c1d4dbac6ab8d2dafe61d7427665950a /src/lib | |
parent | f19937c8c9fd4e1c21988f523d9e78b954f6fcc8 (diff) |
Added Z39.50 protocol.add/z3950
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/ndpi_main.c | 8 | ||||
-rw-r--r-- | src/lib/protocols/z3950.c | 152 |
2 files changed, 160 insertions, 0 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index ff4b0cb5d..648fdbfee 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1681,6 +1681,11 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp "FortiClient", NDPI_PROTOCOL_CATEGORY_VPN, ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); + ndpi_set_proto_defaults(ndpi_str, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_Z3950, + "Z39.50", + NDPI_PROTOCOL_CATEGORY_NETWORK, + ndpi_build_default_ports(ports_a, 210, 0, 0, 0, 0) /* TCP */, + ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); #ifdef CUSTOM_NDPI_PROTOCOLS #include "../../../nDPI-custom/custom_ndpi_main.c" @@ -3871,6 +3876,9 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* Genshin Impact */ init_genshin_impact_dissector(ndpi_str, &a, detection_bitmask); + /* Z39.50 international standard client–server, application layer communications protocol */ + init_z3950_dissector(ndpi_str, &a, detection_bitmask); + #ifdef CUSTOM_NDPI_PROTOCOLS #include "../../../nDPI-custom/custom_ndpi_main_init.c" #endif diff --git a/src/lib/protocols/z3950.c b/src/lib/protocols/z3950.c new file mode 100644 index 000000000..b267c776d --- /dev/null +++ b/src/lib/protocols/z3950.c @@ -0,0 +1,152 @@ +/* + * z3950.c + * + * Copyright (C) 2012-21 - ntop.org + * + * This module 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. + * + * This module 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. + * If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "ndpi_protocol_ids.h" + +#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_Z3950 + +#include <stdlib.h> +#include "ndpi_api.h" + +/* https://github.com/wireshark/wireshark/blob/master/epan/dissectors/asn1/z3950/z3950.asn */ + +static void ndpi_int_z3950_add_connection(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow) +{ + ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_Z3950, NDPI_PROTOCOL_UNKNOWN); +} + +static int z3950_parse_sequences(struct ndpi_packet_struct const * const packet, + int max_sequences) +{ + size_t payload_offset = 2; + int cur_sequences = 0; + u_int8_t pdu_type; + + if (packet->payload_packet_len < 2) + { + return -1; + } + pdu_type = packet->payload[0] & 0x1F; + if ((pdu_type < 20 || pdu_type > 36) && (pdu_type < 43 || pdu_type > 48)) + { + return -1; + } + + while (cur_sequences++ < max_sequences) + { + if (payload_offset + 2 >= packet->payload_packet_len) + { + return -1; + } + + u_int8_t const * payload; + u_int8_t seq_type; + u_int8_t seq_length; + + payload = &packet->payload[payload_offset]; + if (payload[0] == 0x9F) + { + if (payload_offset + 3 >= packet->payload_packet_len) + { + return -1; + } + payload_offset++; + payload = &packet->payload[payload_offset]; + seq_type = payload[0]; + } else { + seq_type = payload[0] & 0x1F; + } + seq_length = payload[1]; + + if (seq_type > 51 && (seq_type < 100 || seq_type > 105) && + (seq_type < 110 || seq_type > 112) && (seq_type < 120 || seq_type > 121) && + (seq_type < 201 || seq_type > 221)) + { + return -1; + } + + if (seq_length >= packet->payload_packet_len - payload_offset + 1) + { + return -1; + } + + payload_offset += seq_length + 2; + + if (payload_offset == packet->payload_packet_len) + { + return cur_sequences; + } + } + + return cur_sequences - 1; +} + +static void ndpi_search_z3950(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow) +{ + struct ndpi_packet_struct * packet = &flow->packet; + int const minimum_expected_sequences = 7; + + NDPI_LOG_DBG(ndpi_struct, "search z39.50\n"); + + if (packet->tcp != NULL && packet->payload_packet_len >= 6 && + flow->packet_counter >= 1 && flow->packet_counter <= 8) + { + int ret = z3950_parse_sequences(packet, minimum_expected_sequences); + if (ret < 0) + { + NDPI_EXCLUDE_PROTO(ndpi_struct, flow); + return; + } + if (ret < minimum_expected_sequences) + { + /* We've seen not enough sequences, wait for the next packet. */ + return; + } + + if (flow->z3950_stage == 3) + { + ndpi_int_z3950_add_connection(ndpi_struct, flow); + } else { + flow->z3950_stage++; + } + return; + } + + NDPI_EXCLUDE_PROTO(ndpi_struct, flow); +} + + +/* ***************************************************************** */ + +void init_z3950_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, + NDPI_PROTOCOL_BITMASK *detection_bitmask) +{ + ndpi_set_bitmask_protocol_detection("Z39.50", + ndpi_struct, detection_bitmask, *id, + NDPI_PROTOCOL_Z3950, + ndpi_search_z3950, + NDPI_SELECTION_BITMASK_PROTOCOL_TCP_WITH_PAYLOAD, + SAVE_DETECTION_BITMASK_AS_UNKNOWN, + ADD_TO_DETECTION_BITMASK); + + *id += 1; +} |