/src/suricata7/src/detect-smb-ntlmssp.c
Line | Count | Source |
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" |
34 | | #include "detect-engine-prefilter.h" |
35 | | #include "detect-engine-content-inspection.h" |
36 | | |
37 | | #include "detect-smb-ntlmssp.h" |
38 | | #include "rust.h" |
39 | | |
40 | 102 | #define BUFFER_NAME "smb_ntlmssp_user" |
41 | 34 | #define KEYWORD_NAME "smb.ntlmssp_user" |
42 | 136 | #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 | 1.88k | { |
48 | 1.88k | if (DetectBufferSetActiveList(de_ctx, s, g_smb_nltmssp_user_buffer_id) < 0) |
49 | 1 | return -1; |
50 | | |
51 | 1.88k | if (DetectSignatureSetAppProto(s, ALPROTO_SMB) < 0) |
52 | 887 | return -1; |
53 | | |
54 | 996 | return 0; |
55 | 1.88k | } |
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 | 147 | { |
61 | 147 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
62 | 147 | if (buffer->inspect == NULL) { |
63 | 147 | uint32_t b_len = 0; |
64 | 147 | const uint8_t *b = NULL; |
65 | | |
66 | 147 | if (rs_smb_tx_get_ntlmssp_user(txv, &b, &b_len) != 1) |
67 | 147 | return NULL; |
68 | 0 | if (b == NULL || b_len == 0) |
69 | 0 | return NULL; |
70 | | |
71 | 0 | InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len); |
72 | 0 | InspectionBufferApplyTransforms(buffer, transforms); |
73 | 0 | } |
74 | 0 | return buffer; |
75 | 147 | } |
76 | | |
77 | | void DetectSmbNtlmsspUserRegister(void) |
78 | 34 | { |
79 | 34 | sigmatch_table[KEYWORD_ID].name = KEYWORD_NAME; |
80 | 34 | sigmatch_table[KEYWORD_ID].Setup = DetectSmbNtlmsspUserSetup; |
81 | 34 | sigmatch_table[KEYWORD_ID].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; |
82 | 34 | sigmatch_table[KEYWORD_ID].desc = "sticky buffer to match on SMB ntlmssp user in session setup"; |
83 | | |
84 | 34 | DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, |
85 | 34 | GetNtlmsspUserData, ALPROTO_SMB, 1); |
86 | | |
87 | 34 | DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_SMB, SIG_FLAG_TOSERVER, 0, |
88 | 34 | DetectEngineInspectBufferGeneric, GetNtlmsspUserData); |
89 | | |
90 | 34 | g_smb_nltmssp_user_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
91 | 34 | } |
92 | | |
93 | | #undef BUFFER_NAME |
94 | | #undef KEYWORD_NAME |
95 | | #undef KEYWORD_ID |
96 | | |
97 | 102 | #define BUFFER_NAME "smb_ntlmssp_domain" |
98 | 34 | #define KEYWORD_NAME "smb.ntlmssp_domain" |
99 | 136 | #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 | 1.04k | { |
105 | 1.04k | if (DetectBufferSetActiveList(de_ctx, s, g_smb_nltmssp_domain_buffer_id) < 0) |
106 | 2 | return -1; |
107 | | |
108 | 1.04k | if (DetectSignatureSetAppProto(s, ALPROTO_SMB) < 0) |
109 | 90 | return -1; |
110 | | |
111 | 956 | return 0; |
112 | 1.04k | } |
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 | 326 | { |
118 | 326 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
119 | 326 | if (buffer->inspect == NULL) { |
120 | 326 | uint32_t b_len = 0; |
121 | 326 | const uint8_t *b = NULL; |
122 | | |
123 | 326 | if (rs_smb_tx_get_ntlmssp_domain(txv, &b, &b_len) != 1) |
124 | 319 | return NULL; |
125 | 7 | if (b == NULL || b_len == 0) |
126 | 0 | return NULL; |
127 | | |
128 | 7 | InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len); |
129 | 7 | InspectionBufferApplyTransforms(buffer, transforms); |
130 | 7 | } |
131 | 7 | return buffer; |
132 | 326 | } |
133 | | |
134 | | void DetectSmbNtlmsspDomainRegister(void) |
135 | 34 | { |
136 | 34 | sigmatch_table[KEYWORD_ID].name = KEYWORD_NAME; |
137 | 34 | sigmatch_table[KEYWORD_ID].Setup = DetectSmbNtlmsspDomainSetup; |
138 | 34 | sigmatch_table[KEYWORD_ID].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; |
139 | 34 | sigmatch_table[KEYWORD_ID].desc = |
140 | 34 | "sticky buffer to match on SMB ntlmssp domain in session setup"; |
141 | | |
142 | 34 | DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, |
143 | 34 | GetNtlmsspDomainData, ALPROTO_SMB, 1); |
144 | | |
145 | 34 | DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_SMB, SIG_FLAG_TOSERVER, 0, |
146 | 34 | DetectEngineInspectBufferGeneric, GetNtlmsspDomainData); |
147 | | |
148 | 34 | g_smb_nltmssp_domain_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
149 | 34 | } |