Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protodetect: improve DCERPC UDP probing parser and simplify app-layer-detect-proto.c code v3 #11679

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rust/src/dcerpc/dcerpc_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ pub unsafe extern "C" fn rs_dcerpc_udp_get_tx_cnt(vtx: *mut std::os::raw::c_void
/// Probe input to see if it looks like DCERPC.
fn probe(input: &[u8]) -> (bool, bool) {
match parser::parse_dcerpc_udp_header(input) {
Ok((_, hdr)) => {
Ok((leftover_bytes, hdr)) => {
let is_request = hdr.pkt_type == 0x00;
let is_dcerpc = hdr.rpc_vers == 0x04 &&
hdr.fragnum == 0 &&
inashivb marked this conversation as resolved.
Show resolved Hide resolved
leftover_bytes.len() >= hdr.fraglen as usize &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. This alone seems good enough to detect your test pcap correctly.
In addition to this, following wireshark's detection logic, possible header field to make probing stronger that isn't already covered:
pkt_type must be <= CANCEL_ACK (10) [this update alone fails the detection test on your s-v pcap though]

lmk wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be more cf https://github.com/secdev/scapy/blob/master/scapy/layers/dcerpc.py#L138

    10: "cancel_ack",
    11: "bind",
    12: "bind_ack",
    13: "bind_nak",
    14: "alter_context",
    15: "alter_context_resp",
    16: "auth3",
    17: "shutdown",
    18: "co_cancel",
    19: "orphaned",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed there are. That condition check was used by wireshark for the detection so only the first pkt I think (probing). All the other types you mentioned are after a connection is established and some of these are even handled by our dcerpc parser like bind + bindack. This is why I thought of suggesting it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go without it in case we probe midstream...

(hdr.flags2 & 0xfc == 0) &&
(hdr.drep[0] & 0xee == 0) &&
(hdr.drep[1] <= 3);
Expand Down
25 changes: 5 additions & 20 deletions src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,6 @@ AppProto AppLayerProtoDetectGetProto(AppLayerProtoDetectThreadCtx *tctx, Flow *f
(flags & STREAM_TOSERVER) ? "toserver" : "toclient");

AppProto alproto = ALPROTO_UNKNOWN;
AppProto pm_alproto = ALPROTO_UNKNOWN;

if (!FLOW_IS_PM_DONE(f, flags)) {
AppProto pm_results[ALPROTO_MAX];
Expand All @@ -1419,38 +1418,24 @@ AppProto AppLayerProtoDetectGetProto(AppLayerProtoDetectThreadCtx *tctx, Flow *f
FLOW_RESET_PP_DONE(f, reverse_dir);
}
}

/* HACK: if detected protocol is dcerpc/udp, we run PP as well
* to avoid misdetecting DNS as DCERPC. */
if (!(ipproto == IPPROTO_UDP && alproto == ALPROTO_DCERPC))
goto end;

pm_alproto = alproto;

/* fall through */
SCReturnUInt(alproto);
}
}

if (!FLOW_IS_PP_DONE(f, flags)) {
bool rflow = false;
alproto = AppLayerProtoDetectPPGetProto(f, buf, buflen, ipproto, flags, &rflow);
DEBUG_VALIDATE_BUG_ON(*reverse_flow);
alproto = AppLayerProtoDetectPPGetProto(f, buf, buflen, ipproto, flags, reverse_flow);
if (AppProtoIsValid(alproto)) {
if (rflow) {
*reverse_flow = true;
}
goto end;
SCReturnUInt(alproto);
}
}

/* Look if flow can be found in expectation list */
if (!FLOW_IS_PE_DONE(f, flags)) {
DEBUG_VALIDATE_BUG_ON(*reverse_flow);
alproto = AppLayerProtoDetectPEGetProto(f, flags);
}

end:
if (!AppProtoIsValid(alproto))
alproto = pm_alproto;

SCReturnUInt(alproto);
}

Expand Down
Loading