suricata
detect-http-server-body.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2018 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 Anoop Saldanha <anoopsaldanha@gmail.com>
29  * \author Victor Julien <victor@inliniac.net>
30  *
31  * Implements support for the http_server_body keyword
32  */
33 
34 #include "suricata-common.h"
35 #include "threads.h"
36 #include "decode.h"
37 
38 #include "detect.h"
39 #include "detect-parse.h"
40 #include "detect-engine.h"
41 #include "detect-engine-mpm.h"
42 #include "detect-engine-state.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"
60 #include "stream-tcp.h"
61 
62 static int DetectHttpServerBodySetup(DetectEngineCtx *, Signature *, const char *);
63 static int DetectHttpServerBodySetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
64 #ifdef UNITTESTS
65 static void DetectHttpServerBodyRegisterTests(void);
66 #endif
67 static int g_file_data_buffer_id = 0;
68 
69 /**
70  * \brief Registers the keyword handlers for the "http_server_body" keyword.
71  */
73 {
74  /* http_server_body content modifier */
75  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].name = "http_server_body";
76  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].desc = "content modifier to match on the HTTP response-body";
77  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-server-body";
78  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].Setup = DetectHttpServerBodySetup;
79 #ifdef UNITTESTS
81 #endif
85 
86  /* http.request_body sticky buffer */
87  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].name = "http.response_body";
88  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].desc = "sticky buffer to match the HTTP response body buffer";
89  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-server-body";
90  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].Setup = DetectHttpServerBodySetupSticky;
93 
94  g_file_data_buffer_id = DetectBufferTypeRegister("file_data");
95 }
96 
97 /**
98  * \brief The setup function for the http_server_body keyword for a signature.
99  *
100  * \param de_ctx Pointer to the detection engine context.
101  * \param s Pointer to signature for the current Signature being parsed
102  * from the rules.
103  * \param m Pointer to the head of the SigMatchs for the current rule
104  * being parsed.
105  * \param arg Pointer to the string holding the keyword value.
106  *
107  * \retval 0 On success
108  * \retval -1 On failure
109  */
110 int DetectHttpServerBodySetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
111 {
112  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
114  g_file_data_buffer_id,
115  ALPROTO_HTTP);
116 }
117 
118 /**
119  * \brief this function setup the http.response_body keyword used in the rule
120  *
121  * \param de_ctx Pointer to the Detection Engine Context
122  * \param s Pointer to the Signature to which the current keyword belongs
123  * \param str Should hold an empty string always
124  *
125  * \retval 0 On success
126  */
127 static int DetectHttpServerBodySetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
128 {
129  if (DetectBufferSetActiveList(s, g_file_data_buffer_id) < 0)
130  return -1;
132  return -1;
133  return 0;
134 }
135 
136 /************************************Unittests*********************************/
137 
138 #ifdef UNITTESTS
140 #endif /* UNITTESTS */
141 
142 /**
143  * @}
144  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1406
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1149
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1353
const char * name
Definition: detect.h:1163
Signature container.
Definition: detect.h:495
main detection engine ctx
Definition: detect.h:723
void DetectHttpServerBodyRegisterTests(void)
#define str(s)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1355
Data structures and function prototypes for keeping state for the detection engine.
uint16_t alternative
Definition: detect.h:1161
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:143
const char * desc
Definition: detect.h:1165
int DetectBufferTypeRegister(const char *name)
#define SIGMATCH_NOOPT
Definition: detect.h:1331
const char * url
Definition: detect.h:1166
int DetectBufferSetActiveList(Signature *s, const int list)
#define DOC_URL
Definition: suricata.h:86
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1157
void(* RegisterTests)(void)
Definition: detect.h:1155
void DetectHttpServerBodyRegister(void)
Registers the keyword handlers for the "http_server_body" keyword.