Skip to content
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
15 changes: 15 additions & 0 deletions doc/userguide/output/eve/eve-json-output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ Alerts are event records for rule matches. They can be amended with
metadata, such as the application layer record (HTTP, DNS, etc) an
alert was generated for, and elements of the rule.

The alert is amended with application layer metadata for signatures
using application layer keywords. It is also the case for protocols
over UDP as each single packet is expected to contain a PDU.

For other signatures, the option ``guess-applayer-tx``
can be used to force the detect engine to tie a transaction
to an alert.
This transaction is not guaranteed to be the relevant one,
depending on your use case and how you define relevant here.
If there are multiple live transactions, none will get
picked up.
The alert event will have ``"tx_guessed": true`` to recognize
these alerts.


Metadata::

- alert:
Expand Down
4 changes: 4 additions & 0 deletions doc/userguide/upgrade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Upgrading to 7.0.8
7.0 releases. It will not be provided in
Suricata 8. Please fix any rules that depend on this
behavior.
- Application layer metadata is logged with alerts by default **only for rules that
use application layer keywords**. For other rules, the configuration parameter
``detect.guess-applayer-tx`` can be used to force the detect engine to find a
transaction, which is not guaranteed to be the one you expect.

Upgrading 6.0 to 7.0
--------------------
Expand Down
4 changes: 4 additions & 0 deletions etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
"tx_id": {
"type": "integer"
},
"tx_guessed": {
"description": "the signature that triggered this alert didn't tie to a transaction, so the transaction (and metadata) logged is a forced estimation and may not be the one you expect",
"type": "boolean"
},
"files": {
"type": "array",
"minItems": 1,
Expand Down
2 changes: 2 additions & 0 deletions src/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ typedef struct PacketAlert_ {
#define PACKET_ALERT_RATE_FILTER_MODIFIED 0x10
/** alert is in a frame, frame_id set */
#define PACKET_ALERT_FLAG_FRAME 0x20
/** alert in a tx was forced */
#define PACKET_ALERT_FLAG_TX_GUESSED 0x040

extern uint16_t packet_alert_max;
#define PACKET_ALERT_MAX 15
Expand Down
9 changes: 8 additions & 1 deletion src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -2922,7 +2922,14 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
SCLogDebug("de_ctx->inspection_recursion_limit: %d",
de_ctx->inspection_recursion_limit);

/* parse port grouping whitelisting settings */
int guess_applayer = 0;
if ((ConfGetBool("detect.guess-applayer-tx", &guess_applayer)) == 1) {
if (guess_applayer == 1) {
de_ctx->guess_applayer = true;
}
}

/* parse port grouping priority settings */

const char *ports = NULL;
(void)ConfGet("detect.grouping.tcp-whitelist", &ports);
Expand Down
46 changes: 37 additions & 9 deletions src/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,28 @@ static inline void DetectRunPrefilterPkt(
#endif
}

static bool isOnlyTxInDirection(Flow *f, uint64_t txid, uint8_t dir)
Copy link
Member

Choose a reason for hiding this comment

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

nit: IsOnly...

{
uint64_t tx_cnt = AppLayerParserGetTxCnt(f, f->alstate);
if (tx_cnt == txid + 1) {
// only live tx
return true;
}
if (tx_cnt == txid + 2) {
// 2 live txs, one after us
void *tx = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, txid + 1);
if (tx) {
AppLayerTxData *txd = AppLayerParserGetTxData(f->proto, f->alproto, tx);
// test if the other tx is unidirectional in the other way
if (txd &&
(AppLayerParserGetTxDetectFlags(txd, dir) & APP_LAYER_TX_SKIP_INSPECT_FLAG)) {
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

if we don't get a tx here, shouldn't we at least add a debug message (as it seems quite weird that we'd have the tx_cnt, but no tx returned)?

Copy link
Member

@jasonish jasonish Dec 11, 2024

Choose a reason for hiding this comment

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

Is it weird? Or would it be the same if we have more than 2 or more transactions open in flight for a non-unidirectional protocol, in which case we don't log any of them either, but we'd still have a tx_cnt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jufajardini did you mean tx or txd ?

For txd, this should be impossible indeed, but it is the way to write code to make code analysis tools happy...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For tx, I think it can happen with HTTP2 : you create 2 transactions, but the second one finishes first.
tx_id = 0, tx_cnt = 2, but tx_id = 1 has been freed and is now NULL

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it was for tx. Ok, forgive my noise.

}
}
return false;
}

static inline void DetectRulePacketRules(
ThreadVars * const tv,
DetectEngineCtx * const de_ctx,
Expand Down Expand Up @@ -782,6 +804,10 @@ static inline void DetectRulePacketRules(
goto next; // handle sig in DetectRunFrame
}

/* skip pkt sigs for flow end packets */
if ((p->flags & PKT_PSEUDO_STREAM_END) != 0 && s->type == SIG_TYPE_PKT)
goto next;

/* don't run mask check for stateful rules.
* There we depend on prefilter */
if ((s->mask & scratch->pkt_mask) != s->mask) {
Expand Down Expand Up @@ -814,16 +840,18 @@ static inline void DetectRulePacketRules(
DetectRunPostMatch(tv, det_ctx, p, s);

uint64_t txid = PACKET_ALERT_NOTX;
if ((alert_flags & PACKET_ALERT_FLAG_STREAM_MATCH) ||
(s->alproto != ALPROTO_UNKNOWN && pflow->proto == IPPROTO_UDP)) {
// if there is a stream match (TCP), or
// a UDP specific app-layer signature,
// try to use the good tx for the packet direction
if (pflow->alstate) {
uint8_t dir =
(p->flowflags & FLOW_PKT_TOCLIENT) ? STREAM_TOCLIENT : STREAM_TOSERVER;
txid = AppLayerParserGetTransactionInspectId(pflow->alparser, dir);
if (pflow && pflow->alstate) {
uint8_t dir = (p->flowflags & FLOW_PKT_TOCLIENT) ? STREAM_TOCLIENT : STREAM_TOSERVER;
txid = AppLayerParserGetTransactionInspectId(pflow->alparser, dir);
if ((s->alproto != ALPROTO_UNKNOWN && pflow->proto == IPPROTO_UDP) ||
(de_ctx->guess_applayer && isOnlyTxInDirection(pflow, txid, dir))) {
// if there is a UDP specific app-layer signature,
// or only one live transaction
// try to use the good tx for the packet direction
alert_flags |= PACKET_ALERT_FLAG_TX;
if (pflow->proto != IPPROTO_UDP) {
alert_flags |= PACKET_ALERT_FLAG_TX_GUESSED;
}
}
}
AlertQueueAppend(det_ctx, s, p, txid, alert_flags);
Expand Down
3 changes: 3 additions & 0 deletions src/detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,9 @@ typedef struct DetectEngineCtx_ {
/* maximum recursion depth for content inspection */
int inspection_recursion_limit;

/* force app-layer tx finding for alerts with signatures not having app-layer keywords */
bool guess_applayer;

/* registration id for per thread ctx for the filemagic/file.magic keywords */
int filemagic_thread_ctx_id;

Expand Down
3 changes: 3 additions & 0 deletions src/output-json-alert.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ void AlertJsonHeader(void *ctx, const Packet *p, const PacketAlert *pa, JsonBuil
if (pa->flags & PACKET_ALERT_FLAG_TX) {
jb_set_uint(js, "tx_id", pa->tx_id);
}
if (pa->flags & PACKET_ALERT_FLAG_TX_GUESSED) {
jb_set_bool(js, "tx_guessed", true);
}

jb_open_object(js, "alert");

Expand Down
5 changes: 5 additions & 0 deletions suricata.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,11 @@ detect:
toserver-groups: 25
sgh-mpm-context: auto
inspection-recursion-limit: 3000
# try to tie an app-layer transaction for rules without app-layer keywords
# if there is only one live transaction for the flow
# allows to log app-layer metadata in alert
# but the transaction may not be the relevant one.
# guess-applayer-tx: no
# If set to yes, the loading of signatures will be made after the capture
# is started. This will limit the downtime in IPS mode.
#delayed-detect: yes
Expand Down