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 
38 #include "rust-snmp-snmp-gen.h"
39 #include "rust-snmp-detect-gen.h"
40 
41 static int DetectSNMPCommunitySetup(DetectEngineCtx *, Signature *,
42  const char *);
43 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
44  const DetectEngineTransforms *transforms,
45  Flow *f, const uint8_t flow_flags,
46  void *txv, const int list_id);
47 #ifdef UNITTESTS
48 static void DetectSNMPCommunityRegisterTests(void);
49 #endif
50 static int g_snmp_rust_id = 0;
51 
53 {
54  sigmatch_table[DETECT_AL_SNMP_COMMUNITY].name = "snmp.community";
56  "SNMP content modifier to match on the SNMP community";
58  DetectSNMPCommunitySetup;
59 #ifdef UNITTESTS
60  sigmatch_table[DETECT_AL_SNMP_COMMUNITY].RegisterTests = DetectSNMPCommunityRegisterTests;
61 #endif
62  sigmatch_table[DETECT_AL_SNMP_COMMUNITY].url = DOC_URL DOC_VERSION "/rules/snmp-keywords.html#snmp-community";
63 
65 
66  /* register inspect engines */
67  DetectAppLayerInspectEngineRegister2("snmp.community",
70  DetectAppLayerMpmRegister2("snmp.community", SIG_FLAG_TOSERVER, 2,
72  DetectAppLayerInspectEngineRegister2("snmp.community",
75  DetectAppLayerMpmRegister2("snmp.community", SIG_FLAG_TOCLIENT, 2,
77 
78  DetectBufferTypeSetDescriptionByName("snmp.community", "SNMP Community identifier");
79 
80  g_snmp_rust_id = DetectBufferTypeGetByName("snmp.community");
81 }
82 
83 static int DetectSNMPCommunitySetup(DetectEngineCtx *de_ctx, Signature *s,
84  const char *str)
85 {
86  if (DetectBufferSetActiveList(s, g_snmp_rust_id) < 0)
87  return -1;
88 
90  return -1;
91 
92  return 0;
93 }
94 
95 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
96  const DetectEngineTransforms *transforms, Flow *f,
97  const uint8_t flow_flags, void *txv, const int list_id)
98 {
99  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
100  if (buffer->inspect == NULL) {
101  uint32_t data_len = 0;
102  const uint8_t *data = NULL;
103 
104  rs_snmp_tx_get_community(txv, &data, &data_len);
105  if (data == NULL || data_len == 0) {
106  return NULL;
107  }
108 
109  InspectionBufferSetup(buffer, data, data_len);
110  InspectionBufferApplyTransforms(buffer, transforms);
111  }
112 
113  return buffer;
114 }
115 
116 #ifdef UNITTESTS
118 #endif /* UNITTESTS */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
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
main detection engine ctx
Definition: detect.h:761
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1393
#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
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
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)
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1194
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1192
void DetectSNMPCommunityRegister(void)