]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
dhcp: adds leasetime keyword
authorPhilippe Antoine <pantoine@oisf.net>
Mon, 4 Jul 2022 12:25:33 +0000 (14:25 +0200)
committerVictor Julien <vjulien@oisf.net>
Thu, 18 Aug 2022 11:40:28 +0000 (13:40 +0200)
As it is logged

Ticket: #5435

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

diff --git a/doc/userguide/rules/dhcp-keywords.rst b/doc/userguide/rules/dhcp-keywords.rst
new file mode 100644 (file)
index 0000000..adac122
--- /dev/null
@@ -0,0 +1,21 @@
+DHCP keywords
+=============
+
+dhcp.leasetime
+--------------
+
+DHCP lease time (integer).
+
+Syntax::
+
+ dhcp.leasetime:[op]<number>
+
+The version can be matched exactly, or compared using the _op_ setting::
+
+ dhcp.leasetime:3    # exactly 3
+ dhcp.leasetime:<3   # smaller than 3
+ dhcp.leasetime:>=2  # greater or equal than 2
+
+Signature example::
+
+ alert dhcp any any -> any any (msg:"small DHCP lease time (<3)"; dhcp.leasetime:<3; sid:1; rev:1;)
index 4ef87bdeb88b970c113fce9cded673f7a115de07..5c0db48ced61b41db25a39b1dba775fd0515a837 100644 (file)
@@ -19,6 +19,7 @@ Suricata Rules
    ja3-keywords
    modbus-keyword
    dcerpc-keywords
+   dhcp-keywords
    dnp3-keywords
    enip-keyword
    ftp-keywords
index e5b92b5449cf99ae526df5784996e35856f5aee3..d3e3321b5436d151dd221f98b976dbb3cd97814d 100644 (file)
@@ -88,6 +88,7 @@ you can pick from. These are:
 * smb
 * dns
 * dcerpc
+* dhcp
 * ssh
 * smtp
 * imap
diff --git a/rust/src/dhcp/detect.rs b/rust/src/dhcp/detect.rs
new file mode 100644 (file)
index 0000000..c257f94
--- /dev/null
@@ -0,0 +1,34 @@
+/* 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.
+ */
+
+use super::dhcp::{DHCPTransaction, DHCP_OPT_ADDRESS_TIME};
+use super::parser::DHCPOptionWrapper;
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_dhcp_tx_get_leasetime(
+    tx: &mut DHCPTransaction, leasetime: *mut u64,
+) -> u8 {
+    for option in &tx.message.options {
+        if option.code == DHCP_OPT_ADDRESS_TIME {
+            if let DHCPOptionWrapper::TimeValue(ref time_value) = option.option {
+                *leasetime = time_value.seconds as u64;
+                return 1;
+            }
+        }
+    }
+    return 0;
+}
index b34dd9859c97d869995c43bd82e521a6f19c1a67..bc859d7b54baf3a3d6b59b77c2900010aa39e011 100644 (file)
@@ -18,3 +18,4 @@
 pub mod dhcp;
 pub mod parser;
 pub mod logger;
+pub mod detect;
index 23e185a60c254e6f73e0f5fbb9fa99e68c52cccd..30762104f46d06fc5af2ad242ed4fb2e94938d43 100755 (executable)
@@ -296,6 +296,7 @@ noinst_HEADERS = \
        detect-snmp-pdu_type.h \
        detect-snmp-usm.h \
        detect-snmp-version.h \
+       detect-dhcp-leasetime.h \
        detect-ssh-hassh.h \
        detect-ssh-hassh-server.h \
        detect-ssh-hassh-server-string.h \
@@ -892,6 +893,7 @@ libsuricata_c_a_SOURCES = \
        detect-snmp-pdu_type.c \
        detect-snmp-usm.c \
        detect-snmp-version.c \
+       detect-dhcp-leasetime.c \
        detect-ssh-hassh.c \
        detect-ssh-hassh-server.c \
        detect-ssh-hassh-server-string.c \
