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 "debug.h"
36 #include "decode.h"
37 #include "detect.h"
38 
39 #include "detect-parse.h"
40 #include "detect-engine.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(const Signature *s, const char **sigerror);
70 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
71  const DetectEngineTransforms *transforms, Flow *_f,
72  const uint8_t _flow_flags, void *txv, const int list_id);
73 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
74  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
75  const int list_id);
76 
77 /**
78  * \brief Registration function for keyword: http_method
79  */
81 {
82  /* http_method content modifier */
83  sigmatch_table[DETECT_AL_HTTP_METHOD].name = "http_method";
84  sigmatch_table[DETECT_AL_HTTP_METHOD].desc = "content modifier to match only on the HTTP method-buffer";
85  sigmatch_table[DETECT_AL_HTTP_METHOD].url = "/rules/http-keywords.html#http-method";
87  sigmatch_table[DETECT_AL_HTTP_METHOD].Setup = DetectHttpMethodSetup;
88 #ifdef UNITTESTS
90 #endif
93 
94  /* http.method sticky buffer */
95  sigmatch_table[DETECT_HTTP_METHOD].name = "http.method";
96  sigmatch_table[DETECT_HTTP_METHOD].desc = "sticky buffer to match specifically and only on the HTTP method buffer";
97  sigmatch_table[DETECT_HTTP_METHOD].url = "/rules/http-keywords.html#http-method";
98  sigmatch_table[DETECT_HTTP_METHOD].Setup = DetectHttpMethodSetupSticky;
100 
102  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
103 
105  GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
106 
108  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
109 
111  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
112 
114  "http request method");
115 
117  DetectHttpMethodValidateCallback);
118 
119  g_http_method_buffer_id = DetectBufferTypeGetByName("http_method");
120 
121  SCLogDebug("registering http_method rule option");
122 }
123 
124 /**
125  * \brief This function is used to add the parsed "http_method" option
126  * into the current signature.
127  *
128  * \param de_ctx Pointer to the Detection Engine Context.
129  * \param s Pointer to the Current Signature.
130  * \param str Pointer to the user provided option string.
131  *
132  * \retval 0 on Success.
133  * \retval -1 on Failure.
134  */
135 static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
136 {
138  de_ctx, s, str, DETECT_AL_HTTP_METHOD, g_http_method_buffer_id, ALPROTO_HTTP1);
139 }
140 
141 /**
142  * \brief this function setup the http.method keyword used in the rule
143  *
144  * \param de_ctx Pointer to the Detection Engine Context
145  * \param s Pointer to the Signature to which the current keyword belongs
146  * \param str Should hold an empty string always
147  *
148  * \retval 0 On success
149  */
150 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
151 {
152  if (DetectBufferSetActiveList(s, g_http_method_buffer_id) < 0)
153  return -1;
154 
156  return -1;
157 
158  return 0;
159 }
160 
161 /**
162  * \retval 1 valid
163  * \retval 0 invalid
164  */
165 static bool DetectHttpMethodValidateCallback(const Signature *s, const char **sigerror)
166 {
167  const SigMatch *sm = s->init_data->smlists[g_http_method_buffer_id];
168  for ( ; sm != NULL; sm = sm->next) {
169  if (sm->type != DETECT_CONTENT)
170  continue;
171  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
172  if (cd->content && cd->content_len) {
173  if (cd->content[cd->content_len-1] == 0x20) {
174  *sigerror = "http_method pattern with trailing space";
175  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
176  return false;
177  } else if (cd->content[0] == 0x20) {
178  *sigerror = "http_method pattern with leading space";
179  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
180  return false;
181  } else if (cd->content[cd->content_len-1] == 0x09) {
182  *sigerror = "http_method pattern with trailing tab";
183  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
184  return false;
185  } else if (cd->content[0] == 0x09) {
186  *sigerror = "http_method pattern with leading tab";
187  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
188  return false;
189  }
190  }
191  }
192  return true;
193 }
194 
195 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
196  const DetectEngineTransforms *transforms, Flow *_f,
197  const uint8_t _flow_flags, void *txv, const int list_id)
198 {
199  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
200  if (buffer->inspect == NULL) {
201  htp_tx_t *tx = (htp_tx_t *)txv;
202 
203  if (tx->request_method == NULL)
204  return NULL;
205 
206  const uint32_t data_len = bstr_len(tx->request_method);
207  const uint8_t *data = bstr_ptr(tx->request_method);
208 
209  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
210  InspectionBufferApplyTransforms(buffer, transforms);
211  }
212 
213  return buffer;
214 }
215 
216 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
217  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
218  const int list_id)
219 {
220  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
221  if (buffer->inspect == NULL) {
222  uint32_t b_len = 0;
223  const uint8_t *b = NULL;
224 
225  if (rs_http2_tx_get_method(txv, &b, &b_len) != 1)
226  return NULL;
227  if (b == NULL || b_len == 0)
228  return NULL;
229 
230  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
231  InspectionBufferApplyTransforms(buffer, transforms);
232  }
233 
234  return buffer;
235 }
236 
237 #ifdef UNITTESTS
239 #endif
240 
241 /**
242  * @}
243  */
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1395
SigTableElmt_::name
const char * name
Definition: detect.h:1201
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:378
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:60
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DETECT_HTTP_METHOD
@ DETECT_HTTP_METHOD
Definition: detect-engine-register.h:121
InspectionBuffer
Definition: detect.h:344
threads.h
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:676
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectContentData_
Definition: detect-content.h:86
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:921
DETECT_AL_HTTP_METHOD
@ DETECT_AL_HTTP_METHOD
Definition: detect-engine-register.h:120
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:324
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1199
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
DetectHttpMethodRegisterTests
void DetectHttpMethodRegisterTests(void)
this function registers unit tests for DetectHttpMethod
Definition: detect-http-method.c:2342
DetectEngineInspectBufferGeneric
int 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:1617
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
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:174
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:587
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1169
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:511
DetectHttpMethodFree
void DetectHttpMethodFree(void *)
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:59
SigMatch_::type
uint8_t type
Definition: detect.h:321
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
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:87
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
str
#define str(s)
Definition: suricata-common.h:272
detect-http-method.h
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:1091
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
SigMatch_
a single match condition for a signature
Definition: detect.h:320
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:63
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:88
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:857
flow.h
flow-var.h
DetectHttpMethodRegister
void DetectHttpMethodRegister(void)
Registration function for keyword: http_method.
Definition: detect-http-method.c:80
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
app-layer.h