suricata
detect-rev.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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 rev 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-rev.h"
31 #include "util-byte.h"
32 #include "util-debug.h"
33 #include "util-error.h"
34 
35 static int DetectRevSetup (DetectEngineCtx *, Signature *, const char *);
36 #ifdef UNITTESTS
37 static void DetectRevRegisterTests(void);
38 #endif
39 
40 void DetectRevRegister (void)
41 {
43  sigmatch_table[DETECT_REV].desc = "set version of the rule";
44  sigmatch_table[DETECT_REV].url = "/rules/meta.html#rev-revision";
45  sigmatch_table[DETECT_REV].Setup = DetectRevSetup;
46 #ifdef UNITTESTS
47  sigmatch_table[DETECT_REV].RegisterTests = DetectRevRegisterTests;
48 #endif
49 }
50 
51 static int DetectRevSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
52 {
53  uint32_t rev = 0;
54  if (ByteExtractStringUint32(&rev, 10, strlen(rawstr), rawstr) <= 0) {
55  SCLogError("invalid input as arg to rev keyword");
56  goto error;
57  }
58  if (rev == 0) {
59  SCLogError("rev value 0 is invalid");
60  goto error;
61  }
62  if (s->rev > 0) {
63  SCLogError("duplicated 'rev' keyword detected");
64  goto error;
65  }
66 
67  s->rev = rev;
68  return 0;
69 
70 error:
71  return -1;
72 }
73 
74 #ifdef UNITTESTS
75 /**
76  * \test RevTestParse01 is a test for a valid rev value
77  */
78 static int RevTestParse01(void)
79 {
82 
83  Signature *s =
84  DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:1; rev:1;)");
85 
86  FAIL_IF_NULL(s);
87  FAIL_IF(s->rev != 1);
88 
90  PASS;
91 }
92 
93 /**
94  * \test RevTestParse02 is a test for an invalid rev value
95  */
96 static int RevTestParse02(void)
97 {
100 
102  DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:1; rev:a;)"));
103 
105  PASS;
106 }
107 
108 /**
109  * \test RevTestParse03 is a test for a rev containing of a single quote.
110  */
111 static int RevTestParse03(void)
112 {
115 
117  de_ctx, "alert tcp any any -> any any (content:\"ABC\"; rev:\";)"));
118 
120  PASS;
121 }
122 
123 /**
124  * \test RevTestParse04 is a test for a rev value of 0
125  */
126 static int RevTestParse04(void)
127 {
130 
132  de_ctx, "alert tcp any any -> any any (content:\"ABC\"; rev:0;)"));
133 
135  PASS;
136 }
137 
138 /**
139  * \brief this function registers unit tests for Rev
140  */
141 static void DetectRevRegisterTests(void)
142 {
143  UtRegisterTest("RevTestParse01", RevTestParse01);
144  UtRegisterTest("RevTestParse02", RevTestParse02);
145  UtRegisterTest("RevTestParse03", RevTestParse03);
146  UtRegisterTest("RevTestParse04", RevTestParse04);
147 }
148 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
DetectRevRegister
void DetectRevRegister(void)
Definition: detect-rev.c:40
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::name
const char * name
Definition: detect.h:1542
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
DETECT_REV
@ DETECT_REV
Definition: detect-engine-register.h:30
ByteExtractStringUint32
int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:194
detect-rev.h
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3965
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
detect.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
Signature_::rev
uint32_t rev
Definition: detect.h:743
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531