suricata
detect-http-stat-code.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_code 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-code.h"
63 #include "stream-tcp-private.h"
64 #include "stream-tcp.h"
65 
66 static int DetectHttpStatCodeSetup(DetectEngineCtx *, Signature *, const char *);
67 static int DetectHttpStatCodeSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
68 #ifdef UNITTESTS
69 static void DetectHttpStatCodeRegisterTests(void);
70 #endif
71 static int g_http_stat_code_buffer_id = 0;
72 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
73  const DetectEngineTransforms *transforms, Flow *_f,
74  const uint8_t _flow_flags, void *txv, const int list_id);
75 
76 /**
77  * \brief Registration function for keyword: http_stat_code
78  */
80 {
81  /* http_stat_code content modifier */
82  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].name = "http_stat_code";
83  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].desc = "content modifier to match only on HTTP stat-code-buffer";
84  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-stat-code";
85  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].Setup = DetectHttpStatCodeSetup;
86 #ifdef UNITTESTS
88 #endif
91 
92  /* http.stat_code content modifier */
93  sigmatch_table[DETECT_HTTP_STAT_CODE].name = "http.stat_code";
94  sigmatch_table[DETECT_HTTP_STAT_CODE].desc = "sticky buffer to match only on HTTP stat-code-buffer";
95  sigmatch_table[DETECT_HTTP_STAT_CODE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http_stat-code";
96  sigmatch_table[DETECT_HTTP_STAT_CODE].Setup = DetectHttpStatCodeSetupSticky;
98 
100  SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
102 
103  DetectAppLayerMpmRegister2("http_stat_code", SIG_FLAG_TOCLIENT, 4,
105  HTP_RESPONSE_LINE);
106 
107  DetectBufferTypeSetDescriptionByName("http_stat_code",
108  "http response status code");
109 
110  g_http_stat_code_buffer_id = DetectBufferTypeGetByName("http_stat_code");
111 }
112 
113 /**
114  * \brief this function setups the http_stat_code 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 DetectHttpStatCodeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
125 {
126  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
128  g_http_stat_code_buffer_id,
129  ALPROTO_HTTP);
130 }
131 
132 /**
133  * \brief this function setup the http.stat_code 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 DetectHttpStatCodeSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
142 {
143  DetectBufferSetActiveList(s, g_http_stat_code_buffer_id);
144  s->alproto = ALPROTO_HTTP;
145  return 0;
146 }
147 
148 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
149  const DetectEngineTransforms *transforms, Flow *_f,
150  const uint8_t _flow_flags, void *txv, const int list_id)
151 {
152  SCEnter();
153 
154  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
155  if (buffer->inspect == NULL) {
156  htp_tx_t *tx = (htp_tx_t *)txv;
157 
158  if (tx->response_status == NULL)
159  return NULL;
160 
161  const uint32_t data_len = bstr_len(tx->response_status);
162  const uint8_t *data = bstr_ptr(tx->response_status);
163 
164  InspectionBufferSetup(buffer, data, data_len);
165  InspectionBufferApplyTransforms(buffer, transforms);
166  }
167 
168  return buffer;
169 }
170 
171 #ifdef UNITTESTS
173 #endif /* UNITTESTS */
174 
175 /**
176  * @}
177  */
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
void DetectHttpStatCodeRegister(void)
Registration function for keyword: http_stat_code.
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.
void DetectHttpStatCodeRegisterTests(void)
Register the UNITTESTS for the http_stat_code keyword.
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
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