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"
41#include "detect-engine-mpm.h"
42#include "detect-engine-state.h"
45#include "detect-content.h"
46#include "detect-pcre.h"
49
50#include "flow.h"
51#include "flow-var.h"
52#include "flow-util.h"
53
54#include "util-debug.h"
55#include "util-unittest.h"
57#include "util-spm.h"
58#include "util-print.h"
59
60#include "app-layer.h"
61#include "app-layer-parser.h"
62
63#include "app-layer-htp.h"
64#include "detect-http-header.h"
65#include "stream-tcp.h"
66
67#define KEYWORD_NAME "http.protocol"
68#define KEYWORD_NAME_LEGACY "http_protocol"
69#define KEYWORD_DOC "http-keywords.html#http-protocol"
70#define BUFFER_NAME "http_protocol"
71#define BUFFER_DESC "http protocol"
72static int g_buffer_id = 0;
73
74static int DetectHttpProtocolSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
75{
76 if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
77 return -1;
78
80 return -1;
81
82 return 0;
83}
84
85static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
86 const DetectEngineTransforms *transforms, Flow *_f,
87 const uint8_t flow_flags, void *txv, const int list_id)
88{
89 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
90 if (buffer->inspect == NULL) {
91 const bstr *str = NULL;
92 htp_tx_t *tx = (htp_tx_t *)txv;
93
94 if (flow_flags & STREAM_TOSERVER)
95 str = htp_tx_request_protocol(tx);
96 else if (flow_flags & STREAM_TOCLIENT)
97 str = htp_tx_response_protocol(tx);
98
99 if (str == NULL) {
100 SCLogDebug("HTTP protocol not set");
101 return NULL;
102 }
103
104 uint32_t data_len = (uint32_t)bstr_size(str);
105 uint8_t *data = bstr_ptr(str);
106 if (data == NULL || data_len == 0) {
107 SCLogDebug("HTTP protocol not present");
108 return NULL;
109 }
110
112 det_ctx, list_id, buffer, data, data_len, transforms);
113 }
114
115 return buffer;
116}
117
118static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
119 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
120 const int list_id)
121{
122 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
123 if (buffer->inspect == NULL) {
125 det_ctx, list_id, buffer, (const uint8_t *)"HTTP/2", strlen("HTTP/2"), transforms);
126 }
127
128 return buffer;
129}
130
131static bool DetectHttpProtocolValidateCallback(
132 const Signature *s, const char **sigerror, const DetectBufferType *dbt)
133{
134 for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
135 if (s->init_data->buffers[x].id != (uint32_t)dbt->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 return true;
152}
153
154/**
155 * \brief Registers the keyword handlers for the "http.protocol" keyword.
156 */
158{
163 sigmatch_table[DETECT_HTTP_PROTOCOL].Setup = DetectHttpProtocolSetup;
165
167 GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
169 GetData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_LINE);
171 HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
173 HTP_RESPONSE_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
174
176 HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
178 GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
180 HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
182 GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
183
186 DetectBufferTypeRegisterValidateCallback(BUFFER_NAME, DetectHttpProtocolValidateCallback);
187
189}
@ ALPROTO_HTTP2
@ ALPROTO_HTTP
@ ALPROTO_HTTP1
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_HTTP_PROTOCOL
Data structures and function prototypes for keeping state for the detection engine.
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
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.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
int DetectBufferTypeGetByName(const char *name)
#define KEYWORD_NAME_LEGACY
#define KEYWORD_DOC
#define BUFFER_DESC
#define BUFFER_NAME
#define KEYWORD_NAME
void DetectHttpProtocolRegister(void)
Registers the keyword handlers for the "http.protocol" keyword.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
a single match condition for a signature
Definition detect.h:356
uint16_t type
Definition detect.h:357
struct SigMatch_ * next
Definition detect.h:360
SigMatchCtx * ctx
Definition detect.h:359
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
SignatureInitDataBuffer * buffers
Definition detect.h:647
uint32_t buffer_index
Definition detect.h:648
Signature container.
Definition detect.h:668
SignatureInitData * init_data
Definition detect.h:747
uint32_t id
Definition detect.h:713
#define str(s)
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition util-debug.h:255