suricata
detect-ssh-hassh-server-string.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 "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"
35 
36 #include "flow.h"
37 #include "flow-var.h"
38 #include "flow-util.h"
39 #include "stream-tcp.h"
40 #include "util-debug.h"
41 #include "util-unittest.h"
42 #include "util-unittest-helper.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 #include "app-layer-ssh.h"
48 #include "rust.h"
49 
50 
51 #define KEYWORD_NAME "ssh.hassh.server.string"
52 #define KEYWORD_ALIAS "ssh-hassh-server-string"
53 #define KEYWORD_DOC "ssh-keywords.html#ssh.hassh.server.string"
54 #define BUFFER_NAME "ssh.hassh.server.string"
55 #define BUFFER_DESC "Ssh Client Key Exchange methods For ssh Servers"
56 static int g_ssh_hassh_server_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 {
63 
64  SCEnter();
65 
66  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
67 
68  if (buffer->inspect == NULL) {
69  const uint8_t *hassh = NULL;
70  uint32_t b_len = 0;
71 
72  if (rs_ssh_tx_get_hassh_string(txv, &hassh, &b_len, flow_flags) != 1)
73  return NULL;
74  if (hassh == NULL || b_len == 0) {
75  SCLogDebug("SSH hassh string is not set");
76  return NULL;
77  }
78 
79  InspectionBufferSetup(det_ctx, list_id, buffer, hassh, b_len);
80  InspectionBufferApplyTransforms(buffer, transforms);
81  }
82 
83  return buffer;
84 }
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 {
99  if (DetectBufferSetActiveList(de_ctx, s, g_ssh_hassh_server_string_buffer_id) < 0)
100  return -1;
101 
103  return -1;
104 
105  /* try to enable Hassh */
106  rs_ssh_enable_hassh();
107 
108  /* Check if Hassh is disabled */
109  if (!RunmodeIsUnittests() && !rs_ssh_hassh_is_enabled()) {
111  SCLogError("hassh support is not enabled");
112  }
113  return -2;
114  }
115 
116  return 0;
117 
118 }
119 
120 /**
121  * \brief Registration function for hasshServer.string keyword.
122  */
124 {
129  sigmatch_table[DETECT_AL_SSH_HASSH_SERVER_STRING].Setup = DetectSshHasshServerStringSetup;
131 
133  GetSshData, ALPROTO_SSH, SshStateBannerDone);
135  SshStateBannerDone, DetectEngineInspectBufferGeneric, GetSshData);
136 
138 
139  g_ssh_hassh_server_string_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
140 }
detect-ssh-hassh-server-string.h
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
app-layer-ssh.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
DetectEngineInspectBufferGeneric
uint8_t 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:2154
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1304
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:408
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
rust.h
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ssh-hassh-server-string.c:55
ALPROTO_SSH
@ ALPROTO_SSH
Definition: app-layer-protos.h:34
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
decode.h
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ssh-hassh-server-string.c:53
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ssh-hassh-server-string.c:54
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
app-layer-parser.h
KEYWORD_ALIAS
#define KEYWORD_ALIAS
Definition: detect-ssh-hassh-server-string.c:52
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ssh-hassh-server-string.c:51
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:251
SigMatchSilentErrorEnabled
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
Definition: detect-parse.c:378
SigTableElmt_::alias
const char * alias
Definition: detect.h:1305
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
DetectSshHasshServerStringRegister
void DetectSshHasshServerStringRegister(void)
Registration function for hasshServer.string keyword.
Definition: detect-ssh-hassh-server-string.c:123
DETECT_AL_SSH_HASSH_SERVER_STRING
@ DETECT_AL_SSH_HASSH_SERVER_STRING
Definition: detect-engine-register.h:195
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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:1595
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
flow.h
flow-var.h
app-layer.h