diff --git a/src/detect-dhcp-leasetime.c b/src/detect-dhcp-leasetime.c
new file mode 100644 (file)
index 0000000..74e25fb
--- /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 leasetime 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
+ * leasetime 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-leasetime.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 leasetime 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 DetectDHCPLeaseTimeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
+        void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
+{
+    SCEnter();
+
+    uint64_t leasetime;
+    if (rs_dhcp_tx_get_leasetime(txv, &leasetime)) {
+        const DetectU64Data *dd = (const DetectU64Data *)ctx;
+        if (DetectU64Match(leasetime, dd)) {
+            SCReturnInt(1);
+        }
+    }
+    SCReturnInt(0);
+}
+
+/**
+ * \internal
+ * \brief Function to free memory associated with DetectU64Data.
+ *
+ * \param de_ptr Pointer to DetectU64Data.
+ */
+static void DetectDHCPLeaseTimeFree(DetectEngineCtx *de_ctx, void *ptr)
+{
+    rs_detect_u64_free(ptr);
+}
+
+/**
+ * \brief Function to add the parsed dhcp leasetime 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 DetectDHCPLeaseTimeSetup(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_LEASETIME].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_LEASETIME;
+    sm->ctx = (void *)dd;
+
+    SigMatchAppendSMToList(s, sm, g_buffer_id);
+    return 0;
+
+error:
+    DetectDHCPLeaseTimeFree(de_ctx, dd);
+    return -1;
+}
+
+/**
+ * \brief Registration function for dhcp.procedure keyword.
+ */
+void DetectDHCPLeaseTimeRegister(void)
+{
+    sigmatch_table[DETECT_AL_DHCP_LEASETIME].name = "dhcp.leasetime";
+    sigmatch_table[DETECT_AL_DHCP_LEASETIME].desc = "match DHCP leasetime";
+    sigmatch_table[DETECT_AL_DHCP_LEASETIME].url = "/rules/dhcp-keywords.html#dhcp-leasetime";
+    sigmatch_table[DETECT_AL_DHCP_LEASETIME].AppLayerTxMatch = DetectDHCPLeaseTimeMatch;
+    sigmatch_table[DETECT_AL_DHCP_LEASETIME].Setup = DetectDHCPLeaseTimeSetup;
+    sigmatch_table[DETECT_AL_DHCP_LEASETIME].Free = DetectDHCPLeaseTimeFree;
+
+    DetectAppLayerInspectEngineRegister2("dhcp.leasetime", ALPROTO_DHCP, SIG_FLAG_TOSERVER, 0,
+            DetectEngineInspectGenericList, NULL);
+
+    DetectAppLayerInspectEngineRegister2("dhcp.leasetime", ALPROTO_DHCP, SIG_FLAG_TOCLIENT, 0,
+            DetectEngineInspectGenericList, NULL);
+
+    g_buffer_id = DetectBufferTypeGetByName("dhcp.leasetime");
+}
diff --git a/src/detect-dhcp-leasetime.h b/src/detect-dhcp-leasetime.h
new file mode 100644 (file)
index 0000000..6a9baeb
--- /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_LEASETIME_H__
+#define __DETECT_DHCP_LEASETIME_H__
+
+void DetectDHCPLeaseTimeRegister(void);
+
+#endif /* __DETECT_DHCP_LEASETIME_H__ */
index 1c9d7a275b030d52d1bc49b5693ce04b78b184d7..4ad49cc09d35905755e1ea131ce0dd565c96f35a 100644 (file)
 #include "detect-rfb-name.h"
 #include "detect-target.h"
 #include "detect-template-rust-buffer.h"
+#include "detect-dhcp-leasetime.h"
 #include "detect-snmp-usm.h"
 #include "detect-snmp-version.h"
 #include "detect-snmp-community.h"
@@ -637,6 +638,7 @@ void SigTableSetup(void)
     DetectRfbNameRegister();
     DetectTargetRegister();
     DetectTemplateRustBufferRegister();
+    DetectDHCPLeaseTimeRegister();
     DetectSNMPUsmRegister();
     DetectSNMPVersionRegister();
     DetectSNMPCommunityRegister();
index 1451bfbd9cf9df67f4e8f6632553ca586a2cff23..c6160f5f99a20fb0e712f26a6418d0e2771ddf07 100644 (file)
@@ -268,6 +268,7 @@ enum DetectKeywordId {
     DETECT_FTPDATA,
     DETECT_TARGET,
     DETECT_AL_TEMPLATE_RUST_BUFFER,
+    DETECT_AL_DHCP_LEASETIME,
     DETECT_AL_SNMP_USM,
     DETECT_AL_SNMP_VERSION,
     DETECT_AL_SNMP_COMMUNITY,