static enum proto_parse_status tns_parse_sql_query_oci(struct sql_proto_info *info, struct cursor *cursor) { SLOG(LOG_DEBUG, "Parsing an oci query"); enum proto_parse_status status; char pattern[] = {0xfe, MAX_OCI_CHUNK}; uint8_t const *new_head = memmem(cursor->head, cursor->cap_len, pattern, sizeof(pattern)); if (new_head != NULL) { size_t gap_size = new_head - cursor->head; SLOG(LOG_DEBUG, "%zu bytes before sql", gap_size); DROP_FIX(cursor, gap_size + 1); } else { SLOG(LOG_DEBUG, "{0xfe 0x40} not found, size might be < 0x40"); if (!lookup_query(cursor)) return PROTO_PARSE_ERR; } char *sql; if (PROTO_OK != (status = cursor_read_chunked_string(cursor, &sql, MAX_OCI_CHUNK))) return status; SLOG(LOG_DEBUG, "Sql parsed: %s", sql); sql_set_query(info, "%s", sql); // Drop the rest if(cursor->cap_len > 0) cursor_drop(cursor, cursor->cap_len - 1); return PROTO_OK; }
static enum proto_parse_status tns_parse_sql_query_oci(struct sql_proto_info *info, struct cursor *cursor) { SLOG(LOG_DEBUG, "Parsing an oci query"); uint8_t query_size = parse_query_header(cursor); struct query_candidate candidate = {.num_candidate_size = 0}; if (query_size > 0) { candidate.candidate_sizes[candidate.num_candidate_size++] = query_size; } bool has_query = lookup_query(cursor, &candidate); info->u.query.sql[0] = '\0'; info->u.query.truncated = 0; if (has_query) { SLOG(LOG_DEBUG, "Found a query, parsing it"); if (candidate.is_chunked) { cursor_read_chunked_string(cursor, info->u.query.sql, sizeof(info->u.query.sql), MAX_OCI_CHUNK); } else { cursor_read_fixed_string(cursor, info->u.query.sql, sizeof(info->u.query.sql), candidate.query_size); } } SLOG(LOG_DEBUG, "Sql parsed: %s", info->u.query.sql); info->set_values |= SQL_SQL; // Drop the rest if(cursor->cap_len > 0) cursor_drop(cursor, cursor->cap_len - 1); return PROTO_OK; } /* * | 1 byte | 1 + 0-8 bytes | 1 byte | 1 + 0-4 bytes | Lots of unknown bytes | variable | * | Unk | Sql len | Unk | Num end fields | Unk | sql query | */ static enum proto_parse_status tns_parse_sql_query_jdbc(struct sql_proto_info *info, struct cursor *cursor) { SLOG(LOG_DEBUG, "Parsing a jdbc query"); enum proto_parse_status status = PROTO_OK; DROP_FIX(cursor, 1); uint_least64_t sql_len; if (PROTO_OK != (status = cursor_read_variable_int(cursor, &sql_len))) return status; SLOG(LOG_DEBUG, "Size sql %zu", sql_len); DROP_FIX(cursor, 1); DROP_VAR(cursor); DROP_FIX(cursor, 2); info->u.query.sql[0] = '\0'; info->u.query.truncated = 0; // TODO Handle truncated if (sql_len > 0) { // Some unknown bytes while (cursor->cap_len > 1 && PROTO_OK != is_range_print(cursor, MIN(MIN_QUERY_SIZE, sql_len), 1)) { // TODO drop to the first non printable cursor_drop(cursor, 1); } CHECK(1); if (sql_len > 0xff && 0xff == cursor_peek_u8(cursor, 0)) { SLOG(LOG_DEBUG, "Looks like prefixed length chunks of size 0xff..."); status = cursor_read_chunked_string(cursor, info->u.query.sql, sizeof(info->u.query.sql), 0xff); } else if (sql_len > MAX_OCI_CHUNK && MAX_OCI_CHUNK == cursor_peek_u8(cursor, 0)) { SLOG(LOG_DEBUG, "Looks like prefixed length chunks of size 0x40..."); status = cursor_read_chunked_string(cursor, info->u.query.sql, sizeof(info->u.query.sql), MAX_OCI_CHUNK); } else { // We don't care about the first non printable character cursor_drop(cursor, 1); CHECK(1); // In rare occurrence where sql_len == first character, we check the byte after the expected query, // if it's printable, the first character is probably the prefixed size. if (cursor_peek_u8(cursor, 0) == sql_len && sql_len < cursor->cap_len && is_print(cursor_peek_u8(cursor, sql_len))) cursor_drop(cursor, 1); SLOG(LOG_DEBUG, "Looks like a fixed string of size %zu", sql_len); int written_bytes = cursor_read_fixed_string(cursor, info->u.query.sql, sizeof(info->u.query.sql), sql_len); if (written_bytes < 0) return PROTO_TOO_SHORT; } if (status != PROTO_OK) return status; } SLOG(LOG_DEBUG, "Sql parsed: %s", info->u.query.sql); info->set_values |= SQL_SQL; return PROTO_OK; }