/src/suricata7/src/detect-sip-uri.c
Line | Count | Source |
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.uri 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" |
34 | | #include "detect-engine-prefilter.h" |
35 | | #include "detect-content.h" |
36 | | #include "detect-pcre.h" |
37 | | #include "detect-urilen.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-uri.h" |
52 | | #include "stream-tcp.h" |
53 | | |
54 | | #include "rust.h" |
55 | | #include "app-layer-sip.h" |
56 | | |
57 | 75 | #define KEYWORD_NAME "sip.uri" |
58 | 75 | #define KEYWORD_DOC "sip-keywords.html#sip-uri" |
59 | 450 | #define BUFFER_NAME "sip.uri" |
60 | 75 | #define BUFFER_DESC "sip request uri" |
61 | | static int g_buffer_id = 0; |
62 | | |
63 | | static bool DetectSipUriValidateCallback(const Signature *s, const char **sigerror) |
64 | 105 | { |
65 | 105 | return DetectUrilenValidateContent(s, g_buffer_id, sigerror); |
66 | 105 | } |
67 | | |
68 | | static void DetectSipUriSetupCallback(const DetectEngineCtx *de_ctx, |
69 | | Signature *s) |
70 | 1.42k | { |
71 | 1.42k | SCLogDebug("callback invoked by %u", s->id); |
72 | 1.42k | DetectUrilenApplyToContent(s, g_buffer_id); |
73 | 1.42k | } |
74 | | |
75 | | static int DetectSipUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str) |
76 | 1.24k | { |
77 | 1.24k | if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0) |
78 | 10 | return -1; |
79 | | |
80 | 1.23k | if (DetectSignatureSetAppProto(s, ALPROTO_SIP) < 0) |
81 | 32 | return -1; |
82 | | |
83 | 1.20k | return 0; |
84 | 1.23k | } |
85 | | |
86 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
87 | | const DetectEngineTransforms *transforms, Flow *_f, |
88 | | const uint8_t _flow_flags, void *txv, const int list_id) |
89 | 0 | { |
90 | 0 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
91 | 0 | if (buffer->inspect == NULL) { |
92 | 0 | const uint8_t *b = NULL; |
93 | 0 | uint32_t b_len = 0; |
94 | |
|
95 | 0 | if (rs_sip_tx_get_uri(txv, &b, &b_len) != 1) |
96 | 0 | return NULL; |
97 | 0 | if (b == NULL || b_len == 0) |
98 | 0 | return NULL; |
99 | | |
100 | 0 | InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len); |
101 | 0 | InspectionBufferApplyTransforms(buffer, transforms); |
102 | 0 | } |
103 | | |
104 | 0 | return buffer; |
105 | 0 | } |
106 | | |
107 | | void DetectSipUriRegister(void) |
108 | 75 | { |
109 | 75 | sigmatch_table[DETECT_AL_SIP_URI].name = KEYWORD_NAME; |
110 | 75 | sigmatch_table[DETECT_AL_SIP_URI].desc = "sticky buffer to match on the SIP URI"; |
111 | 75 | sigmatch_table[DETECT_AL_SIP_URI].url = "/rules/" KEYWORD_DOC; |
112 | 75 | sigmatch_table[DETECT_AL_SIP_URI].Setup = DetectSipUriSetup; |
113 | 75 | sigmatch_table[DETECT_AL_SIP_URI].flags |= SIGMATCH_NOOPT; |
114 | | |
115 | 75 | DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_SIP, |
116 | 75 | SIG_FLAG_TOSERVER, 0, |
117 | 75 | DetectEngineInspectBufferGeneric, GetData); |
118 | | |
119 | 75 | DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, |
120 | 75 | PrefilterGenericMpmRegister, GetData, ALPROTO_SIP, |
121 | 75 | 1); |
122 | | |
123 | 75 | DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); |
124 | | |
125 | 75 | DetectBufferTypeRegisterSetupCallback(BUFFER_NAME, |
126 | 75 | DetectSipUriSetupCallback); |
127 | | |
128 | 75 | DetectBufferTypeRegisterValidateCallback(BUFFER_NAME, |
129 | 75 | DetectSipUriValidateCallback); |
130 | | |
131 | 75 | g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
132 | | |
133 | 75 | SCLogDebug("registering " BUFFER_NAME " rule option"); |
134 | 75 | } |