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 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 /**
80  * \brief Registration function for keyword: http_stat_code
81  */
83 {
84  /* http_stat_code content modifier */
85  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].name = "http_stat_code";
86  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].desc = "content modifier to match only on HTTP stat-code-buffer";
87  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].url = "/rules/http-keywords.html#http-stat-code";
88  sigmatch_table[DETECT_AL_HTTP_STAT_CODE].Setup = DetectHttpStatCodeSetup;
89 #ifdef UNITTESTS
91 #endif
94 
95  /* http.stat_code content modifier */
96  sigmatch_table[DETECT_HTTP_STAT_CODE].name = "http.stat_code";
97  sigmatch_table[DETECT_HTTP_STAT_CODE].desc = "sticky buffer to match only on HTTP stat-code-buffer";
98  sigmatch_table[DETECT_HTTP_STAT_CODE].url = "/rules/http-keywords.html#http-stat-code";
99  sigmatch_table[DETECT_HTTP_STAT_CODE].Setup = DetectHttpStatCodeSetupSticky;
101 
103  HTP_RESPONSE_LINE, DetectEngineInspectBufferGeneric, GetData);
104 
106  GetData, ALPROTO_HTTP1, HTP_RESPONSE_LINE);
107 
109  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
110 
112  GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
113 
114  DetectBufferTypeSetDescriptionByName("http_stat_code",
115  "http response status code");
116 
117  g_http_stat_code_buffer_id = DetectBufferTypeGetByName("http_stat_code");
118 }
119 
120 /**
121  * \brief this function setups the http_stat_code modifier keyword used in the rule
122  *
123  * \param de_ctx Pointer to the Detection Engine Context
124  * \param s Pointer to the Signature to which the current keyword belongs
125  * \param str Should hold an empty string always
126  *
127  * \retval 0 On success
128  * \retval -1 On failure
129  */
130 
131 static int DetectHttpStatCodeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
132 {
134  de_ctx, s, arg, DETECT_AL_HTTP_STAT_CODE, g_http_stat_code_buffer_id, ALPROTO_HTTP1);
135 }
136 
137 /**
138  * \brief this function setup the http.stat_code keyword used in the rule
139  *
140  * \param de_ctx Pointer to the Detection Engine Context
141  * \param s Pointer to the Signature to which the current keyword belongs
142  * \param str Should hold an empty string always
143  *
144  * \retval 0 On success
145  */
146 static int DetectHttpStatCodeSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
147 {
148  if (DetectBufferSetActiveList(s, g_http_stat_code_buffer_id) < 0)
149  return -1;
151  return -1;
152  return 0;
153 }
154 
155 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
156  const DetectEngineTransforms *transforms, Flow *_f,
157  const uint8_t _flow_flags, void *txv, const int list_id)
158 {
159  SCEnter();
160 
161  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
162  if (buffer->inspect == NULL) {
163  htp_tx_t *tx = (htp_tx_t *)txv;
164 
165  if (tx->response_status == NULL)
166  return NULL;
167 
168  const uint32_t data_len = bstr_len(tx->response_status);
169  const uint8_t *data = bstr_ptr(tx->response_status);
170 
171  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
172  InspectionBufferApplyTransforms(buffer, transforms);
173  }
174 
175  return buffer;
176 }
177 
178 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
179  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
180  const int list_id)
181 {
182  SCEnter();
183 
184  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
185  if (buffer->inspect == NULL) {
186  uint32_t b_len = 0;
187  const uint8_t *b = NULL;
188 
189  if (rs_http2_tx_get_status(txv, &b, &b_len) != 1)
190  return NULL;
191  if (b == NULL || b_len == 0)
192  return NULL;
193 
194  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
195  InspectionBufferApplyTransforms(buffer, transforms);
196  }
197 
198  return buffer;
199 }
200 
201 #ifdef UNITTESTS
203 #endif /* UNITTESTS */
204 
205 /**
206  * @}
207  */
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
detect-http-stat-code.c
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1395
SigTableElmt_::name
const char * name
Definition: detect.h:1201
DetectHttpStatCodeRegisterTests
void DetectHttpStatCodeRegisterTests(void)
Register the UNITTESTS for the http_stat_code keyword.
Definition: detect-http-stat-code.c:2495
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:378
InspectionBuffer
Definition: detect.h:344
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:676
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
app-layer-htp.h
decode.h
util-debug.h
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1199
app-layer-parser.h
DetectEngineInspectBufferGeneric
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.
Definition: detect-engine.c:1617
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
stream-tcp-private.h
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:174
detect-http-stat-code.h
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:59
DETECT_HTTP_STAT_CODE
@ DETECT_HTTP_STAT_CODE
Definition: detect-engine-register.h:150
DETECT_AL_HTTP_STAT_CODE
@ DETECT_AL_HTTP_STAT_CODE
Definition: detect-engine-register.h:149
DetectAppLayerMpmRegister2
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
Definition: detect-engine-mpm.c:89
DetectHttpStatCodeRegister
void DetectHttpStatCodeRegister(void)
Registration function for keyword: http_stat_code.
Definition: detect-http-stat-code.c:82
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
str
#define str(s)
Definition: suricata-common.h:272
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:1091
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:63
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:857
flow.h
flow-var.h
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
app-layer.h