/src/suricata7/src/detect-ssh-hassh-string.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 | | |
31 | | #include "detect-engine.h" |
32 | | #include "detect-engine-mpm.h" |
33 | | #include "detect-engine-state.h" |
34 | | #include "detect-engine-prefilter.h" |
35 | | |
36 | | #include "flow.h" |
37 | | #include "flow-var.h" |
38 | | #include "flow-util.h" |
39 | | |
40 | | #include "util-debug.h" |
41 | | #include "util-unittest.h" |
42 | | #include "util-unittest-helper.h" |
43 | | #include "stream-tcp.h" |
44 | | #include "app-layer.h" |
45 | | #include "app-layer-parser.h" |
46 | | #include "app-layer-ssh.h" |
47 | | #include "detect-ssh-hassh-string.h" |
48 | | #include "rust.h" |
49 | | |
50 | | |
51 | 34 | #define KEYWORD_NAME "ssh.hassh.string" |
52 | 34 | #define KEYWORD_ALIAS "ssh-hassh-string" |
53 | 34 | #define KEYWORD_DOC "ssh-keywords.html#hassh.string" |
54 | 170 | #define BUFFER_NAME "ssh.hassh.string" |
55 | 34 | #define BUFFER_DESC "Ssh Client Key Exchange methods For ssh Clients " |
56 | | static int g_ssh_hassh_string_buffer_id = 0; |
57 | | |
58 | | |
59 | | static InspectionBuffer *GetSshData(DetectEngineThreadCtx *det_ctx, |
60 | | const DetectEngineTransforms *transforms, Flow *_f, |
61 | | const uint8_t flow_flags, void *txv, const int list_id) |
62 | 37 | { |
63 | | |
64 | 37 | SCEnter(); |
65 | | |
66 | 37 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
67 | | |
68 | 37 | if (buffer->inspect == NULL) { |
69 | 36 | const uint8_t *hassh = NULL; |
70 | 36 | uint32_t b_len = 0; |
71 | | |
72 | 36 | if (rs_ssh_tx_get_hassh_string(txv, &hassh, &b_len, flow_flags) != 1) |
73 | 35 | return NULL; |
74 | 1 | if (hassh == NULL || b_len == 0) { |
75 | 0 | SCLogDebug("SSH hassh string is not set"); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 1 | InspectionBufferSetup(det_ctx, list_id, buffer, hassh, b_len); |
80 | 1 | InspectionBufferApplyTransforms(buffer, transforms); |
81 | 1 | } |
82 | | |
83 | 2 | return buffer; |
84 | 37 | } |
85 | | |
86 | | /** |
87 | | * \brief this function setup the hassh.string modifier keyword used in the rule |
88 | | * |
89 | | * \param de_ctx Pointer to the Detection Engine Context |
90 | | * \param s Pointer to the Signature to which the current keyword belongs |
91 | | * \param str Should hold an empty string always |
92 | | * |
93 | | * \retval 0 On success |
94 | | * \retval -1 On failure |
95 | | * \retval -2 on failure that should be silent after the first |
96 | | */ |
97 | | static int DetectSshHasshStringSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) |
98 | 1.61k | { |
99 | 1.61k | if (DetectBufferSetActiveList(de_ctx, s, g_ssh_hassh_string_buffer_id) < 0) |
100 | 501 | return -1; |
101 | | |
102 | 1.10k | if (DetectSignatureSetAppProto(s, ALPROTO_SSH) < 0) |
103 | 230 | return -1; |
104 | | |
105 | | /* try to enable Hassh */ |
106 | 879 | rs_ssh_enable_hassh(); |
107 | | |
108 | | /* Check if Hassh is disabled */ |
109 | 879 | if (!RunmodeIsUnittests() && !rs_ssh_hassh_is_enabled()) { |
110 | 0 | if (!SigMatchSilentErrorEnabled(de_ctx, DETECT_AL_SSH_HASSH_STRING)) { |
111 | 0 | SCLogError("hassh support is not enabled"); |
112 | 0 | } |
113 | 0 | return -2; |
114 | 0 | } |
115 | | |
116 | 879 | return 0; |
117 | | |
118 | 879 | } |
119 | | |
120 | | /** |
121 | | * \brief Registration function for hassh.string keyword. |
122 | | */ |
123 | | void DetectSshHasshStringRegister(void) |
124 | 34 | { |
125 | 34 | sigmatch_table[DETECT_AL_SSH_HASSH_STRING].name = KEYWORD_NAME; |
126 | 34 | sigmatch_table[DETECT_AL_SSH_HASSH_STRING].alias = KEYWORD_ALIAS; |
127 | 34 | sigmatch_table[DETECT_AL_SSH_HASSH_STRING].desc = BUFFER_NAME " sticky buffer"; |
128 | 34 | sigmatch_table[DETECT_AL_SSH_HASSH_STRING].url = "/rules/" KEYWORD_DOC; |
129 | 34 | sigmatch_table[DETECT_AL_SSH_HASSH_STRING].Setup = DetectSshHasshStringSetup; |
130 | 34 | sigmatch_table[DETECT_AL_SSH_HASSH_STRING].flags |= SIGMATCH_INFO_STICKY_BUFFER | SIGMATCH_NOOPT; |
131 | | |
132 | | |
133 | 34 | DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, |
134 | 34 | PrefilterGenericMpmRegister, GetSshData, |
135 | 34 | ALPROTO_SSH, SshStateBannerDone); |
136 | 34 | DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_SSH, |
137 | 34 | SIG_FLAG_TOSERVER, SshStateBannerDone, |
138 | 34 | DetectEngineInspectBufferGeneric, GetSshData); |
139 | | |
140 | 34 | DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); |
141 | | |
142 | 34 | g_ssh_hassh_string_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
143 | 34 | } |