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 "detect-engine-register.h"
26 #include "suricata-common.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
30 
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
33 #include "detect-engine-state.h"
36 
37 #include "detect-smb-ntlmssp.h"
38 #include "rust.h"
39 
40 #define BUFFER_NAME "smb_ntlmssp_user"
41 #define KEYWORD_NAME "smb.ntlmssp_user"
42 #define KEYWORD_ID DETECT_SMB_NTLMSSP_USER
43 
44 static int g_smb_nltmssp_user_buffer_id = 0;
45 
46 static int DetectSmbNtlmsspUserSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
47 {
48  if (DetectBufferSetActiveList(s, g_smb_nltmssp_user_buffer_id) < 0)
49  return -1;
50 
52  return -1;
53 
54  return 0;
55 }
56 
57 static InspectionBuffer *GetNtlmsspUserData(DetectEngineThreadCtx *det_ctx,
58  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
59  const int list_id)
60 {
61  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
62  if (buffer->inspect == NULL) {
63  uint32_t b_len = 0;
64  const uint8_t *b = NULL;
65 
66  if (rs_smb_tx_get_ntlmssp_user(txv, &b, &b_len) != 1)
67  return NULL;
68  if (b == NULL || b_len == 0)
69  return NULL;
70 
71  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
72  InspectionBufferApplyTransforms(buffer, 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 
85  GetNtlmsspUserData, ALPROTO_SMB, 1);
86 
88  DetectEngineInspectBufferGeneric, GetNtlmsspUserData);
89 
90  g_smb_nltmssp_user_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
91 }
92 
93 #undef BUFFER_NAME
94 #undef KEYWORD_NAME
95 #undef KEYWORD_ID
96 
97 #define BUFFER_NAME "smb_ntlmssp_domain"
98 #define KEYWORD_NAME "smb.ntlmssp_domain"
99 #define KEYWORD_ID DETECT_SMB_NTLMSSP_DOMAIN
100 
101 static int g_smb_nltmssp_domain_buffer_id = 0;
102 
103 static int DetectSmbNtlmsspDomainSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
104 {
105  if (DetectBufferSetActiveList(s, g_smb_nltmssp_domain_buffer_id) < 0)
106  return -1;
107 
109  return -1;
110 
111  return 0;
112 }
113 
114 static InspectionBuffer *GetNtlmsspDomainData(DetectEngineThreadCtx *det_ctx,
115  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
116  const int list_id)
117 {
118  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
119  if (buffer->inspect == NULL) {
120  uint32_t b_len = 0;
121  const uint8_t *b = NULL;
122 
123  if (rs_smb_tx_get_ntlmssp_domain(txv, &b, &b_len) != 1)
124  return NULL;
125  if (b == NULL || b_len == 0)
126  return NULL;
127 
128  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
129  InspectionBufferApplyTransforms(buffer, 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 
143  GetNtlmsspDomainData, ALPROTO_SMB, 1);
144 
146  DetectEngineInspectBufferGeneric, GetNtlmsspDomainData);
147 
148  g_smb_nltmssp_domain_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
149 }
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1447
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
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:1991
SigTableElmt_::name
const char * name
Definition: detect.h:1238
DetectEngineTransforms
Definition: detect.h:372
DetectSmbNtlmsspDomainRegister
void DetectSmbNtlmsspDomainRegister(void)
Definition: detect-smb-ntlmssp.c:134
InspectionBuffer
Definition: detect.h:337
detect-smb-ntlmssp.h
Flow_
Flow data structure.
Definition: flow.h:357
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:749
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1232
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
detect-engine-register.h
rust.h
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-smb-ntlmssp.c:97
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1364
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
detect-engine-mpm.h
detect.h
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:224
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
detect-engine-content-inspection.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
DetectSmbNtlmsspUserRegister
void DetectSmbNtlmsspUserRegister(void)
Definition: detect-smb-ntlmssp.c:77
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1550
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-smb-ntlmssp.c:99
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-smb-ntlmssp.c:98
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1446
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1423
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1293