/src/suricata7/src/detect-snmp-usm.c
Line | Count | Source |
1 | | /* Copyright (C) 2022 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 | | #include "suricata-common.h" |
19 | | #include "rust.h" |
20 | | #include "detect-snmp-usm.h" |
21 | | #include "detect-engine.h" |
22 | | #include "detect-engine-mpm.h" |
23 | | #include "detect-engine-prefilter.h" |
24 | | #include "detect-parse.h" |
25 | | |
26 | | static int g_buffer_id = 0; |
27 | | |
28 | | static int DetectSNMPUsmSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str) |
29 | 209 | { |
30 | 209 | if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0) |
31 | 2 | return -1; |
32 | | |
33 | 207 | if (DetectSignatureSetAppProto(s, ALPROTO_SNMP) != 0) |
34 | 2 | return -1; |
35 | | |
36 | 205 | return 0; |
37 | 207 | } |
38 | | |
39 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
40 | | const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, |
41 | | const int list_id) |
42 | 169 | { |
43 | 169 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
44 | 169 | if (buffer->inspect == NULL) { |
45 | 88 | uint32_t data_len = 0; |
46 | 88 | const uint8_t *data = NULL; |
47 | | |
48 | 88 | rs_snmp_tx_get_usm(txv, &data, &data_len); |
49 | 88 | if (data == NULL || data_len == 0) { |
50 | 6 | return NULL; |
51 | 6 | } |
52 | | |
53 | 82 | InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len); |
54 | 82 | InspectionBufferApplyTransforms(buffer, transforms); |
55 | 82 | } |
56 | | |
57 | 163 | return buffer; |
58 | 169 | } |
59 | | |
60 | | void DetectSNMPUsmRegister(void) |
61 | 34 | { |
62 | 34 | sigmatch_table[DETECT_AL_SNMP_USM].name = "snmp.usm"; |
63 | 34 | sigmatch_table[DETECT_AL_SNMP_USM].desc = "SNMP content modifier to match on the SNMP usm"; |
64 | 34 | sigmatch_table[DETECT_AL_SNMP_USM].Setup = DetectSNMPUsmSetup; |
65 | | |
66 | 34 | sigmatch_table[DETECT_AL_SNMP_USM].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; |
67 | | |
68 | | /* register inspect engines */ |
69 | 34 | DetectAppLayerInspectEngineRegister2("snmp.usm", ALPROTO_SNMP, SIG_FLAG_TOSERVER, 0, |
70 | 34 | DetectEngineInspectBufferGeneric, GetData); |
71 | 34 | DetectAppLayerMpmRegister2("snmp.usm", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, |
72 | 34 | GetData, ALPROTO_SNMP, 0); |
73 | 34 | DetectAppLayerInspectEngineRegister2("snmp.usm", ALPROTO_SNMP, SIG_FLAG_TOCLIENT, 0, |
74 | 34 | DetectEngineInspectBufferGeneric, GetData); |
75 | 34 | DetectAppLayerMpmRegister2("snmp.usm", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, |
76 | 34 | GetData, ALPROTO_SNMP, 0); |
77 | | |
78 | 34 | DetectBufferTypeSetDescriptionByName("snmp.usm", "SNMP USM"); |
79 | | |
80 | 34 | g_buffer_id = DetectBufferTypeGetByName("snmp.usm"); |
81 | 34 | } |