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 
50 #define KEYWORD_NAME "ssh_proto"
51 #define KEYWORD_DOC "ssh-keywords.html#ssh-proto"
52 #define BUFFER_NAME "ssh_protocol"
53 #define BUFFER_DESC "ssh protocol field"
54 static int g_buffer_id = 0;
55 
56 /** \brief SSH Protocol Mpm prefilter callback
57  *
58  * \param det_ctx detection engine thread ctx
59  * \param p packet to inspect
60  * \param f flow to inspect
61  * \param txv tx to inspect
62  * \param pectx inspection context
63  */
64 static void PrefilterTxSshRequestProtocol(DetectEngineThreadCtx *det_ctx,
65  const void *pectx,
66  Packet *p, Flow *f, void *txv,
67  const uint64_t idx, const uint8_t flags)
68 {
69  SCEnter();
70 
71  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
72  const SshState *ssh_state = txv;
73 
74  if (ssh_state->cli_hdr.proto_version == NULL)
75  return;
76 
77  uint32_t buffer_len = strlen((char *)ssh_state->cli_hdr.proto_version);
78  const uint8_t *buffer = ssh_state->cli_hdr.proto_version;
79 
80  if (buffer_len >= mpm_ctx->minlen) {
81  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
82  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
83  }
84 }
85 
86 static int PrefilterTxSshRequestProtocolRegister(DetectEngineCtx *de_ctx,
87  SigGroupHead *sgh, MpmCtx *mpm_ctx)
88 {
89  SCEnter();
90 
91  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxSshRequestProtocol,
93  mpm_ctx, NULL, KEYWORD_NAME " (request)");
94  return r;
95 }
96 
97 /** \brief SSH Protocol Mpm prefilter callback
98  *
99  * \param det_ctx detection engine thread ctx
100  * \param p packet to inspect
101  * \param f flow to inspect
102  * \param txv tx to inspect
103  * \param pectx inspection context
104  */
105 static void PrefilterTxSshResponseProtocol(DetectEngineThreadCtx *det_ctx,
106  const void *pectx,
107  Packet *p, Flow *f, void *txv,
108  const uint64_t idx, const uint8_t flags)
109 {
110  SCEnter();
111 
112  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
113  const SshState *ssh_state = txv;
114 
115  if (ssh_state->srv_hdr.proto_version == NULL)
116  return;
117 
118  uint32_t buffer_len = strlen((char *)ssh_state->srv_hdr.proto_version);
119  const uint8_t *buffer = ssh_state->srv_hdr.proto_version;
120 
121  if (buffer_len >= mpm_ctx->minlen) {
122  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
123  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
124  }
125 }
126 
127 static int PrefilterTxSshResponseProtocolRegister(DetectEngineCtx *de_ctx,
128  SigGroupHead *sgh, MpmCtx *mpm_ctx)
129 {
130  SCEnter();
131 
132  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxSshResponseProtocol,
134  mpm_ctx, NULL, KEYWORD_NAME " (response)");
135  return r;
136 }
137 
138 static int InspectEngineSshProtocol(ThreadVars *tv,
139  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
140  const Signature *s, const SigMatchData *smd,
141  Flow *f, uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
142 {
143  uint8_t *protocol = NULL;
144  SshState *ssh_state = alstate;
145 
146  if (flags & STREAM_TOSERVER)
147  protocol = ssh_state->cli_hdr.proto_version;
148  else if (flags & STREAM_TOCLIENT)
149  protocol = ssh_state->srv_hdr.proto_version;
150  if (protocol == NULL)
151  goto end;
152 
153  uint32_t buffer_len = strlen((char *)protocol);
154  uint8_t *buffer = protocol;
155  if (buffer == NULL ||buffer_len == 0)
156  goto end;
157 
158  det_ctx->buffer_offset = 0;
159  det_ctx->discontinue_matching = 0;
160  det_ctx->inspection_recursion_counter = 0;
161  int r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd,
162  f,
163  buffer, buffer_len,
166  if (r == 1)
168 
169  end:
173 }
174 
175 static int DetectSshProtocolSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
176 {
177  s->init_data->list = g_buffer_id;
178  return 0;
179 }
180 
182 {
186  sigmatch_table[DETECT_AL_SSH_PROTOCOL].Setup = DetectSshProtocolSetup;
188 
190  PrefilterTxSshRequestProtocolRegister);
192  PrefilterTxSshResponseProtocolRegister);
193 
196  InspectEngineSshProtocol);
199  InspectEngineSshProtocol);
200 
202  BUFFER_DESC);
203 
204  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
205 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
SignatureInitData * init_data
Definition: detect.h:560
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
SshHeader cli_hdr
Definition: app-layer-ssh.h:74
uint16_t minlen
Definition: util-mpm.h:95
#define KEYWORD_NAME
uint16_t discontinue_matching
Definition: detect.h:1026
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx))
register an app layer keyword for mpm
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
uint8_t * proto_version
Definition: app-layer-ssh.h:60
Data needed for Match()
Definition: detect.h:333
Container for matching data for a signature group.
Definition: detect.h:1295
uint32_t buffer_offset
Definition: detect.h:991
#define KEYWORD_DOC
const char * name
Definition: detect.h:1160
uint16_t protocol
Definition: decode-ppp.h:313
Signature container.
Definition: detect.h:492
main detection engine ctx
Definition: detect.h:720
int DetectBufferTypeGetByName(const char *name)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:244
#define DETECT_ENGINE_INSPECT_SIG_MATCH
#define BUFFER_DESC
uint16_t mpm_type
Definition: util-mpm.h:84
SshHeader srv_hdr
Definition: app-layer-ssh.h:73
Data structures and function prototypes for keeping state for the detection engine.
#define SIG_FLAG_TOSERVER
Definition: detect.h:243
#define SCEnter(...)
Definition: util-debug.h:337
PrefilterRuleStore pmq
Definition: detect.h:1061
#define STREAM_TOCLIENT
Definition: stream.h:32
#define BUFFER_NAME
const char * desc
Definition: detect.h:1162
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:165
uint16_t tx_id
MpmThreadCtx mtcu
Definition: detect.h:1059
#define SIGMATCH_NOOPT
Definition: detect.h:1328
int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode, void *data)
Run the actual payload match functions.
const char * url
Definition: detect.h:1163
#define STREAM_TOSERVER
Definition: stream.h:31
int inspection_recursion_counter
Definition: detect.h:1038
#define DETECT_CI_FLAGS_SINGLE
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:158
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Per thread variable structure.
Definition: threadvars.h:57
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1154
void DetectSshProtocolRegister(void)
Flow data structure.
Definition: flow.h:327
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time