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 
74 /**
75  * \brief Registration function for keyword: http_method
76  */
78 {
79  /* http_method content modifier */
80  sigmatch_table[DETECT_AL_HTTP_METHOD].name = "http_method";
81  sigmatch_table[DETECT_AL_HTTP_METHOD].desc = "content modifier to match only on the HTTP method-buffer";
82  sigmatch_table[DETECT_AL_HTTP_METHOD].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-method";
84  sigmatch_table[DETECT_AL_HTTP_METHOD].Setup = DetectHttpMethodSetup;
85 #ifdef UNITTESTS
87 #endif
90 
91  /* http.method sticky buffer */
92  sigmatch_table[DETECT_HTTP_METHOD].name = "http.method";
93  sigmatch_table[DETECT_HTTP_METHOD].desc = "sticky buffer to match specifically and only on the HTTP method buffer";
94  sigmatch_table[DETECT_HTTP_METHOD].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-method";
95  sigmatch_table[DETECT_HTTP_METHOD].Setup = DetectHttpMethodSetupSticky;
97 
99  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
101 
104  HTP_REQUEST_LINE);
105 
107  "http request method");
108 
110  DetectHttpMethodValidateCallback);
111 
112  g_http_method_buffer_id = DetectBufferTypeGetByName("http_method");
113 
114  SCLogDebug("registering http_method rule option");
115 }
116 
117 /**
118  * \brief This function is used to add the parsed "http_method" option
119  * into the current signature.
120  *
121  * \param de_ctx Pointer to the Detection Engine Context.
122  * \param s Pointer to the Current Signature.
123  * \param str Pointer to the user provided option string.
124  *
125  * \retval 0 on Success.
126  * \retval -1 on Failure.
127  */
128 static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
129 {
130  return DetectEngineContentModifierBufferSetup(de_ctx, s, str,
132  g_http_method_buffer_id,
133  ALPROTO_HTTP);
134 }
135 
136 /**
137  * \brief this function setup the http.method keyword used in the rule
138  *
139  * \param de_ctx Pointer to the Detection Engine Context
140  * \param s Pointer to the Signature to which the current keyword belongs
141  * \param str Should hold an empty string always
142  *
143  * \retval 0 On success
144  */
145 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
146 {
147  if (DetectBufferSetActiveList(s, g_http_method_buffer_id) < 0)
148  return -1;
149 
151  return -1;
152 
153  return 0;
154 }
155 
156 /**
157  * \retval 1 valid
158  * \retval 0 invalid
159  */
160 static _Bool DetectHttpMethodValidateCallback(const Signature *s, const char **sigerror)
161 {
162  const SigMatch *sm = s->init_data->smlists[g_http_method_buffer_id];
163  for ( ; sm != NULL; sm = sm->next) {
164  if (sm->type != DETECT_CONTENT)
165  continue;
166  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
167  if (cd->content && cd->content_len) {
168  if (cd->content[cd->content_len-1] == 0x20) {
169  *sigerror = "http_method pattern with trailing space";
170  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
171  return FALSE;
172  } else if (cd->content[0] == 0x20) {
173  *sigerror = "http_method pattern with leading space";
174  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
175  return FALSE;
176  } else if (cd->content[cd->content_len-1] == 0x09) {
177  *sigerror = "http_method pattern with trailing tab";
178  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
179  return FALSE;
180  } else if (cd->content[0] == 0x09) {
181  *sigerror = "http_method pattern with leading tab";
182  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
183  return FALSE;
184  }
185  }
186  }
187  return TRUE;
188 }
189 
190 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
191  const DetectEngineTransforms *transforms, Flow *_f,
192  const uint8_t _flow_flags, void *txv, const int list_id)
193 {
194  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
195  if (buffer->inspect == NULL) {
196  htp_tx_t *tx = (htp_tx_t *)txv;
197 
198  if (tx->request_method == NULL)
199  return NULL;
200 
201  const uint32_t data_len = bstr_len(tx->request_method);
202  const uint8_t *data = bstr_ptr(tx->request_method);
203 
204  InspectionBufferSetup(buffer, data, data_len);
205  InspectionBufferApplyTransforms(buffer, transforms);
206  }
207 
208  return buffer;
209 }
210 
211 #ifdef UNITTESTS
213 #endif
214 
215 /**
216  * @}
217  */
Handle HTTP method match.
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
SignatureInitData * init_data
Definition: detect.h:586
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
#define FALSE
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1384
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1193
Signature container.
Definition: detect.h:517
#define TRUE
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
struct SigMatch_ * next
Definition: detect.h:317
main detection engine ctx
Definition: detect.h:756
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1386
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define SIG_FLAG_TOSERVER
Definition: detect.h:232
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
uint16_t alternative
Definition: detect.h:1191
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1163
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:143
uint8_t type
Definition: detect.h:314
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.
void DetectHttpMethodRegister(void)
Registration function for keyword: http_method.
const char * desc
Definition: detect.h:1195
struct SigMatch_ ** smlists
Definition: detect.h:511
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
SigMatchCtx * ctx
Definition: detect.h:316
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1362
void DetectHttpMethodRegisterTests(void)
this function registers unit tests for DetectHttpMethod
const char * url
Definition: detect.h:1196
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:338
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectBufferTypeRegisterValidateCallback(const char *name, _Bool(*ValidateCallback)(const Signature *, const char **sigerror))
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1187
void DetectHttpMethodFree(void *)
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1185
a single match condition for a signature
Definition: detect.h:313