-
Notifications
You must be signed in to change notification settings - Fork 894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework S7Comm dissector; add S7Comm Plus support #2165
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9f6f322
Rework S7Comm dissector; add S7Comm Plus support
6679cc0
Cleanup s7comm.c
0xA50C1A1 538911c
Improve S7Comm Plus detection
0xA50C1A1 1f1a2ec
Merge branch 'dev' into rework/s7comm
0xA50C1A1 ec0a3a2
s7comm/s7commplus: faster detection
IvanNardi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
/* | ||
* s7comm.c | ||
* | ||
* Copyright (C) 2011-22 - ntop.org | ||
* | ||
* Copyright (C) 2023 - ntop.org | ||
* Copyright (C) 2023 - V.G <[email protected]> | ||
* | ||
* This file is part of nDPI, an open source deep packet inspection | ||
* library based on the OpenDPI and PACE technology by ipoque GmbH | ||
* | ||
|
@@ -20,40 +21,70 @@ | |
* along with nDPI. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "ndpi_protocol_ids.h" | ||
|
||
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_S7COMM | ||
|
||
#include "ndpi_api.h" | ||
#include "ndpi_private.h" | ||
|
||
static void ndpi_search_s7comm_tcp(struct ndpi_detection_module_struct *ndpi_struct, | ||
struct ndpi_flow_struct *flow) { | ||
struct ndpi_packet_struct *packet = &ndpi_struct->packet; | ||
NDPI_LOG_DBG(ndpi_struct, "search S7\n"); | ||
u_int16_t s7comm_port = htons(102); | ||
if(packet->tcp) { | ||
|
||
if((packet->payload_packet_len >= 2) && (packet->payload[0]==0x03)&&(packet->payload[1]==0x00)&&((packet->tcp->dest == s7comm_port) || (packet->tcp->source == s7comm_port))) { | ||
NDPI_LOG_INFO(ndpi_struct, "found S7\n"); | ||
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_S7COMM, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); | ||
|
||
return; | ||
|
||
#define TPKT_PORT 102 | ||
#define S7COMM_MAGIC_BYTE 0x32 | ||
#define S7COMM_PLUS_MAGIC_BYTE 0x72 | ||
|
||
static void ndpi_search_s7comm(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 S7comm\n"); | ||
|
||
if (packet->tcp) { | ||
u_int16_t sport = ntohs(packet->tcp->source); | ||
u_int16_t dport = ntohs(packet->tcp->dest); | ||
|
||
/* S7Comm uses a default TPKT port 102 */ | ||
if (((sport == TPKT_PORT) || (dport == TPKT_PORT)) && | ||
(packet->payload_packet_len > 17)) /* TPKT+COTP+S7Comm header lengths */ | ||
{ | ||
if ((packet->payload[0] == 0x03) && (packet->payload[1] == 0x00) && | ||
(ntohs(get_u_int16_t(packet->payload, 2)) == packet->payload_packet_len)) | ||
{ | ||
if (packet->payload[7] == S7COMM_PLUS_MAGIC_BYTE) { | ||
const u_int16_t trail_byte_offset = packet->payload_packet_len - 4; | ||
if (packet->payload[trail_byte_offset] == S7COMM_PLUS_MAGIC_BYTE) | ||
{ | ||
NDPI_LOG_INFO(ndpi_struct, "found S7CommPlus\n"); | ||
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_S7COMM_PLUS, | ||
NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); | ||
return; | ||
} | ||
} else if (packet->payload[7] == S7COMM_MAGIC_BYTE) { | ||
if (((packet->payload[8] <= 0x03) || (packet->payload[8] == 0x07)) && | ||
(get_u_int16_t(packet->payload, 9) == 0)) | ||
{ | ||
NDPI_LOG_INFO(ndpi_struct, "found S7Comm\n"); | ||
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_S7COMM, | ||
NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); | ||
return; | ||
} | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
|
||
NDPI_EXCLUDE_PROTO(ndpi_struct, flow); | ||
|
||
} | ||
|
||
void init_s7comm_dissector(struct ndpi_detection_module_struct *ndpi_struct, | ||
u_int32_t *id) { | ||
|
||
ndpi_set_bitmask_protocol_detection("S7COMM", ndpi_struct, *id, | ||
NDPI_PROTOCOL_S7COMM, | ||
ndpi_search_s7comm_tcp, | ||
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, | ||
SAVE_DETECTION_BITMASK_AS_UNKNOWN, | ||
ADD_TO_DETECTION_BITMASK); | ||
void init_s7comm_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id) | ||
{ | ||
ndpi_set_bitmask_protocol_detection("S7Comm", ndpi_struct, *id, | ||
NDPI_PROTOCOL_S7COMM, | ||
ndpi_search_s7comm, | ||
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, | ||
SAVE_DETECTION_BITMASK_AS_UNKNOWN, | ||
ADD_TO_DETECTION_BITMASK); | ||
*id += 1; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TCP port 102 is assigned to the TSAP, so I set it to 0 to avoid false positives when guessing. Yea, S7Comm uses this port, but I left the check inside the dissector.