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 "detect.h"
33 #include "detect-parse.h"
34 #include "detect-engine.h"
35 #include "detect-engine-mpm.h"
37 
38 #include "util-debug.h"
39 #include "rust.h"
40 
41 static int g_buffer_id = 0;
42 
43 #ifdef KEYWORD_TOSERVER
44 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
45  const DetectEngineTransforms *transforms, Flow *_f,
46  const uint8_t _flow_flags, void *txv, const int list_id)
47 {
48  SCEnter();
49 
50  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
51  if (buffer->inspect == NULL) {
52  htp_tx_t *tx = (htp_tx_t *)txv;
53 
54  if (tx->request_headers == NULL)
55  return NULL;
56 
57  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
58  HEADER_NAME);
59  if (h == NULL || h->value == NULL) {
60  SCLogDebug("HTTP %s header not present in this request",
61  HEADER_NAME);
62  return NULL;
63  }
64 
65  const uint32_t data_len = bstr_len(h->value);
66  const uint8_t *data = bstr_ptr(h->value);
67 
68  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
69  InspectionBufferApplyTransforms(buffer, transforms);
70  }
71 
72  return buffer;
73 }
74 
75 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
76  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
77  const int list_id)
78 {
79  SCEnter();
80 
81  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
82  if (buffer->inspect == NULL) {
83  uint32_t b_len = 0;
84  const uint8_t *b = NULL;
85 
86  if (rs_http2_tx_get_header_value(txv, STREAM_TOSERVER, HEADER_NAME, &b, &b_len) != 1)
87  return NULL;
88  if (b == NULL || b_len == 0)
89  return NULL;
90 
91  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
92  InspectionBufferApplyTransforms(buffer, transforms);
93  }
94 
95  return buffer;
96 }
97 
98 #endif
99 #ifdef KEYWORD_TOCLIENT
100 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
101  const DetectEngineTransforms *transforms, Flow *_f,
102  const uint8_t _flow_flags, void *txv, const int list_id)
103 {
104  SCEnter();
105 
106  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
107  if (buffer->inspect == NULL) {
108  htp_tx_t *tx = (htp_tx_t *)txv;
109 
110  if (tx->response_headers == NULL)
111  return NULL;
112 
113  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->response_headers,
114  HEADER_NAME);
115  if (h == NULL || h->value == NULL) {
116  SCLogDebug("HTTP %s header not present in this request",
117  HEADER_NAME);
118  return NULL;
119  }
120 
121  const uint32_t data_len = bstr_len(h->value);
122  const uint8_t *data = bstr_ptr(h->value);
123 
124  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
125  InspectionBufferApplyTransforms(buffer, transforms);
126  }
127 
128  return buffer;
129 }
130 
131 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
132  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
133  const int list_id)
134 {
135  SCEnter();
136 
137  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
138  if (buffer->inspect == NULL) {
139  uint32_t b_len = 0;
140  const uint8_t *b = NULL;
141 
142  if (rs_http2_tx_get_header_value(txv, STREAM_TOCLIENT, HEADER_NAME, &b, &b_len) != 1)
143  return NULL;
144  if (b == NULL || b_len == 0)
145  return NULL;
146 
147  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
148  InspectionBufferApplyTransforms(buffer, transforms);
149  }
150 
151  return buffer;
152 }
153 #endif
154 
155 /**
156  * \brief this function setup the http.header keyword used in the rule
157  *
158  * \param de_ctx Pointer to the Detection Engine Context
159  * \param s Pointer to the Signature to which the current keyword belongs
160  * \param str Should hold an empty string always
161  *
162  * \retval 0 On success
163  */
164 static int DetectHttpHeadersSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
165 {
166  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
167  return -1;
168 
170  return -1;
171 
172  return 0;
173 }
174 
175 static void DetectHttpHeadersRegisterStub(void)
176 {
178 #ifdef KEYWORD_NAME_LEGACY
180 #endif
181  sigmatch_table[KEYWORD_ID].desc = KEYWORD_NAME " sticky buffer for the " BUFFER_DESC;
183  sigmatch_table[KEYWORD_ID].Setup = DetectHttpHeadersSetupSticky;
185 
186 #ifdef KEYWORD_TOSERVER
188  GetRequestData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
190  GetRequestData2, ALPROTO_HTTP2, HTTP2StateDataClient);
191 #endif
192 #ifdef KEYWORD_TOCLIENT
194  GetResponseData, ALPROTO_HTTP1, HTP_RESPONSE_HEADERS);
196  GetResponseData2, ALPROTO_HTTP2, HTTP2StateDataServer);
197 #endif
198 #ifdef KEYWORD_TOSERVER
200  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetRequestData);
202  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRequestData2);
203 #endif
204 #ifdef KEYWORD_TOCLIENT
206  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferGeneric, GetResponseData);
208  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetResponseData2);
209 #endif
210 
212 
213  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
214 }
SigTableElmt_::url
const char * url
Definition: detect.h:1244
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1491
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1451
SigTableElmt_::desc
const char * desc
Definition: detect.h:1243
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:1995
SigTableElmt_::name
const char * name
Definition: detect.h:1241
DetectEngineTransforms
Definition: detect.h:372
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
InspectionBuffer
Definition: detect.h:338
Flow_
Flow data structure.
Definition: flow.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:754
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1235
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
HEADER_NAME
#define HEADER_NAME
Definition: detect-http-accept-enc.c:38
rust.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:231
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1226
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1370
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1087
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1034
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
detect-engine-mpm.h
detect.h
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-accept-enc.c:35
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:228
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:62
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-accept-enc.c:37
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
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-accept-enc.c:33
SigTableElmt_::alias
const char * alias
Definition: detect.h:1242
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1553
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:74
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-dce-stub-data.c:61
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:339
str
#define str(s)
Definition: suricata-common.h:272
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1451
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-http-accept-enc.c:39
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-dce-stub-data.c:60
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1427
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1301
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1184
flow.h