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 /**
132  * \brief Registers the keyword handlers for the "http.protocol" keyword.
133  */
135 {
140  sigmatch_table[DETECT_AL_HTTP_PROTOCOL].Setup = DetectHttpProtocolSetup;
142 
144  GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
146  GetData, ALPROTO_HTTP1, HTP_RESPONSE_LINE);
148  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
150  HTP_RESPONSE_LINE, DetectEngineInspectBufferGeneric, GetData);
151 
153  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
155  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
157  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
159  GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
160 
162  BUFFER_DESC);
163 
164  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
165 }
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1500
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
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:2122
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1296
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-protocol.c:70
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:409
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
InspectionBuffer
Definition: detect.h:374
threads.h
Flow_
Flow data structure.
Definition: flow.h:351
DetectHttpProtocolRegister
void DetectHttpProtocolRegister(void)
Registers the keyword handlers for the "http.protocol" keyword.
Definition: detect-http-protocol.c:134
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-http-protocol.c:66
detect-http-header.h
MpmListIdDataArgs::txv
void * txv
Definition: detect-engine-mpm.h:130
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:267
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1479
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
detect-http-protocol.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
util-print.h
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
app-layer-parser.h
DETECT_AL_HTTP_PROTOCOL
@ DETECT_AL_HTTP_PROTOCOL
Definition: detect-engine-register.h:135
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-protocol.c:68
detect-http-header-common.h
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:62
detect-engine-content-inspection.h
SigTableElmt_::alias
const char * alias
Definition: detect.h:1297
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
util-spm.h
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-http-protocol.c:69
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
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:1574
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:67
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
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:1169
flow.h
flow-var.h
app-layer.h