suricata
detect-ssh-proto.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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  * \ingroup sshlayer
20  *
21  * @{
22  */
23 
24 
25 /**
26  * \file
27  *
28  * \author Victor Julien <victor@inliniac.net>
29  *
30  * Implements support ssh_proto sticky buffer
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "decode.h"
36 
37 #include "detect.h"
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.h"
44 
45 #include "app-layer.h"
46 #include "app-layer-parser.h"
47 #include "app-layer-ssh.h"
48 #include "detect-ssh-proto.h"
49 #include "rust.h"
50 
51 #define KEYWORD_NAME "ssh.proto"
52 #define KEYWORD_NAME_LEGACY "ssh_proto"
53 #define KEYWORD_DOC "ssh-keywords.html#ssh-proto"
54 #define BUFFER_NAME "ssh.proto"
55 #define BUFFER_DESC "ssh protocol version field"
56 static int g_buffer_id = 0;
57 
58 static InspectionBuffer *GetSshData(DetectEngineThreadCtx *det_ctx,
59  const DetectEngineTransforms *transforms, Flow *_f,
60  const uint8_t flow_flags, void *txv, const int list_id)
61 {
62  SCEnter();
63 
64  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
65 
66  if (buffer->inspect == NULL) {
67  const uint8_t *protocol = NULL;
68  uint32_t b_len = 0;
69 
70  if (rs_ssh_tx_get_protocol(txv, &protocol, &b_len, flow_flags) != 1)
71  return NULL;
72  if (protocol == NULL || b_len == 0) {
73  SCLogDebug("SSH protocol not set");
74  return NULL;
75  }
76 
77  InspectionBufferSetup(buffer, protocol, b_len);
78  InspectionBufferApplyTransforms(buffer, transforms);
79  }
80 
81  return buffer;
82 }
83 
84 static int DetectSshProtocolSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
85 {
86  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
87  return -1;
88 
90  return -1;
91 
92  return 0;
93 }
94 
96 {
101  sigmatch_table[DETECT_AL_SSH_PROTOCOL].Setup = DetectSshProtocolSetup;
103 
104 
106  PrefilterGenericMpmRegister, GetSshData,
107  ALPROTO_SSH, SshStateBannerDone),
109  PrefilterGenericMpmRegister, GetSshData,
110  ALPROTO_SSH, SshStateBannerDone),
111 
113  ALPROTO_SSH, SIG_FLAG_TOSERVER, SshStateBannerDone,
116  ALPROTO_SSH, SIG_FLAG_TOCLIENT, SshStateBannerDone,
118 
120 
121  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
122 }
SigTableElmt_::url
const char * url
Definition: detect.h:1212
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1468
detect-engine.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ssh-proto.c:51
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1402
app-layer-ssh.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1211
SigTableElmt_::name
const char * name
Definition: detect.h:1209
DetectEngineTransforms
Definition: detect.h:375
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
InspectionBuffer
Definition: detect.h:343
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:611
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1203
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
rust.h
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:1195
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1057
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:876
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
protocol
uint16_t protocol
Definition: decode-ppp.h:2
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1009
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ssh-proto.c:55
DetectSshProtocolRegister
void DetectSshProtocolRegister(void)
Definition: detect-ssh-proto.c:95
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ssh-proto.c:53
app-layer-parser.h
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:1637
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:231
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
DETECT_AL_SSH_PROTOCOL
@ DETECT_AL_SSH_PROTOCOL
Definition: detect-engine-register.h:159
detect-engine-content-inspection.h
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
SigTableElmt_::alias
const char * alias
Definition: detect.h:1210
suricata-common.h
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-ssh-proto.c:52
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1116
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1207
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ssh-proto.c:54
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:344
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
detect-ssh-proto.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1378
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:988
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:904
app-layer.h