Improved HTTP header parsing skipping lines that won't match

This commit is contained in:
Luca Deri
2022-12-03 11:32:29 +01:00
parent 197a9a6cfc
commit 557bbcfc5a
+59 -38
View File
@@ -6799,46 +6799,9 @@ u_int32_t ndpi_bytestream_to_ipv4(const u_int8_t *str, u_int16_t max_chars_to_re
/* ********************************************************************************* */ /* ********************************************************************************* */
/* internal function for every detection to parse one packet and to increase the info buffer */ void ndpi_parse_single_packet_line(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow) {
void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow) {
u_int32_t a;
struct ndpi_packet_struct *packet = &ndpi_str->packet; struct ndpi_packet_struct *packet = &ndpi_str->packet;
if((packet->payload_packet_len < 3) || (packet->payload == NULL))
return;
if(packet->packet_lines_parsed_complete != 0)
return;
packet->packet_lines_parsed_complete = 1;
ndpi_reset_packet_line_info(packet);
packet->line[packet->parsed_lines].ptr = packet->payload;
packet->line[packet->parsed_lines].len = 0;
for(a = 0; ((a+1) < packet->payload_packet_len) && (packet->parsed_lines < NDPI_MAX_PARSE_LINES_PER_PACKET); a++) {
if((packet->payload[a] == 0x0d) && (packet->payload[a+1] == 0x0a)) {
/* If end of line char sequence CR+NL "\r\n", process line */
if(((a + 3) < packet->payload_packet_len)
&& (packet->payload[a+2] == 0x0d)
&& (packet->payload[a+3] == 0x0a)) {
/* \r\n\r\n */
int diff; /* No unsigned ! */
u_int32_t a1 = a + 4;
diff = packet->payload_packet_len - a1;
if(diff > 0) {
diff = ndpi_min((unsigned int)diff, sizeof(flow->initial_binary_bytes));
memcpy(&flow->initial_binary_bytes, &packet->payload[a1], diff);
flow->initial_binary_bytes_len = diff;
}
}
packet->line[packet->parsed_lines].len =
(u_int16_t)(((size_t) &packet->payload[a]) - ((size_t) packet->line[packet->parsed_lines].ptr));
/* First line of a HTTP response parsing. Expected a "HTTP/1.? ???" */ /* First line of a HTTP response parsing. Expected a "HTTP/1.? ???" */
if(packet->parsed_lines == 0 && packet->line[0].len >= NDPI_STATICSTRING_LEN("HTTP/1.X 200 ") && if(packet->parsed_lines == 0 && packet->line[0].len >= NDPI_STATICSTRING_LEN("HTTP/1.X 200 ") &&
strncasecmp((const char *) packet->line[0].ptr, "HTTP/1.", NDPI_STATICSTRING_LEN("HTTP/1.")) == 0 && strncasecmp((const char *) packet->line[0].ptr, "HTTP/1.", NDPI_STATICSTRING_LEN("HTTP/1.")) == 0 &&
@@ -6863,6 +6826,16 @@ void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_str,
} }
} }
if((packet->parsed_lines == 0) && (packet->line[0].len > 0)) {
/*
Check if the file contains a : otherwise ignore the line as this
line i slike "GET /....
*/
if(strchr((char*)packet->line[0].ptr, ':') == NULL)
return;
}
/* "Server:" header line in HTTP response */ /* "Server:" header line in HTTP response */
if(packet->line[packet->parsed_lines].len > NDPI_STATICSTRING_LEN("Server:") + 1 && if(packet->line[packet->parsed_lines].len > NDPI_STATICSTRING_LEN("Server:") + 1 &&
strncasecmp((const char *) packet->line[packet->parsed_lines].ptr, strncasecmp((const char *) packet->line[packet->parsed_lines].ptr,
@@ -7053,6 +7026,51 @@ void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_str,
packet->content_line.len = c - (char *) packet->content_line.ptr; packet->content_line.len = c - (char *) packet->content_line.ptr;
} }
} }
}
/* ********************************************************************************* */
/* internal function for every detection to parse one packet and to increase the info buffer */
void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow) {
u_int32_t a;
struct ndpi_packet_struct *packet = &ndpi_str->packet;
if((packet->payload_packet_len < 3) || (packet->payload == NULL))
return;
if(packet->packet_lines_parsed_complete != 0)
return;
packet->packet_lines_parsed_complete = 1;
ndpi_reset_packet_line_info(packet);
packet->line[packet->parsed_lines].ptr = packet->payload;
packet->line[packet->parsed_lines].len = 0;
for(a = 0; ((a+1) < packet->payload_packet_len) && (packet->parsed_lines < NDPI_MAX_PARSE_LINES_PER_PACKET); a++) {
if((packet->payload[a] == 0x0d) && (packet->payload[a+1] == 0x0a)) {
/* If end of line char sequence CR+NL "\r\n", process line */
if(((a + 3) < packet->payload_packet_len)
&& (packet->payload[a+2] == 0x0d)
&& (packet->payload[a+3] == 0x0a)) {
/* \r\n\r\n */
int diff; /* No unsigned ! */
u_int32_t a1 = a + 4;
diff = packet->payload_packet_len - a1;
if(diff > 0) {
diff = ndpi_min((unsigned int)diff, sizeof(flow->initial_binary_bytes));
memcpy(&flow->initial_binary_bytes, &packet->payload[a1], diff);
flow->initial_binary_bytes_len = diff;
}
}
packet->line[packet->parsed_lines].len =
(u_int16_t)(((size_t) &packet->payload[a]) - ((size_t) packet->line[packet->parsed_lines].ptr));
ndpi_parse_single_packet_line(ndpi_str, flow);
if(packet->line[packet->parsed_lines].len == 0) { if(packet->line[packet->parsed_lines].len == 0) {
packet->empty_line_position = a; packet->empty_line_position = a;
@@ -7074,6 +7092,8 @@ void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_str,
packet->line[packet->parsed_lines].len = packet->line[packet->parsed_lines].len =
(u_int16_t)(((size_t) &packet->payload[packet->payload_packet_len]) - (u_int16_t)(((size_t) &packet->payload[packet->payload_packet_len]) -
((size_t) packet->line[packet->parsed_lines].ptr)); ((size_t) packet->line[packet->parsed_lines].ptr));
ndpi_parse_single_packet_line(ndpi_str, flow);
packet->parsed_lines++; packet->parsed_lines++;
} }
} }
@@ -9050,6 +9070,7 @@ char *ndpi_hostname_sni_set(struct ndpi_flow_struct *flow,
char *ndpi_user_agent_set(struct ndpi_flow_struct *flow, char *ndpi_user_agent_set(struct ndpi_flow_struct *flow,
const u_int8_t *value, size_t value_len) { const u_int8_t *value, size_t value_len) {
if(flow->http.user_agent != NULL) { if(flow->http.user_agent != NULL) {
/* Already set: ignore double set */
return NULL; return NULL;
} }