/src/suricata7/src/detect-mqtt-connect-clientid.c
Line | Count | Source |
1 | | /* Copyright (C) 2020 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 Sascha Steinbiss <sascha@steinbiss.name> |
21 | | * |
22 | | * Implements the mqtt.connect.clientid sticky buffer |
23 | | */ |
24 | | |
25 | | #include "suricata-common.h" |
26 | | #include "detect.h" |
27 | | #include "detect-parse.h" |
28 | | #include "detect-engine.h" |
29 | | #include "detect-engine-mpm.h" |
30 | | #include "detect-engine-prefilter.h" |
31 | | #include "detect-mqtt-connect-clientid.h" |
32 | | #include "rust.h" |
33 | | |
34 | 34 | #define KEYWORD_NAME "mqtt.connect.clientid" |
35 | 34 | #define KEYWORD_DOC "mqtt-keywords.html#mqtt-connect-clientid" |
36 | 136 | #define BUFFER_NAME "mqtt.connect.clientid" |
37 | 34 | #define BUFFER_DESC "MQTT CONNECT client ID" |
38 | | static int g_buffer_id = 0; |
39 | | |
40 | | static int DetectMQTTConnectClientIDSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) |
41 | 53 | { |
42 | 53 | if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0) |
43 | 1 | return -1; |
44 | | |
45 | 52 | if (DetectSignatureSetAppProto(s, ALPROTO_MQTT) < 0) |
46 | 48 | return -1; |
47 | | |
48 | 4 | return 0; |
49 | 52 | } |
50 | | |
51 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
52 | | const DetectEngineTransforms *transforms, |
53 | | Flow *_f, const uint8_t _flow_flags, |
54 | | void *txv, const int list_id) |
55 | 0 | { |
56 | 0 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
57 | 0 | if (buffer->inspect == NULL) { |
58 | 0 | const uint8_t *b = NULL; |
59 | 0 | uint32_t b_len = 0; |
60 | |
|
61 | 0 | if (rs_mqtt_tx_get_connect_clientid(txv, &b, &b_len) != 1) |
62 | 0 | return NULL; |
63 | 0 | if (b == NULL || b_len == 0) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len); |
67 | 0 | InspectionBufferApplyTransforms(buffer, transforms); |
68 | 0 | } |
69 | 0 | return buffer; |
70 | 0 | } |
71 | | |
72 | | void DetectMQTTConnectClientIDRegister(void) |
73 | 34 | { |
74 | | /* mqtt.connect.clientid sticky buffer */ |
75 | 34 | sigmatch_table[DETECT_AL_MQTT_CONNECT_CLIENTID].name = KEYWORD_NAME; |
76 | 34 | sigmatch_table[DETECT_AL_MQTT_CONNECT_CLIENTID].desc = "sticky buffer to match on the MQTT CONNECT client ID"; |
77 | 34 | sigmatch_table[DETECT_AL_MQTT_CONNECT_CLIENTID].url = "/rules/" KEYWORD_DOC; |
78 | 34 | sigmatch_table[DETECT_AL_MQTT_CONNECT_CLIENTID].Setup = DetectMQTTConnectClientIDSetup; |
79 | 34 | sigmatch_table[DETECT_AL_MQTT_CONNECT_CLIENTID].flags |= SIGMATCH_NOOPT; |
80 | | |
81 | 34 | DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_MQTT, |
82 | 34 | SIG_FLAG_TOSERVER, 0, |
83 | 34 | DetectEngineInspectBufferGeneric, GetData); |
84 | | |
85 | 34 | DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, |
86 | 34 | PrefilterGenericMpmRegister, GetData, ALPROTO_MQTT, |
87 | 34 | 1); |
88 | | |
89 | 34 | DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); |
90 | | |
91 | 34 | g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); |
92 | | |
93 | 34 | SCLogDebug("registering " BUFFER_NAME " rule option"); |
94 | 34 | } |