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-buffer.h"
34 #include "detect-engine-mpm.h"
36 #include "detect-content.h"
37 #include "detect-pcre.h"
38 
39 #include "flow.h"
40 #include "flow-var.h"
41 #include "flow-util.h"
42 
43 #include "util-debug.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 (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
64  return -1;
65 
67  return -1;
68 
69  return 0;
70 }
71 
72 static bool DetectSipMethodValidateCallback(
73  const Signature *s, const char **sigerror, const DetectBufferType *dbt)
74 {
75  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
76  if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
77  continue;
78  const SigMatch *sm = s->init_data->buffers[x].head;
79  for (; sm != NULL; sm = sm->next) {
80  if (sm->type != DETECT_CONTENT)
81  continue;
82  const DetectContentData *cd = (const DetectContentData *)sm->ctx;
83  if (cd->content && cd->content_len) {
84  if (cd->content[cd->content_len - 1] == 0x20) {
85  *sigerror = "sip.method pattern with trailing space";
86  SCLogError("%s", *sigerror);
87  return true;
88  } else if (cd->content[0] == 0x20) {
89  *sigerror = "sip.method pattern with leading space";
90  SCLogError("%s", *sigerror);
91  return true;
92  } else if (cd->content[cd->content_len - 1] == 0x09) {
93  *sigerror = "sip.method pattern with trailing tab";
94  SCLogError("%s", *sigerror);
95  return true;
96  } else if (cd->content[0] == 0x09) {
97  *sigerror = "sip.method pattern with leading tab";
98  SCLogError("%s", *sigerror);
99  return true;
100  }
101  }
102  }
103  }
104  return true;
105 }
106 
107 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
108  const DetectEngineTransforms *transforms, Flow *_f,
109  const uint8_t _flow_flags, void *txv, const int list_id)
110 {
111  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
112  if (buffer->inspect == NULL) {
113  const uint8_t *b = NULL;
114  uint32_t b_len = 0;
115 
116  if (SCSipTxGetMethod(txv, &b, &b_len) != 1)
117  return NULL;
118  if (b == NULL || b_len == 0)
119  return NULL;
120 
121  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
122  }
123 
124  return buffer;
125 }
126 
128 {
129  /* sip.method sticky buffer */
131  sigmatch_table[DETECT_SIP_METHOD].desc = "sticky buffer to match on the SIP method buffer";
133  sigmatch_table[DETECT_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:1545
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:543
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:308
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-sip-method.c:55
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
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:2267
flow-util.h
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1542
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:395
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:78
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:359
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1675
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(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
Definition: detect-engine-inspect-buffer.c:197
ALPROTO_SIP
@ ALPROTO_SIP
Definition: app-layer-protos.h:59
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-sip-method.c:57
rust.h
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:454
DetectContentData_
Definition: detect-content.h:93
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2619
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
detect-pcre.h
detect-engine-prefilter.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:275
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1452
DETECT_SIP_METHOD
@ DETECT_SIP_METHOD
Definition: detect-engine-register.h:263
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:274
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:364
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1707
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-sip-method.c:58
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:363
DetectSipMethodRegister
void DetectSipMethodRegister(void)
Definition: detect-sip-method.c:127
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:775
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-sip-method.c:56
detect-sip-method.h
DetectBufferType_::id
int id
Definition: detect.h:457
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:361
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
detect-engine-buffer.h
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:671
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:313
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:534
Signature_
Signature container.
Definition: detect.h:692
SigMatch_
a single match condition for a signature
Definition: detect.h:360
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1549
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:672
flow-var.h
app-layer.h