suricata
detect-ssh-software.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_software 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-software.h"
49 
50 #define KEYWORD_NAME "ssh.software"
51 #define KEYWORD_NAME_LEGACY "ssh_software"
52 #define KEYWORD_DOC "ssh-keywords.html#ssh-software"
53 #define BUFFER_NAME "ssh_software"
54 #define BUFFER_DESC "ssh software field"
55 static int g_buffer_id = 0;
56 
57 static InspectionBuffer *GetSshData(DetectEngineThreadCtx *det_ctx,
58  const DetectEngineTransforms *transforms, Flow *_f,
59  const uint8_t flow_flags, void *txv, const int list_id)
60 {
61  SCEnter();
62 
63  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
64 
65  if (buffer->inspect == NULL) {
66  uint8_t *software = NULL;
67  SshState *ssh_state = (SshState *) txv;
68 
69  if (flow_flags & STREAM_TOSERVER)
70  software = ssh_state->cli_hdr.software_version;
71  else if (flow_flags & STREAM_TOCLIENT)
72  software = ssh_state->srv_hdr.software_version;
73 
74  if (software == NULL) {
75  SCLogDebug("SSL software version not set");
76  return NULL;
77  }
78 
79  uint32_t data_len = strlen((char *)software);
80  uint8_t *data = software;
81  if (data == NULL || data_len == 0) {
82  SCLogDebug("SSL software version not present");
83  return NULL;
84  }
85 
86  InspectionBufferSetup(buffer, data, data_len);
87  InspectionBufferApplyTransforms(buffer, transforms);
88  }
89 
90  return buffer;
91 }
92 
93 static int DetectSshSoftwareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
94 {
95  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
96  return -1;
97 
99  return -1;
100 
101  return 0;
102 }
103 
104 
106 {
111  sigmatch_table[DETECT_AL_SSH_SOFTWARE].Setup = DetectSshSoftwareSetup;
113 
115  PrefilterGenericMpmRegister, GetSshData,
118  PrefilterGenericMpmRegister, GetSshData,
120 
127 
129 
130  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
131 }
SSH_STATE_BANNER_DONE
@ SSH_STATE_BANNER_DONE
Definition: app-layer-ssh.h:67
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1459
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1394
app-layer-ssh.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
SigTableElmt_::name
const char * name
Definition: detect.h:1201
DetectEngineTransforms
Definition: detect.h:369
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
detect-ssh-software.h
InspectionBuffer
Definition: detect.h:342
threads.h
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ssh-software.c:52
Flow_
Flow data structure.
Definition: flow.h:342
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:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:761
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:1187
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1041
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:860
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
decode.h
SshState_
Definition: app-layer-ssh.h:72
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:16
DetectEngineThreadCtx_
Definition: detect.h:1004
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
SCEnter
#define SCEnter(...)
Definition: util-debug.h:337
detect-engine-mpm.h
detect.h
DETECT_AL_SSH_SOFTWARE
@ DETECT_AL_SSH_SOFTWARE
Definition: detect-engine-register.h:161
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:1578
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
SshHeader_::software_version
uint8_t * software_version
Definition: app-layer-ssh.h:61
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SshState_::srv_hdr
SshHeader srv_hdr
Definition: app-layer-ssh.h:73
DetectSshSoftwareRegister
void DetectSshSoftwareRegister(void)
Definition: detect-ssh-software.c:105
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
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-ssh-software.c:53
SigTableElmt_::alias
const char * alias
Definition: detect.h:1202
suricata-common.h
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:1100
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1148
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
DOC_URL
#define DOC_URL
Definition: suricata.h:86
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ssh-software.c:50
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-ssh-software.c:51
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:343
detect-parse.h
Signature_
Signature container.
Definition: detect.h:522
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1370
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:972
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:888
SshState_::cli_hdr
SshHeader cli_hdr
Definition: app-layer-ssh.h:74
DOC_VERSION
#define DOC_VERSION
Definition: suricata.h:91
app-layer.h
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ssh-software.c:54