suricata
detect-pkt-data.c
Go to the documentation of this file.
1 /* Copyright (C) 2012-2020 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 Xavier Lange <xrlange@gmail.com>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "threads.h"
27 #include "debug.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 #include "detect-pkt-data.h"
33 #include "detect-engine.h"
34 #include "detect-engine-mpm.h"
35 #include "detect-engine-state.h"
36 
37 #include "flow.h"
38 #include "flow-var.h"
39 #include "flow-util.h"
40 
41 #include "util-debug.h"
42 #include "util-spm-bm.h"
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 
46 static int DetectPktDataSetup (DetectEngineCtx *, Signature *, const char *);
47 #ifdef UNITTESTS
48 static void DetectPktDataTestRegister(void);
49 #endif
50 
51 /**
52  * \brief Registration function for keyword: file_data
53  */
55 {
56  sigmatch_table[DETECT_PKT_DATA].name = "pkt_data";
57  sigmatch_table[DETECT_PKT_DATA].Setup = DetectPktDataSetup;
58 #ifdef UNITTESTS
59  sigmatch_table[DETECT_PKT_DATA].RegisterTests = DetectPktDataTestRegister;
60 #endif
62 }
63 
64 /**
65  * \brief this function is used to parse pkt_data options
66  * \brief into the current signature
67  *
68  * \param de_ctx pointer to the Detection Engine Context
69  * \param s pointer to the current signature
70  * \param unused unused for keyword with SIGMATCH_NOOPT set
71  *
72  * \retval 0 on Success
73  * \retval -1 on Failure
74  */
75 static int DetectPktDataSetup (DetectEngineCtx *de_ctx, Signature *s, const char *unused)
76 {
77  SCEnter();
78  if (s->init_data->transforms.cnt) {
80  "previous transforms not consumed before 'pkt_data'");
81  SCReturnInt(-1);
82  }
84  SCReturnInt(0);
85 }
86 
87 #ifdef UNITTESTS
88 
89 /************************************Unittests*********************************/
90 static int g_file_data_buffer_id = 0;
91 
92 static int DetectPktDataTest01(void)
93 {
96  de_ctx->flags |= DE_QUIET;
97 
98  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
99  "(file_data; content:\"in file data\";"
100  " pkt_data; content:\"in pkt data\"; sid:1;)");
101  FAIL_IF_NULL(sig);
102 
103  SigMatch *sm = de_ctx->sig_list->sm_lists[g_file_data_buffer_id];
104  FAIL_IF_NULL(sm);
105 
106  sm = de_ctx->sig_list->sm_lists[DETECT_SM_LIST_PMATCH];
107  FAIL_IF_NULL(sm);
108 
110  FAIL_IF_NOT_NULL(sm->next);
111 
114  PASS;
115 }
116 
117 static int DetectPktDataTest02(void)
118 {
121  de_ctx->flags |= DE_QUIET;
122 
123  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
124  "(file_data; compress_whitespace; "
125  " pkt_data; content:\"in pkt data\"; sid:1;)");
126  FAIL_IF_NOT_NULL(sig);
128  PASS;
129 }
130 
131 static void DetectPktDataTestRegister(void)
132 {
133  g_file_data_buffer_id = DetectBufferTypeGetByName("file_data");
134  UtRegisterTest("DetectPktDataTest01", DetectPktDataTest01);
135  UtRegisterTest("DetectPktDataTest02", DetectPktDataTest02);
136 }
137 #endif
DETECT_PKT_DATA
@ DETECT_PKT_DATA
Definition: detect-engine-register.h:173
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:90
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1212
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:60
threads.h
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1206
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
DE_QUIET
#define DE_QUIET
Definition: detect.h:294
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1197
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
decode.h
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
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
SignatureInitData_::list
int list
Definition: detect.h:505
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:324
DetectPktDataRegister
void DetectPktDataRegister(void)
Registration function for keyword: file_data.
Definition: detect-pkt-data.c:54
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:597
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigMatch_::type
uint8_t type
Definition: detect.h:321
DETECT_SM_LIST_NOTSET
#define DETECT_SM_LIST_NOTSET
Definition: detect.h:115
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2361
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
util-spm-bm.h
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:773
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
detect-pkt-data.h
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
DetectEngineTransforms::cnt
int cnt
Definition: detect.h:378
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1381
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:768
SignatureInitData_::transforms
DetectEngineTransforms transforms
Definition: detect.h:508
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1204