suricata
detect-ssh-hassh-server.c
Go to the documentation of this file.
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 "debug.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
31 
32 #include "detect-engine.h"
33 #include "detect-engine-mpm.h"
34 #include "detect-engine-state.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"
49 #include "rust.h"
50 
51 
52 #define KEYWORD_NAME "ssh.hassh.server"
53 #define KEYWORD_ALIAS "ssh-hassh-server"
54 #define KEYWORD_DOC "ssh-keywords.html#ssh.hassh.server"
55 #define BUFFER_NAME "ssh.hassh.server"
56 #define BUFFER_DESC "Ssh Client Fingerprinting For Ssh Servers"
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 {
64 
65  SCEnter();
66 
67  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
68 
69  if (buffer->inspect == NULL) {
70  const uint8_t *hasshServer = NULL;
71  uint32_t b_len = 0;
72 
73  if (rs_ssh_tx_get_hassh(txv, &hasshServer, &b_len, flow_flags) != 1)
74  return NULL;
75  if (hasshServer == NULL || b_len == 0) {
76  SCLogDebug("SSH hassh not set");
77  return NULL;
78  }
79 
80  InspectionBufferSetup(det_ctx, list_id, buffer, hasshServer, b_len);
81  InspectionBufferApplyTransforms(buffer, transforms);
82  }
83 
84  return buffer;
85 }
86 
87 /**
88  * \brief this function setup the ssh.hassh.server 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 DetectSshHasshServerSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
99 {
100  if (DetectBufferSetActiveList(s, g_ssh_hassh_buffer_id) < 0)
101  return -1;
102 
104  return -1;
105 
106  /* try to enable Hassh */
107  rs_ssh_enable_hassh();
108 
109  /* Check if Hassh is disabled */
110  if (!RunmodeIsUnittests() && !rs_ssh_hassh_is_enabled()) {
112  SCLogError(SC_WARN_HASSH_DISABLED, "hassh support is not enabled");
113  }
114  return -2;
115  }
116 
117  return 0;
118 
119 }
120 
121 static bool DetectSshHasshServerHashValidateCallback(const Signature *s, const char **sigerror)
122 {
123  const SigMatch *sm = s->init_data->smlists[g_ssh_hassh_buffer_id];
124  for ( ; sm != NULL; sm = sm->next)
125  {
126  if (sm->type != DETECT_CONTENT)
127  continue;
128 
129  const DetectContentData *cd = (DetectContentData *)sm->ctx;
130 
131  if (cd->flags & DETECT_CONTENT_NOCASE) {
132  *sigerror = "ssh.hassh.server should not be used together with "
133  "nocase, since the rule is automatically "
134  "lowercased anyway which makes nocase redundant.";
135  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
136  }
137 
138  if (cd->content_len != 32)
139  {
140  *sigerror = "Invalid length of the specified ssh.hassh.server (should "
141  "be 32 characters long). This rule will therefore "
142  "never match.";
143  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
144  return false;
145  }
146  for (size_t i = 0; i < cd->content_len; ++i)
147  {
148  if(!isxdigit(cd->content[i]))
149  {
150  *sigerror = "Invalid ssh.hassh.server string (should be string of hexademical characters)."
151  "This rule will therefore never match.";
152  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
153  return false;
154  }
155  }
156  }
157 
158  return true;
159 }
160 
161 static void DetectSshHasshServerHashSetupCallback(const DetectEngineCtx *de_ctx,
162  Signature *s)
163 {
164  SigMatch *sm = s->init_data->smlists[g_ssh_hassh_buffer_id];
165  for ( ; sm != NULL; sm = sm->next)
166  {
167  if (sm->type != DETECT_CONTENT)
168  continue;
169 
171 
172  uint32_t u;
173  for (u = 0; u < cd->content_len; u++)
174  {
175  if (isupper(cd->content[u])) {
176  cd->content[u] = tolower(cd->content[u]);
177  }
178  }
179 
180  SpmDestroyCtx(cd->spm_ctx);
181  cd->spm_ctx = SpmInitCtx(cd->content, cd->content_len, 1,
183  }
184 }
185 
186 /**
187  * \brief Registration function for hasshServer keyword.
188  */
190 {
195  sigmatch_table[DETECT_AL_SSH_HASSH_SERVER].Setup = DetectSshHasshServerSetup;
197 
199  PrefilterGenericMpmRegister, GetSshData,
200  ALPROTO_SSH, SshStateBannerDone);
202  SIG_FLAG_TOCLIENT, SshStateBannerDone,
205 
206  g_ssh_hassh_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
207 
208  DetectBufferTypeRegisterSetupCallback(BUFFER_NAME, DetectSshHasshServerHashSetupCallback);
209  DetectBufferTypeRegisterValidateCallback(BUFFER_NAME, DetectSshHasshServerHashValidateCallback);
210 }
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
app-layer-ssh.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1201
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:378
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:60
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
InspectionBuffer
Definition: detect.h:344
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
Definition: detect-engine.c:902
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:676
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ssh-hassh-server.c:54
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
detect-ssh-hassh-server.h
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ssh-hassh-server.c:56
rust.h
DETECT_AL_SSH_HASSH_SERVER
@ DETECT_AL_SSH_HASSH_SERVER
Definition: detect-engine-register.h:166
DetectSshHasshServerRegister
void DetectSshHasshServerRegister(void)
Registration function for hasshServer keyword.
Definition: detect-ssh-hassh-server.c:189
DetectContentData_
Definition: detect-content.h:86
ALPROTO_SSH
@ ALPROTO_SSH
Definition: app-layer-protos.h:34
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
SC_WARN_POOR_RULE
@ SC_WARN_POOR_RULE
Definition: util-error.h:309
decode.h
util-debug.h
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror))
Definition: detect-engine.c:921
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:324
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
DetectEngineInspectBufferGeneric
int 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:1617
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:174
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:97
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:587
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:511
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:267
SigMatch_::type
uint8_t type
Definition: detect.h:321
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
SigMatchSilentErrorEnabled
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
Definition: detect-parse.c:292
SigTableElmt_::alias
const char * alias
Definition: detect.h:1202
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:87
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
DetectContentData_::spm_ctx
SpmCtx * spm_ctx
Definition: detect-content.h:104
KEYWORD_ALIAS
#define KEYWORD_ALIAS
Definition: detect-ssh-hassh-server.c:53
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
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:1091
Signature_::id
uint32_t id
Definition: detect.h:551
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
SigMatch_
a single match condition for a signature
Definition: detect.h:320
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ssh-hassh-server.c:55
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:88
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectEngineCtx_::spm_global_thread_ctx
SpmGlobalThreadCtx * spm_global_thread_ctx
Definition: detect.h:813
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:857
SC_WARN_HASSH_DISABLED
@ SC_WARN_HASSH_DISABLED
Definition: util-error.h:363
flow.h
SpmDestroyCtx
void SpmDestroyCtx(SpmCtx *ctx)
Definition: util-spm.c:184
flow-var.h
SpmInitCtx
SpmCtx * SpmInitCtx(const uint8_t *needle, uint16_t needle_len, int nocase, SpmGlobalThreadCtx *global_thread_ctx)
Definition: util-spm.c:174
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ssh-hassh-server.c:52
debug.h
app-layer.h