]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
dhcp: adds renewal-time keyword
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 25 Aug 2022 15:21:15 +0000 (17:21 +0200)
committerVictor Julien <vjulien@oisf.net>
Thu, 8 Sep 2022 11:27:51 +0000 (13:27 +0200)
Ticket: #5507

doc/userguide/rules/dhcp-keywords.rst
rust/src/dhcp/detect.rs
src/Makefile.am
src/detect-dhcp-renewal-time.c [new file with mode: 0644]
src/detect-dhcp-renewal-time.h [new file with mode: 0644]
src/detect-engine-register.c
src/detect-engine-register.h

index 05b879eb2ae4967915475a6056ab507436096c7e..05675a947e7321a30e11821db1755904d1a76eee 100644 (file)
@@ -10,7 +10,7 @@ Syntax::
 
  dhcp.leasetime:[op]<number>
 
-The version can be matched exactly, or compared using the _op_ setting::
+The time can be matched exactly, or compared using the _op_ setting::
 
  dhcp.leasetime:3    # exactly 3
  dhcp.leasetime:<3   # smaller than 3
@@ -29,7 +29,7 @@ Syntax::
 
  dhcp.rebinding_time:[op]<number>
 
-The version can be matched exactly, or compared using the _op_ setting::
+The time can be matched exactly, or compared using the _op_ setting::
 
  dhcp.rebinding_time:3    # exactly 3
  dhcp.rebinding_time:<3   # smaller than 3
@@ -38,3 +38,22 @@ The version can be matched exactly, or compared using the _op_ setting::
 Signature example::
 
  alert dhcp any any -> any any (msg:"small DHCP rebinding time (<3)"; dhcp.rebinding_time:<3; sid:1; rev:1;)
+
+dhcp.renewal_time
+-----------------
+
+DHCP renewal time (integer).
+
+Syntax::
+
+ dhcp.renewal_time:[op]<number>
+
+The time can be matched exactly, or compared using the _op_ setting::
+
+ dhcp.renewal_time:3    # exactly 3
+ dhcp.renewal_time:<3   # smaller than 3
+ dhcp.renewal_time:>=2  # greater or equal than 2
+
+Signature example::
+
+ alert dhcp any any -> any any (msg:"small DHCP renewal time (<3)"; dhcp.renewal_time:<3; sid:1; rev:1;)
\ No newline at end of file
index 723df5b99dd9cf37771e44363e49e447bb18f1d8..0215810ae8479c013b23b6e3ff5f923af8f20813 100644 (file)
@@ -15,7 +15,9 @@
  * 02110-1301, USA.
  */
 
-use super::dhcp::{DHCPTransaction, DHCP_OPT_ADDRESS_TIME, DHCP_OPT_REBINDING_TIME};
+use super::dhcp::{
+    DHCPTransaction, DHCP_OPT_ADDRESS_TIME, DHCP_OPT_REBINDING_TIME, DHCP_OPT_RENEWAL_TIME,
+};
 use super::parser::DHCPOptionWrapper;
 
 #[no_mangle]
@@ -47,3 +49,18 @@ pub unsafe extern "C" fn rs_dhcp_tx_get_rebinding_time(
     }
     return 0;
 }
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_dhcp_tx_get_renewal_time(
+    tx: &mut DHCPTransaction, res: *mut u64,
+) -> u8 {
+    for option in &tx.message.options {
+        if option.code == DHCP_OPT_RENEWAL_TIME {
+            if let DHCPOptionWrapper::TimeValue(ref time_value) = option.option {
+                *res = time_value.seconds as u64;
+                return 1;
+            }
+        }
+    }
+    return 0;
+}
index 3a4ad01d63ee0d8b4c324f3cff500ea4eb0eb763..d2f23447d0c03c4c81bc45b0d26773cac7189ce8 100755 (executable)
@@ -298,6 +298,7 @@ noinst_HEADERS = \
        detect-snmp-version.h \
        detect-dhcp-leasetime.h \
        detect-dhcp-rebinding-time.h \
+       detect-dhcp-renewal-time.h \
        detect-ssh-hassh.h \
        detect-ssh-hassh-server.h \
        detect-ssh-hassh-server-string.h \
@@ -897,6 +898,7 @@ libsuricata_c_a_SOURCES = \
        detect-snmp-version.c \
        detect-dhcp-leasetime.c \
        detect-dhcp-rebinding-time.c \
+       detect-dhcp-renewal-time.c \
        detect-ssh-hassh.c \
        detect-ssh-hassh-server.c \
        detect-ssh-hassh-server-string.c \
