suricata
detect-http-protocol.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 http_protocol sticky buffer
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"
47 #include "detect-http-protocol.h"
48 
49 #include "flow.h"
50 #include "flow-var.h"
51 #include "flow-util.h"
52 
53 #include "util-debug.h"
54 #include "util-unittest.h"
55 #include "util-unittest-helper.h"
56 #include "util-spm.h"
57 #include "util-print.h"
58 
59 #include "app-layer.h"
60 #include "app-layer-parser.h"
61 
62 #include "app-layer-htp.h"
63 #include "detect-http-header.h"
64 #include "stream-tcp.h"
65 
66 #include "util-print.h"
67 
68 #define KEYWORD_NAME "http_protocol"
69 #define KEYWORD_DOC "http-keywords.html#http-protocol"
70 #define BUFFER_NAME "http_protocol"
71 #define BUFFER_DESC "http protocol"
72 static int g_buffer_id = 0;
73 
74 /** \brief HTTP Protocol Mpm prefilter callback
75  *
76  * \param det_ctx detection engine thread ctx
77  * \param p packet to inspect
78  * \param f flow to inspect
79  * \param txv tx to inspect
80  * \param pectx inspection context
81  */
82 static void PrefilterTxHttpRequestProtocol(DetectEngineThreadCtx *det_ctx,
83  const void *pectx,
84  Packet *p, Flow *f, void *txv,
85  const uint64_t idx, const uint8_t flags)
86 {
87  SCEnter();
88 
89  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
90  htp_tx_t *tx = (htp_tx_t *)txv;
91 
92  if (tx->request_protocol == NULL)
93  return;
94 
95  uint32_t buffer_len = bstr_size(tx->request_protocol);
96  const uint8_t *buffer = bstr_ptr(tx->request_protocol);
97 
98  if (buffer_len >= mpm_ctx->minlen) {
99  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
100  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
101  }
102 }
103 
104 static int PrefilterTxHttpRequestProtocolRegister(DetectEngineCtx *de_ctx,
105  SigGroupHead *sgh, MpmCtx *mpm_ctx)
106 {
107  SCEnter();
108 
109  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpRequestProtocol,
110  ALPROTO_HTTP, HTP_REQUEST_LINE,
111  mpm_ctx, NULL, KEYWORD_NAME " (request)");
112  return r;
113 }
114 
115 /** \brief HTTP Protocol Mpm prefilter callback
116  *
117  * \param det_ctx detection engine thread ctx
118  * \param p packet to inspect
119  * \param f flow to inspect
120  * \param txv tx to inspect
121  * \param pectx inspection context
122  */
123 static void PrefilterTxHttpResponseProtocol(DetectEngineThreadCtx *det_ctx,
124  const void *pectx,
125  Packet *p, Flow *f, void *txv,
126  const uint64_t idx, const uint8_t flags)
127 {
128  SCEnter();
129 
130  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
131  htp_tx_t *tx = (htp_tx_t *)txv;
132 
133  if (tx->response_protocol == NULL)
134  return;
135 
136  uint32_t buffer_len = bstr_size(tx->response_protocol);
137  const uint8_t *buffer = bstr_ptr(tx->response_protocol);
138 
139  if (buffer_len >= mpm_ctx->minlen) {
140  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
141  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
142  }
143 }
144 
145 static int PrefilterTxHttpResponseProtocolRegister(DetectEngineCtx *de_ctx,
146  SigGroupHead *sgh, MpmCtx *mpm_ctx)
147 {
148  SCEnter();
149 
150  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpResponseProtocol,
151  ALPROTO_HTTP, HTP_RESPONSE_LINE,
152  mpm_ctx, NULL, KEYWORD_NAME " (response)");
153  return r;
154 }
155 
156 static int InspectEngineHttpProtocol(ThreadVars *tv,
157  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
158  const Signature *s, const SigMatchData *smd,
159  Flow *f, uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
160 {
161  bstr *str = NULL;
162  htp_tx_t *http_tx = tx;
163 
164  if (flags & STREAM_TOSERVER)
165  str = http_tx->request_protocol;
166  else if (flags & STREAM_TOCLIENT)
167  str = http_tx->response_protocol;
168 
169  if (str == NULL)
170  goto end;
171 
172  uint32_t buffer_len = bstr_size(str);
173  uint8_t *buffer = bstr_ptr(str);
174  if (buffer == NULL ||buffer_len == 0)
175  goto end;
176 
177  det_ctx->buffer_offset = 0;
178  det_ctx->discontinue_matching = 0;
179  det_ctx->inspection_recursion_counter = 0;
180  int r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd,
181  f,
182  buffer, buffer_len,
185  if (r == 1)
187 
188  end:
189  if (flags & STREAM_TOSERVER) {
190  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) > HTP_REQUEST_LINE)
192  } else {
193  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) > HTP_RESPONSE_LINE)
195  }
197 }
198 
199 static int DetectHttpProtocolSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
200 {
201  s->init_data->list = g_buffer_id;
202  return 0;
203 }
204 
205 /**
206  * \brief Registers the keyword handlers for the "http_header" keyword.
207  */
209 {
213  sigmatch_table[DETECT_AL_HTTP_PROTOCOL].Setup = DetectHttpProtocolSetup;
214 
216 
218  PrefilterTxHttpRequestProtocolRegister);
220  PrefilterTxHttpResponseProtocolRegister);
221 
223  ALPROTO_HTTP, SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
224  InspectEngineHttpProtocol);
226  ALPROTO_HTTP, SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
227  InspectEngineHttpProtocol);
228 
230  BUFFER_DESC);
231 
232  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
233 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
SignatureInitData * init_data
Definition: detect.h:560
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
uint16_t minlen
Definition: util-mpm.h:95
#define KEYWORD_DOC
#define BUFFER_NAME
uint16_t discontinue_matching
Definition: detect.h:1026
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx))
register an app layer keyword for mpm
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
Data needed for Match()
Definition: detect.h:333
Container for matching data for a signature group.
Definition: detect.h:1295
uint32_t buffer_offset
Definition: detect.h:991
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
#define KEYWORD_NAME
main detection engine ctx
Definition: detect.h:720
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:244
#define DETECT_ENGINE_INSPECT_SIG_MATCH
#define BUFFER_DESC
uint16_t mpm_type
Definition: util-mpm.h:84
Data structures and function prototypes for keeping state for the detection engine.
#define SIG_FLAG_TOSERVER
Definition: detect.h:243
#define SCEnter(...)
Definition: util-debug.h:337
PrefilterRuleStore pmq
Definition: detect.h:1061
#define STREAM_TOCLIENT
Definition: stream.h:32
const char * desc
Definition: detect.h:1162
void DetectHttpProtocolRegister(void)
Registers the keyword handlers for the "http_header" keyword.
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:165
uint16_t tx_id
MpmThreadCtx mtcu
Definition: detect.h:1059
#define SIGMATCH_NOOPT
Definition: detect.h:1328
int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode, void *data)
Run the actual payload match functions.
const char * url
Definition: detect.h:1163
#define STREAM_TOSERVER
Definition: stream.h:31
int inspection_recursion_counter
Definition: detect.h:1038
#define DETECT_CI_FLAGS_SINGLE
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:158
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Per thread variable structure.
Definition: threadvars.h:57
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1154
Flow data structure.
Definition: flow.h:327
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time