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 "debug.h"
29 #include "decode.h"
30 #include "detect.h"
31 
32 #include "detect-parse.h"
33 #include "detect-engine.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.h"
45 #include "util-unittest-helper.h"
46 #include "util-spm.h"
47 
48 #include "app-layer.h"
49 #include "app-layer-parser.h"
50 
51 #include "detect-sip-method.h"
52 #include "stream-tcp.h"
53 
54 #include "rust.h"
55 #include "app-layer-sip.h"
56 #include "rust-sip-detect-gen.h"
57 
58 #define KEYWORD_NAME "sip.method"
59 #define KEYWORD_DOC "sip-keywords.html#sip-method"
60 #define BUFFER_NAME "sip.method"
61 #define BUFFER_DESC "sip request method"
62 static int g_buffer_id = 0;
63 
64 static int DetectSipMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
65 {
66  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
67  return -1;
68 
70  return -1;
71 
72  return 0;
73 }
74 
75 static bool DetectSipMethodValidateCallback(const Signature *s, const char **sigerror)
76 {
77  const SigMatch *sm = s->init_data->smlists[g_buffer_id];
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(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
86  return true;
87  } else if (cd->content[0] == 0x20) {
88  *sigerror = "sip.method pattern with leading space";
89  SCLogError(SC_ERR_INVALID_SIGNATURE, "%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(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
94  return true;
95  } else if (cd->content[0] == 0x09) {
96  *sigerror = "sip.method pattern with leading tab";
97  SCLogError(SC_ERR_INVALID_SIGNATURE, "%s", *sigerror);
98  return true;
99  }
100  }
101  }
102  return true;
103 }
104 
105 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
106  const DetectEngineTransforms *transforms, Flow *_f,
107  const uint8_t _flow_flags, void *txv, const int list_id)
108 {
109  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
110  if (buffer->inspect == NULL) {
111  const uint8_t *b = NULL;
112  uint32_t b_len = 0;
113 
114  if (rs_sip_tx_get_method(txv, &b, &b_len) != 1)
115  return NULL;
116  if (b == NULL || b_len == 0)
117  return NULL;
118 
119  InspectionBufferSetup(buffer, b, b_len);
120  InspectionBufferApplyTransforms(buffer, transforms);
121  }
122 
123  return buffer;
124 }
125 
127 {
128  /* sip.method sticky buffer */
130  sigmatch_table[DETECT_AL_SIP_METHOD].desc = "sticky buffer to match on the SIP method buffer";
132  sigmatch_table[DETECT_AL_SIP_METHOD].Setup = DetectSipMethodSetup;
134 
138 
141  1);
142 
144 
146  DetectSipMethodValidateCallback);
147 
148  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
149 
150  SCLogDebug("registering " BUFFER_NAME " rule option");
151 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
SignatureInitData * init_data
Definition: detect.h:591
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
void DetectSipMethodRegister(void)
#define KEYWORD_DOC
#define BUFFER_NAME
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
struct SigMatch_ * next
Definition: detect.h:322
main detection engine ctx
Definition: detect.h:761
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
#define KEYWORD_NAME
uint8_t type
Definition: detect.h:319
int 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.
const char * desc
Definition: detect.h:1202
struct SigMatch_ ** smlists
Definition: detect.h:516
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
SigMatchCtx * ctx
Definition: detect.h:321
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1369
const char * url
Definition: detect.h:1203
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:343
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectBufferTypeRegisterValidateCallback(const char *name, _Bool(*ValidateCallback)(const Signature *, const char **sigerror))
#define BUFFER_DESC
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1194
Flow data structure.
Definition: flow.h:325
a single match condition for a signature
Definition: detect.h:318