41 static void DetectTransformFromBase64DecodeFree(
DetectEngineCtx *,
void *);
43 static void DetectTransformFromBase64DecodeRegisterTests(
void);
45 static void TransformFromBase64Decode(
InspectionBuffer *buffer,
void *options);
47 #define DETECT_TRANSFORM_FROM_BASE64_MODE_DEFAULT (uint8_t) Base64ModeRFC4648
59 DetectTransformFromBase64DecodeRegisterTests;
66 SCTransformBase64Free(ptr);
69 static SCDetectTransformFromBase64Data *DetectTransformFromBase64DecodeParse(
const char *
str)
71 SCDetectTransformFromBase64Data *tbd = SCTransformBase64Parse(
str);
87 static int DetectTransformFromBase64DecodeSetup(
94 SCDetectTransformFromBase64Data *b64d = DetectTransformFromBase64DecodeParse(opts_str);
98 if (b64d->flags & DETECT_TRANSFORM_BASE64_FLAG_OFFSET_VAR) {
99 SCLogError(
"offset value must be a value, not a variable name");
103 if (b64d->flags & DETECT_TRANSFORM_BASE64_FLAG_NBYTES_VAR) {
104 SCLogError(
"byte value must be a value, not a variable name");
112 DetectTransformFromBase64DecodeFree(
de_ctx, b64d);
116 static void TransformFromBase64Decode(
InspectionBuffer *buffer,
void *options)
118 SCDetectTransformFromBase64Data *b64d = options;
119 const uint8_t *input = buffer->
inspect;
121 uint32_t decode_length = input_len;
123 Base64Mode mode = b64d->mode;
124 uint32_t
offset = b64d->offset;
125 uint32_t nbytes = b64d->nbytes;
137 if (nbytes > decode_length) {
138 SCLogDebug(
"byte count %d plus offset %d exceeds length %d; returning", nbytes,
offset,
142 decode_length = nbytes;
144 if (decode_length == 0) {
148 uint32_t decoded_size = Base64DecodeBufferSize(decode_length);
149 uint8_t decoded[decoded_size];
150 uint32_t num_decoded = Base64Decode((
const uint8_t *)input, decode_length, mode, decoded);
151 if (num_decoded > 0) {
159 static int DetectTransformFromBase64DecodeTest01(
void)
161 const uint8_t *input = (
const uint8_t *)
"VGhpcyBpcyBTdXJpY2F0YQ==";
162 uint32_t input_len = strlen((
char *)input);
163 const char *result =
"This is Suricata";
164 uint32_t result_len = strlen((
char *)result);
165 SCDetectTransformFromBase64Data b64d = {
174 TransformFromBase64Decode(&buffer, &b64d);
183 static int DetectTransformFromBase64DecodeTest01a(
void)
185 const uint8_t *input = (
const uint8_t *)
"Zm 9v Ym Fy";
186 uint32_t input_len = strlen((
char *)input);
187 const char *result =
"foobar";
188 uint32_t result_len = strlen((
char *)result);
189 SCDetectTransformFromBase64Data b64d = { .nbytes = input_len, .mode = Base64ModeRFC2045 };
195 TransformFromBase64Decode(&buffer, &b64d);
204 static int DetectTransformFromBase64DecodeTest02(
void)
206 const uint8_t *input = (
const uint8_t *)
"This is Suricata\n";
207 uint32_t input_len = strlen((
char *)input);
208 SCDetectTransformFromBase64Data b64d = { .nbytes = input_len, .mode = Base64ModeStrict };
213 buffer_orig = buffer;
215 TransformFromBase64Decode(&buffer, &b64d);
224 static int DetectTransformFromBase64DecodeTest03(
void)
226 const uint8_t *input = (
const uint8_t *)
"VGhpcyBpcyBTdXJpY2F0YQ==";
227 uint32_t input_len = strlen((
char *)input);
229 SCDetectTransformFromBase64Data b64d = {
230 .nbytes = input_len + 1,
237 TransformFromBase64Decode(&buffer, &b64d);
238 FAIL_IF_NOT(strncmp((
const char *)input, (
const char *)buffer.
inspect, input_len) == 0);
245 static int DetectTransformFromBase64DecodeTest04(
void)
247 const uint8_t *input = (
const uint8_t *)
"VGhpcyBpcyBTdXJpY2F0YQ==";
248 uint32_t input_len = strlen((
char *)input);
250 SCDetectTransformFromBase64Data b64d = {
251 .offset = input_len + 1,
258 TransformFromBase64Decode(&buffer, &b64d);
259 FAIL_IF_NOT(strncmp((
const char *)input, (
const char *)buffer.
inspect, input_len) == 0);
266 static int DetectTransformFromBase64DecodeTest05(
void)
268 const uint8_t *input = (
const uint8_t *)
"VGhpcyBpcyBTdXJpY2F0YQ==";
269 uint32_t input_len = strlen((
char *)input);
270 const char *result =
"This is S";
271 uint32_t result_len = strlen((
char *)result);
273 SCDetectTransformFromBase64Data b64d = {
282 TransformFromBase64Decode(&buffer, &b64d);
291 static int DetectTransformFromBase64DecodeTest06(
void)
293 const uint8_t *input = (
const uint8_t *)
"VGhpcyBpcyBTdXJpY2F0YQ==";
294 uint32_t input_len = strlen((
char *)input);
295 const char *result =
"s is Suricata";
296 uint32_t result_len = strlen((
char *)result);
298 SCDetectTransformFromBase64Data b64d = {
307 TransformFromBase64Decode(&buffer, &b64d);
316 static int DetectTransformFromBase64DecodeTest07(
void)
319 const uint8_t *input = (
const uint8_t *)
"SGVs bG8 gV29y bGQ=";
320 uint32_t input_len = strlen((
char *)input);
321 const char *result =
"Hello Wor";
322 uint32_t result_len = strlen((
char *)result);
324 SCDetectTransformFromBase64Data b64d = { .nbytes = input_len - 4,
325 .mode = Base64ModeRFC2045 };
331 TransformFromBase64Decode(&buffer, &b64d);
340 static int DetectTransformFromBase64DecodeTest08(
void)
343 const uint8_t *input = (
const uint8_t *)
"This is not base64-encoded";
344 uint32_t input_len = strlen((
char *)input);
346 SCDetectTransformFromBase64Data b64d = { .nbytes = input_len, .mode = Base64ModeRFC2045 };
352 TransformFromBase64Decode(&buffer, &b64d);
358 static void DetectTransformFromBase64DecodeRegisterTests(
void)
360 UtRegisterTest(
"DetectTransformFromBase64DecodeTest01", DetectTransformFromBase64DecodeTest01);
362 "DetectTransformFromBase64DecodeTest01a", DetectTransformFromBase64DecodeTest01a);
363 UtRegisterTest(
"DetectTransformFromBase64DecodeTest02", DetectTransformFromBase64DecodeTest02);
364 UtRegisterTest(
"DetectTransformFromBase64DecodeTest03", DetectTransformFromBase64DecodeTest03);
365 UtRegisterTest(
"DetectTransformFromBase64DecodeTest04", DetectTransformFromBase64DecodeTest04);
366 UtRegisterTest(
"DetectTransformFromBase64DecodeTest05", DetectTransformFromBase64DecodeTest05);
367 UtRegisterTest(
"DetectTransformFromBase64DecodeTest06", DetectTransformFromBase64DecodeTest06);
368 UtRegisterTest(
"DetectTransformFromBase64DecodeTest07", DetectTransformFromBase64DecodeTest07);
369 UtRegisterTest(
"DetectTransformFromBase64DecodeTest08", DetectTransformFromBase64DecodeTest08);