suricata
detect-http-method.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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
89 
90  /* http.method sticky buffer */
91  sigmatch_table[DETECT_HTTP_METHOD].name = "http.method";
92  sigmatch_table[DETECT_HTTP_METHOD].desc = "sticky buffer to match specifically and only on the HTTP method buffer";
93  sigmatch_table[DETECT_HTTP_METHOD].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-method";
94  sigmatch_table[DETECT_HTTP_METHOD].Setup = DetectHttpMethodSetupSticky;
96 
98  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
100 
103  HTP_REQUEST_LINE);
104 
106  "http request method");
107 
109  DetectHttpMethodValidateCallback);
110 
111  g_http_method_buffer_id = DetectBufferTypeGetByName("http_method");
112 
113  SCLogDebug("registering http_method rule option");
114 }
115 
116 /**
117  * \brief This function is used to add the parsed "http_method" option
118  * into the current signature.
119  *
120  * \param de_ctx Pointer to the Detection Engine Context.
121  * \param s Pointer to the Current Signature.
122  * \param str Pointer to the user provided option string.
123  *
124  * \retval 0 on Success.
125  * \retval -1 on Failure.
126  */
127 static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
128 {
129  return DetectEngineContentModifierBufferSetup(de_ctx, s, str,
131  g_http_method_buffer_id,
132  ALPROTO_HTTP);
133 }
134 
135 /**
136  * \brief this function setup the http.method keyword used in the rule
137  *
138  * \param de_ctx Pointer to the Detection Engine Context
139  * \param s Pointer to the Signature to which the current keyword belongs
140  * \param str Should hold an empty string always
141  *
142  * \retval 0 On success
143  */
144 static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
145 {
146  DetectBufferSetActiveList(s, g_http_method_buffer_id);
147  s->alproto = ALPROTO_HTTP;
148  return 0;
149 }
150 
151 /**
152  * \retval 1 valid
153  * \retval 0 invalid
154  */
155 static _Bool DetectHttpMethodValidateCallback(const Signature *s, const char **sigerror)
156 {
157  const SigMatch *sm = s->init_data->smlists[g_http_method_buffer_id];
158  for ( ; sm != NULL; sm = sm->next) {
159  if (sm->type != DETECT_CONTENT)
160  continue;
161  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
162  if (cd->content && cd->content_len) {
163  if (cd->content[cd->content_len-1] == 0x20) {
164  *sigerror = "http_method pattern with trailing space";
165  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
166  return FALSE;
167  } else if (cd->content[0] == 0x20) {
168  *sigerror = "http_method pattern with leading space";
169  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
170  return FALSE;
171  } else if (cd->content[cd->content_len-1] == 0x09) {
172  *sigerror = "http_method pattern with trailing tab";
173  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
174  return FALSE;
175  } else if (cd->content[0] == 0x09) {
176  *sigerror = "http_method pattern with leading tab";
177  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
178  return FALSE;
179  }
180  }
181  }
182  return TRUE;
183 }
184 
185 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
186  const DetectEngineTransforms *transforms, Flow *_f,
187  const uint8_t _flow_flags, void *txv, const int list_id)
188 {
189  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
190  if (buffer->inspect == NULL) {
191  htp_tx_t *tx = (htp_tx_t *)txv;
192 
193  if (tx->request_method == NULL)
194  return NULL;
195 
196  const uint32_t data_len = bstr_len(tx->request_method);
197  const uint8_t *data = bstr_ptr(tx->request_method);
198 
199  InspectionBufferSetup(buffer, data, data_len);
200  InspectionBufferApplyTransforms(buffer, transforms);
201  }
202 
203  return buffer;
204 }
205 
206 #ifdef UNITTESTS
208 #endif
209 
210 /**
211  * @}
212  */
Handle HTTP method match.
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
SignatureInitData * init_data
Definition: detect.h:560
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
#define SCLogDebug(...)
Definition: util-debug.h:335
#define FALSE
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
#define TRUE
struct SigMatch_ * next
Definition: detect.h:328
main detection engine ctx
Definition: detect.h:720
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define SIG_FLAG_TOSERVER
Definition: detect.h:243
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
AppProto alproto
Definition: detect.h:496
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:325
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:1162
struct SigMatch_ ** smlists
Definition: detect.h:486
SigMatchCtx * ctx
Definition: detect.h:327
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:1328
int(* Match)(ThreadVars *, DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1129
void DetectHttpMethodRegisterTests(void)
this function registers unit tests for DetectHttpMethod
const char * url
Definition: detect.h:1163
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:349
#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:1154
void DetectHttpMethodFree(void *)
Flow data structure.
Definition: flow.h:327
void(* RegisterTests)(void)
Definition: detect.h:1152
a single match condition for a signature
Definition: detect.h:324
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine