suricata
detect-http-headers-stub.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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  * Stub for per HTTP header detection keyword. Meant to be included into
20  * a C file.
21  */
22 
23 /**
24  * \ingroup httplayer
25  *
26  * @{
27  */
28 
29 #include "suricata-common.h"
30 #include "flow.h"
31 
32 #include "htp/htp_rs.h"
33 
34 #include "detect.h"
35 #include "detect-parse.h"
36 #include "detect-engine.h"
37 #include "detect-engine-buffer.h"
38 #include "detect-engine-mpm.h"
40 
41 #include "util-debug.h"
42 #include "rust.h"
43 
44 static int g_buffer_id = 0;
45 static int g_http2_thread_id = 0;
46 
47 #ifdef KEYWORD_TOSERVER
48 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
49  const DetectEngineTransforms *transforms, Flow *_f,
50  const uint8_t _flow_flags, void *txv, const int list_id)
51 {
52  SCEnter();
53 
54  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
55  if (buffer->inspect == NULL) {
56  htp_tx_t *tx = (htp_tx_t *)txv;
57 
58  if (htp_tx_request_headers(tx) == NULL)
59  return NULL;
60 
61  const htp_header_t *h = htp_tx_request_header(tx, HEADER_NAME);
62  if (h == NULL || htp_header_value(h) == NULL) {
63  SCLogDebug("HTTP %s header not present in this request",
64  HEADER_NAME);
65  return NULL;
66  }
67 
68  const uint32_t data_len = (uint32_t)htp_header_value_len(h);
69  const uint8_t *data = htp_header_value_ptr(h);
70 
72  det_ctx, list_id, buffer, data, data_len, transforms);
73  }
74 
75  return buffer;
76 }
77 
78 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
79  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
80  const int list_id)
81 {
82  SCEnter();
83 
84  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
85  if (buffer->inspect == NULL) {
86  uint32_t b_len = 0;
87  const uint8_t *b = NULL;
88 
89  void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
90  if (thread_buf == NULL)
91  return NULL;
92  if (SCHttp2TxGetHeaderValue(txv, STREAM_TOSERVER, HEADER_NAME, &b, &b_len, thread_buf) != 1)
93  return NULL;
94  if (b == NULL || b_len == 0)
95  return NULL;
96 
97  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
98  }
99 
100  return buffer;
101 }
102 
103 #endif
104 #ifdef KEYWORD_TOCLIENT
105 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
106  const DetectEngineTransforms *transforms, Flow *_f,
107  const uint8_t _flow_flags, void *txv, const int list_id)
108 {
109  SCEnter();
110 
111  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
112  if (buffer->inspect == NULL) {
113  htp_tx_t *tx = (htp_tx_t *)txv;
114 
115  if (htp_tx_response_headers(tx) == NULL)
116  return NULL;
117 
118  const htp_header_t *h = htp_tx_response_header(tx, HEADER_NAME);
119  if (h == NULL || htp_header_value(h) == NULL) {
120  SCLogDebug("HTTP %s header not present in this request",
121  HEADER_NAME);
122  return NULL;
123  }
124 
125  const uint32_t data_len = (uint32_t)htp_header_value_len(h);
126  const uint8_t *data = htp_header_value_ptr(h);
127 
129  det_ctx, list_id, buffer, data, data_len, transforms);
130  }
131 
132  return buffer;
133 }
134 
135 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
136  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
137  const int list_id)
138 {
139  SCEnter();
140 
141  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
142  if (buffer->inspect == NULL) {
143  uint32_t b_len = 0;
144  const uint8_t *b = NULL;
145 
146  void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
147  if (thread_buf == NULL)
148  return NULL;
149  if (SCHttp2TxGetHeaderValue(txv, STREAM_TOCLIENT, HEADER_NAME, &b, &b_len, thread_buf) != 1)
150  return NULL;
151  if (b == NULL || b_len == 0)
152  return NULL;
153 
154  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
155  }
156 
157  return buffer;
158 }
159 #endif
160 
161 /**
162  * \brief this function setup the http.header keyword used in the rule
163  *
164  * \param de_ctx Pointer to the Detection Engine Context
165  * \param s Pointer to the Signature to which the current keyword belongs
166  * \param str Should hold an empty string always
167  *
168  * \retval 0 On success
169  */
170 static int DetectHttpHeadersSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
171 {
172  if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
173  return -1;
174 
176  return -1;
177 
178  return 0;
179 }
180 
181 static void DetectHttpHeadersRegisterStub(void)
182 {
184 #ifdef KEYWORD_NAME_LEGACY
186 #endif
187  sigmatch_table[KEYWORD_ID].desc = KEYWORD_NAME " sticky buffer for the " BUFFER_DESC;
189  sigmatch_table[KEYWORD_ID].Setup = DetectHttpHeadersSetupSticky;
190 #if defined(KEYWORD_TOSERVER) && defined(KEYWORD_TOSERVER)
193 #else
195 #endif
196 
197 #ifdef KEYWORD_TOSERVER
199  GetRequestData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
201  GetRequestData2, ALPROTO_HTTP2, HTTP2StateDataClient);
202 #endif
203 #ifdef KEYWORD_TOCLIENT
205  GetResponseData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_HEADERS);
207  GetResponseData2, ALPROTO_HTTP2, HTTP2StateDataServer);
208 #endif
209 #ifdef KEYWORD_TOSERVER
211  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetRequestData);
213  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRequestData2);
214 #endif
215 #ifdef KEYWORD_TOCLIENT
217  HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetResponseData);
219  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetResponseData2);
220 #endif
221 
223 
224  g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs(
225  BUFFER_NAME, SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree);
226 
227  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
228 }
SigTableElmt_::url
const char * url
Definition: detect.h:1464
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1678
SigTableElmt_::desc
const char * desc
Definition: detect.h:1463
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
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:2049
SigTableElmt_::name
const char * name
Definition: detect.h:1461
DetectEngineTransforms
Definition: detect.h:390
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1452
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-command-data.c:47
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-command-data.c:49
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
HEADER_NAME
#define HEADER_NAME
Definition: detect-http-accept-enc.c:38
rust.h
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2236
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1443
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
SIGMATCH_SUPPORT_DIR
#define SIGMATCH_SUPPORT_DIR
Definition: detect.h:1686
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-command-data.c:46
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:1577
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:152
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-accept-enc.c:33
SigTableElmt_::alias
const char * alias
Definition: detect.h:1462
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
detect-engine-buffer.h
SIGMATCH_OPTIONAL_OPT
#define SIGMATCH_OPTIONAL_OPT
Definition: detect.h:1663
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:308
DetectThreadCtxGetGlobalKeywordThreadCtx
void * DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3849
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-http-accept-enc.c:39
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-dce-stub-data.c:61
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
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-inspect-buffer.c:197
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1653
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:273
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1375
flow.h
DetectRegisterThreadCtxGlobalFuncs
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:3805