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
90 
91  /* http.stat_code content modifier */
92  sigmatch_table[DETECT_HTTP_STAT_CODE].name = "http.stat_code";
93  sigmatch_table[DETECT_HTTP_STAT_CODE].desc = "sticky buffer to match only on HTTP stat-code-buffer";
94  sigmatch_table[DETECT_HTTP_STAT_CODE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http_stat-code";
95  sigmatch_table[DETECT_HTTP_STAT_CODE].Setup = DetectHttpStatCodeSetupSticky;
97 
99  SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
101 
102  DetectAppLayerMpmRegister2("http_stat_code", SIG_FLAG_TOCLIENT, 4,
104  HTP_RESPONSE_LINE);
105 
106  DetectBufferTypeSetDescriptionByName("http_stat_code",
107  "http response status code");
108 
109  g_http_stat_code_buffer_id = DetectBufferTypeGetByName("http_stat_code");
110 }
111 
112 /**
113  * \brief this function setups the http_stat_code modifier 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  * \retval -1 On failure
121  */
122 
123 static int DetectHttpStatCodeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
124 {
125  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
127  g_http_stat_code_buffer_id,
128  ALPROTO_HTTP);
129 }
130 
131 /**
132  * \brief this function setup the http.stat_code keyword used in the rule
133  *
134  * \param de_ctx Pointer to the Detection Engine Context
135  * \param s Pointer to the Signature to which the current keyword belongs
136  * \param str Should hold an empty string always
137  *
138  * \retval 0 On success
139  */
140 static int DetectHttpStatCodeSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
141 {
142  DetectBufferSetActiveList(s, g_http_stat_code_buffer_id);
143  s->alproto = ALPROTO_HTTP;
144  return 0;
145 }
146 
147 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
148  const DetectEngineTransforms *transforms, Flow *_f,
149  const uint8_t _flow_flags, void *txv, const int list_id)
150 {
151  SCEnter();
152 
153  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
154  if (buffer->inspect == NULL) {
155  htp_tx_t *tx = (htp_tx_t *)txv;
156 
157  if (tx->response_status == NULL)
158  return NULL;
159 
160  const uint32_t data_len = bstr_len(tx->response_status);
161  const uint8_t *data = bstr_ptr(tx->response_status);
162 
163  InspectionBufferSetup(buffer, data, data_len);
164  InspectionBufferApplyTransforms(buffer, transforms);
165  }
166 
167  return buffer;
168 }
169 
170 #ifdef UNITTESTS
172 #endif /* UNITTESTS */
173 
174 /**
175  * @}
176  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1406
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1149
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
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:1163
Signature container.
Definition: detect.h:495
main detection engine ctx
Definition: detect.h:723
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:242
#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
AppProto alproto
Definition: detect.h:499
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:1165
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:1331
const char * url
Definition: detect.h:1166
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:347
#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:1157
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1155
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine