]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/smb: add smb.ntlmssp_user keyword
authorEric Leblond <el@stamus-networks.com>
Thu, 13 Jan 2022 10:28:54 +0000 (11:28 +0100)
committerVictor Julien <vjulien@oisf.net>
Mon, 3 Oct 2022 08:51:06 +0000 (10:51 +0200)
Feature #5411.

rust/src/smb/detect.rs
src/Makefile.am
src/detect-engine-register.c
src/detect-engine-register.h
src/detect-smb-ntlmssp.c [new file with mode: 0644]
src/detect-smb-ntlmssp.h [new file with mode: 0644]

index da1d4e317787139fae5eaddddc5d53c092b5da56..526db406c2caf33bf6321ecebb3fd966ceaa5f2d 100644 (file)
@@ -170,3 +170,26 @@ pub extern "C" fn rs_smb_tx_get_dce_iface(state: &mut SMBState,
     }
     return 0;
 }
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_smb_tx_get_ntlmssp_user(tx: &mut SMBTransaction,
+                                            buffer: *mut *const u8,
+                                            buffer_len: *mut u32)
+                                            -> u8
+{
+    match tx.type_data {
+        Some(SMBTransactionTypeData::SESSIONSETUP(ref x)) => {
+            if let Some(ref ntlmssp) = x.ntlmssp {
+                *buffer = ntlmssp.user.as_ptr();
+                *buffer_len = ntlmssp.user.len() as u32;
+                return 1;
+            }
+        }
+        _ => {
+        }
+    }
+
+    *buffer = ptr::null();
+    *buffer_len = 0;
+    return 0;
+}
index 799a8bd972808a4d38275ff4218f3410c9eb2fd2..016b81f601807ea38344a88230f309d49f234639 100755 (executable)
@@ -291,6 +291,7 @@ noinst_HEADERS = \
        detect-sip-stat-code.h \
        detect-sip-stat-msg.h \
        detect-sip-uri.h \
+       detect-smb-ntlmssp.h \
        detect-smb-share.h \
        detect-snmp-community.h \
        detect-snmp-pdu_type.h \
@@ -895,6 +896,7 @@ libsuricata_c_a_SOURCES = \
        detect-sip-stat-code.c \
        detect-sip-stat-msg.c \
        detect-sip-uri.c \
+       detect-smb-ntlmssp.c \
        detect-smb-share.c \
        detect-snmp-community.c \
        detect-snmp-pdu_type.c \
index ca1221ed742369d1c117622d4b2e1a2bb6cc7365..95ea173faf053f39ecb95641d14e02babe32ed4a 100644 (file)
@@ -21,6 +21,7 @@
  * \author Victor Julien <victor@inliniac.net>
  */
 
+#include "detect-smb-ntlmssp.h"
 #include "suricata-common.h"
 #include "suricata.h"
 #include "detect.h"
@@ -591,6 +592,7 @@ void SigTableSetup(void)
     DetectDceStubDataRegister();
     DetectSmbNamedPipeRegister();
     DetectSmbShareRegister();
+    DetectSmbNtlmsspUserRegister();
     DetectTlsRegister();
     DetectTlsValidityRegister();
     DetectTlsVersionRegister();
index 2918b1cca817f2122c98d2f8a3d6f097cb8ac16f..cfb892d97ffc07b9a87d32fbb4d27adee05b575e 100644 (file)
@@ -192,6 +192,7 @@ enum DetectKeywordId {
     DETECT_DCE_STUB_DATA,
     DETECT_SMB_NAMED_PIPE,
     DETECT_SMB_SHARE,
+    DETECT_SMB_NTLMSSP_USER,
 
     DETECT_ASN1,
 
diff --git a/src/detect-smb-ntlmssp.c b/src/detect-smb-ntlmssp.c
new file mode 100644 (file)
index 0000000..a0afde8
--- /dev/null
@@ -0,0 +1,90 @@
+/* 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.
+ */
+
+/**
+ * \file
+ *
+ * \author Eric Leblond <el@stamus-networks.com>
+ *
+ */
+
+#include "suricata-common.h"
+
+#include "detect.h"
+#include "detect-parse.h"
+
+#include "detect-engine.h"
+#include "detect-engine-mpm.h"
+#include "detect-engine-state.h"
+#include "detect-engine-prefilter.h"
+#include "detect-engine-content-inspection.h"
+
+#include "detect-smb-ntlmssp.h"
+#include "rust.h"
+
+#define BUFFER_NAME  "smb_ntlmssp_user"
+#define KEYWORD_NAME "smb.ntlmssp_user"
+#define KEYWORD_ID   DETECT_SMB_NTLMSSP_USER
+
+static int g_smb_nltmssp_user_buffer_id = 0;
+
+static int DetectSmbNtlmsspUserSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
+{
+    if (DetectBufferSetActiveList(s, g_smb_nltmssp_user_buffer_id) < 0)
+        return -1;
+
+    if (DetectSignatureSetAppProto(s, ALPROTO_SMB) < 0)
+        return -1;
+
+    return 0;
+}
+
+static InspectionBuffer *GetNtlmsspUserData(DetectEngineThreadCtx *det_ctx,
+        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
+        const int list_id)
+{
+    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
+    if (buffer->inspect == NULL) {
+        uint32_t b_len = 0;
+        const uint8_t *b = NULL;
+
+        if (rs_smb_tx_get_ntlmssp_user(txv, &b, &b_len) != 1)
+            return NULL;
+        if (b == NULL || b_len == 0)
+            return NULL;
+
+        InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
+        InspectionBufferApplyTransforms(buffer, transforms);
+    }
+    return buffer;
+}
+
+void DetectSmbNtlmsspUserRegister(void)
+{
+    sigmatch_table[KEYWORD_ID].name = KEYWORD_NAME;
+    sigmatch_table[KEYWORD_ID].Setup = DetectSmbNtlmsspUserSetup;
+    sigmatch_table[KEYWORD_ID].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
+    sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB ntlmssp user in session setup";
+
+    DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
+            GetNtlmsspUserData, ALPROTO_SMB, 1);
+
+    DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_SMB, SIG_FLAG_TOSERVER, 0,
+            DetectEngineInspectBufferGeneric, GetNtlmsspUserData);
+
+    g_smb_nltmssp_user_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
+}
diff --git a/src/detect-smb-ntlmssp.h b/src/detect-smb-ntlmssp.h
new file mode 100644 (file)
index 0000000..054f0ae
--- /dev/null
@@ -0,0 +1,29 @@
+/* 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.
+ */
+
+/**
+ * \file
+ *
+ * \author Eric Leblond <el@stamus-networks.com>
+ */
+
+#ifndef __DETECT_SMB_NTLMSSP_H__
+#define __DETECT_SMB_NTLMSSP_H__
+
+void DetectSmbNtlmsspUserRegister(void);
+
+#endif /* __DETECT_SMB_NTLMSSP_H__ */