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_NAME_LEGACY "http_protocol"
70 #define KEYWORD_DOC "http-keywords.html#http-protocol"
71 #define BUFFER_NAME "http_protocol"
72 #define BUFFER_DESC "http protocol"
73 static int g_buffer_id = 0;
74 
75 static int DetectHttpProtocolSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
76 {
77  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
78  return -1;
79 
81  return -1;
82 
83  return 0;
84 }
85 
86 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
87  const DetectEngineTransforms *transforms, Flow *_f,
88  const uint8_t flow_flags, void *txv, const int list_id)
89 {
90  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
91  if (buffer->inspect == NULL) {
92  bstr *str = NULL;
93  htp_tx_t *tx = (htp_tx_t *)txv;
94 
95  if (flow_flags & STREAM_TOSERVER)
96  str = tx->request_protocol;
97  else if (flow_flags & STREAM_TOCLIENT)
98  str = tx->response_protocol;
99 
100  if (str == NULL) {
101  SCLogDebug("HTTP protocol not set");
102  return NULL;
103  }
104 
105  uint32_t data_len = bstr_size(str);
106  uint8_t *data = bstr_ptr(str);
107  if (data == NULL || data_len == 0) {
108  SCLogDebug("HTTP protocol not present");
109  return NULL;
110  }
111 
112  InspectionBufferSetup(buffer, data, data_len);
113  InspectionBufferApplyTransforms(buffer, transforms);
114  }
115 
116  return buffer;
117 }
118 
119 /**
120  * \brief Registers the keyword handlers for the "http.protocol" keyword.
121  */
123 {
128  sigmatch_table[DETECT_AL_HTTP_PROTOCOL].Setup = DetectHttpProtocolSetup;
130 
133  HTP_REQUEST_LINE);
136  HTP_RESPONSE_LINE);
138  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
141  SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
143 
145  BUFFER_DESC);
146 
147  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
148 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1409
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1150
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
#define KEYWORD_DOC
#define BUFFER_NAME
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1164
Signature container.
Definition: detect.h:496
#define KEYWORD_NAME
main detection engine ctx
Definition: detect.h:724
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:242
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1356
#define KEYWORD_NAME_LEGACY
#define BUFFER_DESC
Data structures and function prototypes for keeping state for the detection engine.
#define SIG_FLAG_TOSERVER
Definition: detect.h:241
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
#define STREAM_TOCLIENT
Definition: stream.h:32
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.
const char * desc
Definition: detect.h:1166
void DetectHttpProtocolRegister(void)
Registers the keyword handlers for the "http.protocol" keyword.
const char * alias
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:1332
const char * url
Definition: detect.h:1167
#define STREAM_TOSERVER
Definition: stream.h:31
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:1158
Flow data structure.
Definition: flow.h:325
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