]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
flow/pkts: allow matching on either direction 12373/head
authorShivani Bhardwaj <shivani@oisf.net>
Fri, 23 Aug 2024 06:57:35 +0000 (12:27 +0530)
committerVictor Julien <victor@inliniac.net>
Fri, 10 Jan 2025 21:32:40 +0000 (22:32 +0100)
For flow.bytes and flow.pkts keywords, allow matching in either
direction.

Feature 5646

doc/userguide/rules/flow-keywords.rst
src/detect-flow-pkts.c

index 2ab6128e72074f3e56fdb5da0382c079f17d94fc..3ba6bbe06a9a68ea81428e3806e974d970bf65ec 100644 (file)
@@ -331,6 +331,8 @@ following directions:
 
 * toserver
 
+* either
+
 Syntax::
 
  flow.pkts:<direction>,[op]<number>
@@ -339,6 +341,7 @@ The number of packets can be matched exactly, or compared using the _op_ setting
 
  flow.pkts:toclient,3    # exactly 3
  flow.pkts:toserver,<3   # smaller than 3
+ flow.pkts:either,>=2  # greater than or equal to 2
 
 Signature example::
 
@@ -361,6 +364,8 @@ following directions:
 
 * toserver
 
+* either
+
 Syntax::
 
  flow.bytes:<direction>,[op]<number>
@@ -369,6 +374,7 @@ The number of bytes can be matched exactly, or compared using the _op_ setting::
 
  flow.bytes:toclient,3    # exactly 3
  flow.bytes:toserver,<3   # smaller than 3
+ flow.bytes:either,>=2  # greater than or equal to 2
 
 Signature example::
 
index 884ba2f1cba218870e29ecdd998bcc3c85dac4dc..0ed1b487750dad855257059553405127ec783410 100644 (file)
@@ -26,6 +26,7 @@
 enum FlowDirection {
     DETECT_FLOW_TOSERVER = 1,
     DETECT_FLOW_TOCLIENT,
+    DETECT_FLOW_TOEITHER,
 };
 
 typedef struct DetectFlowPkts_ {
@@ -50,6 +51,11 @@ static int DetectFlowPktsMatch(
         return DetectU32Match(p->flow->todstpktcnt, df->pkt_data);
     } else if (df->dir == DETECT_FLOW_TOCLIENT) {
         return DetectU32Match(p->flow->tosrcpktcnt, df->pkt_data);
+    } else if (df->dir == DETECT_FLOW_TOEITHER) {
+        if (DetectU32Match(p->flow->tosrcpktcnt, df->pkt_data)) {
+            return 1;
+        }
+        return DetectU32Match(p->flow->todstpktcnt, df->pkt_data);
     }
     return 0;
 }
@@ -141,6 +147,8 @@ static int DetectFlowPktsSetup(DetectEngineCtx *de_ctx, Signature *s, const char
                 dir = DETECT_FLOW_TOSERVER;
             } else if (strcmp(token, "toclient") == 0) {
                 dir = DETECT_FLOW_TOCLIENT;
+            } else if (strcmp(token, "either") == 0) {
+                dir = DETECT_FLOW_TOEITHER;
             } else {
                 SCLogError("Invalid direction given: %s", token);
                 return -1;
@@ -277,6 +285,11 @@ static int DetectFlowBytesMatch(
         return DetectU64Match(p->flow->todstbytecnt, df->byte_data);
     } else if (df->dir == DETECT_FLOW_TOCLIENT) {
         return DetectU64Match(p->flow->tosrcbytecnt, df->byte_data);
+    } else if (df->dir == DETECT_FLOW_TOEITHER) {
+        if (DetectU64Match(p->flow->tosrcbytecnt, df->byte_data)) {
+            return 1;
+        }
+        return DetectU64Match(p->flow->todstbytecnt, df->byte_data);
     }
     return 0;
 }
@@ -368,6 +381,8 @@ static int DetectFlowBytesSetup(DetectEngineCtx *de_ctx, Signature *s, const cha
                 dir = DETECT_FLOW_TOSERVER;
             } else if (strcmp(token, "toclient") == 0) {
                 dir = DETECT_FLOW_TOCLIENT;
+            } else if (strcmp(token, "either") == 0) {
+                dir = DETECT_FLOW_TOEITHER;
             } else {
                 SCLogError("Invalid direction given: %s", token);
                 return -1;