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 #ifdef UNITTESTS
39 static void DetectTransformStripWhitespaceRegisterTests(void);
40 #endif
41 static void TransformStripWhitespace(InspectionBuffer *buffer, void *options);
42 static bool TransformStripWhitespaceValidate(const uint8_t *content, uint16_t content_len, void *options);
43 
45 {
48  "modify buffer to strip whitespace before inspection";
50  "/rules/transforms.html#strip-whitespace";
52  TransformStripWhitespace;
54  TransformStripWhitespaceValidate;
56  DetectTransformStripWhitespaceSetup;
57 #ifdef UNITTESTS
59  DetectTransformStripWhitespaceRegisterTests;
60 #endif
62 }
63 
64 /**
65  * \internal
66  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
67  * \param det_ctx detection engine ctx
68  * \param s signature
69  * \param nullstr should be null
70  * \retval 0 ok
71  * \retval -1 failure
72  */
73 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
74 {
75  SCEnter();
77  SCReturnInt(r);
78 }
79 
80 /*
81  * \brief Validate content bytes to see if it's compatible with this transform
82  * \param content Byte array to check for compatibility
83  * \param content_len Number of bytes to check
84  * \param options Ignored
85  * \retval false If the string contains spaces
86  * \retval true Otherwise.
87  */
88 static bool TransformStripWhitespaceValidate(const uint8_t *content,
89  uint16_t content_len, void *options)
90 {
91  if (content) {
92  for (uint32_t i = 0; i < content_len; i++) {
93  if (isspace(*content++)) {
94  return false;
95  }
96  }
97  }
98  return true;
99 }
100 
101 static void TransformStripWhitespace(InspectionBuffer *buffer, void *options)
102 {
103  const uint8_t *input = buffer->inspect;
104  const uint32_t input_len = buffer->inspect_len;
105  uint8_t output[input_len]; // we can only shrink
106  uint8_t *oi = output, *os = output;
107 
108  //PrintRawDataFp(stdout, input, input_len);
109  for (uint32_t i = 0; i < input_len; i++) {
110  if (!isspace(*input)) {
111  *oi++ = *input;
112  }
113  input++;
114  }
115  uint32_t output_size = oi - os;
116  //PrintRawDataFp(stdout, output, output_size);
117 
118  InspectionBufferCopy(buffer, os, output_size);
119 }
120 
121 #ifdef UNITTESTS
122 static int TransformDoubleWhitespace(InspectionBuffer *buffer)
123 {
124  const uint8_t *input = buffer->inspect;
125  const uint32_t input_len = buffer->inspect_len;
126  uint8_t output[input_len * 2]; // if all chars are whitespace this fits
127  uint8_t *oi = output, *os = output;
128 
129  PrintRawDataFp(stdout, input, input_len);
130  for (uint32_t i = 0; i < input_len; i++) {
131  if (isspace(*input)) {
132  *oi++ = *input;
133  }
134  *oi++ = *input;
135  input++;
136  }
137  uint32_t output_size = oi - os;
138  PrintRawDataFp(stdout, output, output_size);
139 
140  InspectionBufferCopy(buffer, os, output_size);
141  return 0;
142 }
143 
144 static int DetectTransformStripWhitespaceTest01(void)
145 {
146  const uint8_t *input = (const uint8_t *)" A B C D ";
147  uint32_t input_len = strlen((char *)input);
148 
149  InspectionBuffer buffer;
150  InspectionBufferInit(&buffer, 8);
151  InspectionBufferSetup(&buffer, input, input_len);
152  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
153  TransformStripWhitespace(&buffer, NULL);
154  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
155  InspectionBufferFree(&buffer);
156  PASS;
157 }
158 
159 static int DetectTransformStripWhitespaceTest02(void)
160 {
161  const uint8_t *input = (const uint8_t *)" A B C D ";
162  uint32_t input_len = strlen((char *)input);
163 
164  InspectionBuffer buffer;
165  InspectionBufferInit(&buffer, 8);
166  InspectionBufferSetup(&buffer, input, input_len);
167  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
168  TransformDoubleWhitespace(&buffer);
169  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
170  TransformDoubleWhitespace(&buffer);
171  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
172  TransformStripWhitespace(&buffer, NULL);
173  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
174  InspectionBufferFree(&buffer);
175  PASS;
176 }
177 
178 static int DetectTransformStripWhitespaceTest03(void)
179 {
180  const char rule[] = "alert http any any -> any any (http_request_line; strip_whitespace; content:\"GET/HTTP\"; sid:1;)";
181  ThreadVars th_v;
182  DetectEngineThreadCtx *det_ctx = NULL;
183  memset(&th_v, 0, sizeof(th_v));
184 
188  FAIL_IF_NULL(s);
190  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
191  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
193  PASS;
194 }
195 
196 static void DetectTransformStripWhitespaceRegisterTests(void)
197 {
198  UtRegisterTest("DetectTransformStripWhitespaceTest01",
199  DetectTransformStripWhitespaceTest01);
200  UtRegisterTest("DetectTransformStripWhitespaceTest02",
201  DetectTransformStripWhitespaceTest02);
202  UtRegisterTest("DetectTransformStripWhitespaceTest03",
203  DetectTransformStripWhitespaceTest03);
204 }
205 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1214
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:1213
SigTableElmt_::name
const char * name
Definition: detect.h:1211
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:344
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
detect-engine-prefilter.h
util-unittest.h
DetectTransformStripWhitespaceRegister
void DetectTransformStripWhitespaceRegister(void)
Definition: detect-transform-strip-whitespace.c:44
InspectionBufferInit
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
Definition: detect-engine.c:1110
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, void *context)
Definition: detect.h:1193
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:1010
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:1888
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1156
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
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:2797
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3005
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:1120
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1192
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:347
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
DetectSignatureAddTransform
int DetectSignatureAddTransform(Signature *s, int transform, void *options)
Definition: detect-parse.c:1455
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1380
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
DETECT_TRANSFORM_STRIP_WHITESPACE
@ DETECT_TRANSFORM_STRIP_WHITESPACE
Definition: detect-engine-register.h:293
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine.c:1127
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203