suricata
detect-http-stat-msg.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 Gurvinder Singh <gurvindersinghdahiya@gmail.com>
29  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
30  *
31  * Implements the http_stat_msg keyword
32  */
33 
34 #include "suricata-common.h"
35 #include "threads.h"
36 #include "decode.h"
37 #include "detect.h"
38 
39 #include "detect-parse.h"
40 #include "detect-engine.h"
41 #include "detect-content.h"
42 #include "detect-pcre.h"
43 #include "detect-engine-mpm.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-error.h"
52 #include "util-unittest.h"
53 #include "util-unittest-helper.h"
54 #include "util-spm.h"
55 #include "util-print.h"
56 
57 #include "app-layer.h"
58 #include "app-layer-parser.h"
59 
60 #include "app-layer-htp.h"
61 #include "detect-http-stat-msg.h"
62 #include "stream-tcp-private.h"
63 #include "stream-tcp.h"
64 
65 static int DetectHttpStatMsgSetup(DetectEngineCtx *, Signature *, const char *);
66 #ifdef UNITTESTS
67 static void DetectHttpStatMsgRegisterTests(void);
68 #endif
69 static int g_http_stat_msg_buffer_id = 0;
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 int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
74 
75 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
76  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
77  const int list_id)
78 {
79  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
80  if (buffer->inspect == NULL) {
81  InspectionBufferSetup(det_ctx, list_id, buffer, (const uint8_t *)"", 0);
82  InspectionBufferApplyTransforms(buffer, transforms);
83  }
84 
85  return buffer;
86 }
87 
88 /**
89  * \brief Registration function for keyword: http_stat_msg
90  */
92 {
93  /* http_stat_msg content modifier */
94  sigmatch_table[DETECT_HTTP_STAT_MSG_CM].name = "http_stat_msg";
96  "content modifier to match on HTTP stat-msg-buffer";
97  sigmatch_table[DETECT_HTTP_STAT_MSG_CM].url = "/rules/http-keywords.html#http-stat-msg";
98  sigmatch_table[DETECT_HTTP_STAT_MSG_CM].Setup = DetectHttpStatMsgSetup;
99 #ifdef UNITTESTS
101 #endif
105 
106  /* http.stat_msg sticky buffer */
107  sigmatch_table[DETECT_HTTP_STAT_MSG].name = "http.stat_msg";
108  sigmatch_table[DETECT_HTTP_STAT_MSG].desc = "sticky buffer to match on the HTTP response status message";
109  sigmatch_table[DETECT_HTTP_STAT_MSG].url = "/rules/http-keywords.html#http-stat-msg";
110  sigmatch_table[DETECT_HTTP_STAT_MSG].Setup = DetectHttpStatMsgSetupSticky;
112 
115 
118 
120  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
122  GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
123 
124  DetectBufferTypeSetDescriptionByName("http_stat_msg",
125  "http response status message");
126 
127  g_http_stat_msg_buffer_id = DetectBufferTypeGetByName("http_stat_msg");
128 }
129 
130 /**
131  * \brief this function setups the http_stat_msg modifier keyword used in the rule
132  *
133  * \param de_ctx Pointer to the Detection Engine Context
134  * \param s Pointer to the Signature to which the current keyword belongs
135  * \param str Should hold an empty string always
136  *
137  * \retval 0 On success
138  * \retval -1 On failure
139  */
140 
141 static int DetectHttpStatMsgSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
142 {
144  de_ctx, s, arg, DETECT_HTTP_STAT_MSG_CM, g_http_stat_msg_buffer_id, ALPROTO_HTTP1);
145 }
146 
147 /**
148  * \brief this function setup the http.stat_msg keyword used in the rule
149  *
150  * \param de_ctx Pointer to the Detection Engine Context
151  * \param s Pointer to the Signature to which the current keyword belongs
152  * \param str Should hold an empty string always
153  *
154  * \retval 0 On success
155  */
156 static int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
157 {
158  if (DetectBufferSetActiveList(de_ctx, s, g_http_stat_msg_buffer_id) < 0)
159  return -1;
161  return -1;
162  return 0;
163 }
164 
165 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
166  const DetectEngineTransforms *transforms, Flow *_f,
167  const uint8_t _flow_flags, void *txv, const int list_id)
168 {
169  SCEnter();
170 
171  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
172  if (buffer->inspect == NULL) {
173  htp_tx_t *tx = (htp_tx_t *)txv;
174 
175  if (htp_tx_response_message(tx) == NULL)
176  return NULL;
177 
178  const uint32_t data_len = bstr_len(htp_tx_response_message(tx));
179  const uint8_t *data = bstr_ptr(htp_tx_response_message(tx));
180 
181  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
182  InspectionBufferApplyTransforms(buffer, transforms);
183  }
184 
185  return buffer;
186 }
187 
188 #ifdef UNITTESTS
190 #endif /* UNITTESTS */
191 
192 /**
193  * @}
194  */
SigTableElmt_::url
const char * url
Definition: detect.h:1322
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1770
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1527
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
DetectEngineInspectBufferGeneric
uint8_t 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:2157
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1525
SigTableElmt_::name
const char * name
Definition: detect.h:1319
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:410
DetectHttpStatMsgRegisterTests
void DetectHttpStatMsgRegisterTests(void)
Register the UNITTESTS for the http_stat_msg keyword.
Definition: detect-http-stat-msg.c:2173
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1359
InspectionBuffer
Definition: detect.h:375
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
htp_tx_response_message
#define htp_tx_response_message(tx)
Definition: app-layer-htp-libhtp.h:177
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1304
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1503
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1096
DETECT_HTTP_STAT_MSG
@ DETECT_HTTP_STAT_MSG
Definition: detect-engine-register.h:180
app-layer-htp.h
decode.h
util-debug.h
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1109
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1317
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
app-layer-parser.h
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:220
stream-tcp-private.h
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
detect-http-stat-msg.c
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1714
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
str
#define str(s)
Definition: suricata-common.h:300
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:1598
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
DetectHttpStatMsgRegister
void DetectHttpStatMsgRegister(void)
Registration function for keyword: http_stat_msg.
Definition: detect-http-stat-msg.c:91
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1503
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:242
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1193
flow.h
flow-var.h
detect-http-stat-msg.h
DETECT_HTTP_STAT_MSG_CM
@ DETECT_HTTP_STAT_MSG_CM
Definition: detect-engine-register.h:179
HTP_RESPONSE_PROGRESS_LINE
#define HTP_RESPONSE_PROGRESS_LINE
Definition: app-layer-htp-libhtp.h:93
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1311
app-layer.h