/src/suricata7/src/detect-ssh-hassh.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2020 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 Vadym Malakhatko <v.malakhatko@sirinsoftware.com> |
22 | | */ |
23 | | |
24 | | #include "suricata-common.h" |
25 | | #include "threads.h" |
26 | | #include "decode.h" |
27 | | |
28 | | #include "detect.h" |
29 | | #include "detect-parse.h" |
30 | | #include "detect-content.h" |
31 | | |
32 | | #include "detect-engine.h" |
33 | | #include "detect-engine-mpm.h" |
34 | | #include "detect-engine-state.h" |
35 | | #include "detect-engine-prefilter.h" |
36 | | |
37 | | #include "flow.h" |
38 | | #include "flow-var.h" |
39 | | #include "flow-util.h" |
40 | | |
41 | | #include "util-debug.h" |
42 | | #include "util-unittest.h" |
43 | | #include "util-unittest-helper.h" |
44 | | #include "stream-tcp.h" |
45 | | #include "app-layer.h" |
46 | | #include "app-layer-parser.h" |
47 | | #include "app-layer-ssh.h" |
48 | | #include "detect-ssh-hassh.h" |
49 | | #include "rust.h" |
50 | | |
51 | | |
52 | 73 | #define KEYWORD_NAME "ssh.hassh" |
53 | 73 | #define KEYWORD_ALIAS "ssh-hassh" |
54 | 73 | #define KEYWORD_DOC "ssh-keywords.html#hassh" |
55 | 511 | #define BUFFER_NAME "ssh.hassh" |
56 | 73 | #define BUFFER_DESC "Ssh Client Fingerprinting For Ssh Clients " |
57 | | static int g_ssh_hassh_buffer_id = 0; |
58 | | |
59 | | |
60 | | static InspectionBuffer *GetSshData(DetectEngineThreadCtx *det_ctx, |
61 | | const DetectEngineTransforms *transforms, Flow *_f, |
62 | | const uint8_t flow_flags, void *txv, const int list_id) |
63 | 147 | { |
64 | | |
65 | 147 | SCEnter(); |
66 | | |
67 | 147 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
68 | | |
69 | 147 | if (buffer->inspect == NULL) { |
70 | 136 | const uint8_t *hassh = NULL; |
71 | 136 | uint32_t b_len = 0; |
72 | | |
73 | 136 | if (rs_ssh_tx_get_hassh(txv, &hassh, &b_len, flow_flags) != 1) |
74 | 108 | return NULL; |
75 | 28 | if (hassh == NULL || b_len == 0) { |
76 | 0 | SCLogDebug("SSH hassh not set"); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | | |
80 | 28 | InspectionBufferSetup(det_ctx, list_id, buffer, hassh, b_len); |
81 | 28 | InspectionBufferApplyTransforms(buffer, transforms); |
82 | 28 | } |
83 | | |
84 | 39 | return buffer; |
85 | 147 | } |
86 | | |
87 | | /** |
88 | | * \brief this function setup the hassh modifier keyword used in the rule |
89 | | * |
90 | | * \param de_ctx Pointer to the Detection Engine Context |
91 | | * \param s Pointer to the Signature to which the current keyword belongs |
92 | | * \param str Should hold an empty string always |
93 | | * |
94 | | * \retval 0 On success |
95 | | * \retval -1 On failure |
96 | | * \retval -2 on failure that should be silent after the first |
97 | | */ |
98 | | static int DetectSshHasshSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) |
99 | 11.4k | { |
100 | 11.4k | if (DetectBufferSetActiveList(de_ctx, s, g_ssh_hassh_buffer_id) < 0) |
101 | 12 | return -1; |
102 | | |
103 | 11.4k | if (DetectSignatureSetAppProto(s, ALPROTO_SSH) < 0) |
104 | 483 | return -1; |
105 | | |
106 | | /* try to enable Hassh */ |
107 | 10.9k | rs_ssh_enable_hassh(); |
108 | | |
109 | | /* Check if Hassh is disabled */ |
110 | 10.9k | if (!RunmodeIsUnittests() && !rs_ssh_hassh_is_enabled()) { |
111 | 0 | if (!SigMatchSilentErrorEnabled(de_ctx, DETECT_AL_SSH_HASSH)) { |
112 | 0 | SCLogError("hassh support is not enabled"); |
113 | 0 | } |
114 | 0 | return -2; |
115 | 0 | } |
116 | | |
117 | 10.9k | return 0; |
118 | | |
119 | 10.9k | } |
120 | | |
121 | | |
122 | | static bool DetectSshHasshHashValidateCallback(const Signature *s, |
123 | | const char **sigerror) |
124 | 601 | { |
125 | 717 | for (uint32_t x = 0; x < s->init_data->buffer_index; x++) { |
126 | 605 | if (s->init_data->buffers[x].id != (uint32_t)g_ssh_hassh_buffer_id) |
127 | 4 | continue; |
128 | 601 | const SigMatch *sm = s->init_data->buffers[x].head; |
129 | 909 | for (; sm != NULL; sm = sm->next) { |
130 | 797 | if (sm->type != DETECT_CONTENT) |
131 | 204 | continue; |
132 | | |
133 | 593 | const DetectContentData *cd = (DetectContentData *)sm->ctx; |
134 | | |
135 | 593 | if (cd->flags & DETECT_CONTENT_NOCASE) { |
136 | 29 | *sigerror = "ssh.hassh should not be used together with " |
137 | 29 | "nocase, since the rule is automatically " |
138 | 29 | "lowercased anyway which makes nocase redundant."; |
139 | 29 | SCLogWarning("rule %u: %s", s->id, *sigerror); |
140 | 29 | } |
141 | | |
142 | 593 | if (cd->content_len != 32) { |
143 | 442 | *sigerror = "Invalid length of the specified ssh.hassh (should " |
144 | 442 | "be 32 characters long). This rule will therefore " |
145 | 442 | "never match."; |
146 | 442 | SCLogWarning("rule %u: %s", s->id, *sigerror); |
147 | 442 | return false; |
148 | 442 | } |
149 | 3.49k | for (size_t i = 0; i < cd->content_len; ++i) { |
150 | 3.38k | if (!isxdigit(cd->content[i])) { |
151 | 47 | *sigerror = |
152 | 47 | "Invalid ssh.hassh string (should be string of hexadecimal characters)." |
153 | 47 | "This rule will therefore never match."; |
154 | 47 | SCLogWarning("rule %u: %s", s->id, *sigerror); |
155 | 47 | return false; |
156 | 47 | } |
157 | 3.38k | } |
158 | 151 | } |
159 | 601 | } |
160 | 112 | return true; |
161 | 601 | } |
162 | | |
163 | | static void DetectSshHasshHashSetupCallback(const DetectEngineCtx *de_ctx, |
164 | | Signature *s) |
165 | 6.70k | { |
166 | 15.7k | for (uint32_t x = 0; x < s->init_data->buffer_index; x++) { |
167 | 9.00k | if (s->init_data->buffers[x].id != (uint32_t)g_ssh_hassh_buffer_id) |
168 | 2.31k | continue; |
169 | 6.69k | SigMatch *sm = s->init_data->buffers[x].head; |
170 | 17.2k | for (; sm != NULL; sm = sm->next) { |
171 | 10.5k | if (sm->type != DETECT_CONTENT) |
172 | 2.36k | continue; |
173 | | |
174 | 8.14k | DetectContentData *cd = (DetectContentData *)sm->ctx; |
175 | | |
176 | 8.14k | uint32_t u; |
177 | 487k | for (u = 0; u < cd->content_len; u++) { |
178 | 478k | if (isupper(cd->content[u])) { |
179 | 57.9k | cd->content[u] = u8_tolower(cd->content[u]); |
180 | 57.9k | } |
181 | 478k | } |
182 | | |
183 | 8.14k | SpmDestroyCtx(cd->spm_ctx); |
184 | 8.14k | cd->spm_ctx = |
185 | 8.14k | SpmInitCtx(cd->content, cd->content_len, 1, de_ctx->spm_global_thread_ctx); |
186 | 8.14k | } |
187 | 6.69k | } |
188 | 6.70k | } |
189 | | |
190 | | /** |
191 | | * \brief Registration function for hassh keyword. |
192 | | */ |
193 | | void DetectSshHasshRegister(void) |
194 | 73 | { |
195 | 73 | sigmatch_table[DETECT_AL_SSH_HASSH].name = KEYWORD_NAME; |
196 | 73 | sigmatch_table[DETECT_AL_SSH_HASSH].alias = KEYWORD_ALIAS; |
197 | 73 | sigmatch_table[DETECT_AL_SSH_HASSH].desc = BUFFER_NAME " sticky buffer"; |
198 | 73 | sigmatch_table[DETECT_AL_SSH_HASSH].url = "/rules/" KEYWORD_DOC; |
199 | 73 | sigmatch_table[DETECT_AL_SSH_HASSH].Setup = DetectSshHasshSetup; |
200 | 73 | sigmatch_table[DETECT_AL_SSH_HASSH].flags |= SIGMATCH_INFO_STICKY_BUFFER | SIGMATCH_NOOPT; |
201 | | |
202 | | |
203 | 73 | DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, |
204 | 73 | PrefilterGenericMpmRegister, GetSshData, |
205 | 73 | ALPROTO_SSH, SshStateBannerDone), |
206 | 73 | DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_SSH, |
207 | 73 | SIG_FLAG_TOSERVER, SshStateBannerDone, |
208 | 73 | DetectEngineInspectBufferGeneric, GetSshData); |
209 | 73 | DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); |
210 | | |
211 | 73 | g_ssh_hassh_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
212 | | |
213 | 73 | DetectBufferTypeRegisterSetupCallback(BUFFER_NAME, DetectSshHasshHashSetupCallback); |
214 | 73 | DetectBufferTypeRegisterValidateCallback(BUFFER_NAME, DetectSshHasshHashValidateCallback); |
215 | 73 | } |
216 | | |