suricata
detect-http-response-line.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 Victor Julien <victor@inliniac.net>
29  *
30  * Implements support for the http_response_line keyword.
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "decode.h"
36 
37 #include "detect.h"
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.h"
44 #include "detect-content.h"
45 #include "detect-pcre.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-unittest.h"
53 #include "util-unittest-helper.h"
54 #include "util-spm.h"
55 
56 #include "app-layer.h"
57 #include "app-layer-parser.h"
58 
59 #include "app-layer-htp.h"
60 #include "stream-tcp.h"
62 
63 static int DetectHttpResponseLineSetup(DetectEngineCtx *, Signature *, const char *);
64 #ifdef UNITTESTS
65 static void DetectHttpResponseLineRegisterTests(void);
66 #endif
67 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms,
69  Flow *_f, const uint8_t _flow_flags,
70  void *txv, const int list_id);
71 static int g_http_response_line_id = 0;
72 
73 /**
74  * \brief Registers the keyword handlers for the "http_response_line" keyword.
75  */
77 {
78  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].name = "http.response_line";
79  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].alias = "http_response_line";
80  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].desc = "content modifier to match only on the HTTP response line";
81  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].url = "/rules/http-keywords.html#http-response-line";
82  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests;
85 #endif
87 
89  HTP_RESPONSE_LINE, DetectEngineInspectBufferGeneric, GetData);
90 
91  DetectAppLayerMpmRegister2("http_response_line", SIG_FLAG_TOCLIENT, 2,
92  PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_RESPONSE_LINE);
93 
94  DetectBufferTypeSetDescriptionByName("http_response_line",
95  "http response line");
96 
97  g_http_response_line_id = DetectBufferTypeGetByName("http_response_line");
98 }
99 
100 /**
101  * \brief The setup function for the http_response_line keyword for a signature.
102  *
103  * \param de_ctx Pointer to the detection engine context.
104  * \param s Pointer to the signature for the current Signature being
105  * parsed from the rules.
106  * \param m Pointer to the head of the SigMatch for the current rule
107  * being parsed.
108  * \param arg Pointer to the string holding the keyword value.
109  *
110  * \retval 0 On success
111  * \retval -1 On failure
112  */
113 static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
114 {
115  if (DetectBufferSetActiveList(s, g_http_response_line_id) < 0)
116  return -1;
117 
119  return -1;
120 
121  return 0;
122 }
123 
124 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
125  const DetectEngineTransforms *transforms,
126  Flow *_f, const uint8_t _flow_flags,
127  void *txv, const int list_id)
128 {
129  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
130  if (buffer->inspect == NULL) {
131  htp_tx_t *tx = (htp_tx_t *)txv;
132  if (unlikely(tx->response_line == NULL)) {
133  return NULL;
134  }
135  const uint32_t data_len = bstr_len(tx->response_line);
136  const uint8_t *data = bstr_ptr(tx->response_line);
137 
138  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
139  InspectionBufferApplyTransforms(buffer, transforms);
140  }
141  return buffer;
142 }
143 
144 /************************************Unittests*********************************/
145 
146 #ifdef UNITTESTS
147 
148 #include "stream-tcp-reassemble.h"
149 
150 /**
151  * \test Test that a signature containting a http_response_line is correctly parsed
152  * and the keyword is registered.
153  */
154 static int DetectHttpResponseLineTest01(void)
155 {
158 
159  de_ctx->flags |= DE_QUIET;
160  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
161  "(http_response_line; content:\"200 OK\"; sid:1;)");
163 
165  PASS;
166 }
167 
168 void DetectHttpResponseLineRegisterTests(void)
169 {
170  UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01);
171 }
172 #endif /* UNITTESTS */
173 /**
174  * @}
175  */
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-content.h
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1267
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:379
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
InspectionBuffer
Definition: detect.h:345
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
SigInit
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2115
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:753
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2433
DE_QUIET
#define DE_QUIET
Definition: detect.h:295
stream-tcp-reassemble.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1360
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
app-layer-htp.h
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
detect-http-response-line.h
detect-engine-mpm.h
detect.h
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:1983
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:225
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
DETECT_AL_HTTP_RESPONSE_LINE
@ DETECT_AL_HTTP_RESPONSE_LINE
Definition: detect-engine-register.h:160
detect-engine-content-inspection.h
DetectHttpResponseLineRegister
void DetectHttpResponseLineRegister(void)
Registers the keyword handlers for the "http_response_line" keyword.
Definition: detect-http-response-line.c:76
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
SigTableElmt_::alias
const char * alias
Definition: detect.h:1268
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1543
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
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:817
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:346
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:1441
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2394
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1453
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:812
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1174
flow.h
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
app-layer.h