suricata
detect-snmp-community.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-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  * \file
20  *
21  * \author Pierre Chifflier <chifflier@wzdftpd.net>
22  *
23  * Set up of the "snmp.community" keyword to allow content
24  * inspections on the decoded snmp community.
25  */
26 
27 #include "suricata-common.h"
28 #include "conf.h"
29 #include "detect.h"
30 #include "detect-parse.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
35 #include "detect-snmp-community.h"
36 #include "app-layer-parser.h"
37 #include "rust.h"
38 
39 static int DetectSNMPCommunitySetup(DetectEngineCtx *, Signature *,
40  const char *);
41 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
42  const DetectEngineTransforms *transforms,
43  Flow *f, const uint8_t flow_flags,
44  void *txv, const int list_id);
45 #ifdef UNITTESTS
46 static void DetectSNMPCommunityRegisterTests(void);
47 #endif
48 static int g_snmp_rust_id = 0;
49 
51 {
52  sigmatch_table[DETECT_AL_SNMP_COMMUNITY].name = "snmp.community";
54  "SNMP content modifier to match on the SNMP community";
56  DetectSNMPCommunitySetup;
57 #ifdef UNITTESTS
58  sigmatch_table[DETECT_AL_SNMP_COMMUNITY].RegisterTests = DetectSNMPCommunityRegisterTests;
59 #endif
60  sigmatch_table[DETECT_AL_SNMP_COMMUNITY].url = "/rules/snmp-keywords.html#snmp-community";
61 
63 
64  /* register inspect engines */
65  DetectAppLayerInspectEngineRegister2("snmp.community",
68  DetectAppLayerMpmRegister2("snmp.community", SIG_FLAG_TOSERVER, 2,
70  DetectAppLayerInspectEngineRegister2("snmp.community",
73  DetectAppLayerMpmRegister2("snmp.community", SIG_FLAG_TOCLIENT, 2,
75 
76  DetectBufferTypeSetDescriptionByName("snmp.community", "SNMP Community identifier");
77 
78  g_snmp_rust_id = DetectBufferTypeGetByName("snmp.community");
79 }
80 
81 static int DetectSNMPCommunitySetup(DetectEngineCtx *de_ctx, Signature *s,
82  const char *str)
83 {
84  if (DetectBufferSetActiveList(s, g_snmp_rust_id) < 0)
85  return -1;
86 
88  return -1;
89 
90  return 0;
91 }
92 
93 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
94  const DetectEngineTransforms *transforms, Flow *f,
95  const uint8_t flow_flags, void *txv, const int list_id)
96 {
97  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
98  if (buffer->inspect == NULL) {
99  uint32_t data_len = 0;
100  const uint8_t *data = NULL;
101 
102  rs_snmp_tx_get_community(txv, &data, &data_len);
103  if (data == NULL || data_len == 0) {
104  return NULL;
105  }
106 
107  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
108  InspectionBufferApplyTransforms(buffer, transforms);
109  }
110 
111  return buffer;
112 }
113 
114 #ifdef UNITTESTS
116 #endif /* UNITTESTS */
DETECT_AL_SNMP_COMMUNITY
@ DETECT_AL_SNMP_COMMUNITY
Definition: detect-engine-register.h:269
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
SigTableElmt_::name
const char * name
Definition: detect.h:1201
DetectEngineTransforms
Definition: detect.h:378
InspectionBuffer
Definition: detect.h:344
Flow_
Flow data structure.
Definition: flow.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:676
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
rust.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:52
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
detect-engine-mpm.h
detect.h
app-layer-parser.h
DetectEngineInspectBufferGeneric
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.
Definition: detect-engine.c:1617
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:174
conf.h
detect-engine-content-inspection.h
DetectAppLayerMpmRegister2
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
Definition: detect-engine-mpm.c:89
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
str
#define str(s)
Definition: suricata-common.h:272
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:1091
DetectSNMPCommunityRegister
void DetectSNMPCommunityRegister(void)
Definition: detect-snmp-community.c:50
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
detect-snmp-community.c
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:857
detect-snmp-community.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193