/src/suricata8/src/detect-ssh-hassh-server-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-buffer.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 | | #include "stream-tcp.h" |
41 | | #include "util-debug.h" |
42 | | #include "util-unittest.h" |
43 | | #include "util-unittest-helper.h" |
44 | | |
45 | | #include "app-layer.h" |
46 | | #include "app-layer-parser.h" |
47 | | #include "app-layer-ssh.h" |
48 | | #include "detect-ssh-hassh-server-string.h" |
49 | | #include "rust.h" |
50 | | |
51 | | |
52 | 39 | #define KEYWORD_NAME "ssh.hassh.server.string" |
53 | 39 | #define KEYWORD_ALIAS "ssh-hassh-server-string" |
54 | 39 | #define KEYWORD_DOC "ssh-keywords.html#ssh.hassh.server.string" |
55 | 195 | #define BUFFER_NAME "ssh.hassh.server.string" |
56 | 39 | #define BUFFER_DESC "Ssh Client Key Exchange methods For ssh Servers" |
57 | | static int g_ssh_hassh_server_string_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 | 0 | { |
64 | | |
65 | 0 | SCEnter(); |
66 | |
|
67 | 0 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
68 | |
|
69 | 0 | if (buffer->inspect == NULL) { |
70 | 0 | const uint8_t *hassh = NULL; |
71 | 0 | uint32_t b_len = 0; |
72 | |
|
73 | 0 | if (SCSshTxGetHasshString(txv, &hassh, &b_len, flow_flags) != 1) |
74 | 0 | return NULL; |
75 | 0 | if (hassh == NULL || b_len == 0) { |
76 | 0 | SCLogDebug("SSH hassh string is not set"); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | | |
80 | 0 | InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, hassh, b_len, transforms); |
81 | 0 | } |
82 | | |
83 | 0 | return buffer; |
84 | 0 | } |
85 | | |
86 | | /** |
87 | | * \brief this function setup the ssh.hassh.server.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 DetectSshHasshServerStringSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) |
98 | 319 | { |
99 | 319 | if (SCDetectBufferSetActiveList(de_ctx, s, g_ssh_hassh_server_string_buffer_id) < 0) |
100 | 60 | return -1; |
101 | | |
102 | 259 | if (SCDetectSignatureSetAppProto(s, ALPROTO_SSH) < 0) |
103 | 1 | return -1; |
104 | | |
105 | | /* try to enable Hassh */ |
106 | 258 | SCSshEnableHassh(); |
107 | | |
108 | | /* Check if Hassh is disabled */ |
109 | 258 | if (!RunmodeIsUnittests() && !SCSshHasshIsEnabled()) { |
110 | 0 | if (!SigMatchSilentErrorEnabled(de_ctx, DETECT_SSH_HASSH_SERVER_STRING)) { |
111 | 0 | SCLogError("hassh support is not enabled"); |
112 | 0 | } |
113 | 0 | return -2; |
114 | 0 | } |
115 | | |
116 | 258 | return 0; |
117 | | |
118 | 258 | } |
119 | | |
120 | | /** |
121 | | * \brief Registration function for hasshServer.string keyword. |
122 | | */ |
123 | | void DetectSshHasshServerStringRegister(void) |
124 | 39 | { |
125 | 39 | sigmatch_table[DETECT_SSH_HASSH_SERVER_STRING].name = KEYWORD_NAME; |
126 | 39 | sigmatch_table[DETECT_SSH_HASSH_SERVER_STRING].alias = KEYWORD_ALIAS; |
127 | 39 | sigmatch_table[DETECT_SSH_HASSH_SERVER_STRING].desc = BUFFER_NAME " sticky buffer"; |
128 | 39 | sigmatch_table[DETECT_SSH_HASSH_SERVER_STRING].url = "/rules/" KEYWORD_DOC; |
129 | 39 | sigmatch_table[DETECT_SSH_HASSH_SERVER_STRING].Setup = DetectSshHasshServerStringSetup; |
130 | 39 | sigmatch_table[DETECT_SSH_HASSH_SERVER_STRING].flags |= |
131 | 39 | SIGMATCH_INFO_STICKY_BUFFER | SIGMATCH_NOOPT; |
132 | | |
133 | 39 | DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, |
134 | 39 | GetSshData, ALPROTO_SSH, SshStateBannerDone); |
135 | 39 | DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_SSH, SIG_FLAG_TOCLIENT, |
136 | 39 | SshStateBannerDone, DetectEngineInspectBufferGeneric, GetSshData); |
137 | | |
138 | 39 | DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); |
139 | | |
140 | 39 | g_ssh_hassh_server_string_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
141 | 39 | } |