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"
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"
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
64static int g_buffer_initiator_id = 0;
65static int g_buffer_responder_id = 0;
66
67static 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
78static 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
89static 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
109static 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}
@ ALPROTO_IKE
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
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
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_IKE_SPI_INITIATOR
@ DETECT_IKE_SPI_RESPONDER
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
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.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
#define KEYWORD_DOC_INITIATOR
#define KEYWORD_NAME_RESPONDER
#define KEYWORD_NAME_INITIATOR
#define KEYWORD_DOC_RESPONDER
void DetectIkeSpiRegister(void)
#define BUFFER_NAME_RESPONDER
#define BUFFER_NAME_INITIATOR
#define BUFFER_DESC_RESPONDER
#define BUFFER_DESC_INITIATOR
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCLogDebug(...)
Definition util-debug.h:275