suricata
detect-http-method.c
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  * \ingroup httplayer
20  *
21  * @{
22  */
23 
24 
25 /**
26  * \file
27  *
28  * \author Brian Rectanus <brectanu@gmail.com>
29  *
30  * Implements the http_method keyword
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "decode.h"
36 #include "detect.h"
37 
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
42 #include "detect-content.h"
43 #include "detect-pcre.h"
44 
45 #include "flow.h"
46 #include "flow-var.h"
47 #include "flow-util.h"
48 
49 #include "util-debug.h"
50 #include "util-unittest.h"
51 #include "util-unittest-helper.h"
52 #include "util-spm.h"
53 
54 #include "app-layer.h"
55 #include "app-layer-parser.h"
56 
57 #include "app-layer-htp.h"
58 #include "detect-http-method.h"
59 #include "stream-tcp.h"
60 
61 static int g_http_method_buffer_id = 0;
62 static int DetectHttpMethodSetup(DetectEngineCtx *, Signature *, const char *);
63 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
64 #ifdef UNITTESTS
66 #endif
67 void DetectHttpMethodFree(void *);
68 static bool DetectHttpMethodValidateCallback(const Signature *s, const char **sigerror);
69 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
70  const DetectEngineTransforms *transforms, Flow *_f,
71  const uint8_t _flow_flags, void *txv, const int list_id);
72 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
73  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
74  const int list_id);
75 
76 /**
77  * \brief Registration function for keyword: http_method
78  */
80 {
81  /* http_method content modifier */
82  sigmatch_table[DETECT_AL_HTTP_METHOD].name = "http_method";
83  sigmatch_table[DETECT_AL_HTTP_METHOD].desc = "content modifier to match only on the HTTP method-buffer";
84  sigmatch_table[DETECT_AL_HTTP_METHOD].url = "/rules/http-keywords.html#http-method";
86  sigmatch_table[DETECT_AL_HTTP_METHOD].Setup = DetectHttpMethodSetup;
87 #ifdef UNITTESTS
89 #endif
92 
93  /* http.method sticky buffer */
94  sigmatch_table[DETECT_HTTP_METHOD].name = "http.method";
95  sigmatch_table[DETECT_HTTP_METHOD].desc = "sticky buffer to match specifically and only on the HTTP method buffer";
96  sigmatch_table[DETECT_HTTP_METHOD].url = "/rules/http-keywords.html#http-method";
97  sigmatch_table[DETECT_HTTP_METHOD].Setup = DetectHttpMethodSetupSticky;
99 
101  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
102 
104  GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
105 
107  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
108 
110  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
111 
113  "http request method");
114 
116  DetectHttpMethodValidateCallback);
117 
118  g_http_method_buffer_id = DetectBufferTypeGetByName("http_method");
119 
120  SCLogDebug("registering http_method rule option");
121 }
122 
123 /**
124  * \brief This function is used to add the parsed "http_method" option
125  * into the current signature.
126  *
127  * \param de_ctx Pointer to the Detection Engine Context.
128  * \param s Pointer to the Current Signature.
129  * \param str Pointer to the user provided option string.
130  *
131  * \retval 0 on Success.
132  * \retval -1 on Failure.
133  */
134 static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
135 {
137  de_ctx, s, str, DETECT_AL_HTTP_METHOD, g_http_method_buffer_id, ALPROTO_HTTP1);
138 }
139 
140 /**
141  * \brief this function setup the http.method keyword used in the rule
142  *
143  * \param de_ctx Pointer to the Detection Engine Context
144  * \param s Pointer to the Signature to which the current keyword belongs
145  * \param str Should hold an empty string always
146  *
147  * \retval 0 On success
148  */
149 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
150 {
151  if (DetectBufferSetActiveList(de_ctx, s, g_http_method_buffer_id) < 0)
152  return -1;
153 
155  return -1;
156 
157  return 0;
158 }
159 
160 /**
161  * \retval 1 valid
162  * \retval 0 invalid
163  */
164 static bool DetectHttpMethodValidateCallback(const Signature *s, const char **sigerror)
165 {
166  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
167  if (s->init_data->buffers[x].id != (uint32_t)g_http_method_buffer_id)
168  continue;
169  const SigMatch *sm = s->init_data->buffers[x].head;
170  for (; sm != NULL; sm = sm->next) {
171  if (sm->type != DETECT_CONTENT)
172  continue;
173  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
174  if (cd->content && cd->content_len) {
175  if (cd->content[cd->content_len - 1] == 0x20) {
176  *sigerror = "http_method pattern with trailing space";
177  SCLogError("%s", *sigerror);
178  return false;
179  } else if (cd->content[0] == 0x20) {
180  *sigerror = "http_method pattern with leading space";
181  SCLogError("%s", *sigerror);
182  return false;
183  } else if (cd->content[cd->content_len - 1] == 0x09) {
184  *sigerror = "http_method pattern with trailing tab";
185  SCLogError("%s", *sigerror);
186  return false;
187  } else if (cd->content[0] == 0x09) {
188  *sigerror = "http_method pattern with leading tab";
189  SCLogError("%s", *sigerror);
190  return false;
191  }
192  }
193  }
194  }
195  return true;
196 }
197 
198 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
199  const DetectEngineTransforms *transforms, Flow *_f,
200  const uint8_t _flow_flags, void *txv, const int list_id)
201 {
202  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
203  if (buffer->inspect == NULL) {
204  htp_tx_t *tx = (htp_tx_t *)txv;
205 
206  if (tx->request_method == NULL)
207  return NULL;
208 
209  const uint32_t data_len = bstr_len(tx->request_method);
210  const uint8_t *data = bstr_ptr(tx->request_method);
211 
212  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
213  InspectionBufferApplyTransforms(buffer, transforms);
214  }
215 
216  return buffer;
217 }
218 
219 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
220  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
221  const int list_id)
222 {
223  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
224  if (buffer->inspect == NULL) {
225  uint32_t b_len = 0;
226  const uint8_t *b = NULL;
227 
228  if (rs_http2_tx_get_method(txv, &b, &b_len) != 1)
229  return NULL;
230  if (b == NULL || b_len == 0)
231  return NULL;
232 
233  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
234  InspectionBufferApplyTransforms(buffer, transforms);
235  }
236 
237  return buffer;
238 }
239 
240 #ifdef UNITTESTS
242 #endif
243 
244 /**
245  * @}
246  */
SigTableElmt_::url
const char * url
Definition: detect.h:1287
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1703
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:518
detect-content.h
detect-engine.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1490
SigTableElmt_::desc
const char * desc
Definition: detect.h:1286
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:2165
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1488
SigTableElmt_::name
const char * name
Definition: detect.h:1284
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:399
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DETECT_HTTP_METHOD
@ DETECT_HTTP_METHOD
Definition: detect-engine-register.h:128
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1387
InspectionBuffer
Definition: detect.h:364
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1278
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:826
MpmListIdDataArgs::txv
void * txv
Definition: detect-engine-mpm.h:133
DetectContentData_
Definition: detect-content.h:93
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1269
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1526
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
app-layer-htp.h
decode.h
util-debug.h
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror))
Definition: detect-engine.c:1336
DETECT_AL_HTTP_METHOD
@ DETECT_AL_HTTP_METHOD
Definition: detect-engine-register.h:127
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1074
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:344
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1282
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
DetectHttpMethodRegisterTests
void DetectHttpMethodRegisterTests(void)
this function registers unit tests for DetectHttpMethod
Definition: detect-http-method.c:2155
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:207
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:216
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:653
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1252
DetectHttpMethodFree
void DetectHttpMethodFree(void *)
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1725
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:571
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:365
str
#define str(s)
Definition: suricata-common.h:286
detect-http-method.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
detect-http-method.c
Handle HTTP method match.
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:1621
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:513
Signature_
Signature container.
Definition: detect.h:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1466
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1221
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:572
flow-var.h
DetectHttpMethodRegister
void DetectHttpMethodRegister(void)
Registration function for keyword: http_method.
Definition: detect-http-method.c:79
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1276
app-layer.h