aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVladimir Gavrilov <105977161+0xA50C1A1@users.noreply.github.com>2023-11-30 11:13:45 +0300
committerGitHub <noreply@github.com>2023-11-30 09:13:45 +0100
commit24df1913ac7bd585a010d1b1601768206e578353 (patch)
treebcb902d53014c046b6b1c20a4c831e43e08a1291 /src
parent6f046df0dcb07dae74be127af555ad96e8576acf (diff)
Add Beckhoff ADS protocol dissector (#2181)
* Add Beckhoff ADS protocol dissector * Remove redundant le32toh * Fix detection on big-endian architectures
Diffstat (limited to 'src')
-rw-r--r--src/include/ndpi_protocol_ids.h1
-rw-r--r--src/lib/ndpi_main.c7
-rw-r--r--src/lib/ndpi_private.h1
-rw-r--r--src/lib/protocols/beckhoff_ads.c125
4 files changed, 134 insertions, 0 deletions
diff --git a/src/include/ndpi_protocol_ids.h b/src/include/ndpi_protocol_ids.h
index a1f0732ea..01c7ff69e 100644
--- a/src/include/ndpi_protocol_ids.h
+++ b/src/include/ndpi_protocol_ids.h
@@ -393,6 +393,7 @@ typedef enum {
NDPI_PROTOCOL_FINS = 362,
NDPI_PROTOCOL_ETHERSIO = 363,
NDPI_PROTOCOL_UMAS = 364,
+ NDPI_PROTOCOL_BECKHOFF_ADS = 365,
#ifdef CUSTOM_NDPI_PROTOCOLS
#include "../../../nDPI-custom/custom_ndpi_protocol_ids.h"
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index 58c64728b..073850376 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -2158,6 +2158,10 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
"UMAS", NDPI_PROTOCOL_CATEGORY_IOT_SCADA,
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 */, 0 /* nw proto */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_BECKHOFF_ADS,
+ "BeckhoffADS", NDPI_PROTOCOL_CATEGORY_IOT_SCADA,
+ ndpi_build_default_ports(ports_a, 48898, 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"
@@ -5606,6 +5610,9 @@ static int ndpi_callback_init(struct ndpi_detection_module_struct *ndpi_str) {
/* Ether-S-I/O */
init_ethersio_dissector(ndpi_str, &a);
+ /* Automation Device Specification */
+ init_beckhoff_ads_dissector(ndpi_str, &a);
+
#ifdef CUSTOM_NDPI_PROTOCOLS
#include "../../../nDPI-custom/custom_ndpi_main_init.c"
#endif
diff --git a/src/lib/ndpi_private.h b/src/lib/ndpi_private.h
index 436b1f53e..0d18be6d4 100644
--- a/src/lib/ndpi_private.h
+++ b/src/lib/ndpi_private.h
@@ -624,6 +624,7 @@ void init_rtps_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int
void init_opc_ua_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
void init_fins_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
void init_ethersio_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
+void init_beckhoff_ads_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
#endif
diff --git a/src/lib/protocols/beckhoff_ads.c b/src/lib/protocols/beckhoff_ads.c
new file mode 100644
index 000000000..ea3287116
--- /dev/null
+++ b/src/lib/protocols/beckhoff_ads.c
@@ -0,0 +1,125 @@
+/*
+ * beckhoff_ads.c
+ *
+ * Beckhoff Automation Device Specification
+ *
+ * Copyright (C) 2023 - ntop.org
+ * Copyright (C) 2023 - V.G <jacendi@protonmail.com>
+ *
+ * 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_BECKHOFF_ADS
+
+#include "ndpi_api.h"
+#include "ndpi_private.h"
+
+PACK_ON
+struct ams_tcp_hdr {
+ u_int16_t reserved;
+ u_int32_t length;
+} PACK_OFF;
+
+struct ams_hdr {
+#if defined(__LITTLE_ENDIAN__)
+ u_int64_t target_netid : 48;
+ u_int64_t target_port : 16;
+ u_int64_t source_netid : 48;
+ u_int64_t source_port : 16;
+#elif defined(__BIG_ENDIAN__)
+ u_int64_t target_port : 16;
+ u_int64_t target_netid : 48;
+ u_int64_t source_port : 16;
+ u_int64_t source_netid : 48;
+#else
+#error "Missing endian macro definitions."
+#endif
+ u_int16_t command_id;
+ u_int16_t state_flags;
+ u_int32_t length;
+ u_int32_t error_code;
+ u_int32_t invoke_id;
+};
+
+static void ndpi_int_beckhoff_ads_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
+ struct ndpi_flow_struct * const flow)
+{
+ NDPI_LOG_INFO(ndpi_struct, "found Beckhoff ADS\n");
+ ndpi_set_detected_protocol(ndpi_struct, flow,
+ NDPI_PROTOCOL_BECKHOFF_ADS,
+ NDPI_PROTOCOL_UNKNOWN,
+ NDPI_CONFIDENCE_DPI);
+}
+
+static void ndpi_search_beckhoff_ads(struct ndpi_detection_module_struct *ndpi_struct,
+ struct ndpi_flow_struct *flow)
+{
+ struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
+
+ NDPI_LOG_DBG(ndpi_struct, "search Beckhoff ADS\n");
+
+ if (packet->payload_packet_len >= 38) {
+ struct ams_tcp_hdr const * const ams_tcp = (struct ams_tcp_hdr *)packet->payload;
+ u_int16_t ams_message_length = packet->payload_packet_len - sizeof(struct ams_tcp_hdr);
+
+ if ((ams_tcp->reserved != 0) ||
+ (le32toh(ams_tcp->length) != ams_message_length))
+ {
+ goto not_beckhoff_ads;
+ }
+
+ struct ams_hdr const * const ams = (struct ams_hdr *)&packet->payload[6];
+ u_int16_t ams_data_len = ams_message_length - sizeof(struct ams_hdr);
+
+ if (le32toh(ams->length) == ams_data_len) {
+ /* Just additional checks to avoid potential
+ * false positives */
+ if ((le16toh(ams->state_flags) != 0x0004) &&
+ (le16toh(ams->state_flags) != 0x0005))
+ {
+ goto not_beckhoff_ads;
+ }
+
+ if ((le16toh(ams->command_id) > 0x0009) ||
+ (le32toh(ams->error_code) > 0x0000001E))
+ {
+ goto not_beckhoff_ads;
+ }
+
+ ndpi_int_beckhoff_ads_add_connection(ndpi_struct, flow);
+ return;
+ }
+ }
+
+not_beckhoff_ads:
+ NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
+}
+
+void init_beckhoff_ads_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id)
+{
+ ndpi_set_bitmask_protocol_detection("BeckhoffADS", ndpi_struct, *id,
+ NDPI_PROTOCOL_BECKHOFF_ADS,
+ ndpi_search_beckhoff_ads,
+ NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
+ SAVE_DETECTION_BITMASK_AS_UNKNOWN,
+ ADD_TO_DETECTION_BITMASK);
+
+ *id += 1;
+}