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.h>
33 #include "app-layer-htp-libhtp.h"
34 
35 #include "detect.h"
36 #include "detect-parse.h"
37 #include "detect-engine.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 
46 #ifdef KEYWORD_TOSERVER
47 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
48  const DetectEngineTransforms *transforms, Flow *_f,
49  const uint8_t _flow_flags, void *txv, const int list_id)
50 {
51  SCEnter();
52 
53  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
54  if (buffer->inspect == NULL) {
55  htp_tx_t *tx = (htp_tx_t *)txv;
56 
57  if (htp_tx_request_headers(tx) == NULL)
58  return NULL;
59 
60  const htp_header_t *h = htp_tx_request_header(tx, HEADER_NAME);
61  if (h == NULL || htp_header_value(h) == NULL) {
62  SCLogDebug("HTTP %s header not present in this request",
63  HEADER_NAME);
64  return NULL;
65  }
66 
67  const uint32_t data_len = htp_header_value_len(h);
68  const uint8_t *data = htp_header_value_ptr(h);
69 
71  det_ctx, list_id, buffer, data, data_len, transforms);
72  }
73 
74  return buffer;
75 }
76 
77 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
78  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
79  const int list_id)
80 {
81  SCEnter();
82 
83  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
84  if (buffer->inspect == NULL) {
85  uint32_t b_len = 0;
86  const uint8_t *b = NULL;
87 
88  if (rs_http2_tx_get_header_value(txv, STREAM_TOSERVER, HEADER_NAME, &b, &b_len) != 1)
89  return NULL;
90  if (b == NULL || b_len == 0)
91  return NULL;
92 
93  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
94  }
95 
96  return buffer;
97 }
98 
99 #endif
100 #ifdef KEYWORD_TOCLIENT
101 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
102  const DetectEngineTransforms *transforms, Flow *_f,
103  const uint8_t _flow_flags, void *txv, const int list_id)
104 {
105  SCEnter();
106 
107  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
108  if (buffer->inspect == NULL) {
109  htp_tx_t *tx = (htp_tx_t *)txv;
110 
111  if (htp_tx_response_headers(tx) == NULL)
112  return NULL;
113 
114  const htp_header_t *h = htp_tx_response_header(tx, HEADER_NAME);
115  if (h == NULL || htp_header_value(h) == 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 = htp_header_value_len(h);
122  const uint8_t *data = htp_header_value_ptr(h);
123 
125  det_ctx, list_id, buffer, data, data_len, 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  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
148  }
149 
150  return buffer;
151 }
152 #endif
153 
154 /**
155  * \brief this function setup the http.header keyword used in the rule
156  *
157  * \param de_ctx Pointer to the Detection Engine Context
158  * \param s Pointer to the Signature to which the current keyword belongs
159  * \param str Should hold an empty string always
160  *
161  * \retval 0 On success
162  */
163 static int DetectHttpHeadersSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
164 {
165  if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
166  return -1;
167 
169  return -1;
170 
171  return 0;
172 }
173 
174 static void DetectHttpHeadersRegisterStub(void)
175 {
177 #ifdef KEYWORD_NAME_LEGACY
179 #endif
180  sigmatch_table[KEYWORD_ID].desc = KEYWORD_NAME " sticky buffer for the " BUFFER_DESC;
182  sigmatch_table[KEYWORD_ID].Setup = DetectHttpHeadersSetupSticky;
184 
185 #ifdef KEYWORD_TOSERVER
189  GetRequestData2, ALPROTO_HTTP2, HTTP2StateDataClient);
190 #endif
191 #ifdef KEYWORD_TOCLIENT
193  GetResponseData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_HEADERS);
195  GetResponseData2, ALPROTO_HTTP2, HTTP2StateDataServer);
196 #endif
197 #ifdef KEYWORD_TOSERVER
201  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRequestData2);
202 #endif
203 #ifdef KEYWORD_TOCLIENT
207  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetResponseData2);
208 #endif
209 
211 
212  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
213 }
htp_header_value_len
#define htp_header_value_len(h)
Definition: app-layer-htp-libhtp.h:191
SigTableElmt_::url
const char * url
Definition: detect.h:1329
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1870
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1534
SigTableElmt_::desc
const char * desc
Definition: detect.h:1328
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
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:2195
SigTableElmt_::name
const char * name
Definition: detect.h:1326
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:1662
DetectEngineTransforms
Definition: detect.h:409
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
HTP_RESPONSE_PROGRESS_HEADERS
#define HTP_RESPONSE_PROGRESS_HEADERS
Definition: app-layer-htp-libhtp.h:94
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1377
InspectionBuffer
Definition: detect.h:374
Flow_
Flow data structure.
Definition: flow.h:354
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1320
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:860
HEADER_NAME
#define HEADER_NAME
Definition: detect-http-accept-enc.c:38
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-ftp-command.c:47
htp_tx_response_headers
#define htp_tx_response_headers(tx)
Definition: app-layer-htp-libhtp.h:174
htp_tx_response_header
#define htp_tx_response_header(tx, header)
Definition: app-layer-htp-libhtp.h:185
rust.h
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-ftp-command.c:45
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1311
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1521
htp_header_value
#define htp_header_value(h)
Definition: app-layer-htp-libhtp.h:193
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1114
htp_tx_request_headers
#define htp_tx_request_headers(tx)
Definition: app-layer-htp-libhtp.h:173
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1116
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-ftp-command.c:44
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:761
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
htp_header_value_ptr
#define htp_header_value_ptr(h)
Definition: app-layer-htp-libhtp.h:192
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-accept-enc.c:33
SigTableElmt_::alias
const char * alias
Definition: detect.h:1327
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
HTP_REQUEST_PROGRESS_HEADERS
#define HTP_REQUEST_PROGRESS_HEADERS
Definition: app-layer-htp-libhtp.h:89
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
str
#define str(s)
Definition: suricata-common.h:300
app-layer-htp-libhtp.h
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-http-accept-enc.c:39
detect-parse.h
Signature_
Signature container.
Definition: detect.h:618
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-dce-stub-data.c:60
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1510
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
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1211
flow.h
htp_tx_request_header
#define htp_tx_request_header(tx, header)
Definition: app-layer-htp-libhtp.h:184