suricata
detect-transform-strip-whitespace.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 Victor Julien <victor@inliniac.net>
22  *
23  * Implements the nocase keyword
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "detect.h"
29 #include "detect-engine.h"
31 #include "detect-parse.h"
33 
34 #include "util-unittest.h"
35 #include "util-print.h"
36 
37 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *, Signature *, const char *);
38 static void DetectTransformStripWhitespaceRegisterTests(void);
39 
40 static void TransformStripWhitespace(InspectionBuffer *buffer, void *options);
41 static bool TransformStripWhitespaceValidate(const uint8_t *content, uint16_t content_len, void *options);
42 
44 {
47  "modify buffer to strip whitespace before inspection";
49  "/rules/transforms.html#strip-whitespace";
51  TransformStripWhitespace;
53  TransformStripWhitespaceValidate;
55  DetectTransformStripWhitespaceSetup;
57  DetectTransformStripWhitespaceRegisterTests;
58 
60 }
61 
62 /**
63  * \internal
64  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
65  * \param det_ctx detection engine ctx
66  * \param s signature
67  * \param nullstr should be null
68  * \retval 0 ok
69  * \retval -1 failure
70  */
71 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
72 {
73  SCEnter();
75  SCReturnInt(r);
76 }
77 
78 /*
79  * \brief Validate content bytes to see if it's compatible with this transform
80  * \param content Byte array to check for compatibility
81  * \param content_len Number of bytes to check
82  * \param options Ignored
83  * \retval false If the string contains spaces
84  * \retval true Otherwise.
85  */
86 static bool TransformStripWhitespaceValidate(const uint8_t *content,
87  uint16_t content_len, void *options)
88 {
89  if (content) {
90  for (uint32_t i = 0; i < content_len; i++) {
91  if (isspace(*content++)) {
92  return false;
93  }
94  }
95  }
96  return true;
97 }
98 
99 static void TransformStripWhitespace(InspectionBuffer *buffer, void *options)
100 {
101  const uint8_t *input = buffer->inspect;
102  const uint32_t input_len = buffer->inspect_len;
103  uint8_t output[input_len]; // we can only shrink
104  uint8_t *oi = output, *os = output;
105 
106  //PrintRawDataFp(stdout, input, input_len);
107  for (uint32_t i = 0; i < input_len; i++) {
108  if (!isspace(*input)) {
109  *oi++ = *input;
110  }
111  input++;
112  }
113  uint32_t output_size = oi - os;
114  //PrintRawDataFp(stdout, output, output_size);
115 
116  InspectionBufferCopy(buffer, os, output_size);
117 }
118 
119 #ifdef UNITTESTS
120 static int TransformDoubleWhitespace(InspectionBuffer *buffer)
121 {
122  const uint8_t *input = buffer->inspect;
123  const uint32_t input_len = buffer->inspect_len;
124  uint8_t output[input_len * 2]; // if all chars are whitespace this fits
125  uint8_t *oi = output, *os = output;
126 
127  PrintRawDataFp(stdout, input, input_len);
128  for (uint32_t i = 0; i < input_len; i++) {
129  if (isspace(*input)) {
130  *oi++ = *input;
131  }
132  *oi++ = *input;
133  input++;
134  }
135  uint32_t output_size = oi - os;
136  PrintRawDataFp(stdout, output, output_size);
137 
138  InspectionBufferCopy(buffer, os, output_size);
139  return 0;
140 }
141 
142 static int DetectTransformStripWhitespaceTest01(void)
143 {
144  const uint8_t *input = (const uint8_t *)" A B C D ";
145  uint32_t input_len = strlen((char *)input);
146 
147  InspectionBuffer buffer;
148  InspectionBufferInit(&buffer, 8);
149  InspectionBufferSetup(&buffer, input, input_len);
150  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
151  TransformStripWhitespace(&buffer, NULL);
152  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
153  InspectionBufferFree(&buffer);
154  PASS;
155 }
156 
157 static int DetectTransformStripWhitespaceTest02(void)
158 {
159  const uint8_t *input = (const uint8_t *)" A B C D ";
160  uint32_t input_len = strlen((char *)input);
161 
162  InspectionBuffer buffer;
163  InspectionBufferInit(&buffer, 8);
164  InspectionBufferSetup(&buffer, input, input_len);
165  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
166  TransformDoubleWhitespace(&buffer);
167  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
168  TransformDoubleWhitespace(&buffer);
169  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
170  TransformStripWhitespace(&buffer, NULL);
171  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
172  InspectionBufferFree(&buffer);
173  PASS;
174 }
175 
176 static int DetectTransformStripWhitespaceTest03(void)
177 {
178  const char rule[] = "alert http any any -> any any (http_request_line; strip_whitespace; content:\"GET/HTTP\"; sid:1;)";
179  ThreadVars th_v;
180  DetectEngineThreadCtx *det_ctx = NULL;
181  memset(&th_v, 0, sizeof(th_v));
182 
186  FAIL_IF_NULL(s);
188  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
189  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
191  PASS;
192 }
193 
194 #endif
195 
196 static void DetectTransformStripWhitespaceRegisterTests(void)
197 {
198 #ifdef UNITTESTS
199  UtRegisterTest("DetectTransformStripWhitespaceTest01",
200  DetectTransformStripWhitespaceTest01);
201  UtRegisterTest("DetectTransformStripWhitespaceTest02",
202  DetectTransformStripWhitespaceTest02);
203  UtRegisterTest("DetectTransformStripWhitespaceTest03",
204  DetectTransformStripWhitespaceTest03);
205 #endif
206 }
SigTableElmt_::url
const char * url
Definition: detect.h:1212
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:1211
SigTableElmt_::name
const char * name
Definition: detect.h:1209
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
detect-transform-strip-whitespace.h
InspectionBuffer
Definition: detect.h:343
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1203
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2089
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
detect-engine-prefilter.h
util-unittest.h
DetectTransformStripWhitespaceRegister
void DetectTransformStripWhitespaceRegister(void)
Definition: detect-transform-strip-whitespace.c:43
InspectionBufferInit
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
Definition: detect-engine.c:1106
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, void *context)
Definition: detect.h:1192
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:1009
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
PrintRawDataFp
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1876
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1152
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:2326
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:2793
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3001
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1116
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1191
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:346
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:344
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2044
DetectSignatureAddTransform
int DetectSignatureAddTransform(Signature *s, int transform, void *options)
Definition: detect-parse.c:1443
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1378
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
DETECT_TRANSFORM_STRIP_WHITESPACE
@ DETECT_TRANSFORM_STRIP_WHITESPACE
Definition: detect-engine-register.h:261
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine.c:1123
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201