suricata
detect-smb-ntlmssp.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Eric Leblond <el@stamus-networks.com>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "detect-engine-register.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
30 
31 #include "detect-engine.h"
32 #include "detect-engine-buffer.h"
33 #include "detect-engine-mpm.h"
34 #include "detect-engine-state.h"
37 
38 #include "detect-smb-ntlmssp.h"
39 #include "rust.h"
40 
41 #define BUFFER_NAME "smb_ntlmssp_user"
42 #define KEYWORD_NAME "smb.ntlmssp_user"
43 #define KEYWORD_ID DETECT_SMB_NTLMSSP_USER
44 
45 static int g_smb_nltmssp_user_buffer_id = 0;
46 
47 static int DetectSmbNtlmsspUserSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
48 {
49  if (SCDetectBufferSetActiveList(de_ctx, s, g_smb_nltmssp_user_buffer_id) < 0)
50  return -1;
51 
53  return -1;
54 
55  return 0;
56 }
57 
58 static InspectionBuffer *GetNtlmsspUserData(DetectEngineThreadCtx *det_ctx,
59  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
60  const int list_id)
61 {
62  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
63  if (buffer->inspect == NULL) {
64  uint32_t b_len = 0;
65  const uint8_t *b = NULL;
66 
67  if (SCSmbTxGetNtlmsspUser(txv, &b, &b_len) != 1)
68  return NULL;
69  if (b == NULL || b_len == 0)
70  return NULL;
71 
72  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
73  }
74  return buffer;
75 }
76 
78 {
80  sigmatch_table[KEYWORD_ID].Setup = DetectSmbNtlmsspUserSetup;
82  sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB ntlmssp user in session setup";
83  sigmatch_table[KEYWORD_ID].url = "/rules/smb-keywords.html#smb-ntlmssp-user";
84 
86  GetNtlmsspUserData, ALPROTO_SMB, 1);
87 
89  DetectEngineInspectBufferGeneric, GetNtlmsspUserData);
90 
91  g_smb_nltmssp_user_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
92 }
93 
94 #undef BUFFER_NAME
95 #undef KEYWORD_NAME
96 #undef KEYWORD_ID
97 
98 #define BUFFER_NAME "smb_ntlmssp_domain"
99 #define KEYWORD_NAME "smb.ntlmssp_domain"
100 #define KEYWORD_ID DETECT_SMB_NTLMSSP_DOMAIN
101 
102 static int g_smb_nltmssp_domain_buffer_id = 0;
103 
104 static int DetectSmbNtlmsspDomainSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
105 {
106  if (SCDetectBufferSetActiveList(de_ctx, s, g_smb_nltmssp_domain_buffer_id) < 0)
107  return -1;
108 
110  return -1;
111 
112  return 0;
113 }
114 
115 static InspectionBuffer *GetNtlmsspDomainData(DetectEngineThreadCtx *det_ctx,
116  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
117  const int list_id)
118 {
119  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
120  if (buffer->inspect == NULL) {
121  uint32_t b_len = 0;
122  const uint8_t *b = NULL;
123 
124  if (SCSmbTxGetNtlmsspDomain(txv, &b, &b_len) != 1)
125  return NULL;
126  if (b == NULL || b_len == 0)
127  return NULL;
128 
129  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
130  }
131  return buffer;
132 }
133 
135 {
137  sigmatch_table[KEYWORD_ID].Setup = DetectSmbNtlmsspDomainSetup;
140  "sticky buffer to match on SMB ntlmssp domain in session setup";
141  sigmatch_table[KEYWORD_ID].url = "/rules/smb-keywords.html#smb-ntlmssp-domain";
142 
144  GetNtlmsspDomainData, ALPROTO_SMB, 1);
145 
147  DetectEngineInspectBufferGeneric, GetNtlmsspDomainData);
148 
149  g_smb_nltmssp_domain_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
150 }
SigTableElmt_::url
const char * url
Definition: detect.h:1462
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1676
SigTableElmt_::desc
const char * desc
Definition: detect.h:1461
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2057
SigTableElmt_::name
const char * name
Definition: detect.h:1459
DetectEngineTransforms
Definition: detect.h:391
DetectSmbNtlmsspDomainRegister
void DetectSmbNtlmsspDomainRegister(void)
Definition: detect-smb-ntlmssp.c:134
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
detect-smb-ntlmssp.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1450
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:932
detect-engine-register.h
rust.h
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-smb-ntlmssp.c:98
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2229
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1441
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1277
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:271
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1244
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1584
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:152
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
detect-engine-content-inspection.h
DetectSmbNtlmsspUserRegister
void DetectSmbNtlmsspUserRegister(void)
Definition: detect-smb-ntlmssp.c:77
suricata-common.h
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-smb-ntlmssp.c:100
detect-engine-buffer.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-smb-ntlmssp.c:99
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:197
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:43
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1651
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:272