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 #include "app-layer-sip.h"
55 
56 #define KEYWORD_NAME "sip.method"
57 #define KEYWORD_DOC "sip-keywords.html#sip-method"
58 #define BUFFER_NAME "sip.method"
59 #define BUFFER_DESC "sip request method"
60 static int g_buffer_id = 0;
61 
62 static int DetectSipMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
63 {
64  if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
65  return -1;
66 
68  return -1;
69 
70  return 0;
71 }
72 
73 static bool DetectSipMethodValidateCallback(const Signature *s, const char **sigerror)
74 {
75  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
76  if (s->init_data->buffers[x].id != (uint32_t)g_buffer_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 = InspectionBufferGet(det_ctx, list_id);
112  if (buffer->inspect == NULL) {
113  const uint8_t *b = NULL;
114  uint32_t b_len = 0;
115 
116  if (rs_sip_tx_get_method(txv, &b, &b_len) != 1)
117  return NULL;
118  if (b == NULL || b_len == 0)
119  return NULL;
120 
121  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
122  InspectionBufferApplyTransforms(buffer, transforms);
123  }
124 
125  return buffer;
126 }
127 
129 {
130  /* sip.method sticky buffer */
132  sigmatch_table[DETECT_AL_SIP_METHOD].desc = "sticky buffer to match on the SIP method buffer";
134  sigmatch_table[DETECT_AL_SIP_METHOD].Setup = DetectSipMethodSetup;
136 
139 
141  GetData, ALPROTO_SIP, 1);
142 
144 
146  DetectSipMethodValidateCallback);
147 
148  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
149 
150  SCLogDebug("registering " BUFFER_NAME " rule option");
151 }
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:530
detect-content.h
detect-engine.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-sip-method.c:56
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
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:409
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
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
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
ALPROTO_SIP
@ ALPROTO_SIP
Definition: app-layer-protos.h:54
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-sip-method.c:58
rust.h
DetectContentData_
Definition: detect-content.h:93
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
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
decode.h
util-debug.h
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror))
Definition: detect-engine.c:1284
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:354
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-sip-method.c:59
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
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:353
DetectSipMethodRegister
void DetectSipMethodRegister(void)
Definition: detect-sip-method.c:128
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:665
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-sip-method.c:57
app-layer-sip.h
detect-sip-method.h
DETECT_AL_SIP_METHOD
@ DETECT_AL_SIP_METHOD
Definition: detect-engine-register.h:271
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:351
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:586
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
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:1574
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:523
Signature_
Signature container.
Definition: detect.h:596
SigMatch_
a single match condition for a signature
Definition: detect.h:350
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
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
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1169
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:587
flow-var.h
app-layer.h