suricata
detect-http-server-body.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2023 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"
42 #include "detect-content.h"
43 
44 #include "flow.h"
45 #include "flow-util.h"
46 
47 #include "util-unittest.h"
48 #include "util-unittest-helper.h"
49 #include "util-profiling.h"
50 
51 #include "app-layer.h"
52 #include "app-layer-parser.h"
53 
54 #include "app-layer-htp.h"
56 #include "stream-tcp.h"
57 
58 static int DetectHttpServerBodySetup(DetectEngineCtx *, Signature *, const char *);
59 static int DetectHttpServerBodySetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
60 #ifdef UNITTESTS
61 static void DetectHttpServerBodyRegisterTests(void);
62 #endif
63 static int g_buffer_id = 0;
64 
65 /**
66  * \brief Registers the keyword handlers for the "http_server_body" keyword.
67  */
69 {
70  /* http_server_body content modifier */
71  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].name = "http_server_body";
72  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].desc = "content modifier to match on the HTTP response-body";
73  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].url = "/rules/http-keywords.html#http-server-body";
74  sigmatch_table[DETECT_AL_HTTP_SERVER_BODY].Setup = DetectHttpServerBodySetup;
75 #ifdef UNITTESTS
77 #endif
81 
82  /* http.request_body sticky buffer */
83  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].name = "http.response_body";
84  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].desc = "sticky buffer to match the HTTP response body buffer";
85  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].url = "/rules/http-keywords.html#http-server-body";
86  sigmatch_table[DETECT_HTTP_RESPONSE_BODY].Setup = DetectHttpServerBodySetupSticky;
89 
90  g_buffer_id = DetectBufferTypeRegister("file_data");
91 }
92 
93 /**
94  * \brief The setup function for the http_server_body keyword for a signature.
95  *
96  * \param de_ctx Pointer to the detection engine context.
97  * \param s Pointer to signature for the current Signature being parsed
98  * from the rules.
99  * \param m Pointer to the head of the SigMatchs for the current rule
100  * being parsed.
101  * \param arg Pointer to the string holding the keyword value.
102  *
103  * \retval 0 On success
104  * \retval -1 On failure
105  */
106 int DetectHttpServerBodySetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
107 {
109  de_ctx, s, arg, DETECT_AL_HTTP_SERVER_BODY, g_buffer_id, ALPROTO_HTTP1);
110 }
111 
112 /**
113  * \brief this function setup the http.response_body keyword used in the rule
114  *
115  * \param de_ctx Pointer to the Detection Engine Context
116  * \param s Pointer to the Signature to which the current keyword belongs
117  * \param str Should hold an empty string always
118  *
119  * \retval 0 On success
120  */
121 static int DetectHttpServerBodySetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
122 {
123  if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
124  return -1;
126  return -1;
127  // file data is on both directions, but we only take the one to client here
128  s->flags |= SIG_FLAG_TOCLIENT;
129  s->flags &= ~SIG_FLAG_TOSERVER;
130  return 0;
131 }
132 
133 /************************************Unittests*********************************/
134 
135 #ifdef UNITTESTS
137 #endif /* UNITTESTS */
138 
139 /**
140  * @}
141  */
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1510
SigTableElmt_::name
const char * name
Definition: detect.h:1304
stream-tcp.h
DetectHttpServerBodyRegister
void DetectHttpServerBodyRegister(void)
Registers the keyword handlers for the "http_server_body" keyword.
Definition: detect-http-server-body.c:68
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
threads.h
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
util-unittest.h
util-unittest-helper.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
app-layer-htp.h
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
detect.h
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1302
app-layer-parser.h
util-profiling.h
Signature_::flags
uint32_t flags
Definition: detect.h:602
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:194
DETECT_HTTP_RESPONSE_BODY
@ DETECT_HTTP_RESPONSE_BODY
Definition: detect-engine-register.h:155
detect-engine-content-inspection.h
detect-http-server-body.h
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1029
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
str
#define str(s)
Definition: suricata-common.h:291
detect-http-server-body.c
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:70
DETECT_AL_HTTP_SERVER_BODY
@ DETECT_AL_HTTP_SERVER_BODY
Definition: detect-engine-register.h:154
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
flow.h
DetectHttpServerBodyRegisterTests
void DetectHttpServerBodyRegisterTests(void)
Definition: detect-http-server-body.c:3097
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1296
app-layer.h