Replace complicated TPKT header validation with an helper function (#2201)
* Replace complicated TPKT header validation with an helper function * Move tpkt_verify_hdr function definition to ndpi_utils.c
This commit is contained in:
@@ -417,7 +417,8 @@ u_int32_t make_mining_key(struct ndpi_flow_struct *flow);
|
||||
/* Stun */
|
||||
int stun_search_into_zoom_cache(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
|
||||
|
||||
|
||||
/* TPKT */
|
||||
int tpkt_verify_hdr(const struct ndpi_packet_struct * const packet);
|
||||
|
||||
/* Protocols init */
|
||||
void init_diameter_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id);
|
||||
|
||||
@@ -3023,3 +3023,11 @@ u_int32_t ndpi_nearest_power_of_two(u_int32_t x) {
|
||||
return(x);
|
||||
}
|
||||
|
||||
/* ******************************************* */
|
||||
|
||||
int tpkt_verify_hdr(const struct ndpi_packet_struct * const packet)
|
||||
{
|
||||
return ((packet->tcp != NULL) && (packet->payload_packet_len > 4) &&
|
||||
(packet->payload[0] == 3) && (packet->payload[1] == 0) &&
|
||||
(get_u_int16_t(packet->payload,2) == htons(packet->payload_packet_len)));
|
||||
}
|
||||
|
||||
@@ -37,23 +37,18 @@ static void ndpi_int_h323_add_connection(struct ndpi_detection_module_struct *nd
|
||||
static void ndpi_search_h323(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow)
|
||||
{
|
||||
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
||||
const struct ndpi_packet_struct * const packet = &ndpi_struct->packet;
|
||||
u_int16_t dport = 0, sport = 0;
|
||||
|
||||
NDPI_LOG_DBG(ndpi_struct, "search H323\n");
|
||||
|
||||
/* TPKT header length + Q.931 header length without IE */
|
||||
if ((packet->payload_packet_len) > 10 && (packet->tcp != NULL)) {
|
||||
if ((packet->payload[0] == 0x03) &&
|
||||
(packet->payload[1] == 0x00) &&
|
||||
(ntohs(get_u_int16_t(packet->payload, 2)) == packet->payload_packet_len))
|
||||
{
|
||||
if (tpkt_verify_hdr(packet) && (packet->payload_packet_len > 10)) {
|
||||
/* Check Q.931 Protocol Discriminator and call reference value length */
|
||||
if ((packet->payload[4] == 0x08) && ((packet->payload[5] & 0xF) <= 3)) {
|
||||
ndpi_int_h323_add_connection(ndpi_struct, flow);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (packet->udp != NULL) {
|
||||
sport = ntohs(packet->udp->source), dport = ntohs(packet->udp->dest);
|
||||
NDPI_LOG_DBG2(ndpi_struct, "calculated dport over udp\n");
|
||||
|
||||
@@ -47,9 +47,7 @@ static void ndpi_search_iso9506_1_mms(struct ndpi_detection_module_struct *ndpi_
|
||||
|
||||
NDPI_LOG_DBG(ndpi_struct, "search ISO 9506-1 MMS\n");
|
||||
|
||||
if ((packet->payload_packet_len > 60) && (packet->payload[0] == 3) &&
|
||||
(packet->payload[1] == 0) &&
|
||||
(get_u_int16_t(packet->payload, 2) == htons(packet->payload_packet_len)))
|
||||
if ((packet->payload_packet_len > 60) && tpkt_verify_hdr(packet))
|
||||
{
|
||||
if (current_pkt_from_client_to_server(ndpi_struct, flow)) {
|
||||
/* Check COTP and ISO 8327-1 headers */
|
||||
|
||||
@@ -41,15 +41,13 @@ static void ndpi_int_rdp_add_connection(struct ndpi_detection_module_struct *ndp
|
||||
|
||||
static void ndpi_search_rdp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
|
||||
const struct ndpi_packet_struct * const packet = &ndpi_struct->packet;
|
||||
|
||||
NDPI_LOG_DBG(ndpi_struct, "search RDP\n");
|
||||
|
||||
if (packet->tcp != NULL) {
|
||||
if(packet->payload_packet_len > 13 &&
|
||||
/* TPKT */
|
||||
packet->payload[0] == 0x03 && packet->payload[1] == 0x00 &&
|
||||
ntohs(*(uint16_t *)&packet->payload[2]) == packet->payload_packet_len &&
|
||||
tpkt_verify_hdr(packet) &&
|
||||
/* COTP */
|
||||
packet->payload[4] == packet->payload_packet_len - 5) {
|
||||
|
||||
|
||||
+20
-30
@@ -40,39 +40,29 @@ static void ndpi_search_s7comm(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
|
||||
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 (tpkt_verify_hdr(packet) && (packet->payload_packet_len > 17) &&
|
||||
((packet->tcp->source == htons(TPKT_PORT)) ||
|
||||
(packet->tcp->dest == htons(TPKT_PORT))))
|
||||
{
|
||||
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))
|
||||
{
|
||||
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_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);
|
||||
|
||||
Reference in New Issue
Block a user