suricata
detect-snmp-community.c
Go to the documentation of this file.
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 #include "util-unittest.h"
19 #include "util-unittest-helper.h"
20 #include "app-layer-parser.h"
21 #include "detect-engine.h"
22 #include "detect-parse.h"
23 #include "flow-util.h"
24 #include "stream-tcp.h"
25 #include "detect-engine-build.h"
26 #include "detect-engine-alert.h"
27 
28 static int DetectSNMPCommunityTest(void)
29 {
31  DetectEngineThreadCtx *det_ctx = NULL;
32  DetectEngineCtx *de_ctx = NULL;
33  Flow f;
34  Packet *p;
35  TcpSession tcp;
36  ThreadVars tv;
37  Signature *s;
38 
39  uint8_t request[] = {
40  0x30, 0x27, 0x02, 0x01, 0x01, 0x04, 0x0b, 0x5b,
41  0x52, 0x30, 0x5f, 0x43, 0x40, 0x63, 0x74, 0x69,
42  0x21, 0x5d, 0xa1, 0x15, 0x02, 0x04, 0x2b, 0x13,
43  0x3f, 0x85, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00,
44  0x30, 0x07, 0x30, 0x05, 0x06, 0x01, 0x01, 0x05,
45  0x00
46  };
47 
48  /* Setup flow. */
49  memset(&f, 0, sizeof(Flow));
50  memset(&tcp, 0, sizeof(TcpSession));
51  memset(&tv, 0, sizeof(ThreadVars));
52  p = UTHBuildPacket(request, sizeof(request), IPPROTO_UDP);
53  FLOW_INITIALIZE(&f);
55  f.protoctx = (void *)&tcp;
56  f.proto = IPPROTO_UDP;
58  f.flags |= FLOW_IPV4;
59  p->flow = &f;
62  StreamTcpInitConfig(true);
63 
66 
67  /* This rule should match. */
69  "alert snmp any any -> any any ("
70  "msg:\"SNMP Test Rule\"; "
71  "snmp.community; content:\"[R0_C@cti!]\"; "
72  "sid:1; rev:1;)");
73  FAIL_IF_NULL(s);
74 
75  /* This rule should not match. */
77  "alert snmp any any -> any any ("
78  "msg:\"SNMP Test Rule\"; "
79  "snmp.community; content:\"private\"; "
80  "sid:2; rev:1;)");
81  FAIL_IF_NULL(s);
82 
84  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
85 
86  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_SNMP,
87  STREAM_TOSERVER, request, sizeof(request));
88  FAIL_IF(r != 0);
89 
90  /* Check that we have app-layer state. */
92 
93  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
94  FAIL_IF(!PacketAlertCheck(p, 1));
96 
97  /* Cleanup. */
102  StreamTcpFreeConfig(true);
103  FLOW_DESTROY(&f);
104  UTHFreePacket(p);
105 
106  PASS;
107 }
108 
109 static void DetectSNMPCommunityRegisterTests(void)
110 {
111  UtRegisterTest("DetectSNMPCommunityTest",
112  DetectSNMPCommunityTest);
113 }
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1005
flow-util.h
stream-tcp.h
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_::proto
uint8_t proto
Definition: flow.h:371
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:141
Packet_::flags
uint32_t flags
Definition: decode.h:467
Flow_
Flow data structure.
Definition: flow.h:349
Flow_::protomap
uint8_t protomap
Definition: flow.h:443
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:832
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2586
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:313
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:221
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:337
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1824
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2602
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:461
Flow_::protoctx
void * protoctx
Definition: flow.h:439
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:96
util-unittest.h
util-unittest-helper.h
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:53
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:359
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1080
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:22
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
app-layer-parser.h
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2045
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t proto)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:97
Packet_
Definition: decode.h:430
detect-engine-build.h
detect-engine-alert.h
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1976
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:292
Packet_::flow
struct Flow_ * flow
Definition: decode.h:469
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3293
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool quiet)
Definition: stream-tcp.c:690
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1304
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3503
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:485
Flow_::alstate
void * alstate
Definition: flow.h:474
Flow_::flags
uint32_t flags
Definition: flow.h:419
detect-parse.h
Signature_
Signature container.
Definition: detect.h:587
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:223
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2547
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:66
TcpSession_
Definition: stream-tcp-private.h:283
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:448
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:121
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1002