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 "debug.h"
37 #include "decode.h"
38 #include "detect.h"
39 
40 #include "detect-parse.h"
41 #include "detect-engine.h"
42 #include "detect-content.h"
43 #include "detect-pcre.h"
44 #include "detect-engine-mpm.h"
46 
47 #include "flow.h"
48 #include "flow-var.h"
49 #include "flow-util.h"
50 
51 #include "util-debug.h"
52 #include "util-error.h"
53 #include "util-unittest.h"
54 #include "util-unittest-helper.h"
55 #include "util-spm.h"
56 #include "util-print.h"
57 
58 #include "app-layer.h"
59 #include "app-layer-parser.h"
60 
61 #include "app-layer-htp.h"
62 #include "detect-http-stat-msg.h"
63 #include "stream-tcp-private.h"
64 #include "stream-tcp.h"
65 
66 static int DetectHttpStatMsgSetup(DetectEngineCtx *, Signature *, const char *);
67 #ifdef UNITTESTS
68 static void DetectHttpStatMsgRegisterTests(void);
69 #endif
70 static int g_http_stat_msg_buffer_id = 0;
71 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
72  const DetectEngineTransforms *transforms, Flow *_f,
73  const uint8_t _flow_flags, void *txv, const int list_id);
74 static int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
75 
76 /**
77  * \brief Registration function for keyword: http_stat_msg
78  */
80 {
81  /* http_stat_msg content modifier */
82  sigmatch_table[DETECT_AL_HTTP_STAT_MSG].name = "http_stat_msg";
83  sigmatch_table[DETECT_AL_HTTP_STAT_MSG].desc = "content modifier to match on HTTP stat-msg-buffer";
84  sigmatch_table[DETECT_AL_HTTP_STAT_MSG].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-stat-msg";
85  sigmatch_table[DETECT_AL_HTTP_STAT_MSG].Setup = DetectHttpStatMsgSetup;
86 #ifdef UNITTESTS
88 #endif
91 
92  /* http.stat_msg sticky buffer */
93  sigmatch_table[DETECT_HTTP_STAT_MSG].name = "http.stat_msg";
94  sigmatch_table[DETECT_HTTP_STAT_MSG].desc = "sticky buffer to match on the HTTP response status message";
95  sigmatch_table[DETECT_HTTP_STAT_MSG].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http_stat-msg";
96  sigmatch_table[DETECT_HTTP_STAT_MSG].Setup = DetectHttpStatMsgSetupSticky;
98 
100  SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
102 
103  DetectAppLayerMpmRegister2("http_stat_msg", SIG_FLAG_TOCLIENT, 3,
105  HTP_RESPONSE_LINE);
106 
107  DetectBufferTypeSetDescriptionByName("http_stat_msg",
108  "http response status message");
109 
110  g_http_stat_msg_buffer_id = DetectBufferTypeGetByName("http_stat_msg");
111 }
112 
113 /**
114  * \brief this function setups the http_stat_msg modifier keyword used in the rule
115  *
116  * \param de_ctx Pointer to the Detection Engine Context
117  * \param s Pointer to the Signature to which the current keyword belongs
118  * \param str Should hold an empty string always
119  *
120  * \retval 0 On success
121  * \retval -1 On failure
122  */
123 
124 static int DetectHttpStatMsgSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
125 {
126  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
128  g_http_stat_msg_buffer_id,
129  ALPROTO_HTTP);
130 }
131 
132 /**
133  * \brief this function setup the http.stat_msg keyword used in the rule
134  *
135  * \param de_ctx Pointer to the Detection Engine Context
136  * \param s Pointer to the Signature to which the current keyword belongs
137  * \param str Should hold an empty string always
138  *
139  * \retval 0 On success
140  */
141 static int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
142 {
143  if (DetectBufferSetActiveList(s, g_http_stat_msg_buffer_id) < 0)
144  return -1;
145  s->alproto = ALPROTO_HTTP;
146  return 0;
147 }
148 
149 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
150  const DetectEngineTransforms *transforms, Flow *_f,
151  const uint8_t _flow_flags, void *txv, const int list_id)
152 {
153  SCEnter();
154 
155  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
156  if (buffer->inspect == NULL) {
157  htp_tx_t *tx = (htp_tx_t *)txv;
158 
159  if (tx->response_message == NULL)
160  return NULL;
161 
162  const uint32_t data_len = bstr_len(tx->response_message);
163  const uint8_t *data = bstr_ptr(tx->response_message);
164 
165  InspectionBufferSetup(buffer, data, data_len);
166  InspectionBufferApplyTransforms(buffer, transforms);
167  }
168 
169  return buffer;
170 }
171 
172 #ifdef UNITTESTS
174 #endif /* UNITTESTS */
175 
176 /**
177  * @}
178  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#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
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
main detection engine ctx
Definition: detect.h:756
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:233
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1386
#define SCEnter(...)
Definition: util-debug.h:337
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
AppProto alproto
Definition: detect.h:521
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:143
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.
const char * desc
Definition: detect.h:1195
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
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 DetectHttpStatMsgRegister(void)
Registration function for keyword: http_stat_msg.
void DetectHttpStatMsgRegisterTests(void)
Register the UNITTESTS for the http_stat_msg keyword.
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)
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1187
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1185