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 #define KEYWORD_NAME "http.protocol"
67 #define KEYWORD_NAME_LEGACY "http_protocol"
68 #define KEYWORD_DOC "http-keywords.html#http-protocol"
69 #define BUFFER_NAME "http_protocol"
70 #define BUFFER_DESC "http protocol"
71 static int g_buffer_id = 0;
72 
73 static int DetectHttpProtocolSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
74 {
75  if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
76  return -1;
77 
79  return -1;
80 
81  return 0;
82 }
83 
84 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
85  const DetectEngineTransforms *transforms, Flow *_f,
86  const uint8_t flow_flags, void *txv, const int list_id)
87 {
88  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
89  if (buffer->inspect == NULL) {
90  bstr *str = NULL;
91  htp_tx_t *tx = (htp_tx_t *)txv;
92 
93  if (flow_flags & STREAM_TOSERVER)
94  str = tx->request_protocol;
95  else if (flow_flags & STREAM_TOCLIENT)
96  str = tx->response_protocol;
97 
98  if (str == NULL) {
99  SCLogDebug("HTTP protocol not set");
100  return NULL;
101  }
102 
103  uint32_t data_len = bstr_size(str);
104  uint8_t *data = bstr_ptr(str);
105  if (data == NULL || data_len == 0) {
106  SCLogDebug("HTTP protocol not present");
107  return NULL;
108  }
109 
110  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
111  InspectionBufferApplyTransforms(buffer, transforms);
112  }
113 
114  return buffer;
115 }
116 
117 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
118  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
119  const int list_id)
120 {
121  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
122  if (buffer->inspect == NULL) {
124  det_ctx, list_id, buffer, (const uint8_t *)"HTTP/2", strlen("HTTP/2"));
125  InspectionBufferApplyTransforms(buffer, transforms);
126  }
127 
128  return buffer;
129 }
130 
131 static bool DetectHttpProtocolValidateCallback(const Signature *s, const char **sigerror)
132 {
133 #ifdef HAVE_HTP_CONFIG_SET_ALLOW_SPACE_URI
134  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
135  if (s->init_data->buffers[x].id != (uint32_t)g_buffer_id)
136  continue;
137  const SigMatch *sm = s->init_data->buffers[x].head;
138  for (; sm != NULL; sm = sm->next) {
139  if (sm->type != DETECT_CONTENT)
140  continue;
141  const DetectContentData *cd = (DetectContentData *)sm->ctx;
142  for (size_t i = 0; i < cd->content_len; ++i) {
143  if (cd->content[i] == ' ') {
144  *sigerror = "Invalid http.protocol string containing a space";
145  SCLogWarning("rule %u: %s", s->id, *sigerror);
146  return false;
147  }
148  }
149  }
150  }
151 #endif
152  return true;
153 }
154 
155 /**
156  * \brief Registers the keyword handlers for the "http.protocol" keyword.
157  */
159 {
164  sigmatch_table[DETECT_AL_HTTP_PROTOCOL].Setup = DetectHttpProtocolSetup;
166 
168  GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
170  GetData, ALPROTO_HTTP1, HTP_RESPONSE_LINE);
172  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
174  HTP_RESPONSE_LINE, DetectEngineInspectBufferGeneric, GetData);
175 
177  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
179  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
181  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
183  GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
184 
186  BUFFER_DESC);
187  DetectBufferTypeRegisterValidateCallback(BUFFER_NAME, DetectHttpProtocolValidateCallback);
188 
189  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
190 }
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:535
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
DetectEngineInspectBufferGeneric
uint8_t 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:2142
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1304
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-protocol.c:70
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:408
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:70
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectHttpProtocolRegister
void DetectHttpProtocolRegister(void)
Registers the keyword handlers for the "http.protocol" keyword.
Definition: detect-http-protocol.c:158
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-http-protocol.c:66
detect-http-header.h
DetectContentData_
Definition: detect-content.h:93
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
detect-http-protocol.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
app-layer-htp.h
decode.h
util-debug.h
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror))
Definition: detect-engine.c:1305
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
util-print.h
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:352
DETECT_AL_HTTP_PROTOCOL
@ DETECT_AL_HTTP_PROTOCOL
Definition: detect-engine-register.h:150
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-protocol.c:68
detect-http-header-common.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:670
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:64
detect-engine-content-inspection.h
SigTableElmt_::alias
const char * alias
Definition: detect.h:1305
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:350
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1699
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:591
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-http-protocol.c:69
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
str
#define str(s)
Definition: suricata-common.h:291
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:1595
Signature_::id
uint32_t id
Definition: detect.h:636
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:528
Signature_
Signature container.
Definition: detect.h:601
SigMatch_
a single match condition for a signature
Definition: detect.h:349
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:70
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-protocol.c:67
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:592
flow-var.h
app-layer.h