aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2022-06-04 17:14:06 +0200
committerlns <matzeton@googlemail.com>2022-06-04 17:57:56 +0200
commitc9ab5573765b605ed56b3f4781124df457ad3b52 (patch)
treebb0a1f93626168d202b7d2f5483a26f0e5d6bd0f /src/lib
parent338427d71290a3df44c9f43b49a8609964ec63a6 (diff)
Added RSH dissector. Fixes #202.add/rsh
- added syslog false-positive pcap that was missing in 09fbe0a64a11b08a35435f516e9a19f7e0c20d7c - added NDPI_ARRAY_LENGTH() macro, usable on `type var[]` declarations Signed-off-by: lns <matzeton@googlemail.com>
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ndpi_main.c7
-rw-r--r--src/lib/protocols/rsh.c163
2 files changed, 170 insertions, 0 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index 1e90d4bf1..88730350a 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -1898,6 +1898,10 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
"MpegDash", NDPI_PROTOCOL_CATEGORY_MEDIA,
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, 1 /* cleartext */, NDPI_PROTOCOL_UNSAFE, NDPI_PROTOCOL_RSH,
+ "RSH", NDPI_PROTOCOL_CATEGORY_REMOTE_ACCESS,
+ ndpi_build_default_ports(ports_a, 514, 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"
@@ -4391,6 +4395,9 @@ static int ndpi_callback_init(struct ndpi_detection_module_struct *ndpi_str) {
/* MpegDash */
init_mpegdash_dissector(ndpi_str, &a, detection_bitmask);
+ /* RSH */
+ init_rsh_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/rsh.c b/src/lib/protocols/rsh.c
new file mode 100644
index 000000000..3344c8660
--- /dev/null
+++ b/src/lib/protocols/rsh.c
@@ -0,0 +1,163 @@
+/*
+ * rsh.c
+ *
+ * Copyright (C) 2022 - ntop.org
+ *
+ * This file is part of nDPI, an open source deep packet inspection
+ * library based on the OpenDPI and PACE technology by ipoque GmbH
+ *
+ * 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_RSH
+
+#include "ndpi_api.h"
+
+#define RSH_DEFAULT_PORT 514
+
+static void ndpi_int_rsh_add_connection(struct ndpi_detection_module_struct * ndpi_struct,
+ struct ndpi_flow_struct * flow)
+{
+ ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_RSH,
+ NDPI_CONFIDENCE_DPI);
+}
+
+void ndpi_search_rsh(struct ndpi_detection_module_struct * ndpi_struct,
+ struct ndpi_flow_struct *flow)
+{
+ struct ndpi_packet_struct *packet = &ndpi_struct->packet;
+
+ /* Use a port based approach for midstream detection. */
+ if (packet->tcp->dest == RSH_DEFAULT_PORT ||
+ packet->tcp->source == RSH_DEFAULT_PORT)
+ {
+ if (packet->payload[packet->payload_packet_len - 1] == '\n')
+ {
+ if (flow->packet_counter > 5)
+ {
+ ndpi_int_rsh_add_connection(ndpi_struct, flow);
+ flow->protos.rsh.client_username[0] = '\0';
+ flow->protos.rsh.server_username[0] = '\0';
+ flow->protos.rsh.command[0] = '\0';
+ }
+ return;
+ }
+ }
+
+ switch (flow->packet_counter)
+ {
+ case 1:
+ if (packet->payload_packet_len >= 2 &&
+ packet->payload_packet_len <= 6)
+ {
+ int i;
+
+ for (i = 0; i < packet->payload_packet_len - 1; ++i)
+ {
+ if (ndpi_isdigit(packet->payload[i]) == 0)
+ {
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+ return;
+ }
+ }
+ } else {
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+ }
+ return;
+
+ case 2:
+ if (packet->payload_packet_len < 3 ||
+ packet->payload[packet->payload_packet_len - 1] != '\0')
+ {
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+ return;
+ }
+
+ {
+ char const * dissected_info[] = { (char const *)packet->payload,
+ NULL, NULL };
+ size_t i;
+
+ for (i = 1; i < NDPI_ARRAY_LENGTH(dissected_info); ++i)
+ {
+ dissected_info[i] = memchr(dissected_info[i - 1], '\0',
+ packet->payload_packet_len -
+ (dissected_info[i - 1] - dissected_info[0]));
+
+ if (dissected_info[i] == NULL ||
+ ndpi_is_printable_buffer((uint8_t const *)dissected_info[i - 1],
+ (dissected_info[i] - dissected_info[i - 1])) == 0)
+ {
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+ return;
+ }
+
+ if (dissected_info[i] - dissected_info[0] >= packet->payload_packet_len - 1)
+ {
+ if (dissected_info[NDPI_ARRAY_LENGTH(dissected_info) - 1] == NULL)
+ {
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+ return;
+ }
+ break;
+ }
+
+ dissected_info[i]++;
+ }
+
+ ndpi_int_rsh_add_connection(ndpi_struct, flow);
+
+ strncpy(flow->protos.rsh.client_username, dissected_info[0],
+ ndpi_min(NDPI_ARRAY_LENGTH(flow->protos.rsh.client_username),
+ (unsigned long)(dissected_info[1] - dissected_info[0])));
+ strncpy(flow->protos.rsh.server_username, dissected_info[1],
+ ndpi_min(NDPI_ARRAY_LENGTH(flow->protos.rsh.server_username),
+ (unsigned long)(dissected_info[2] - dissected_info[1])));
+ strncpy(flow->protos.rsh.command, dissected_info[2],
+ ndpi_min(NDPI_ARRAY_LENGTH(flow->protos.rsh.command),
+ (unsigned long)packet->payload_packet_len -
+ (unsigned long)(dissected_info[2] - dissected_info[0])));
+
+ char str[64];
+ if (snprintf(str, NDPI_ARRAY_LENGTH(str), "User '%s' executing '%s'",
+ flow->protos.rsh.server_username,
+ flow->protos.rsh.command) < 0)
+ {
+ str[0] = '\0';
+ }
+ ndpi_set_risk(ndpi_struct, flow, NDPI_CLEAR_TEXT_CREDENTIALS, str);
+ }
+ return;
+
+ default:
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+ return;
+ }
+}
+
+
+void init_rsh_dissector(struct ndpi_detection_module_struct * ndpi_struct,
+ u_int32_t * id, NDPI_PROTOCOL_BITMASK * detection_bitmask)
+{
+ ndpi_set_bitmask_protocol_detection("RSH", ndpi_struct, detection_bitmask, *id,
+ NDPI_PROTOCOL_RSH, ndpi_search_rsh,
+ NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
+ SAVE_DETECTION_BITMASK_AS_UNKNOWN,
+ ADD_TO_DETECTION_BITMASK);
+
+ *id += 1;
+}