suricata
detect-ike-spi.c
Go to the documentation of this file.
1 /* Copyright (C) 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  *
20  * \author Frank Honza <frank.honza@dcso.de>
21  */
22 
23 #include "suricata-common.h"
24 #include "threads.h"
25 #include "decode.h"
26 #include "detect.h"
27 
28 #include "detect-parse.h"
29 #include "detect-engine.h"
30 #include "detect-engine-buffer.h"
31 #include "detect-engine-mpm.h"
33 #include "detect-urilen.h"
34 
35 #include "flow.h"
36 #include "flow-var.h"
37 #include "flow-util.h"
38 
39 #include "util-debug.h"
40 #include "util-unittest.h"
41 #include "util-unittest-helper.h"
42 #include "util-spm.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "detect-ike-spi.h"
48 #include "stream-tcp.h"
49 
50 #include "rust.h"
51 #include "app-layer-ike.h"
52 #include "rust-bindings.h"
53 
54 #define KEYWORD_NAME_INITIATOR "ike.init_spi"
55 #define KEYWORD_DOC_INITIATOR "ike-keywords.html#ike-init_spi";
56 #define BUFFER_NAME_INITIATOR "ike.init_spi"
57 #define BUFFER_DESC_INITIATOR "ike init spi"
58 
59 #define KEYWORD_NAME_RESPONDER "ike.resp_spi"
60 #define KEYWORD_DOC_RESPONDER "ike-keywords.html#ike-resp_spi";
61 #define BUFFER_NAME_RESPONDER "ike.resp_spi"
62 #define BUFFER_DESC_RESPONDER "ike resp spi"
63 
64 static int g_buffer_initiator_id = 0;
65 static int g_buffer_responder_id = 0;
66 
67 static int DetectSpiInitiatorSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
68 {
69  if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_initiator_id) < 0)
70  return -1;
71 
73  return -1;
74 
75  return 0;
76 }
77 
78 static int DetectSpiResponderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
79 {
80  if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_responder_id) < 0)
81  return -1;
82 
84  return -1;
85 
86  return 0;
87 }
88 
89 static InspectionBuffer *GetInitiatorData(DetectEngineThreadCtx *det_ctx,
90  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
91  const int list_id)
92 {
93  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
94  if (buffer->inspect == NULL) {
95  const uint8_t *b = NULL;
96  uint32_t b_len = 0;
97 
98  if (SCIkeStateGetSpiInitiator(txv, &b, &b_len) != 1)
99  return NULL;
100  if (b == NULL || b_len == 0)
101  return NULL;
102 
103  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
104  }
105 
106  return buffer;
107 }
108 
109 static InspectionBuffer *GetResponderData(DetectEngineThreadCtx *det_ctx,
110  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
111  const int list_id)
112 {
113  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
114  if (buffer->inspect == NULL) {
115  const uint8_t *b = NULL;
116  uint32_t b_len = 0;
117 
118  if (SCIkeStateGetSpiResponder(txv, &b, &b_len) != 1)
119  return NULL;
120  if (b == NULL || b_len == 0)
121  return NULL;
122 
123  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
124  }
125 
126  return buffer;
127 }
128 
130 {
131  // register initiator
135  "sticky buffer to match on the IKE spi initiator";
136  sigmatch_table[DETECT_IKE_SPI_INITIATOR].Setup = DetectSpiInitiatorSetup;
138 
140  DetectEngineInspectBufferGeneric, GetInitiatorData);
141 
143  PrefilterGenericMpmRegister, GetInitiatorData, ALPROTO_IKE, 1);
144 
146 
147  g_buffer_initiator_id = DetectBufferTypeGetByName(BUFFER_NAME_INITIATOR);
148  SCLogDebug("registering " BUFFER_NAME_INITIATOR " rule option");
149 
150  // register responder
154  "sticky buffer to match on the IKE spi responder";
155  sigmatch_table[DETECT_IKE_SPI_RESPONDER].Setup = DetectSpiResponderSetup;
157 
159  DetectEngineInspectBufferGeneric, GetResponderData);
160 
162  PrefilterGenericMpmRegister, GetResponderData, ALPROTO_IKE, 1);
163 
165 
166  g_buffer_responder_id = DetectBufferTypeGetByName(BUFFER_NAME_RESPONDER);
167  SCLogDebug("registering " BUFFER_NAME_RESPONDER " rule option");
168 }
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1645
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:55
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
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:2103
flow-util.h
BUFFER_DESC_RESPONDER
#define BUFFER_DESC_RESPONDER
Definition: detect-ike-spi.c:62
SigTableElmt_::name
const char * name
Definition: detect.h:1428
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine.c:1570
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:415
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DETECT_IKE_SPI_RESPONDER
@ DETECT_IKE_SPI_RESPONDER
Definition: detect-engine-register.h:321
InspectionBuffer
Definition: detect.h:380
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
KEYWORD_DOC_RESPONDER
#define KEYWORD_DOC_RESPONDER
Definition: detect-ike-spi.c:60
rust.h
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1429
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
app-layer-ike.h
DetectEngineThreadCtx_
Definition: detect.h:1223
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:1547
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_NAME_RESPONDER
#define KEYWORD_NAME_RESPONDER
Definition: detect-ike-spi.c:59
detect-ike-spi.h
BUFFER_NAME_INITIATOR
#define BUFFER_NAME_INITIATOR
Definition: detect-ike-spi.c:56
DETECT_IKE_SPI_INITIATOR
@ DETECT_IKE_SPI_INITIATOR
Definition: detect-engine-register.h:320
suricata-common.h
BUFFER_DESC_INITIATOR
#define BUFFER_DESC_INITIATOR
Definition: detect-ike-spi.c:57
util-spm.h
DetectIkeSpiRegister
void DetectIkeSpiRegister(void)
Definition: detect-ike-spi.c:129
detect-engine-buffer.h
KEYWORD_NAME_INITIATOR
#define KEYWORD_NAME_INITIATOR
Definition: detect-ike-spi.c:54
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
str
#define str(s)
Definition: suricata-common.h:308
BUFFER_NAME_RESPONDER
#define BUFFER_NAME_RESPONDER
Definition: detect-ike-spi.c:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
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:245
KEYWORD_DOC_INITIATOR
#define KEYWORD_DOC_INITIATOR
Definition: detect-ike-spi.c:55
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1254
flow.h
flow-var.h
app-layer.h