suricata
detect-sid.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 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 Victor Julien <victor@inliniac.net>
22 *
23 * Implements the sid keyword
24 */
25
26#include "suricata-common.h"
27#include "detect.h"
28#include "detect-engine.h"
29#include "detect-parse.h"
30#include "detect-sid.h"
31#include "util-byte.h"
32#include "util-debug.h"
33#include "util-error.h"
34#include "util-unittest.h"
35
36static int DetectSidSetup (DetectEngineCtx *, Signature *, const char *);
37#ifdef UNITTESTS
38static void DetectSidRegisterTests(void);
39#endif
40
42{
44 sigmatch_table[DETECT_SID].desc = "set rule ID";
45 sigmatch_table[DETECT_SID].url = "/rules/meta.html#sid-signature-id";
47 sigmatch_table[DETECT_SID].Setup = DetectSidSetup;
49#ifdef UNITTESTS
50 sigmatch_table[DETECT_SID].RegisterTests = DetectSidRegisterTests;
51#endif
52}
53
54static int DetectSidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *sidstr)
55{
56 uint32_t id = 0;
57 if (ByteExtractStringUint32(&id, 10, strlen(sidstr), sidstr) <= 0) {
58 SCLogError("invalid input as arg to sid keyword");
59 goto error;
60 }
61 if (id == 0) {
62 SCLogError("sid value 0 is invalid");
63 goto error;
64 }
65 if (s->id > 0) {
66 SCLogError("duplicated 'sid' keyword detected");
67 goto error;
68 }
69
70 s->id = id;
71 return 0;
72
73 error:
74 return -1;
75}
76
77#ifdef UNITTESTS
78
79static int SidTestParse01(void)
80{
83
84 Signature *s =
85 DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:1; gid:1;)");
86 FAIL_IF_NULL(s);
87 FAIL_IF(s->id != 1);
88
90 PASS;
91}
92
93static int SidTestParse02(void)
94{
97
99 DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:a; gid:1;)"));
100
102 PASS;
103}
104
105static int SidTestParse03(void)
106{
109
111 de_ctx, "alert tcp any any -> any any (content:\"ABC\"; sid:\";)"));
112
114 PASS;
115}
116
117static int SidTestParse04(void)
118{
121
123 de_ctx, "alert tcp any any -> any any (content:\"ABC\"; sid: 0;)"));
124
125 /* Let's also make sure that Suricata fails a rule which doesn't have a sid at all */
127 DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (content:\"ABC\";)"));
128
130 PASS;
131}
132
133/**
134 * \brief Register DetectSid unit tests.
135 */
136static void DetectSidRegisterTests(void)
137{
138 UtRegisterTest("SidTestParse01", SidTestParse01);
139 UtRegisterTest("SidTestParse02", SidTestParse02);
140 UtRegisterTest("SidTestParse03", SidTestParse03);
141 UtRegisterTest("SidTestParse04", SidTestParse04);
142}
143#endif /* UNITTESTS */
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
uint32_t id
SigTableElmt * sigmatch_table
void DetectSidRegister(void)
Definition detect-sid.c:41
#define SIGMATCH_SUPPORT_FIREWALL
Definition detect.h:1682
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
main detection engine ctx
Definition detect.h:932
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
uint32_t id
Definition detect.h:713
int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition util-byte.c:239
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267