pop3: trigger raw stream inspection

Internals
---------
Suricata's stream engine returns data for inspection to the detection
engine from the stream when the chunk size is reached.

Bug
---
Inspection triggered only in the specified chunk sizes may be too late
when it comes to inspection of smaller protocol specific data which
could result in delayed inspection, incorrect data logged with a transaction
and logs misindicating the pkt that triggered an alert.

Fix
---
Fix this by making an explicit call from all respective applayer parsers to
trigger raw stream inspection which shall make the data available for inspection
in the following call of the stream engine. This needs to happen per direction
on the completion of an entity like a request or a response.

Important notes
---------------
1. The above mentioned behavior with and without this patch is
affected internally by the following conditions.
- inspection depth
- stream depth
In these special cases, the inspection window will be affected and
Suricata may not consider all the data that could be expected to be
inspected.
2. This only applies to applayer protocols running over TCP.
3. The inspection window is only considered up to the ACK'd data.
4. This entire issue is about IDS mode only.

POP3 has a classic request response model where a request is mapped to
a response although not by any ID. Appropriate calls to trigger raw stream
inspection have been added on completion of each request and response.

Task 7026
Bug 7004
pull/13292/head
Shivani Bhardwaj 2 months ago committed by Victor Julien
parent c0aa60c573
commit f709631cf7

@ -21,7 +21,7 @@
use crate::applayer::*;
use crate::conf::{conf_get, get_memval};
use crate::core::{ALPROTO_FAILED, ALPROTO_UNKNOWN, IPPROTO_TCP};
use crate::core::{ALPROTO_FAILED, ALPROTO_UNKNOWN, IPPROTO_TCP, sc_app_layer_parser_trigger_raw_stream_inspection};
use crate::flow::Flow;
use crate::direction;
use std;
@ -169,7 +169,7 @@ impl POP3State {
.find(|tx| tx.response.is_none())
}
fn parse_request(&mut self, input: &[u8]) -> AppLayerResult {
fn parse_request(&mut self, flow: *const Flow, input: &[u8]) -> AppLayerResult {
// We're not interested in empty requests.
if input.is_empty() {
return AppLayerResult::ok();
@ -203,6 +203,7 @@ impl POP3State {
tx.error_flags_to_events(msg.error_flags);
tx.request = Some(command);
self.transactions.push_back(tx);
sc_app_layer_parser_trigger_raw_stream_inspection(flow, direction::Direction::ToServer as i32);
}
start = rem;
@ -278,6 +279,7 @@ impl POP3State {
tx.error_flags_to_events(msg.error_flags);
tx.complete = true;
sc_app_layer_parser_trigger_raw_stream_inspection(flow, direction::Direction::ToClient as i32);
if response.status == sawp_pop3::Status::OK && tx.request.is_some() {
let command = tx.request.as_ref().unwrap();
@ -397,7 +399,7 @@ unsafe extern "C" fn pop3_state_tx_free(state: *mut c_void, tx_id: u64) {
}
unsafe extern "C" fn pop3_parse_request(
_flow: *const Flow, state: *mut c_void, pstate: *mut c_void, stream_slice: StreamSlice,
flow: *const Flow, state: *mut c_void, pstate: *mut c_void, stream_slice: StreamSlice,
_data: *const c_void,
) -> AppLayerResult {
let eof = AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0;
@ -416,7 +418,7 @@ unsafe extern "C" fn pop3_parse_request(
AppLayerResult::ok()
} else {
let buf = stream_slice.as_slice();
state.parse_request(buf)
state.parse_request(flow, buf)
}
}

Loading…
Cancel
Save