Improved OpenVPN support for UDP-based VPNs

This commit is contained in:
Luca Deri
2019-06-21 12:01:07 +02:00
parent b6ac750e21
commit b66606bc16
+32 -7
View File
@@ -40,12 +40,19 @@
#define P_PACKET_ID_ARRAY_LEN_OFFSET(hmac_size) (P_HARD_RESET_PACKET_ID_OFFSET(hmac_size) + 8) #define P_PACKET_ID_ARRAY_LEN_OFFSET(hmac_size) (P_HARD_RESET_PACKET_ID_OFFSET(hmac_size) + 8)
#define P_HARD_RESET_CLIENT_MAX_COUNT 5 #define P_HARD_RESET_CLIENT_MAX_COUNT 5
static void ndpi_int_openvpn_add_connection(struct ndpi_detection_module_struct
*ndpi_struct, struct ndpi_flow_struct *flow) {
NDPI_LOG_INFO(ndpi_struct, "found memcached\n");
ndpi_set_detected_protocol(ndpi_struct, flow,
NDPI_PROTOCOL_MEMCACHED, NDPI_PROTOCOL_UNKNOWN);
}
static static
#ifndef WIN32 #ifndef WIN32
inline inline
#endif #endif
u_int32_t get_packet_id(const u_int8_t * payload, u_int8_t hms) { u_int32_t get_packet_id(const u_int8_t * payload, u_int8_t hms) {
return ntohl(*(u_int32_t*)(payload + P_HARD_RESET_PACKET_ID_OFFSET(hms))); return(ntohl(*(u_int32_t*)(payload + P_HARD_RESET_PACKET_ID_OFFSET(hms))));
} }
static static
@@ -56,9 +63,11 @@ int8_t check_pkid_and_detect_hmac_size(const u_int8_t * payload) {
// try to guess // try to guess
if(get_packet_id(payload, P_HMAC_160) == 1) if(get_packet_id(payload, P_HMAC_160) == 1)
return P_HMAC_160; return P_HMAC_160;
if(get_packet_id(payload, P_HMAC_128) == 1) if(get_packet_id(payload, P_HMAC_128) == 1)
return P_HMAC_128; return P_HMAC_128;
return -1;
return(-1);
} }
void ndpi_search_openvpn(struct ndpi_detection_module_struct* ndpi_struct, void ndpi_search_openvpn(struct ndpi_detection_module_struct* ndpi_struct,
@@ -78,9 +87,25 @@ void ndpi_search_openvpn(struct ndpi_detection_module_struct* ndpi_struct,
opcode = ovpn_payload[0] & P_OPCODE_MASK; opcode = ovpn_payload[0] & P_OPCODE_MASK;
if(packet->udp) {
#ifdef DEBUG
printf("[opcode: %u][Packet ID: %d][%u <-> %u][len: %u]\n", opcode, check_pkid_and_detect_hmac_size(ovpn_payload),
htons(packet->udp->source), htons(packet->udp->dest), packet->payload_packet_len);
#endif
if(
((packet->payload_packet_len == 112) && (opcode == 168))
|| ((packet->payload_packet_len == 80)
&& ((opcode == 184) || (opcode == 88) || (opcode == 160) || (opcode == 168)))
) {
NDPI_LOG_INFO(ndpi_struct,"found openvpn\n");
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_OPENVPN, NDPI_PROTOCOL_UNKNOWN);
return;
}
}
if(flow->ovpn_counter < P_HARD_RESET_CLIENT_MAX_COUNT && (opcode == P_CONTROL_HARD_RESET_CLIENT_V1 || if(flow->ovpn_counter < P_HARD_RESET_CLIENT_MAX_COUNT && (opcode == P_CONTROL_HARD_RESET_CLIENT_V1 ||
opcode == P_CONTROL_HARD_RESET_CLIENT_V2)) { opcode == P_CONTROL_HARD_RESET_CLIENT_V2)) {
if(check_pkid_and_detect_hmac_size(ovpn_payload) > 0) { if(check_pkid_and_detect_hmac_size(ovpn_payload) > 0) {
memcpy(flow->ovpn_session_id, ovpn_payload+1, 8); memcpy(flow->ovpn_session_id, ovpn_payload+1, 8);
@@ -101,8 +126,8 @@ void ndpi_search_openvpn(struct ndpi_detection_module_struct* ndpi_struct,
if(memcmp(flow->ovpn_session_id, session_remote, 8) == 0) { if(memcmp(flow->ovpn_session_id, session_remote, 8) == 0) {
NDPI_LOG_INFO(ndpi_struct,"found openvpn\n"); NDPI_LOG_INFO(ndpi_struct,"found openvpn\n");
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_OPENVPN, NDPI_PROTOCOL_UNKNOWN); ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_OPENVPN, NDPI_PROTOCOL_UNKNOWN);
} return;
else { } else {
NDPI_LOG_DBG2(ndpi_struct, NDPI_LOG_DBG2(ndpi_struct,
"key mismatch: %02x%02x%02x%02x%02x%02x%02x%02x\n", "key mismatch: %02x%02x%02x%02x%02x%02x%02x%02x\n",
session_remote[0], session_remote[1], session_remote[2], session_remote[3], session_remote[0], session_remote[1], session_remote[2], session_remote[3],
@@ -122,8 +147,8 @@ void ndpi_search_openvpn(struct ndpi_detection_module_struct* ndpi_struct,
} }
} }
void init_openvpn_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) void init_openvpn_dissector(struct ndpi_detection_module_struct *ndpi_struct,
{ u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) {
ndpi_set_bitmask_protocol_detection("OpenVPN", ndpi_struct, detection_bitmask, *id, ndpi_set_bitmask_protocol_detection("OpenVPN", ndpi_struct, detection_bitmask, *id,
NDPI_PROTOCOL_OPENVPN, NDPI_PROTOCOL_OPENVPN,
ndpi_search_openvpn, ndpi_search_openvpn,