http: create a common function to parse User Agent field

Prepare the code to handle UA information from flows other than HTTP
This commit is contained in:
Nardi Ivan
2020-09-08 10:34:05 +02:00
parent 601928cc8e
commit a1014e8895
3 changed files with 95 additions and 81 deletions
+1 -1
View File
@@ -1268,10 +1268,10 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
if(flow->http.content_type[0] != '\0') if(flow->http.content_type[0] != '\0')
fprintf(out, "[Content-Type: %s]", flow->http.content_type); fprintf(out, "[Content-Type: %s]", flow->http.content_type);
}
if(flow->http.user_agent[0] != '\0') if(flow->http.user_agent[0] != '\0')
fprintf(out, "[User-Agent: %s]", flow->http.user_agent); fprintf(out, "[User-Agent: %s]", flow->http.user_agent);
}
if(flow->risk) { if(flow->risk) {
u_int i; u_int i;
+3
View File
@@ -1139,6 +1139,7 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
} }
/* TLS */ /* TLS */
else if((is_ndpi_proto(flow, NDPI_PROTOCOL_TLS)) else if((is_ndpi_proto(flow, NDPI_PROTOCOL_TLS))
|| ((is_ndpi_proto(flow, NDPI_PROTOCOL_QUIC)))
|| (flow->detected_protocol.master_protocol == NDPI_PROTOCOL_TLS) || (flow->detected_protocol.master_protocol == NDPI_PROTOCOL_TLS)
|| (flow->ndpi_flow->protos.stun_ssl.ssl.ja3_client[0] != '\0') || (flow->ndpi_flow->protos.stun_ssl.ssl.ja3_client[0] != '\0')
) { ) {
@@ -1147,6 +1148,8 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
sizeof(flow->ssh_tls.client_requested_server_name), "%s", sizeof(flow->ssh_tls.client_requested_server_name), "%s",
flow->ndpi_flow->protos.stun_ssl.ssl.client_requested_server_name); flow->ndpi_flow->protos.stun_ssl.ssl.client_requested_server_name);
snprintf(flow->http.user_agent, sizeof(flow->http.user_agent), "%s", flow->ndpi_flow->http.user_agent ? flow->ndpi_flow->http.user_agent : "");
if(flow->ndpi_flow->protos.stun_ssl.ssl.server_names_len > 0 && flow->ndpi_flow->protos.stun_ssl.ssl.server_names) if(flow->ndpi_flow->protos.stun_ssl.ssl.server_names_len > 0 && flow->ndpi_flow->protos.stun_ssl.ssl.server_names)
flow->ssh_tls.server_names = ndpi_strdup(flow->ndpi_flow->protos.stun_ssl.ssl.server_names); flow->ssh_tls.server_names = ndpi_strdup(flow->ndpi_flow->protos.stun_ssl.ssl.server_names);
flow->ssh_tls.notBefore = flow->ndpi_flow->protos.stun_ssl.ssl.notBefore; flow->ssh_tls.notBefore = flow->ndpi_flow->protos.stun_ssl.ssl.notBefore;
+87 -76
View File
@@ -278,83 +278,20 @@ static void ndpi_check_user_agent(struct ndpi_detection_module_struct *ndpi_stru
} }
} }
/* ************************************************************* */ int http_process_user_agent(struct ndpi_detection_module_struct *ndpi_struct,
static void ndpi_check_numeric_ip(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
char *ip, u_int ip_len) { const u_int8_t *ua_ptr, u_int16_t ua_ptr_len)
char buf[22]; {
struct in_addr ip_addr;
strncpy(buf, ip, ip_len);
buf[ip_len] = '\0';
ip_addr.s_addr = inet_addr(buf);
if(strcmp(inet_ntoa(ip_addr), buf) == 0)
NDPI_SET_BIT(flow->risk, NDPI_HTTP_NUMERIC_IP_HOST);
}
/* ************************************************************* */
static void ndpi_check_http_url(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow,
char *url) {
/* Nothing to do */
}
/* ************************************************************* */
/**
NOTE
ndpi_parse_packet_line_info is in ndpi_main.c
*/
static void check_content_type_and_change_protocol(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) {
struct ndpi_packet_struct *packet = &flow->packet;
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_HTTP, NDPI_PROTOCOL_UNKNOWN);
if(flow->http_detected && (flow->http.response_status_code != 0))
return;
if((flow->http.url == NULL)
&& (packet->http_url_name.len > 0)
&& (packet->host_line.len > 0)) {
int len = packet->http_url_name.len + packet->host_line.len + 1;
if(isdigit(packet->host_line.ptr[0])
&& (packet->host_line.len < 21))
ndpi_check_numeric_ip(ndpi_struct, flow, (char*)packet->host_line.ptr, packet->host_line.len);
flow->http.url = ndpi_malloc(len);
if(flow->http.url) {
strncpy(flow->http.url, (char*)packet->host_line.ptr, packet->host_line.len);
strncpy(&flow->http.url[packet->host_line.len], (char*)packet->http_url_name.ptr,
packet->http_url_name.len);
flow->http.url[len-1] = '\0';
ndpi_check_http_url(ndpi_struct, flow, &flow->http.url[packet->host_line.len]);
}
flow->http.method = ndpi_http_str2method((const char*)flow->packet.http_method.ptr, flow->packet.http_method.len);
}
if(packet->server_line.ptr != NULL && (packet->server_line.len > 7)) {
if(strncmp((const char *)packet->server_line.ptr, "ntopng ", 7) == 0)
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_NTOP, NDPI_PROTOCOL_HTTP);
}
if(packet->user_agent_line.ptr != NULL && packet->user_agent_line.len != 0) {
/** /**
Format examples: Format examples:
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) .... Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) ....
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0 Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0
*/ */
if(packet->user_agent_line.len > 7) { if(ua_ptr_len > 7) {
char ua[256]; char ua[256];
u_int mlen = ndpi_min(packet->user_agent_line.len, sizeof(ua)-1); u_int mlen = ndpi_min(ua_ptr_len, sizeof(ua)-1);
strncpy(ua, (const char *)packet->user_agent_line.ptr, mlen); strncpy(ua, (const char *)ua_ptr, mlen);
ua[mlen] = '\0'; ua[mlen] = '\0';
if(strncmp(ua, "Mozilla", 7) == 0) { if(strncmp(ua, "Mozilla", 7) == 0) {
@@ -401,28 +338,102 @@ static void check_content_type_and_change_protocol(struct ndpi_detection_module_
setHttpUserAgent(ndpi_struct, flow, token); setHttpUserAgent(ndpi_struct, flow, token);
} }
} }
} else if((packet->user_agent_line.len > 14) && (memcmp(ua, "netflix-ios-app", 15) == 0)) { } else if((ua_ptr_len > 14) && (memcmp(ua, "netflix-ios-app", 15) == 0)) {
NDPI_LOG_INFO(ndpi_struct, "found netflix\n"); NDPI_LOG_INFO(ndpi_struct, "found netflix\n");
ndpi_int_http_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_NETFLIX, NDPI_PROTOCOL_CATEGORY_STREAMING); ndpi_int_http_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_NETFLIX, NDPI_PROTOCOL_CATEGORY_STREAMING);
return; return -1;
} }
} }
if(flow->http.user_agent == NULL) { if(flow->http.user_agent == NULL) {
int len = packet->user_agent_line.len + 1; int len = ua_ptr_len + 1;
flow->http.user_agent = ndpi_malloc(len); flow->http.user_agent = ndpi_malloc(len);
if(flow->http.user_agent) { if(flow->http.user_agent) {
strncpy(flow->http.user_agent, (char*)packet->user_agent_line.ptr, strncpy(flow->http.user_agent, (char*)ua_ptr, ua_ptr_len);
packet->user_agent_line.len); flow->http.user_agent[ua_ptr_len] = '\0';
flow->http.user_agent[packet->user_agent_line.len] = '\0';
ndpi_check_user_agent(ndpi_struct, flow, flow->http.user_agent); ndpi_check_user_agent(ndpi_struct, flow, flow->http.user_agent);
} }
} }
NDPI_LOG_DBG2(ndpi_struct, "User Agent Type line found %.*s\n", NDPI_LOG_DBG2(ndpi_struct, "User Agent Type line found %.*s\n",
packet->user_agent_line.len, packet->user_agent_line.ptr); ua_ptr_len, ua_ptr);
return 0;
}
/* ************************************************************* */
static void ndpi_check_numeric_ip(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow,
char *ip, u_int ip_len) {
char buf[22];
struct in_addr ip_addr;
strncpy(buf, ip, ip_len);
buf[ip_len] = '\0';
ip_addr.s_addr = inet_addr(buf);
if(strcmp(inet_ntoa(ip_addr), buf) == 0)
NDPI_SET_BIT(flow->risk, NDPI_HTTP_NUMERIC_IP_HOST);
}
/* ************************************************************* */
static void ndpi_check_http_url(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow,
char *url) {
/* Nothing to do */
}
/* ************************************************************* */
/**
NOTE
ndpi_parse_packet_line_info is in ndpi_main.c
*/
static void check_content_type_and_change_protocol(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) {
struct ndpi_packet_struct *packet = &flow->packet;
int ret;
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_HTTP, NDPI_PROTOCOL_UNKNOWN);
if(flow->http_detected && (flow->http.response_status_code != 0))
return;
if((flow->http.url == NULL)
&& (packet->http_url_name.len > 0)
&& (packet->host_line.len > 0)) {
int len = packet->http_url_name.len + packet->host_line.len + 1;
if(isdigit(packet->host_line.ptr[0])
&& (packet->host_line.len < 21))
ndpi_check_numeric_ip(ndpi_struct, flow, (char*)packet->host_line.ptr, packet->host_line.len);
flow->http.url = ndpi_malloc(len);
if(flow->http.url) {
strncpy(flow->http.url, (char*)packet->host_line.ptr, packet->host_line.len);
strncpy(&flow->http.url[packet->host_line.len], (char*)packet->http_url_name.ptr,
packet->http_url_name.len);
flow->http.url[len-1] = '\0';
ndpi_check_http_url(ndpi_struct, flow, &flow->http.url[packet->host_line.len]);
}
flow->http.method = ndpi_http_str2method((const char*)flow->packet.http_method.ptr, flow->packet.http_method.len);
}
if(packet->server_line.ptr != NULL && (packet->server_line.len > 7)) {
if(strncmp((const char *)packet->server_line.ptr, "ntopng ", 7) == 0)
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_NTOP, NDPI_PROTOCOL_HTTP);
}
if(packet->user_agent_line.ptr != NULL && packet->user_agent_line.len != 0) {
ret = http_process_user_agent(ndpi_struct, flow, packet->user_agent_line.ptr, packet->user_agent_line.len);
/* TODO: Is it correct to avoid setting ua, host_name,... if we have a (Netflix) subclassification? */
if(ret != 0)
return;
} }
/* check for host line */ /* check for host line */