diff --git a/src/detect-dhcp-renewal-time.c b/src/detect-dhcp-renewal-time.c
new file mode 100644 (file)
index 0000000..2dc816a
--- /dev/null
@@ -0,0 +1,130 @@
+/* Copyright (C) 2022 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "suricata-common.h"
+#include "rust.h"
+#include "detect-dhcp-renewal-time.h"
+#include "detect-engine.h"
+#include "detect-engine-mpm.h"
+#include "detect-engine-prefilter.h"
+#include "detect-engine-uint.h"
+#include "detect-parse.h"
+
+static int g_buffer_id = 0;
+
+/**
+ * \internal
+ * \brief Function to match renewal time of a TX
+ *
+ * \param t       Pointer to thread vars.
+ * \param det_ctx Pointer to the pattern matcher thread.
+ * \param f       Pointer to the current flow.
+ * \param flags   Flags.
+ * \param state   App layer state.
+ * \param s       Pointer to the Signature.
+ * \param m       Pointer to the sigmatch that we will cast into
+ *                DetectU64Data.
+ *
+ * \retval 0 no match.
+ * \retval 1 match.
+ */
+static int DetectDHCPRenewalTimeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
+        void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
+{
+    SCEnter();
+
+    uint64_t res;
+    if (rs_dhcp_tx_get_renewal_time(txv, &res)) {
+        const DetectU64Data *dd = (const DetectU64Data *)ctx;
+        if (DetectU64Match(res, dd)) {
+            SCReturnInt(1);
+        }
+    }
+    SCReturnInt(0);
+}
+
+/**
+ * \internal
+ * \brief Function to free memory associated with DetectU64Data.
+ *
+ * \param de_ptr Pointer to DetectU64Data.
+ */
+static void DetectDHCPRenewalTimeFree(DetectEngineCtx *de_ctx, void *ptr)
+{
+    rs_detect_u64_free(ptr);
+}
+
+/**
+ * \brief Function to add the parsed dhcp renewal time field into the current signature.
+ *
+ * \param de_ctx Pointer to the Detection Engine Context.
+ * \param s      Pointer to the Current Signature.
+ * \param rawstr Pointer to the user provided flags options.
+ * \param type   Defines if this is notBefore or notAfter.
+ *
+ * \retval 0 on Success.
+ * \retval -1 on Failure.
+ */
+static int DetectDHCPRenewalTimeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
+{
+    if (DetectSignatureSetAppProto(s, ALPROTO_DHCP) != 0)
+        return -1;
+
+    DetectU64Data *dd = DetectU64Parse(rawstr);
+    if (dd == NULL) {
+        SCLogError(SC_ERR_INVALID_ARGUMENT, "Parsing \'%s\' failed for %s", rawstr,
+                sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].name);
+        return -1;
+    }
+
+    /* okay so far so good, lets get this into a SigMatch
+     * and put it in the Signature. */
+    SigMatch *sm = SigMatchAlloc();
+    if (sm == NULL)
+        goto error;
+
+    sm->type = DETECT_AL_DHCP_RENEWAL_TIME;
+    sm->ctx = (void *)dd;
+
+    SigMatchAppendSMToList(s, sm, g_buffer_id);
+    return 0;
+
+error:
+    DetectDHCPRenewalTimeFree(de_ctx, dd);
+    return -1;
+}
+
+/**
+ * \brief Registration function for dhcp.procedure keyword.
+ */
+void DetectDHCPRenewalTimeRegister(void)
+{
+    sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].name = "dhcp.renewal_time";
+    sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].desc = "match DHCP renewal time";
+    sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].url = "/rules/dhcp-keywords.html#dhcp-renewal-time";
+    sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].AppLayerTxMatch = DetectDHCPRenewalTimeMatch;
+    sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].Setup = DetectDHCPRenewalTimeSetup;
+    sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].Free = DetectDHCPRenewalTimeFree;
+
+    DetectAppLayerInspectEngineRegister2("dhcp.renewal-time", ALPROTO_DHCP, SIG_FLAG_TOSERVER, 0,
+            DetectEngineInspectGenericList, NULL);
+
+    DetectAppLayerInspectEngineRegister2("dhcp.renewal-time", ALPROTO_DHCP, SIG_FLAG_TOCLIENT, 0,
+            DetectEngineInspectGenericList, NULL);
+
+    g_buffer_id = DetectBufferTypeGetByName("dhcp.renewal-time");
+}
diff --git a/src/detect-dhcp-renewal-time.h b/src/detect-dhcp-renewal-time.h
new file mode 100644 (file)
index 0000000..55c2751
--- /dev/null
@@ -0,0 +1,23 @@
+/* Copyright (C) 2022 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __DETECT_DHCP_RENEWAL_TIME_H__
+#define __DETECT_DHCP_RENEWAL_TIME_H__
+
+void DetectDHCPRenewalTimeRegister(void);
+
+#endif /* __DETECT_DHCP_RENEWAL_TIME_H__ */
index 0d1ff9072bed88a0f16c2e0f3ae2fc808f1a76aa..d59f87c789963f4f80e1c8758b877a5ad62d2683 100644 (file)
 #include "detect-template-rust-buffer.h"
 #include "detect-dhcp-leasetime.h"
 #include "detect-dhcp-rebinding-time.h"
+#include "detect-dhcp-renewal-time.h"
 #include "detect-snmp-usm.h"
 #include "detect-snmp-version.h"
 #include "detect-snmp-community.h"
@@ -643,6 +644,7 @@ void SigTableSetup(void)
     DetectTemplateRustBufferRegister();
     DetectDHCPLeaseTimeRegister();
     DetectDHCPRebindingTimeRegister();
+    DetectDHCPRenewalTimeRegister();
     DetectSNMPUsmRegister();
     DetectSNMPVersionRegister();
     DetectSNMPCommunityRegister();
index 589b6f27b24c6e3f3126c63edaffda0b1dbb422a..f5b72f15b811892ec01db8126a9b84efb61848f2 100644 (file)
@@ -273,6 +273,7 @@ enum DetectKeywordId {
     DETECT_AL_TEMPLATE_RUST_BUFFER,
     DETECT_AL_DHCP_LEASETIME,
     DETECT_AL_DHCP_REBINDING_TIME,
+    DETECT_AL_DHCP_RENEWAL_TIME,
     DETECT_AL_SNMP_USM,
     DETECT_AL_SNMP_VERSION,
     DETECT_AL_SNMP_COMMUNITY,