suricata
detect-sip-method.c
Go to the documentation of this file.
1 /* Copyright (C) 2019 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  *
20  * \author Giuseppe Longo <giuseppe@glongo.it>
21  *
22  * Implements the sip.method sticky buffer
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "decode.h"
29 #include "detect.h"
30 
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-engine-mpm.h"
35 #include "detect-content.h"
36 #include "detect-pcre.h"
37 
38 #include "flow.h"
39 #include "flow-var.h"
40 #include "flow-util.h"
41 
42 #include "util-debug.h"
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 #include "util-spm.h"
46 
47 #include "app-layer.h"
48 #include "app-layer-parser.h"
49 
50 #include "detect-sip-method.h"
51 #include "stream-tcp.h"
52 
53 #include "rust.h"
54 
55 #define KEYWORD_NAME "sip.method"
56 #define KEYWORD_DOC "sip-keywords.html#sip-method"
57 #define BUFFER_NAME "sip.method"
58 #define BUFFER_DESC "sip request method"
59 static int g_buffer_id = 0;
60 
61 static int DetectSipMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
62 {
63  if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
64  return -1;
65 
67  return -1;
68 
69  return 0;
70 }
71 
72 static bool DetectSipMethodValidateCallback(const Signature *s, const char **sigerror)
73 {
74  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
75  if (s->init_data->buffers[x].id != (uint32_t)g_buffer_id)
76  continue;
77  const SigMatch *sm = s->init_data->buffers[x].head;
78  for (; sm != NULL; sm = sm->next) {
79  if (sm->type != DETECT_CONTENT)
80  continue;
81  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
82  if (cd->content && cd->content_len) {
83  if (cd->content[cd->content_len - 1] == 0x20) {
84  *sigerror = "sip.method pattern with trailing space";
85  SCLogError("%s", *sigerror);
86  return true;
87  } else if (cd->content[0] == 0x20) {
88  *sigerror = "sip.method pattern with leading space";
89  SCLogError("%s", *sigerror);
90  return true;
91  } else if (cd->content[cd->content_len - 1] == 0x09) {
92  *sigerror = "sip.method pattern with trailing tab";
93  SCLogError("%s", *sigerror);
94  return true;
95  } else if (cd->content[0] == 0x09) {
96  *sigerror = "sip.method pattern with leading tab";
97  SCLogError("%s", *sigerror);
98  return true;
99  }
100  }
101  }
102  }
103  return true;
104 }
105 
106 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
107  const DetectEngineTransforms *transforms, Flow *_f,
108  const uint8_t _flow_flags, void *txv, const int list_id)
109 {
110  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
111  if (buffer->inspect == NULL) {
112  const uint8_t *b = NULL;
113  uint32_t b_len = 0;
114 
115  if (rs_sip_tx_get_method(txv, &b, &b_len) != 1)
116  return NULL;
117  if (b == NULL || b_len == 0)
118  return NULL;
119 
120  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
121  InspectionBufferApplyTransforms(buffer, transforms);
122  }
123 
124  return buffer;
125 }
126 
128 {
129  /* sip.method sticky buffer */
131  sigmatch_table[DETECT_AL_SIP_METHOD].desc = "sticky buffer to match on the SIP method buffer";
133  sigmatch_table[DETECT_AL_SIP_METHOD].Setup = DetectSipMethodSetup;
135 
138 
140  GetData, ALPROTO_SIP, 1);
141 
143 
145  DetectSipMethodValidateCallback);
146 
147  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
148 
149  SCLogDebug("registering " BUFFER_NAME " rule option");
150 }
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
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-sip-method.c:55
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:2154
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1304
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
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
ALPROTO_SIP
@ ALPROTO_SIP
Definition: app-layer-protos.h:54
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-sip-method.c:57
rust.h
DetectContentData_
Definition: detect-content.h:93
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
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
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
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
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-sip-method.c:58
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
DetectSipMethodRegister
void DetectSipMethodRegister(void)
Definition: detect-sip-method.c:127
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:670
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-sip-method.c:56
detect-sip-method.h
DETECT_AL_SIP_METHOD
@ DETECT_AL_SIP_METHOD
Definition: detect-engine-register.h:276
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:350
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:591
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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
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
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
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