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-buffer.h"
41 #include "detect-engine-mpm.h"
43 #include "detect-content.h"
44 #include "detect-pcre.h"
45 
46 #include "flow.h"
47 #include "flow-var.h"
48 #include "flow-util.h"
49 
50 #include "util-debug.h"
51 #include "util-unittest.h"
52 #include "util-unittest-helper.h"
53 #include "util-spm.h"
54 
55 #include "app-layer.h"
56 #include "app-layer-parser.h"
57 
58 #include "app-layer-htp.h"
59 #include "detect-http-method.h"
60 #include "stream-tcp.h"
61 
62 static int g_http_method_buffer_id = 0;
63 static int DetectHttpMethodSetup(DetectEngineCtx *, Signature *, const char *);
64 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
65 #ifdef UNITTESTS
67 #endif
68 void DetectHttpMethodFree(void *);
69 static bool DetectHttpMethodValidateCallback(
70  const Signature *s, const char **sigerror, const DetectBufferType *dbt);
71 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
72  const DetectEngineTransforms *transforms, Flow *_f,
73  const uint8_t _flow_flags, void *txv, const int list_id);
74 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
75  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
76  const int list_id);
77 
78 /**
79  * \brief Registration function for keyword: http_method
80  */
82 {
83  /* http_method content modifier */
84  sigmatch_table[DETECT_HTTP_METHOD_CM].name = "http_method";
86  "content modifier to match only on the HTTP method-buffer";
87  sigmatch_table[DETECT_HTTP_METHOD_CM].url = "/rules/http-keywords.html#http-method";
89  sigmatch_table[DETECT_HTTP_METHOD_CM].Setup = DetectHttpMethodSetup;
90 #ifdef UNITTESTS
92 #endif
95 
96  /* http.method sticky buffer */
97  sigmatch_table[DETECT_HTTP_METHOD].name = "http.method";
98  sigmatch_table[DETECT_HTTP_METHOD].desc = "sticky buffer to match specifically and only on the HTTP method buffer";
99  sigmatch_table[DETECT_HTTP_METHOD].url = "/rules/http-keywords.html#http-method";
100  sigmatch_table[DETECT_HTTP_METHOD].Setup = DetectHttpMethodSetupSticky;
102 
104  HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
105 
107  GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
108 
110  HTTP2TxTypeStream, HTTP2ProgHeaders, DetectEngineInspectBufferGeneric, GetData2);
111 
113  PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2TxTypeStream,
114  HTTP2ProgHeaders);
115 
117  "http request method");
118 
120  DetectHttpMethodValidateCallback);
121 
122  g_http_method_buffer_id = DetectBufferTypeGetByName("http_method");
123 
124  SCLogDebug("registering http_method rule option");
125 }
126 
127 /**
128  * \brief This function is used to add the parsed "http_method" option
129  * into the current signature.
130  *
131  * \param de_ctx Pointer to the Detection Engine Context.
132  * \param s Pointer to the Current Signature.
133  * \param str Pointer to the user provided option string.
134  *
135  * \retval 0 on Success.
136  * \retval -1 on Failure.
137  */
138 static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
139 {
141  de_ctx, s, str, DETECT_HTTP_METHOD_CM, g_http_method_buffer_id, ALPROTO_HTTP1);
142 }
143 
144 /**
145  * \brief this function setup the http.method keyword used in the rule
146  *
147  * \param de_ctx Pointer to the Detection Engine Context
148  * \param s Pointer to the Signature to which the current keyword belongs
149  * \param str Should hold an empty string always
150  *
151  * \retval 0 On success
152  */
153 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
154 {
155  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_method_buffer_id) < 0)
156  return -1;
157 
159  return -1;
160 
161  return 0;
162 }
163 
164 /**
165  * \retval 1 valid
166  * \retval 0 invalid
167  */
168 static bool DetectHttpMethodValidateCallback(
169  const Signature *s, const char **sigerror, const DetectBufferType *dbt)
170 {
171  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
172  if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
173  continue;
174  const SigMatch *sm = s->init_data->buffers[x].head;
175  for (; sm != NULL; sm = sm->next) {
176  if (sm->type != DETECT_CONTENT)
177  continue;
178  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
179  if (cd->content && cd->content_len) {
180  if (cd->content[cd->content_len - 1] == 0x20) {
181  *sigerror = "http_method pattern with trailing space";
182  SCLogError("%s", *sigerror);
183  return false;
184  } else if (cd->content[0] == 0x20) {
185  *sigerror = "http_method pattern with leading space";
186  SCLogError("%s", *sigerror);
187  return false;
188  } else if (cd->content[cd->content_len - 1] == 0x09) {
189  *sigerror = "http_method pattern with trailing tab";
190  SCLogError("%s", *sigerror);
191  return false;
192  } else if (cd->content[0] == 0x09) {
193  *sigerror = "http_method pattern with leading tab";
194  SCLogError("%s", *sigerror);
195  return false;
196  }
197  }
198  }
199  }
200  return true;
201 }
202 
203 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
204  const DetectEngineTransforms *transforms, Flow *_f,
205  const uint8_t _flow_flags, void *txv, const int list_id)
206 {
207  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
208  if (buffer->inspect == NULL) {
209  htp_tx_t *tx = (htp_tx_t *)txv;
210 
211  if (htp_tx_request_method(tx) == NULL)
212  return NULL;
213 
214  const uint32_t data_len = (uint32_t)bstr_len(htp_tx_request_method(tx));
215  const uint8_t *data = bstr_ptr(htp_tx_request_method(tx));
216 
218  det_ctx, list_id, buffer, data, data_len, transforms);
219  }
220 
221  return buffer;
222 }
223 
224 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
225  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
226  const int list_id)
227 {
228  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
229  if (buffer->inspect == NULL) {
230  uint32_t b_len = 0;
231  const uint8_t *b = NULL;
232 
233  if (SCHttp2TxGetMethod(txv, &b, &b_len) != 1)
234  return NULL;
235  if (b == NULL || b_len == 0)
236  return NULL;
237 
238  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
239  }
240 
241  return buffer;
242 }
243 
244 #ifdef UNITTESTS
246 #endif
247 
248 /**
249  * @}
250  */
SigTableElmt_::url
const char * url
Definition: detect.h:1521
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:536
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:306
SigTableElmt_::desc
const char * desc
Definition: detect.h:1520
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:2226
flow-util.h
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1518
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:391
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:76
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1509
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DETECT_HTTP_METHOD
@ DETECT_HTTP_METHOD
Definition: detect-engine-register.h:162
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:981
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1658
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(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
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:450
DetectContentData_
Definition: detect-content.h:93
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2461
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1500
detect-pcre.h
DetectAppLayerMpmRegisterSubState
void DetectAppLayerMpmRegisterSubState(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t sub_state, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:167
detect-engine-prefilter.h
util-unittest.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:276
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1455
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1300
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1674
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1516
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
DetectHttpMethodRegisterTests
void DetectHttpMethodRegisterTests(void)
this function registers unit tests for DetectHttpMethod
Definition: detect-http-method.c:772
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect-engine-register.h:326
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:761
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1480
DetectHttpMethodFree
void DetectHttpMethodFree(void *)
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
DetectBufferType_::id
int id
Definition: detect.h:453
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:357
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
detect-engine-buffer.h
DetectAppLayerInspectEngineRegisterSubState
void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register an app inspection engine for a tx type
Definition: detect-engine.c:299
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:661
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:316
detect-http-method.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
detect-http-method.c
Handle HTTP method match.
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:527
Signature_
Signature container.
Definition: detect.h:682
SigMatch_
a single match condition for a signature
Definition: detect.h:356
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:77
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:328
DETECT_HTTP_METHOD_CM
@ DETECT_HTTP_METHOD_CM
Definition: detect-engine-register.h:161
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1552
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:662
flow-var.h
DetectHttpMethodRegister
void DetectHttpMethodRegister(void)
Registration function for keyword: http_method.
Definition: detect-http-method.c:81
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1507
app-layer.h