Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/detect-byte-extract.c
Line
Count
Source (jump to first uncovered line)
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 Anoop Saldanha <anoopsaldanha@gmail.com>
22
 */
23
24
#include "suricata-common.h"
25
#include "threads.h"
26
#include "decode.h"
27
28
#include "detect.h"
29
#include "detect-parse.h"
30
#include "detect-engine.h"
31
#include "detect-engine-mpm.h"
32
#include "detect-engine-state.h"
33
#include "detect-content.h"
34
#include "detect-pcre.h"
35
#include "detect-bytejump.h"
36
#include "detect-bytetest.h"
37
#include "detect-byte-extract.h"
38
#include "detect-isdataat.h"
39
#include "detect-engine-build.h"
40
41
#include "app-layer-protos.h"
42
43
#include "flow.h"
44
#include "flow-var.h"
45
#include "flow-util.h"
46
47
#include "util-byte.h"
48
#include "util-debug.h"
49
#include "util-unittest.h"
50
#include "util-unittest-helper.h"
51
#include "util-spm.h"
52
53
/* the default value of endianness to be used, if none's specified */
54
3.64k
#define DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT DETECT_BYTE_EXTRACT_ENDIAN_BIG
55
56
/* the base to be used if string mode is specified.  These options would be
57
 * specified in DetectByteParseData->base */
58
4.66k
#define DETECT_BYTE_EXTRACT_BASE_NONE 0
59
210
#define DETECT_BYTE_EXTRACT_BASE_HEX  16
60
3.78k
#define DETECT_BYTE_EXTRACT_BASE_DEC  10
61
1.96k
#define DETECT_BYTE_EXTRACT_BASE_OCT   8
62
63
/* the default value for multiplier.  Either ways we always store a
64
 * multiplier, 1 or otherwise, so that we can always multiply the extracted
65
 * value and store it, instead of checking if a multiplier is set or not */
66
6.19k
#define DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT 1
67
/* the min/max limit for multiplier */
68
8
#define DETECT_BYTE_EXTRACT_MULTIPLIER_MIN_LIMIT 1
69
8
#define DETECT_BYTE_EXTRACT_MULTIPLIER_MAX_LIMIT 65535
70
71
/* the max no of bytes that can be extracted in string mode - (string, hex)
72
 * (string, oct) or (string, dec) */
73
10
#define STRING_MAX_BYTES_TO_EXTRACT_FOR_OCT 23
74
1.81k
#define STRING_MAX_BYTES_TO_EXTRACT_FOR_DEC 20
75
79
#define STRING_MAX_BYTES_TO_EXTRACT_FOR_HEX 14
76
/* the max no of bytes that can be extracted in non-string mode */
77
4.29k
#define NO_STRING_MAX_BYTES_TO_EXTRACT 8
78
79
73
#define PARSE_REGEX "^"                                                  \
80
73
    "\\s*([0-9]+)\\s*"                                                   \
81
73
    ",\\s*(-?[0-9]+)\\s*"                                               \
82
73
    ",\\s*([^\\s,]+)\\s*"                                                \
83
73
    "(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
84
73
    "(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
85
73
    "(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
86
73
    "(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
87
73
    "(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
88
73
    "$"
89
90
static DetectParseRegex parse_regex;
91
92
static int DetectByteExtractSetup(DetectEngineCtx *, Signature *, const char *);
93
#ifdef UNITTESTS
94
static void DetectByteExtractRegisterTests(void);
95
#endif
96
static void DetectByteExtractFree(DetectEngineCtx *, void *);
97
98
/**
99
 * \brief Registers the keyword handlers for the "byte_extract" keyword.
100
 */
101
void DetectByteExtractRegister(void)
102
73
{
103
73
    sigmatch_table[DETECT_BYTE_EXTRACT].name = "byte_extract";
104
73
    sigmatch_table[DETECT_BYTE_EXTRACT].desc = "extract <num of bytes> at a particular <offset> and store it in <var_name>";
105
73
    sigmatch_table[DETECT_BYTE_EXTRACT].url = "/rules/payload-keywords.html#byte-extract";
106
73
    sigmatch_table[DETECT_BYTE_EXTRACT].Match = NULL;
107
73
    sigmatch_table[DETECT_BYTE_EXTRACT].Setup = DetectByteExtractSetup;
108
73
    sigmatch_table[DETECT_BYTE_EXTRACT].Free = DetectByteExtractFree;
109
#ifdef UNITTESTS
110
    sigmatch_table[DETECT_BYTE_EXTRACT].RegisterTests = DetectByteExtractRegisterTests;
111
#endif
112
73
    DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
113
73
}
114
115
int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData *smd,
116
        const Signature *s, const uint8_t *payload, uint32_t payload_len, uint64_t *value,
117
        uint8_t endian)
118
85.6k
{
119
85.6k
    DetectByteExtractData *data = (DetectByteExtractData *)smd->ctx;
120
85.6k
    const uint8_t *ptr = NULL;
121
85.6k
    int32_t len = 0;
122
85.6k
    uint64_t val = 0;
123
85.6k
    int extbytes;
124
125
85.6k
    if (payload_len == 0) {
126
1.69k
        return 0;
127
1.69k
    }
128
129
    /* Calculate the ptr value for the bytetest and length remaining in
130
     * the packet from that point.
131
     */
132
83.9k
    if (data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE) {
133
41.0k
        SCLogDebug("relative, working with det_ctx->buffer_offset %"PRIu32", "
134
41.0k
                   "data->offset %"PRIu32"", det_ctx->buffer_offset, data->offset);
135
136
41.0k
        ptr = payload + det_ctx->buffer_offset;
137
41.0k
        len = payload_len - det_ctx->buffer_offset;
138
139
41.0k
        ptr += data->offset;
140
41.0k
        len -= data->offset;
141
142
        /* No match if there is no relative base */
143
41.0k
        if (len <= 0) {
144
3.94k
            return 0;
145
3.94k
        }
146
        //PrintRawDataFp(stdout,ptr,len);
147
42.9k
    } else {
148
42.9k
        SCLogDebug("absolute, data->offset %"PRIu32"", data->offset);
149
150
42.9k
        ptr = payload + data->offset;
151
42.9k
        len = payload_len - data->offset;
152
42.9k
    }
153
154
    /* Validate that the to-be-extracted is within the packet */
155
80.0k
    if (ptr < payload || data->nbytes > len) {
156
8.54k
        SCLogDebug("Data not within payload pkt=%p, ptr=%p, len=%"PRIu32", nbytes=%d",
157
8.54k
                    payload, ptr, len, data->nbytes);
158
8.54k
        return 0;
159
8.54k
    }
160
161
    /* Extract the byte data */
162
71.5k
    if (data->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) {
163
9.71k
        extbytes = ByteExtractStringUint64(&val, data->base,
164
9.71k
                                           data->nbytes, (const char *)ptr);
165
9.71k
        if (extbytes <= 0) {
166
            /* strtoull() return 0 if there is no numeric value in data string */
167
8.27k
            if (val == 0) {
168
8.26k
                SCLogDebug("No Numeric value");
169
8.26k
                return 0;
170
8.26k
            } else {
171
10
                SCLogDebug("error extracting %d bytes of string data: %d",
172
10
                        data->nbytes, extbytes);
173
10
                return -1;
174
10
            }
175
8.27k
        }
176
61.7k
    } else {
177
61.7k
        int endianness = (endian == DETECT_BYTE_EXTRACT_ENDIAN_BIG) ?
178
59.4k
                          BYTE_BIG_ENDIAN : BYTE_LITTLE_ENDIAN;
179
61.7k
        extbytes = ByteExtractUint64(&val, endianness, data->nbytes, ptr);
180
61.7k
        if (extbytes != data->nbytes) {
181
0
            SCLogDebug("error extracting %d bytes of numeric data: %d",
182
0
                    data->nbytes, extbytes);
183
0
            return 0;
184
0
        }
185
61.7k
    }
186
187
    /* Adjust the jump value based on flags */
188
63.2k
    val *= data->multiplier_value;
189
63.2k
    if (data->flags & DETECT_BYTE_EXTRACT_FLAG_ALIGN) {
190
0
        if ((val % data->align_value) != 0) {
191
0
            val += data->align_value - (val % data->align_value);
192
0
        }
193
0
    }
194
195
63.2k
    ptr += extbytes;
196
197
63.2k
    det_ctx->buffer_offset = ptr - payload;
198
199
63.2k
    *value = val;
200
63.2k
    SCLogDebug("extracted value is %"PRIu64, val);
201
63.2k
    return 1;
202
71.5k
}
203
204
/**
205
 * \internal
206
 * \brief Used to parse byte_extract arg.
207
 *
208
 * \param de_ctx Pointer to the detection engine context
209
 * \arg The argument to parse.
210
 *
211
 * \param bed On success an instance containing the parsed data.
212
 *            On failure, NULL.
213
 */
214
static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_ctx, const char *arg)
215
8.25k
{
216
8.25k
    DetectByteExtractData *bed = NULL;
217
8.25k
    int res = 0;
218
8.25k
    size_t pcre2len;
219
8.25k
    int i = 0;
220
8.25k
    pcre2_match_data *match = NULL;
221
222
8.25k
    int ret = DetectParsePcreExec(&parse_regex, &match, arg, 0, 0);
223
8.25k
    if (ret < 3 || ret > 19) {
224
558
        SCLogError("parse error, ret %" PRId32 ", string \"%s\"", ret, arg);
225
558
        SCLogError("Invalid arg to byte_extract : %s "
226
558
                   "for byte_extract",
227
558
                arg);
228
558
        goto error;
229
558
    }
230
231
7.70k
    bed = SCMalloc(sizeof(DetectByteExtractData));
232
7.70k
    if (unlikely(bed == NULL))
233
0
        goto error;
234
7.70k
    memset(bed, 0, sizeof(DetectByteExtractData));
235
236
    /* no of bytes to extract */
237
7.70k
    char nbytes_str[64] = "";
238
7.70k
    pcre2len = sizeof(nbytes_str);
239
7.70k
    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)nbytes_str, &pcre2len);
240
7.70k
    if (res < 0) {
241
1
        SCLogError("pcre2_substring_copy_bynumber failed "
242
1
                   "for arg 1 for byte_extract");
243
1
        goto error;
244
1
    }
245
7.69k
    if (StringParseUint8(&bed->nbytes, 10, 0,
246
7.69k
                               (const char *)nbytes_str) < 0) {
247
624
        SCLogError("Invalid value for number of bytes"
248
624
                   " to be extracted: \"%s\".",
249
624
                nbytes_str);
250
624
        goto error;
251
624
    }
252
253
    /* offset */
254
7.07k
    char offset_str[64] = "";
255
7.07k
    pcre2len = sizeof(offset_str);
256
7.07k
    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)offset_str, &pcre2len);
257
7.07k
    if (res < 0) {
258
2
        SCLogError("pcre2_substring_copy_bynumber failed "
259
2
                   "for arg 2 for byte_extract");
260
2
        goto error;
261
2
    }
262
7.07k
    int32_t offset;
263
7.07k
    if (StringParseI32RangeCheck(&offset, 10, 0, (const char *)offset_str, -65535, 65535) < 0) {
264
266
        SCLogError("Invalid value for offset: \"%s\".", offset_str);
265
266
        goto error;
266
266
    }
267
6.80k
    bed->offset = offset;
268
269
    /* var name */
270
6.80k
    char varname_str[256] = "";
271
6.80k
    pcre2len = sizeof(varname_str);
272
6.80k
    res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)varname_str, &pcre2len);
273
6.80k
    if (res < 0) {
274
11
        SCLogError("pcre2_substring_copy_bynumber failed "
275
11
                   "for arg 3 for byte_extract");
276
11
        goto error;
277
11
    }
278
6.79k
    bed->name = SCStrdup(varname_str);
279
6.79k
    if (bed->name == NULL)
280
0
        goto error;
281
282
    /* check out other optional args */
283
17.2k
    for (i = 4; i < ret; i++) {
284
11.0k
        char opt_str[64] = "";
285
11.0k
        pcre2len = sizeof(opt_str);
286
11.0k
        res = SC_Pcre2SubstringCopy(match, i, (PCRE2_UCHAR8 *)opt_str, &pcre2len);
287
11.0k
        if (res < 0) {
288
19
            SCLogError("pcre2_substring_copy_bynumber failed "
289
19
                       "for arg %d for byte_extract with %d",
290
19
                    i, res);
291
19
            goto error;
292
19
        }
293
294
11.0k
        if (strcmp("relative", opt_str) == 0) {
295
2.31k
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE) {
296
4
                SCLogError("relative specified more "
297
4
                           "than once for byte_extract");
298
4
                goto error;
299
4
            }
300
2.31k
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_RELATIVE;
301
8.70k
        } else if (strcmp("multiplier", opt_str) == 0) {
302
11
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_MULTIPLIER) {
303
1
                SCLogError("multiplier specified more "
304
1
                           "than once for byte_extract");
305
1
                goto error;
306
1
            }
307
10
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_MULTIPLIER;
308
10
            i++;
309
310
10
            char multiplier_str[16] = "";
311
10
            pcre2len = sizeof(multiplier_str);
312
10
            res = pcre2_substring_copy_bynumber(
313
10
                    match, i, (PCRE2_UCHAR8 *)multiplier_str, &pcre2len);
314
10
            if (res < 0) {
315
2
                SCLogError("pcre2_substring_copy_bynumber failed "
316
2
                           "for arg %d for byte_extract",
317
2
                        i);
318
2
                goto error;
319
2
            }
320
8
            uint16_t multiplier;
321
8
            if (StringParseU16RangeCheck(&multiplier, 10, 0, (const char *)multiplier_str,
322
8
                        DETECT_BYTE_EXTRACT_MULTIPLIER_MIN_LIMIT,
323
8
                        DETECT_BYTE_EXTRACT_MULTIPLIER_MAX_LIMIT) < 0) {
324
3
                SCLogError("Invalid value for"
325
3
                           "multiplier: \"%s\".",
326
3
                        multiplier_str);
327
3
                goto error;
328
3
            }
329
5
            bed->multiplier_value = multiplier;
330
8.69k
        } else if (strcmp("big", opt_str) == 0) {
331
37
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_ENDIAN) {
332
1
                SCLogError("endian option specified "
333
1
                           "more than once for byte_extract");
334
1
                goto error;
335
1
            }
336
36
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_ENDIAN;
337
36
            bed->endian = DETECT_BYTE_EXTRACT_ENDIAN_BIG;
338
8.66k
        } else if (strcmp("little", opt_str) == 0) {
339
315
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_ENDIAN) {
340
1
                SCLogError("endian option specified "
341
1
                           "more than once for byte_extract");
342
1
                goto error;
343
1
            }
344
314
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_ENDIAN;
345
314
            bed->endian = DETECT_BYTE_EXTRACT_ENDIAN_LITTLE;
346
8.34k
        } else if (strcmp("dce", opt_str) == 0) {
347
65
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_ENDIAN) {
348
2
                SCLogError("endian option specified "
349
2
                           "more than once for byte_extract");
350
2
                goto error;
351
2
            }
352
63
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_ENDIAN;
353
63
            bed->endian = DETECT_BYTE_EXTRACT_ENDIAN_DCE;
354
8.28k
        } else if (strcmp("string", opt_str) == 0) {
355
1.95k
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) {
356
3
                SCLogError("string specified more "
357
3
                           "than once for byte_extract");
358
3
                goto error;
359
3
            }
360
1.94k
            if (bed->base != DETECT_BYTE_EXTRACT_BASE_NONE) {
361
0
                SCLogError("The right way to specify "
362
0
                           "base is (string, base) and not (base, string) "
363
0
                           "for byte_extract");
364
0
                goto error;
365
0
            }
366
1.94k
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_STRING;
367
6.32k
        } else if (strcmp("hex", opt_str) == 0) {
368
82
            if (!(bed->flags & DETECT_BYTE_EXTRACT_FLAG_STRING)) {
369
2
                SCLogError("Base(hex) specified "
370
2
                           "without specifying string.  The right way is "
371
2
                           "(string, base) and not (base, string)");
372
2
                goto error;
373
2
            }
374
80
            if (bed->base != DETECT_BYTE_EXTRACT_BASE_NONE) {
375
1
                SCLogError("More than one base "
376
1
                           "specified for byte_extract");
377
1
                goto error;
378
1
            }
379
79
            bed->base = DETECT_BYTE_EXTRACT_BASE_HEX;
380
6.24k
        } else if (strcmp("oct", opt_str) == 0) {
381
11
            if (!(bed->flags & DETECT_BYTE_EXTRACT_FLAG_STRING)) {
382
1
                SCLogError("Base(oct) specified "
383
1
                           "without specifying string.  The right way is "
384
1
                           "(string, base) and not (base, string)");
385
1
                goto error;
386
1
            }
387
10
            if (bed->base != DETECT_BYTE_EXTRACT_BASE_NONE) {
388
0
                SCLogError("More than one base "
389
0
                           "specified for byte_extract");
390
0
                goto error;
391
0
            }
392
10
            bed->base = DETECT_BYTE_EXTRACT_BASE_OCT;
393
6.23k
        } else if (strcmp("dec", opt_str) == 0) {
394
734
            if (!(bed->flags & DETECT_BYTE_EXTRACT_FLAG_STRING)) {
395
16
                SCLogError("Base(dec) specified "
396
16
                           "without specifying string.  The right way is "
397
16
                           "(string, base) and not (base, string)");
398
16
                goto error;
399
16
            }
400
718
            if (bed->base != DETECT_BYTE_EXTRACT_BASE_NONE) {
401
16
                SCLogError("More than one base "
402
16
                           "specified for byte_extract");
403
16
                goto error;
404
16
            }
405
702
            bed->base = DETECT_BYTE_EXTRACT_BASE_DEC;
406
5.50k
        } else if (strcmp("align", opt_str) == 0) {
407
3
            if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_ALIGN) {
408
0
                SCLogError("Align specified more "
409
0
                           "than once for byte_extract");
410
0
                goto error;
411
0
            }
412
3
            bed->flags |= DETECT_BYTE_EXTRACT_FLAG_ALIGN;
413
3
            i++;
414
415
3
            char align_str[16] = "";
416
3
            pcre2len = sizeof(align_str);
417
3
            res = pcre2_substring_copy_bynumber(match, i, (PCRE2_UCHAR8 *)align_str, &pcre2len);
418
3
            if (res < 0) {
419
2
                SCLogError("pcre2_substring_copy_bynumber failed "
420
2
                           "for arg %d in byte_extract",
421
2
                        i);
422
2
                goto error;
423
2
            }
424
1
            if (StringParseUint8(&bed->align_value, 10, 0,
425
1
                                       (const char *)align_str) < 0) {
426
1
                SCLogError("Invalid align_value: "
427
1
                           "\"%s\".",
428
1
                        align_str);
429
1
                goto error;
430
1
            }
431
0
            if (!(bed->align_value == 2 || bed->align_value == 4)) {
432
0
                SCLogError("Invalid align_value for "
433
0
                           "byte_extract - \"%d\"",
434
0
                        bed->align_value);
435
0
                goto error;
436
0
            }
437
5.49k
        } else if (strcmp("", opt_str) == 0) {
438
4.97k
            ;
439
4.97k
        } else {
440
523
            SCLogError("Invalid option - \"%s\" "
441
523
                       "specified in byte_extract",
442
523
                    opt_str);
443
523
            goto error;
444
523
        }
445
11.0k
    } /* for (i = 4; i < ret; i++) */
446
447
    /* validation */
448
6.19k
    if (!(bed->flags & DETECT_BYTE_EXTRACT_FLAG_MULTIPLIER)) {
449
        /* default value */
450
6.19k
        bed->multiplier_value = DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT;
451
6.19k
    }
452
453
6.19k
    if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) {
454
1.90k
        if (bed->base == DETECT_BYTE_EXTRACT_BASE_NONE) {
455
            /* Default to decimal if base not specified. */
456
1.13k
            bed->base = DETECT_BYTE_EXTRACT_BASE_DEC;
457
1.13k
        }
458
1.90k
        if (bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE) {
459
2
            SCLogError("byte_extract can't have "
460
2
                       "endian \"big\" or \"little\" specified along with "
461
2
                       "\"string\"");
462
2
            goto error;
463
2
        }
464
1.90k
        if (bed->base == DETECT_BYTE_EXTRACT_BASE_OCT) {
465
            /* if are dealing with octal nos, the max no that can fit in a 8
466
             * byte value is 01777777777777777777777 */
467
10
            if (bed->nbytes > STRING_MAX_BYTES_TO_EXTRACT_FOR_OCT) {
468
1
                SCLogError("byte_extract can't process "
469
1
                           "more than %d bytes in \"string\" extraction",
470
1
                        STRING_MAX_BYTES_TO_EXTRACT_FOR_OCT);
471
1
                goto error;
472
1
            }
473
1.89k
        } else if (bed->base == DETECT_BYTE_EXTRACT_BASE_DEC) {
474
            /* if are dealing with decimal nos, the max no that can fit in a 8
475
             * byte value is 18446744073709551615 */
476
1.81k
            if (bed->nbytes > STRING_MAX_BYTES_TO_EXTRACT_FOR_DEC) {
477
1
                SCLogError("byte_extract can't process "
478
1
                           "more than %d bytes in \"string\" extraction",
479
1
                        STRING_MAX_BYTES_TO_EXTRACT_FOR_DEC);
480
1
                goto error;
481
1
            }
482
1.81k
        } else if (bed->base == DETECT_BYTE_EXTRACT_BASE_HEX) {
483
            /* if are dealing with hex nos, the max no that can fit in a 8
484
             * byte value is 0xFFFFFFFFFFFFFFFF */
485
79
            if (bed->nbytes > STRING_MAX_BYTES_TO_EXTRACT_FOR_HEX) {
486
1
                SCLogError("byte_extract can't process "
487
1
                           "more than %d bytes in \"string\" extraction",
488
1
                        STRING_MAX_BYTES_TO_EXTRACT_FOR_HEX);
489
1
                goto error;
490
1
            }
491
79
        } else {
492
0
            ; // just a placeholder.  we won't reach here.
493
0
        }
494
4.29k
    } else {
495
4.29k
        if (bed->nbytes > NO_STRING_MAX_BYTES_TO_EXTRACT) {
496
246
            SCLogError("byte_extract can't process "
497
246
                       "more than %d bytes in \"non-string\" extraction",
498
246
                    NO_STRING_MAX_BYTES_TO_EXTRACT);
499
246
            goto error;
500
246
        }
501
        /* if string has not been specified and no endian option has been
502
         * specified, then set the default endian level of BIG */
503
4.04k
        if (!(bed->flags & DETECT_BYTE_EXTRACT_FLAG_ENDIAN))
504
3.64k
            bed->endian = DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT;
505
4.04k
    }
506
507
5.94k
    pcre2_match_data_free(match);
508
509
5.94k
    return bed;
510
2.31k
 error:
511
2.31k
    if (bed != NULL)
512
1.75k
        DetectByteExtractFree(de_ctx, bed);
513
2.31k
    if (match) {
514
2.31k
        pcre2_match_data_free(match);
515
2.31k
    }
516
2.31k
    return NULL;
517
6.19k
}
518
519
/**
520
 * \brief The setup function for the byte_extract keyword for a signature.
521
 *
522
 * \param de_ctx Pointer to the detection engine context.
523
 * \param s      Pointer to signature for the current Signature being parsed
524
 *               from the rules.
525
 * \param m      Pointer to the head of the SigMatch for the current rule
526
 *               being parsed.
527
 * \param arg    Pointer to the string holding the keyword value.
528
 *
529
 * \retval  0 On success.
530
 * \retval -1 On failure.
531
 */
532
static int DetectByteExtractSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
533
8.25k
{
534
8.25k
    SigMatch *sm = NULL;
535
8.25k
    SigMatch *prev_pm = NULL;
536
8.25k
    DetectByteExtractData *data = NULL;
537
8.25k
    int ret = -1;
538
539
8.25k
    data = DetectByteExtractParse(de_ctx, arg);
540
8.25k
    if (data == NULL)
541
2.31k
        goto error;
542
543
5.94k
    int sm_list;
544
5.94k
    if (s->init_data->list != DETECT_SM_LIST_NOTSET) {
545
2.71k
        sm_list = s->init_data->list;
546
547
2.71k
        if (data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE) {
548
929
            prev_pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, DETECT_PCRE, -1);
549
929
        }
550
3.23k
    } else if (data->endian == DETECT_BYTE_EXTRACT_ENDIAN_DCE) {
551
55
        if (data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE) {
552
20
            prev_pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, DETECT_PCRE,
553
20
                    DETECT_BYTETEST, DETECT_BYTEJUMP, DETECT_BYTE_EXTRACT,
554
20
                    DETECT_BYTEMATH, DETECT_ISDATAAT, -1);
555
20
            if (prev_pm == NULL) {
556
10
                sm_list = DETECT_SM_LIST_PMATCH;
557
10
            } else {
558
10
                sm_list = SigMatchListSMBelongsTo(s, prev_pm);
559
10
                if (sm_list < 0)
560
0
                    goto error;
561
10
            }
562
35
        } else {
563
35
            sm_list = DETECT_SM_LIST_PMATCH;
564
35
        }
565
566
55
        if (DetectSignatureSetAppProto(s, ALPROTO_DCERPC) < 0)
567
3
            goto error;
568
52
        s->flags |= SIG_FLAG_APPLAYER;
569
570
3.17k
    } else if (data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE) {
571
1.33k
        prev_pm = DetectGetLastSMFromLists(s,
572
1.33k
                DETECT_CONTENT, DETECT_PCRE,
573
1.33k
                DETECT_BYTETEST, DETECT_BYTEJUMP, DETECT_BYTE_EXTRACT,
574
1.33k
                DETECT_BYTEMATH, DETECT_ISDATAAT, -1);
575
1.33k
        if (prev_pm == NULL) {
576
153
            sm_list = DETECT_SM_LIST_PMATCH;
577
1.18k
        } else {
578
1.18k
            sm_list = SigMatchListSMBelongsTo(s, prev_pm);
579
1.18k
            if (sm_list < 0)
580
0
                goto error;
581
1.18k
            if (sm_list != DETECT_SM_LIST_PMATCH)
582
463
                s->flags |= SIG_FLAG_APPLAYER;
583
1.18k
        }
584
585
1.84k
    } else {
586
1.84k
        sm_list = DETECT_SM_LIST_PMATCH;
587
1.84k
    }
588
589
5.94k
    if (data->endian == DETECT_BYTE_EXTRACT_ENDIAN_DCE) {
590
53
        if (DetectSignatureSetAppProto(s, ALPROTO_DCERPC) != 0)
591
1
            goto error;
592
593
52
        if ((data->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) ||
594
52
            (data->base == DETECT_BYTE_EXTRACT_BASE_DEC) ||
595
52
            (data->base == DETECT_BYTE_EXTRACT_BASE_HEX) ||
596
52
            (data->base == DETECT_BYTE_EXTRACT_BASE_OCT) ) {
597
0
            SCLogError("Invalid option. "
598
0
                       "A byte_jump keyword with dce holds other invalid modifiers.");
599
0
            goto error;
600
0
        }
601
52
    }
602
603
5.94k
    SigMatch *prev_bed_sm = DetectGetLastSMByListId(s, sm_list,
604
5.94k
            DETECT_BYTE_EXTRACT, -1);
605
5.94k
    if (prev_bed_sm == NULL)
606
2.43k
        data->local_id = 0;
607
3.50k
    else
608
3.50k
        data->local_id = ((DetectByteExtractData *)prev_bed_sm->ctx)->local_id + 1;
609
5.94k
    if (data->local_id > de_ctx->byte_extract_max_local_id)
610
434
        de_ctx->byte_extract_max_local_id = data->local_id;
611
612
613
5.94k
    sm = SigMatchAlloc();
614
5.94k
    if (sm == NULL)
615
0
        goto error;
616
5.94k
    sm->type = DETECT_BYTE_EXTRACT;
617
5.94k
    sm->ctx = (void *)data;
618
5.94k
    SigMatchAppendSMToList(s, sm, sm_list);
619
620
621
5.94k
    if (!(data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE))
622
3.66k
        goto okay;
623
624
2.28k
    if (prev_pm == NULL)
625
554
        goto okay;
626
627
1.72k
    if (prev_pm->type == DETECT_CONTENT) {
628
622
        DetectContentData *cd = (DetectContentData *)prev_pm->ctx;
629
622
        cd->flags |= DETECT_CONTENT_RELATIVE_NEXT;
630
1.10k
    } else if (prev_pm->type == DETECT_PCRE) {
631
313
        DetectPcreData *pd = (DetectPcreData *)prev_pm->ctx;
632
313
        pd->flags |= DETECT_PCRE_RELATIVE_NEXT;
633
313
    }
634
635
5.94k
 okay:
636
5.94k
    ret = 0;
637
5.94k
    return ret;
638
2.31k
 error:
639
2.31k
    DetectByteExtractFree(de_ctx, data);
640
2.31k
    return ret;
641
1.72k
}
642
643
/**
644
 * \brief Used to free instances of DetectByteExtractData.
645
 *
646
 * \param ptr Instance of DetectByteExtractData to be freed.
647
 */
648
static void DetectByteExtractFree(DetectEngineCtx *de_ctx, void *ptr)
649
10.0k
{
650
10.0k
    if (ptr != NULL) {
651
7.70k
        DetectByteExtractData *bed = ptr;
652
7.70k
        if (bed->name != NULL)
653
6.79k
            SCFree((void *)bed->name);
654
7.70k
        SCFree(bed);
655
7.70k
    }
656
657
10.0k
    return;
658
10.0k
}
659
660
/**
661
 * \brief Lookup the SigMatch for a named byte_extract variable.
662
 *
663
 * \param arg The name of the byte_extract variable to lookup.
664
 * \param s Pointer the signature to look in.
665
 *
666
 * \retval A pointer to the SigMatch if found, otherwise NULL.
667
 */
668
SigMatch *DetectByteExtractRetrieveSMVar(const char *arg, const Signature *s)
669
10.1k
{
670
19.0k
    for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
671
9.58k
        SigMatch *sm = s->init_data->buffers[x].head;
672
36.2k
        while (sm != NULL) {
673
27.2k
            if (sm->type == DETECT_BYTE_EXTRACT) {
674
1.50k
                const DetectByteExtractData *bed = (const DetectByteExtractData *)sm->ctx;
675
1.50k
                if (strcmp(bed->name, arg) == 0) {
676
634
                    return sm;
677
634
                }
678
1.50k
            }
679
26.6k
            sm = sm->next;
680
26.6k
        }
681
9.58k
    }
682
683
61.3k
    for (int list = 0; list < DETECT_SM_LIST_MAX; list++) {
684
54.3k
        SigMatch *sm = s->init_data->smlists[list];
685
87.2k
        while (sm != NULL) {
686
35.3k
            if (sm->type == DETECT_BYTE_EXTRACT) {
687
3.89k
                const DetectByteExtractData *bed = (const DetectByteExtractData *)sm->ctx;
688
3.89k
                if (strcmp(bed->name, arg) == 0) {
689
2.45k
                    return sm;
690
2.45k
                }
691
3.89k
            }
692
32.9k
            sm = sm->next;
693
32.9k
        }
694
54.3k
    }
695
696
7.03k
    return NULL;
697
9.48k
}
698
699
/*************************************Unittests********************************/
700
701
#ifdef UNITTESTS
702
703
static int g_file_data_buffer_id = 0;
704
static int g_http_uri_buffer_id = 0;
705
706
static int DetectByteExtractTest01(void)
707
{
708
    int result = 0;
709
710
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one");
711
    if (bed == NULL)
712
        goto end;
713
714
    if (bed->nbytes != 4 ||
715
        bed->offset != 2 ||
716
        strcmp(bed->name, "one") != 0 ||
717
        bed->flags != 0 ||
718
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
719
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
720
        bed->align_value != 0 ||
721
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
722
        goto end;
723
    }
724
725
    result = 1;
726
 end:
727
    if (bed != NULL)
728
        DetectByteExtractFree(NULL, bed);
729
    return result;
730
}
731
732
static int DetectByteExtractTest02(void)
733
{
734
    int result = 0;
735
736
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, relative");
737
    if (bed == NULL)
738
        goto end;
739
740
    if (bed->nbytes != 4 ||
741
        bed->offset != 2 ||
742
        strcmp(bed->name, "one") != 0 ||
743
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_RELATIVE ||
744
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
745
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
746
        bed->align_value != 0 ||
747
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
748
        goto end;
749
    }
750
751
    result = 1;
752
 end:
753
    if (bed != NULL)
754
        DetectByteExtractFree(NULL, bed);
755
    return result;
756
}
757
758
static int DetectByteExtractTest03(void)
759
{
760
    int result = 0;
761
762
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, multiplier 10");
763
    if (bed == NULL)
764
        goto end;
765
766
    if (bed->nbytes != 4 ||
767
        bed->offset != 2 ||
768
        strcmp(bed->name, "one") != 0 ||
769
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_MULTIPLIER ||
770
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
771
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
772
        bed->align_value != 0 ||
773
        bed->multiplier_value != 10) {
774
        goto end;
775
    }
776
777
    result = 1;
778
 end:
779
    if (bed != NULL)
780
        DetectByteExtractFree(NULL, bed);
781
    return result;
782
}
783
784
static int DetectByteExtractTest04(void)
785
{
786
    int result = 0;
787
788
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, relative, multiplier 10");
789
    if (bed == NULL)
790
        goto end;
791
792
    if (bed->nbytes != 4 ||
793
        bed->offset != 2 ||
794
        strcmp(bed->name, "one") != 0 ||
795
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
796
                       DETECT_BYTE_EXTRACT_FLAG_MULTIPLIER) ||
797
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
798
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
799
        bed->align_value != 0 ||
800
        bed->multiplier_value != 10) {
801
        goto end;
802
    }
803
804
    result = 1;
805
 end:
806
    if (bed != NULL)
807
        DetectByteExtractFree(NULL, bed);
808
    return result;
809
}
810
811
static int DetectByteExtractTest05(void)
812
{
813
    int result = 0;
814
815
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, big");
816
    if (bed == NULL)
817
        goto end;
818
819
    if (bed->nbytes != 4 ||
820
        bed->offset != 2 ||
821
        strcmp(bed->name, "one") != 0 ||
822
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_ENDIAN ||
823
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_BIG ||
824
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
825
        bed->align_value != 0 ||
826
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
827
        goto end;
828
    }
829
830
    result = 1;
831
 end:
832
    if (bed != NULL)
833
        DetectByteExtractFree(NULL, bed);
834
    return result;
835
}
836
837
static int DetectByteExtractTest06(void)
838
{
839
    int result = 0;
840
841
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, little");
842
    if (bed == NULL)
843
        goto end;
844
845
    if (bed->nbytes != 4 ||
846
        bed->offset != 2 ||
847
        strcmp(bed->name, "one") != 0 ||
848
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_ENDIAN ||
849
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_LITTLE ||
850
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
851
        bed->align_value != 0 ||
852
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
853
        goto end;
854
    }
855
856
    result = 1;
857
 end:
858
    if (bed != NULL)
859
        DetectByteExtractFree(NULL, bed);
860
    return result;
861
}
862
863
static int DetectByteExtractTest07(void)
864
{
865
    int result = 0;
866
867
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, dce");
868
    if (bed == NULL)
869
        goto end;
870
871
    if (bed->nbytes != 4 ||
872
        bed->offset != 2 ||
873
        strcmp(bed->name, "one") != 0 ||
874
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_ENDIAN ||
875
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DCE ||
876
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
877
        bed->align_value != 0 ||
878
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
879
        goto end;
880
    }
881
882
    result = 1;
883
 end:
884
    if (bed != NULL)
885
        DetectByteExtractFree(NULL, bed);
886
    return result;
887
}
888
889
static int DetectByteExtractTest08(void)
890
{
891
    int result = 0;
892
893
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, string, hex");
894
    if (bed == NULL)
895
        goto end;
896
897
    if (bed->nbytes != 4 ||
898
        bed->offset != 2 ||
899
        strcmp(bed->name, "one") != 0 ||
900
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
901
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
902
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
903
        bed->align_value != 0 ||
904
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
905
        goto end;
906
    }
907
908
    result = 1;
909
 end:
910
    if (bed != NULL)
911
        DetectByteExtractFree(NULL, bed);
912
    return result;
913
}
914
915
static int DetectByteExtractTest09(void)
916
{
917
    int result = 0;
918
919
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, string, oct");
920
    if (bed == NULL)
921
        goto end;
922
923
    if (bed->nbytes != 4 ||
924
        bed->offset != 2 ||
925
        strcmp(bed->name, "one") != 0 ||
926
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
927
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
928
        bed->base != DETECT_BYTE_EXTRACT_BASE_OCT ||
929
        bed->align_value != 0 ||
930
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
931
        goto end;
932
    }
933
934
    result = 1;
935
 end:
936
    if (bed != NULL)
937
        DetectByteExtractFree(NULL, bed);
938
    return result;
939
}
940
941
static int DetectByteExtractTest10(void)
942
{
943
    int result = 0;
944
945
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, string, dec");
946
    if (bed == NULL)
947
        goto end;
948
949
    if (bed->nbytes != 4 ||
950
        bed->offset != 2 ||
951
        strcmp(bed->name, "one") != 0 ||
952
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
953
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
954
        bed->base != DETECT_BYTE_EXTRACT_BASE_DEC ||
955
        bed->align_value != 0 ||
956
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
957
        goto end;
958
    }
959
960
    result = 1;
961
 end:
962
    if (bed != NULL)
963
        DetectByteExtractFree(NULL, bed);
964
    return result;
965
}
966
967
static int DetectByteExtractTest11(void)
968
{
969
    int result = 0;
970
971
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4");
972
    if (bed == NULL)
973
        goto end;
974
975
    if (bed->nbytes != 4 ||
976
        bed->offset != 2 ||
977
        strcmp(bed->name, "one") != 0 ||
978
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_ALIGN ||
979
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
980
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
981
        bed->align_value != 4 ||
982
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
983
        goto end;
984
    }
985
986
    result = 1;
987
 end:
988
    if (bed != NULL)
989
        DetectByteExtractFree(NULL, bed);
990
    return result;
991
}
992
993
static int DetectByteExtractTest12(void)
994
{
995
    int result = 0;
996
997
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, relative");
998
    if (bed == NULL)
999
        goto end;
1000
1001
    if (bed->nbytes != 4 ||
1002
        bed->offset != 2 ||
1003
        strcmp(bed->name, "one") != 0 ||
1004
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_ALIGN |
1005
                       DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
1006
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
1007
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
1008
        bed->align_value != 4 ||
1009
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1010
        goto end;
1011
    }
1012
1013
    result = 1;
1014
 end:
1015
    if (bed != NULL)
1016
        DetectByteExtractFree(NULL, bed);
1017
    return result;
1018
}
1019
1020
static int DetectByteExtractTest13(void)
1021
{
1022
    int result = 0;
1023
1024
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, relative, big");
1025
    if (bed == NULL)
1026
        goto end;
1027
1028
    if (bed->nbytes != 4 ||
1029
        bed->offset != 2 ||
1030
        strcmp(bed->name, "one") != 0 ||
1031
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_ALIGN |
1032
                       DETECT_BYTE_EXTRACT_FLAG_ENDIAN |
1033
                       DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
1034
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_BIG ||
1035
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
1036
        bed->align_value != 4 ||
1037
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1038
        goto end;
1039
    }
1040
1041
    result = 1;
1042
 end:
1043
    if (bed != NULL)
1044
        DetectByteExtractFree(NULL, bed);
1045
    return result;
1046
}
1047
1048
static int DetectByteExtractTest14(void)
1049
{
1050
    int result = 0;
1051
1052
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, relative, dce");
1053
    if (bed == NULL)
1054
        goto end;
1055
1056
    if (bed->nbytes != 4 ||
1057
        bed->offset != 2 ||
1058
        strcmp(bed->name, "one") != 0 ||
1059
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_ALIGN |
1060
                       DETECT_BYTE_EXTRACT_FLAG_ENDIAN |
1061
                       DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
1062
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DCE ||
1063
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
1064
        bed->align_value != 4 ||
1065
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1066
        goto end;
1067
    }
1068
1069
    result = 1;
1070
 end:
1071
    if (bed != NULL)
1072
        DetectByteExtractFree(NULL, bed);
1073
    return result;
1074
}
1075
1076
static int DetectByteExtractTest15(void)
1077
{
1078
    int result = 0;
1079
1080
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, relative, little");
1081
    if (bed == NULL)
1082
        goto end;
1083
1084
    if (bed->nbytes != 4 ||
1085
        bed->offset != 2 ||
1086
        strcmp(bed->name, "one") != 0 ||
1087
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_ALIGN |
1088
                       DETECT_BYTE_EXTRACT_FLAG_ENDIAN |
1089
                       DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
1090
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_LITTLE ||
1091
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
1092
        bed->align_value != 4 ||
1093
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1094
        goto end;
1095
    }
1096
1097
    result = 1;
1098
 end:
1099
    if (bed != NULL)
1100
        DetectByteExtractFree(NULL, bed);
1101
    return result;
1102
}
1103
1104
static int DetectByteExtractTest16(void)
1105
{
1106
    int result = 0;
1107
1108
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, relative, little, multiplier 2");
1109
    if (bed == NULL)
1110
        goto end;
1111
1112
    if (bed->nbytes != 4 ||
1113
        bed->offset != 2 ||
1114
        strcmp(bed->name, "one") != 0 ||
1115
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_ALIGN |
1116
                       DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
1117
                       DETECT_BYTE_EXTRACT_FLAG_ENDIAN |
1118
                       DETECT_BYTE_EXTRACT_FLAG_MULTIPLIER) ||
1119
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_LITTLE ||
1120
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
1121
        bed->align_value != 4 ||
1122
        bed->multiplier_value != 2) {
1123
        goto end;
1124
    }
1125
1126
    result = 1;
1127
 end:
1128
    if (bed != NULL)
1129
        DetectByteExtractFree(NULL, bed);
1130
    return result;
1131
}
1132
1133
static int DetectByteExtractTest17(void)
1134
{
1135
    int result = 0;
1136
1137
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1138
                                                        "relative, little, "
1139
                                                        "multiplier 2, string hex");
1140
    if (bed != NULL)
1141
        goto end;
1142
1143
    result = 1;
1144
 end:
1145
    if (bed != NULL)
1146
        DetectByteExtractFree(NULL, bed);
1147
    return result;
1148
}
1149
1150
static int DetectByteExtractTest18(void)
1151
{
1152
    int result = 0;
1153
1154
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1155
                                                        "relative, little, "
1156
                                                        "multiplier 2, "
1157
                                                        "relative");
1158
    if (bed != NULL)
1159
        goto end;
1160
1161
    result = 1;
1162
 end:
1163
    if (bed != NULL)
1164
        DetectByteExtractFree(NULL, bed);
1165
    return result;
1166
}
1167
1168
static int DetectByteExtractTest19(void)
1169
{
1170
    int result = 0;
1171
1172
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1173
                                                        "relative, little, "
1174
                                                        "multiplier 2, "
1175
                                                        "little");
1176
    if (bed != NULL)
1177
        goto end;
1178
1179
    result = 1;
1180
 end:
1181
    if (bed != NULL)
1182
        DetectByteExtractFree(NULL, bed);
1183
    return result;
1184
}
1185
1186
static int DetectByteExtractTest20(void)
1187
{
1188
    int result = 0;
1189
1190
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1191
                                                        "relative, "
1192
                                                        "multiplier 2, "
1193
                                                        "align 2");
1194
    if (bed != NULL)
1195
        goto end;
1196
1197
    result = 1;
1198
 end:
1199
    if (bed != NULL)
1200
        DetectByteExtractFree(NULL, bed);
1201
    return result;
1202
}
1203
1204
static int DetectByteExtractTest21(void)
1205
{
1206
    int result = 0;
1207
1208
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1209
                                                        "multiplier 2, "
1210
                                                        "relative, "
1211
                                                        "multiplier 2");
1212
    if (bed != NULL)
1213
        goto end;
1214
1215
    result = 1;
1216
 end:
1217
    if (bed != NULL)
1218
        DetectByteExtractFree(NULL, bed);
1219
    return result;
1220
}
1221
1222
static int DetectByteExtractTest22(void)
1223
{
1224
    int result = 0;
1225
1226
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1227
                                                        "string hex, "
1228
                                                        "relative, "
1229
                                                        "string hex");
1230
    if (bed != NULL)
1231
        goto end;
1232
1233
    result = 1;
1234
 end:
1235
    if (bed != NULL)
1236
        DetectByteExtractFree(NULL, bed);
1237
    return result;
1238
}
1239
1240
static int DetectByteExtractTest23(void)
1241
{
1242
    int result = 0;
1243
1244
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1245
                                                        "string hex, "
1246
                                                        "relative, "
1247
                                                        "string oct");
1248
    if (bed != NULL)
1249
        goto end;
1250
1251
    result = 1;
1252
 end:
1253
    if (bed != NULL)
1254
        DetectByteExtractFree(NULL, bed);
1255
    return result;
1256
}
1257
1258
static int DetectByteExtractTest24(void)
1259
{
1260
    int result = 0;
1261
1262
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "24, 2, one, align 4, "
1263
                                                        "string hex, "
1264
                                                        "relative");
1265
    if (bed != NULL)
1266
        goto end;
1267
1268
    result = 1;
1269
 end:
1270
    if (bed != NULL)
1271
        DetectByteExtractFree(NULL, bed);
1272
    return result;
1273
}
1274
1275
static int DetectByteExtractTest25(void)
1276
{
1277
    int result = 0;
1278
1279
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "9, 2, one, align 4, "
1280
                                                        "little, "
1281
                                                        "relative");
1282
    if (bed != NULL)
1283
        goto end;
1284
1285
    result = 1;
1286
 end:
1287
    if (bed != NULL)
1288
        DetectByteExtractFree(NULL, bed);
1289
    return result;
1290
}
1291
1292
static int DetectByteExtractTest26(void)
1293
{
1294
    int result = 0;
1295
1296
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1297
                                                        "little, "
1298
                                                        "relative, "
1299
                                                        "multiplier 65536");
1300
    if (bed != NULL)
1301
        goto end;
1302
1303
    result = 1;
1304
 end:
1305
    if (bed != NULL)
1306
        DetectByteExtractFree(NULL, bed);
1307
    return result;
1308
}
1309
1310
static int DetectByteExtractTest27(void)
1311
{
1312
    int result = 0;
1313
1314
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, align 4, "
1315
                                                        "little, "
1316
                                                        "relative, "
1317
                                                        "multiplier 0");
1318
    if (bed != NULL)
1319
        goto end;
1320
1321
    result = 1;
1322
 end:
1323
    if (bed != NULL)
1324
        DetectByteExtractFree(NULL, bed);
1325
    return result;
1326
}
1327
1328
static int DetectByteExtractTest28(void)
1329
{
1330
    int result = 0;
1331
1332
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "23, 2, one, string, oct");
1333
    if (bed == NULL)
1334
        goto end;
1335
1336
    result = 1;
1337
 end:
1338
    if (bed != NULL)
1339
        DetectByteExtractFree(NULL, bed);
1340
    return result;
1341
}
1342
1343
static int DetectByteExtractTest29(void)
1344
{
1345
    int result = 0;
1346
1347
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "24, 2, one, string, oct");
1348
    if (bed != NULL)
1349
        goto end;
1350
1351
    result = 1;
1352
 end:
1353
    if (bed != NULL)
1354
        DetectByteExtractFree(NULL, bed);
1355
    return result;
1356
}
1357
1358
static int DetectByteExtractTest30(void)
1359
{
1360
    int result = 0;
1361
1362
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "20, 2, one, string, dec");
1363
    if (bed == NULL)
1364
        goto end;
1365
1366
    result = 1;
1367
 end:
1368
    if (bed != NULL)
1369
        DetectByteExtractFree(NULL, bed);
1370
    return result;
1371
}
1372
1373
static int DetectByteExtractTest31(void)
1374
{
1375
    int result = 0;
1376
1377
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "21, 2, one, string, dec");
1378
    if (bed != NULL)
1379
        goto end;
1380
1381
    result = 1;
1382
 end:
1383
    if (bed != NULL)
1384
        DetectByteExtractFree(NULL, bed);
1385
    return result;
1386
}
1387
1388
static int DetectByteExtractTest32(void)
1389
{
1390
    int result = 0;
1391
1392
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "14, 2, one, string, hex");
1393
    if (bed == NULL)
1394
        goto end;
1395
1396
    result = 1;
1397
 end:
1398
    if (bed != NULL)
1399
        DetectByteExtractFree(NULL, bed);
1400
    return result;
1401
}
1402
1403
static int DetectByteExtractTest33(void)
1404
{
1405
    int result = 0;
1406
1407
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "15, 2, one, string, hex");
1408
    if (bed != NULL)
1409
        goto end;
1410
1411
    result = 1;
1412
 end:
1413
    if (bed != NULL)
1414
        DetectByteExtractFree(NULL, bed);
1415
    return result;
1416
}
1417
1418
static int DetectByteExtractTest34(void)
1419
{
1420
    DetectEngineCtx *de_ctx = NULL;
1421
    int result = 0;
1422
    Signature *s = NULL;
1423
    SigMatch *sm = NULL;
1424
    DetectContentData *cd = NULL;
1425
    DetectByteExtractData *bed = NULL;
1426
1427
    de_ctx = DetectEngineCtxInit();
1428
    if (de_ctx == NULL)
1429
        goto end;
1430
1431
    de_ctx->flags |= DE_QUIET;
1432
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1433
                                   "(msg:\"Testing bytejump_body\"; "
1434
                                   "content:\"one\"; "
1435
                                   "byte_extract:4,2,two,relative,string,hex; "
1436
                                   "sid:1;)");
1437
    if (de_ctx->sig_list == NULL) {
1438
        result = 0;
1439
        goto end;
1440
    }
1441
1442
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1443
        result = 0;
1444
        goto end;
1445
    }
1446
1447
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
1448
    if (sm->type != DETECT_CONTENT) {
1449
        result = 0;
1450
        goto end;
1451
    }
1452
    cd = (DetectContentData *)sm->ctx;
1453
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
1454
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
1455
        cd->flags & DETECT_CONTENT_NOCASE ||
1456
        cd->flags & DETECT_CONTENT_WITHIN ||
1457
        cd->flags & DETECT_CONTENT_DISTANCE ||
1458
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
1459
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
1460
        cd->flags & DETECT_CONTENT_NEGATED ) {
1461
        printf("one failed\n");
1462
        result = 0;
1463
        goto end;
1464
    }
1465
1466
    sm = sm->next;
1467
    if (sm->type != DETECT_BYTE_EXTRACT) {
1468
        result = 0;
1469
        goto end;
1470
    }
1471
    bed = (DetectByteExtractData *)sm->ctx;
1472
    if (bed->nbytes != 4 ||
1473
        bed->offset != 2 ||
1474
        strncmp(bed->name, "two", cd->content_len) != 0 ||
1475
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
1476
                       DETECT_BYTE_EXTRACT_FLAG_STRING) ||
1477
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
1478
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
1479
        bed->align_value != 0 ||
1480
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1481
        goto end;
1482
    }
1483
1484
    result = 1;
1485
1486
 end:
1487
    SigGroupCleanup(de_ctx);
1488
    SigCleanSignatures(de_ctx);
1489
    DetectEngineCtxFree(de_ctx);
1490
1491
    return result;
1492
}
1493
1494
static int DetectByteExtractTest35(void)
1495
{
1496
    DetectEngineCtx *de_ctx = NULL;
1497
    int result = 0;
1498
    Signature *s = NULL;
1499
    SigMatch *sm = NULL;
1500
    DetectContentData *cd = NULL;
1501
    DetectPcreData *pd = NULL;
1502
    DetectByteExtractData *bed = NULL;
1503
1504
    de_ctx = DetectEngineCtxInit();
1505
    if (de_ctx == NULL)
1506
        goto end;
1507
1508
    de_ctx->flags |= DE_QUIET;
1509
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1510
                                   "(msg:\"Testing bytejump_body\"; "
1511
                                   "content:\"one\"; pcre:/asf/; "
1512
                                   "byte_extract:4,0,two,relative,string,hex; "
1513
                                   "sid:1;)");
1514
    if (de_ctx->sig_list == NULL) {
1515
        result = 0;
1516
        goto end;
1517
    }
1518
1519
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1520
        result = 0;
1521
        goto end;
1522
    }
1523
1524
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
1525
    if (sm->type != DETECT_CONTENT) {
1526
        result = 0;
1527
        goto end;
1528
    }
1529
    cd = (DetectContentData *)sm->ctx;
1530
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
1531
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
1532
        cd->flags & DETECT_CONTENT_NOCASE ||
1533
        cd->flags & DETECT_CONTENT_WITHIN ||
1534
        cd->flags & DETECT_CONTENT_DISTANCE ||
1535
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
1536
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
1537
        cd->flags & DETECT_CONTENT_NEGATED ) {
1538
        printf("one failed\n");
1539
        result = 0;
1540
        goto end;
1541
    }
1542
1543
    sm = sm->next;
1544
    if (sm->type != DETECT_PCRE) {
1545
        result = 0;
1546
        goto end;
1547
    }
1548
    pd = (DetectPcreData *)sm->ctx;
1549
    if (pd->flags != DETECT_PCRE_RELATIVE_NEXT) {
1550
        result = 0;
1551
        goto end;
1552
    }
1553
1554
    sm = sm->next;
1555
    if (sm->type != DETECT_BYTE_EXTRACT) {
1556
        result = 0;
1557
        goto end;
1558
    }
1559
    bed = (DetectByteExtractData *)sm->ctx;
1560
    if (bed->nbytes != 4 ||
1561
        bed->offset != 0 ||
1562
        strcmp(bed->name, "two") != 0 ||
1563
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
1564
                       DETECT_BYTE_EXTRACT_FLAG_STRING) ||
1565
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
1566
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
1567
        bed->align_value != 0 ||
1568
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1569
        goto end;
1570
    }
1571
1572
    result = 1;
1573
1574
 end:
1575
    SigGroupCleanup(de_ctx);
1576
    SigCleanSignatures(de_ctx);
1577
    DetectEngineCtxFree(de_ctx);
1578
1579
    return result;
1580
}
1581
1582
static int DetectByteExtractTest36(void)
1583
{
1584
    DetectEngineCtx *de_ctx = NULL;
1585
    int result = 0;
1586
    Signature *s = NULL;
1587
    SigMatch *sm = NULL;
1588
    DetectContentData *cd = NULL;
1589
    DetectBytejumpData *bjd = NULL;
1590
    DetectByteExtractData *bed = NULL;
1591
1592
    de_ctx = DetectEngineCtxInit();
1593
    if (de_ctx == NULL)
1594
        goto end;
1595
1596
    de_ctx->flags |= DE_QUIET;
1597
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1598
                                   "(msg:\"Testing bytejump_body\"; "
1599
                                   "content:\"one\"; byte_jump:1,13; "
1600
                                   "byte_extract:4,0,two,relative,string,hex; "
1601
                                   "sid:1;)");
1602
    if (de_ctx->sig_list == NULL) {
1603
        result = 0;
1604
        goto end;
1605
    }
1606
1607
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1608
        result = 0;
1609
        goto end;
1610
    }
1611
1612
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
1613
    if (sm->type != DETECT_CONTENT) {
1614
        result = 0;
1615
        goto end;
1616
    }
1617
    cd = (DetectContentData *)sm->ctx;
1618
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
1619
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
1620
        cd->flags & DETECT_CONTENT_NOCASE ||
1621
        cd->flags & DETECT_CONTENT_WITHIN ||
1622
        cd->flags & DETECT_CONTENT_DISTANCE ||
1623
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
1624
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
1625
        cd->flags & DETECT_CONTENT_NEGATED ) {
1626
        printf("one failed\n");
1627
        result = 0;
1628
        goto end;
1629
    }
1630
1631
    sm = sm->next;
1632
    if (sm->type != DETECT_BYTEJUMP) {
1633
        result = 0;
1634
        goto end;
1635
    }
1636
    bjd = (DetectBytejumpData *)sm->ctx;
1637
    if (bjd->flags != 0) {
1638
        result = 0;
1639
        goto end;
1640
    }
1641
1642
    sm = sm->next;
1643
    if (sm->type != DETECT_BYTE_EXTRACT) {
1644
        result = 0;
1645
        goto end;
1646
    }
1647
    bed = (DetectByteExtractData *)sm->ctx;
1648
    if (bed->nbytes != 4 ||
1649
        bed->offset != 0 ||
1650
        strcmp(bed->name, "two") != 0 ||
1651
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
1652
                       DETECT_BYTE_EXTRACT_FLAG_STRING) ||
1653
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
1654
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
1655
        bed->align_value != 0 ||
1656
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1657
        goto end;
1658
    }
1659
1660
    result = 1;
1661
1662
 end:
1663
    SigGroupCleanup(de_ctx);
1664
    SigCleanSignatures(de_ctx);
1665
    DetectEngineCtxFree(de_ctx);
1666
1667
    return result;
1668
}
1669
1670
static int DetectByteExtractTest37(void)
1671
{
1672
    DetectEngineCtx *de_ctx = NULL;
1673
    int result = 0;
1674
    Signature *s = NULL;
1675
    SigMatch *sm = NULL;
1676
    DetectContentData *cd = NULL;
1677
    DetectContentData *ud = NULL;
1678
    DetectByteExtractData *bed = NULL;
1679
1680
    de_ctx = DetectEngineCtxInit();
1681
    if (de_ctx == NULL)
1682
        goto end;
1683
1684
    de_ctx->flags |= DE_QUIET;
1685
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1686
                                   "(msg:\"Testing bytejump_body\"; "
1687
                                   "content:\"one\"; uricontent:\"two\"; "
1688
                                   "byte_extract:4,0,two,relative,string,hex; "
1689
                                   "sid:1;)");
1690
    if (de_ctx->sig_list == NULL) {
1691
        result = 0;
1692
        goto end;
1693
    }
1694
1695
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1696
        result = 0;
1697
        goto end;
1698
    }
1699
1700
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
1701
    if (sm->type != DETECT_CONTENT) {
1702
        result = 0;
1703
        goto end;
1704
    }
1705
    cd = (DetectContentData *)sm->ctx;
1706
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
1707
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
1708
        cd->flags & DETECT_CONTENT_NOCASE ||
1709
        cd->flags & DETECT_CONTENT_WITHIN ||
1710
        cd->flags & DETECT_CONTENT_DISTANCE ||
1711
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
1712
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
1713
        cd->flags & DETECT_CONTENT_NEGATED ) {
1714
        printf("one failed\n");
1715
        result = 0;
1716
        goto end;
1717
    }
1718
1719
    if (sm->next != NULL) {
1720
        result = 0;
1721
        goto end;
1722
    }
1723
1724
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
1725
    if (sm->type != DETECT_CONTENT) {
1726
        result = 0;
1727
        goto end;
1728
    }
1729
    ud = (DetectContentData *)sm->ctx;
1730
    if (ud->flags & DETECT_CONTENT_RAWBYTES ||
1731
        strncmp((char *)ud->content, "two", cd->content_len) != 0 ||
1732
        ud->flags & DETECT_CONTENT_NOCASE ||
1733
        ud->flags & DETECT_CONTENT_WITHIN ||
1734
        ud->flags & DETECT_CONTENT_DISTANCE ||
1735
        ud->flags & DETECT_CONTENT_FAST_PATTERN ||
1736
        !(ud->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
1737
        ud->flags & DETECT_CONTENT_NEGATED ) {
1738
        printf("two failed\n");
1739
        result = 0;
1740
        goto end;
1741
    }
1742
1743
    sm = sm->next;
1744
    if (sm->type != DETECT_BYTE_EXTRACT) {
1745
        result = 0;
1746
        goto end;
1747
    }
1748
    bed = (DetectByteExtractData *)sm->ctx;
1749
    if (bed->nbytes != 4 ||
1750
        bed->offset != 0 ||
1751
        strcmp(bed->name, "two") != 0 ||
1752
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
1753
                       DETECT_BYTE_EXTRACT_FLAG_STRING) ||
1754
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
1755
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
1756
        bed->align_value != 0 ||
1757
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1758
        goto end;
1759
    }
1760
1761
    result = 1;
1762
1763
 end:
1764
    SigGroupCleanup(de_ctx);
1765
    SigCleanSignatures(de_ctx);
1766
    DetectEngineCtxFree(de_ctx);
1767
1768
    return result;
1769
}
1770
1771
static int DetectByteExtractTest38(void)
1772
{
1773
    DetectEngineCtx *de_ctx = NULL;
1774
    int result = 0;
1775
    Signature *s = NULL;
1776
    SigMatch *sm = NULL;
1777
    DetectContentData *cd = NULL;
1778
    DetectContentData *ud = NULL;
1779
    DetectByteExtractData *bed = NULL;
1780
1781
    de_ctx = DetectEngineCtxInit();
1782
    if (de_ctx == NULL)
1783
        goto end;
1784
1785
    de_ctx->flags |= DE_QUIET;
1786
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1787
                                   "(msg:\"Testing bytejump_body\"; "
1788
                                   "content:\"one\"; uricontent:\"two\"; "
1789
                                   "byte_extract:4,0,two,string,hex; "
1790
                                   "sid:1;)");
1791
    if (de_ctx->sig_list == NULL) {
1792
        result = 0;
1793
        goto end;
1794
    }
1795
1796
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1797
        result = 0;
1798
        goto end;
1799
    }
1800
1801
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
1802
    if (sm->type != DETECT_CONTENT) {
1803
        result = 0;
1804
        goto end;
1805
    }
1806
    cd = (DetectContentData *)sm->ctx;
1807
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
1808
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
1809
        cd->flags & DETECT_CONTENT_NOCASE ||
1810
        cd->flags & DETECT_CONTENT_WITHIN ||
1811
        cd->flags & DETECT_CONTENT_DISTANCE ||
1812
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
1813
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
1814
        cd->flags & DETECT_CONTENT_NEGATED ) {
1815
        printf("one failed\n");
1816
        result = 0;
1817
        goto end;
1818
    }
1819
1820
    sm = sm->next;
1821
    if (sm->type != DETECT_BYTE_EXTRACT) {
1822
        result = 0;
1823
        goto end;
1824
    }
1825
    bed = (DetectByteExtractData *)sm->ctx;
1826
    if (bed->nbytes != 4 ||
1827
        bed->offset != 0 ||
1828
        strcmp(bed->name, "two") != 0 ||
1829
        bed->flags !=DETECT_BYTE_EXTRACT_FLAG_STRING ||
1830
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
1831
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
1832
        bed->align_value != 0 ||
1833
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1834
        goto end;
1835
    }
1836
1837
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
1838
    if (sm->type != DETECT_CONTENT) {
1839
        result = 0;
1840
        goto end;
1841
    }
1842
    ud = (DetectContentData *)sm->ctx;
1843
    if (ud->flags & DETECT_CONTENT_RAWBYTES ||
1844
        strncmp((char *)ud->content, "two", cd->content_len) != 0 ||
1845
        ud->flags & DETECT_CONTENT_NOCASE ||
1846
        ud->flags & DETECT_CONTENT_WITHIN ||
1847
        ud->flags & DETECT_CONTENT_DISTANCE ||
1848
        ud->flags & DETECT_CONTENT_FAST_PATTERN ||
1849
        ud->flags & DETECT_CONTENT_RELATIVE_NEXT ||
1850
        ud->flags & DETECT_CONTENT_NEGATED ) {
1851
        printf("two failed\n");
1852
        result = 0;
1853
        goto end;
1854
    }
1855
1856
    if (sm->next != NULL) {
1857
        result = 0;
1858
        goto end;
1859
    }
1860
1861
    result = 1;
1862
1863
 end:
1864
    SigGroupCleanup(de_ctx);
1865
    SigCleanSignatures(de_ctx);
1866
    DetectEngineCtxFree(de_ctx);
1867
1868
    return result;
1869
}
1870
1871
static int DetectByteExtractTest39(void)
1872
{
1873
    DetectEngineCtx *de_ctx = NULL;
1874
    int result = 0;
1875
    Signature *s = NULL;
1876
    SigMatch *sm = NULL;
1877
    DetectContentData *cd = NULL;
1878
    DetectContentData *ud = NULL;
1879
    DetectByteExtractData *bed = NULL;
1880
1881
    de_ctx = DetectEngineCtxInit();
1882
    if (de_ctx == NULL)
1883
        goto end;
1884
1885
    de_ctx->flags |= DE_QUIET;
1886
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1887
                                   "(msg:\"Testing bytejump_body\"; "
1888
                                   "content:\"one\"; content:\"two\"; http_uri; "
1889
                                   "byte_extract:4,0,two,relative,string,hex; "
1890
                                   "sid:1;)");
1891
    if (de_ctx->sig_list == NULL) {
1892
        result = 0;
1893
        goto end;
1894
    }
1895
1896
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1897
        result = 0;
1898
        goto end;
1899
    }
1900
1901
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
1902
    if (sm->type != DETECT_CONTENT) {
1903
        result = 0;
1904
        goto end;
1905
    }
1906
    cd = (DetectContentData *)sm->ctx;
1907
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
1908
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
1909
        cd->flags & DETECT_CONTENT_NOCASE ||
1910
        cd->flags & DETECT_CONTENT_WITHIN ||
1911
        cd->flags & DETECT_CONTENT_DISTANCE ||
1912
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
1913
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
1914
        cd->flags & DETECT_CONTENT_NEGATED ) {
1915
        printf("one failed\n");
1916
        result = 0;
1917
        goto end;
1918
    }
1919
1920
    if (sm->next != NULL) {
1921
        result = 0;
1922
        goto end;
1923
    }
1924
1925
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
1926
    if (sm->type != DETECT_CONTENT) {
1927
        result = 0;
1928
        goto end;
1929
    }
1930
    ud = (DetectContentData *)sm->ctx;
1931
    if (ud->flags & DETECT_CONTENT_RAWBYTES ||
1932
        strncmp((char *)ud->content, "two", cd->content_len) != 0 ||
1933
        ud->flags & DETECT_CONTENT_NOCASE ||
1934
        ud->flags & DETECT_CONTENT_WITHIN ||
1935
        ud->flags & DETECT_CONTENT_DISTANCE ||
1936
        ud->flags & DETECT_CONTENT_FAST_PATTERN ||
1937
        !(ud->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
1938
        ud->flags & DETECT_CONTENT_NEGATED ) {
1939
        printf("two failed\n");
1940
        result = 0;
1941
        goto end;
1942
    }
1943
1944
    sm = sm->next;
1945
    if (sm->type != DETECT_BYTE_EXTRACT) {
1946
        result = 0;
1947
        goto end;
1948
    }
1949
    bed = (DetectByteExtractData *)sm->ctx;
1950
    if (bed->nbytes != 4 ||
1951
        bed->offset != 0 ||
1952
        strcmp(bed->name, "two") != 0 ||
1953
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
1954
                       DETECT_BYTE_EXTRACT_FLAG_STRING) ||
1955
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
1956
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
1957
        bed->align_value != 0 ||
1958
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
1959
        goto end;
1960
    }
1961
1962
    result = 1;
1963
1964
 end:
1965
    SigGroupCleanup(de_ctx);
1966
    SigCleanSignatures(de_ctx);
1967
    DetectEngineCtxFree(de_ctx);
1968
1969
    return result;
1970
}
1971
1972
static int DetectByteExtractTest40(void)
1973
{
1974
    DetectEngineCtx *de_ctx = NULL;
1975
    int result = 0;
1976
    Signature *s = NULL;
1977
    SigMatch *sm = NULL;
1978
    DetectContentData *cd = NULL;
1979
    DetectContentData *ud = NULL;
1980
    DetectByteExtractData *bed = NULL;
1981
1982
    de_ctx = DetectEngineCtxInit();
1983
    if (de_ctx == NULL)
1984
        goto end;
1985
1986
    de_ctx->flags |= DE_QUIET;
1987
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1988
                                   "(msg:\"Testing bytejump_body\"; "
1989
                                   "content:\"one\"; content:\"two\"; http_uri; "
1990
                                   "byte_extract:4,0,two,string,hex; "
1991
                                   "sid:1;)");
1992
    if (de_ctx->sig_list == NULL) {
1993
        result = 0;
1994
        goto end;
1995
    }
1996
1997
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
1998
        result = 0;
1999
        goto end;
2000
    }
2001
2002
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2003
    if (sm->type != DETECT_CONTENT) {
2004
        result = 0;
2005
        goto end;
2006
    }
2007
    cd = (DetectContentData *)sm->ctx;
2008
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2009
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2010
        cd->flags & DETECT_CONTENT_NOCASE ||
2011
        cd->flags & DETECT_CONTENT_WITHIN ||
2012
        cd->flags & DETECT_CONTENT_DISTANCE ||
2013
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2014
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2015
        cd->flags & DETECT_CONTENT_NEGATED ) {
2016
        printf("one failed\n");
2017
        result = 0;
2018
        goto end;
2019
    }
2020
2021
    sm = sm->next;
2022
    if (sm->type != DETECT_BYTE_EXTRACT) {
2023
        result = 0;
2024
        goto end;
2025
    }
2026
    bed = (DetectByteExtractData *)sm->ctx;
2027
    if (bed->nbytes != 4 ||
2028
        bed->offset != 0 ||
2029
        strcmp(bed->name, "two") != 0 ||
2030
        bed->flags !=DETECT_BYTE_EXTRACT_FLAG_STRING ||
2031
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2032
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2033
        bed->align_value != 0 ||
2034
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2035
        goto end;
2036
    }
2037
2038
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
2039
    if (sm->type != DETECT_CONTENT) {
2040
        result = 0;
2041
        goto end;
2042
    }
2043
    ud = (DetectContentData *)sm->ctx;
2044
    if (ud->flags & DETECT_CONTENT_RAWBYTES ||
2045
        strncmp((char *)ud->content, "two", cd->content_len) != 0 ||
2046
        ud->flags & DETECT_CONTENT_NOCASE ||
2047
        ud->flags & DETECT_CONTENT_WITHIN ||
2048
        ud->flags & DETECT_CONTENT_DISTANCE ||
2049
        ud->flags & DETECT_CONTENT_FAST_PATTERN ||
2050
        ud->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2051
        ud->flags & DETECT_CONTENT_NEGATED ) {
2052
        printf("two failed\n");
2053
        result = 0;
2054
        goto end;
2055
    }
2056
2057
    if (sm->next != NULL) {
2058
        result = 0;
2059
        goto end;
2060
    }
2061
2062
    result = 1;
2063
2064
 end:
2065
    SigGroupCleanup(de_ctx);
2066
    SigCleanSignatures(de_ctx);
2067
    DetectEngineCtxFree(de_ctx);
2068
2069
    return result;
2070
}
2071
2072
static int DetectByteExtractTest41(void)
2073
{
2074
    DetectEngineCtx *de_ctx = NULL;
2075
    int result = 0;
2076
    Signature *s = NULL;
2077
    SigMatch *sm = NULL;
2078
    DetectContentData *cd = NULL;
2079
    DetectByteExtractData *bed = NULL;
2080
2081
    de_ctx = DetectEngineCtxInit();
2082
    if (de_ctx == NULL)
2083
        goto end;
2084
2085
    de_ctx->flags |= DE_QUIET;
2086
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2087
                                   "(msg:\"Testing bytejump_body\"; "
2088
                                   "content:\"one\"; "
2089
                                   "byte_extract:4,0,two,string,hex; "
2090
                                   "byte_extract:4,0,three,string,hex; "
2091
                                   "sid:1;)");
2092
    if (de_ctx->sig_list == NULL) {
2093
        result = 0;
2094
        goto end;
2095
    }
2096
2097
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2098
        result = 0;
2099
        goto end;
2100
    }
2101
2102
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2103
    if (sm->type != DETECT_CONTENT) {
2104
        result = 0;
2105
        goto end;
2106
    }
2107
    cd = (DetectContentData *)sm->ctx;
2108
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2109
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2110
        cd->flags & DETECT_CONTENT_NOCASE ||
2111
        cd->flags & DETECT_CONTENT_WITHIN ||
2112
        cd->flags & DETECT_CONTENT_DISTANCE ||
2113
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2114
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2115
        cd->flags & DETECT_CONTENT_NEGATED ) {
2116
        printf("one failed\n");
2117
        result = 0;
2118
        goto end;
2119
    }
2120
2121
    sm = sm->next;
2122
    if (sm->type != DETECT_BYTE_EXTRACT) {
2123
        result = 0;
2124
        goto end;
2125
    }
2126
    bed = (DetectByteExtractData *)sm->ctx;
2127
    if (bed->nbytes != 4 ||
2128
        bed->offset != 0 ||
2129
        strcmp(bed->name, "two") != 0 ||
2130
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2131
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2132
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2133
        bed->align_value != 0 ||
2134
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2135
        goto end;
2136
    }
2137
    if (bed->local_id != 0) {
2138
        result = 0;
2139
        goto end;
2140
    }
2141
2142
    sm = sm->next;
2143
    if (sm->type != DETECT_BYTE_EXTRACT) {
2144
        result = 0;
2145
        goto end;
2146
    }
2147
    bed = (DetectByteExtractData *)sm->ctx;
2148
    if (bed->nbytes != 4 ||
2149
        bed->offset != 0 ||
2150
        strcmp(bed->name, "three") != 0 ||
2151
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2152
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2153
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2154
        bed->align_value != 0 ||
2155
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2156
        goto end;
2157
    }
2158
    if (bed->local_id != 1) {
2159
        result = 0;
2160
        goto end;
2161
    }
2162
2163
    result = 1;
2164
2165
 end:
2166
    SigGroupCleanup(de_ctx);
2167
    SigCleanSignatures(de_ctx);
2168
    DetectEngineCtxFree(de_ctx);
2169
2170
    return result;
2171
}
2172
2173
static int DetectByteExtractTest42(void)
2174
{
2175
    DetectEngineCtx *de_ctx = NULL;
2176
    int result = 0;
2177
    Signature *s = NULL;
2178
    SigMatch *sm = NULL;
2179
    DetectContentData *cd = NULL;
2180
    DetectContentData *ud = NULL;
2181
    DetectByteExtractData *bed = NULL;
2182
2183
    de_ctx = DetectEngineCtxInit();
2184
    if (de_ctx == NULL)
2185
        goto end;
2186
2187
    de_ctx->flags |= DE_QUIET;
2188
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2189
                                   "(msg:\"Testing bytejump_body\"; "
2190
                                   "content:\"one\"; "
2191
                                   "byte_extract:4,0,two,string,hex; "
2192
                                   "uricontent: \"three\"; "
2193
                                   "byte_extract:4,0,four,string,hex,relative; "
2194
                                   "byte_extract:4,0,five,string,hex; "
2195
                                   "sid:1;)");
2196
    if (de_ctx->sig_list == NULL) {
2197
        result = 0;
2198
        goto end;
2199
    }
2200
2201
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2202
        result = 0;
2203
        goto end;
2204
    }
2205
2206
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2207
    if (sm->type != DETECT_CONTENT) {
2208
        result = 0;
2209
        goto end;
2210
    }
2211
    cd = (DetectContentData *)sm->ctx;
2212
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2213
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2214
        cd->flags & DETECT_CONTENT_NOCASE ||
2215
        cd->flags & DETECT_CONTENT_WITHIN ||
2216
        cd->flags & DETECT_CONTENT_DISTANCE ||
2217
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2218
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2219
        cd->flags & DETECT_CONTENT_NEGATED ) {
2220
        printf("one failed\n");
2221
        result = 0;
2222
        goto end;
2223
    }
2224
2225
    sm = sm->next;
2226
    if (sm->type != DETECT_BYTE_EXTRACT) {
2227
        result = 0;
2228
        goto end;
2229
    }
2230
    bed = (DetectByteExtractData *)sm->ctx;
2231
    if (bed->nbytes != 4 ||
2232
        bed->offset != 0 ||
2233
        strcmp(bed->name, "two") != 0 ||
2234
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2235
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2236
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2237
        bed->align_value != 0 ||
2238
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2239
        goto end;
2240
    }
2241
    if (bed->local_id != 0) {
2242
        result = 0;
2243
        goto end;
2244
    }
2245
2246
    sm = sm->next;
2247
    if (sm->type != DETECT_BYTE_EXTRACT) {
2248
        result = 0;
2249
        goto end;
2250
    }
2251
    bed = (DetectByteExtractData *)sm->ctx;
2252
    if (bed->nbytes != 4 ||
2253
        bed->offset != 0 ||
2254
        strcmp(bed->name, "five") != 0 ||
2255
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2256
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2257
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2258
        bed->align_value != 0 ||
2259
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2260
        goto end;
2261
    }
2262
    if (bed->local_id != 1) {
2263
        result = 0;
2264
        goto end;
2265
    }
2266
2267
    if (sm->next != NULL)
2268
        goto end;
2269
2270
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
2271
    if (sm->type != DETECT_CONTENT) {
2272
        result = 0;
2273
        goto end;
2274
    }
2275
    ud = (DetectContentData *)sm->ctx;
2276
    if (ud->flags & DETECT_CONTENT_RAWBYTES ||
2277
        strncmp((char *)ud->content, "three", cd->content_len) != 0 ||
2278
        ud->flags & DETECT_CONTENT_NOCASE ||
2279
        ud->flags & DETECT_CONTENT_WITHIN ||
2280
        ud->flags & DETECT_CONTENT_DISTANCE ||
2281
        ud->flags & DETECT_CONTENT_FAST_PATTERN ||
2282
        !(ud->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
2283
        ud->flags & DETECT_CONTENT_NEGATED ) {
2284
        printf("two failed\n");
2285
        result = 0;
2286
        goto end;
2287
    }
2288
2289
    sm = sm->next;
2290
    if (sm->type != DETECT_BYTE_EXTRACT) {
2291
        result = 0;
2292
        goto end;
2293
    }
2294
    bed = (DetectByteExtractData *)sm->ctx;
2295
    if (bed->nbytes != 4 ||
2296
        bed->offset != 0 ||
2297
        strcmp(bed->name, "four") != 0 ||
2298
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_RELATIVE |
2299
                       DETECT_BYTE_EXTRACT_FLAG_STRING) ||
2300
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2301
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2302
        bed->align_value != 0 ||
2303
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2304
        goto end;
2305
    }
2306
    if (bed->local_id != 0) {
2307
        result = 0;
2308
        goto end;
2309
    }
2310
2311
    if (sm->next != NULL)
2312
        goto end;
2313
2314
    result = 1;
2315
2316
 end:
2317
    SigGroupCleanup(de_ctx);
2318
    SigCleanSignatures(de_ctx);
2319
    DetectEngineCtxFree(de_ctx);
2320
2321
    return result;
2322
}
2323
2324
static int DetectByteExtractTest43(void)
2325
{
2326
    DetectEngineCtx *de_ctx = NULL;
2327
    int result = 0;
2328
    Signature *s = NULL;
2329
    SigMatch *sm = NULL;
2330
    DetectContentData *cd = NULL;
2331
    DetectByteExtractData *bed = NULL;
2332
2333
    de_ctx = DetectEngineCtxInit();
2334
    if (de_ctx == NULL)
2335
        goto end;
2336
2337
    de_ctx->flags |= DE_QUIET;
2338
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2339
                                   "(msg:\"Testing bytejump_body\"; "
2340
                                   "content:\"one\"; "
2341
                                   "byte_extract:4,0,two,string,hex; "
2342
                                   "content: \"three\"; offset:two; "
2343
                                   "sid:1;)");
2344
    if (de_ctx->sig_list == NULL) {
2345
        result = 0;
2346
        goto end;
2347
    }
2348
2349
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2350
        result = 0;
2351
        goto end;
2352
    }
2353
2354
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2355
    if (sm->type != DETECT_CONTENT) {
2356
        result = 0;
2357
        goto end;
2358
    }
2359
    cd = (DetectContentData *)sm->ctx;
2360
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2361
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2362
        cd->flags & DETECT_CONTENT_NOCASE ||
2363
        cd->flags & DETECT_CONTENT_WITHIN ||
2364
        cd->flags & DETECT_CONTENT_DISTANCE ||
2365
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2366
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2367
        cd->flags & DETECT_CONTENT_NEGATED ) {
2368
        printf("one failed\n");
2369
        result = 0;
2370
        goto end;
2371
    }
2372
2373
    sm = sm->next;
2374
    if (sm->type != DETECT_BYTE_EXTRACT) {
2375
        result = 0;
2376
        goto end;
2377
    }
2378
    bed = (DetectByteExtractData *)sm->ctx;
2379
    if (bed->nbytes != 4 ||
2380
        bed->offset != 0 ||
2381
        strcmp(bed->name, "two") != 0 ||
2382
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2383
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2384
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2385
        bed->align_value != 0 ||
2386
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2387
        goto end;
2388
    }
2389
    if (bed->local_id != 0) {
2390
        result = 0;
2391
        goto end;
2392
    }
2393
2394
    sm = sm->next;
2395
    if (sm->type != DETECT_CONTENT) {
2396
        result = 0;
2397
        goto end;
2398
    }
2399
    cd = (DetectContentData *)sm->ctx;
2400
    if (strncmp((char *)cd->content, "three", cd->content_len) != 0 ||
2401
        cd->flags != (DETECT_CONTENT_OFFSET_VAR |
2402
                      DETECT_CONTENT_OFFSET) ||
2403
        cd->offset != bed->local_id) {
2404
        printf("three failed\n");
2405
        result = 0;
2406
        goto end;
2407
    }
2408
2409
    if (sm->next != NULL)
2410
        goto end;
2411
2412
    result = 1;
2413
2414
 end:
2415
    SigGroupCleanup(de_ctx);
2416
    SigCleanSignatures(de_ctx);
2417
    DetectEngineCtxFree(de_ctx);
2418
2419
    return result;
2420
}
2421
2422
static int DetectByteExtractTest44(void)
2423
{
2424
    DetectEngineCtx *de_ctx = NULL;
2425
    int result = 0;
2426
    Signature *s = NULL;
2427
    SigMatch *sm = NULL;
2428
    DetectContentData *cd = NULL;
2429
    DetectByteExtractData *bed1 = NULL;
2430
    DetectByteExtractData *bed2 = NULL;
2431
2432
    de_ctx = DetectEngineCtxInit();
2433
    if (de_ctx == NULL)
2434
        goto end;
2435
2436
    de_ctx->flags |= DE_QUIET;
2437
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2438
                                   "(msg:\"Testing bytejump_body\"; "
2439
                                   "content:\"one\"; "
2440
                                   "byte_extract:4,0,two,string,hex; "
2441
                                   "byte_extract:4,0,three,string,hex; "
2442
                                   "content: \"four\"; offset:two; "
2443
                                   "content: \"five\"; offset:three; "
2444
                                   "sid:1;)");
2445
    if (de_ctx->sig_list == NULL) {
2446
        result = 0;
2447
        goto end;
2448
    }
2449
2450
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2451
        result = 0;
2452
        goto end;
2453
    }
2454
2455
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2456
    if (sm->type != DETECT_CONTENT) {
2457
        result = 0;
2458
        goto end;
2459
    }
2460
    cd = (DetectContentData *)sm->ctx;
2461
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2462
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2463
        cd->flags & DETECT_CONTENT_NOCASE ||
2464
        cd->flags & DETECT_CONTENT_WITHIN ||
2465
        cd->flags & DETECT_CONTENT_DISTANCE ||
2466
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2467
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2468
        cd->flags & DETECT_CONTENT_NEGATED ) {
2469
        printf("one failed\n");
2470
        result = 0;
2471
        goto end;
2472
    }
2473
2474
    sm = sm->next;
2475
    if (sm->type != DETECT_BYTE_EXTRACT) {
2476
        result = 0;
2477
        goto end;
2478
    }
2479
    bed1 = (DetectByteExtractData *)sm->ctx;
2480
    if (bed1->nbytes != 4 ||
2481
        bed1->offset != 0 ||
2482
        strcmp(bed1->name, "two") != 0 ||
2483
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2484
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2485
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2486
        bed1->align_value != 0 ||
2487
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2488
        goto end;
2489
    }
2490
    if (bed1->local_id != 0) {
2491
        result = 0;
2492
        goto end;
2493
    }
2494
2495
    sm = sm->next;
2496
    if (sm->type != DETECT_BYTE_EXTRACT) {
2497
        result = 0;
2498
        goto end;
2499
    }
2500
    bed2 = (DetectByteExtractData *)sm->ctx;
2501
2502
    sm = sm->next;
2503
    if (sm->type != DETECT_CONTENT) {
2504
        result = 0;
2505
        goto end;
2506
    }
2507
    cd = (DetectContentData *)sm->ctx;
2508
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
2509
        cd->flags != (DETECT_CONTENT_OFFSET_VAR |
2510
                      DETECT_CONTENT_OFFSET) ||
2511
        cd->offset != bed1->local_id) {
2512
        printf("four failed\n");
2513
        result = 0;
2514
        goto end;
2515
    }
2516
2517
    sm = sm->next;
2518
    if (sm->type != DETECT_CONTENT) {
2519
        result = 0;
2520
        goto end;
2521
    }
2522
    cd = (DetectContentData *)sm->ctx;
2523
    if (strncmp((char *)cd->content, "five", cd->content_len) != 0 ||
2524
        cd->flags != (DETECT_CONTENT_OFFSET_VAR |
2525
                      DETECT_CONTENT_OFFSET) ||
2526
        cd->offset != bed2->local_id) {
2527
        printf("five failed\n");
2528
        result = 0;
2529
        goto end;
2530
    }
2531
2532
    if (sm->next != NULL)
2533
        goto end;
2534
2535
    result = 1;
2536
2537
 end:
2538
    SigGroupCleanup(de_ctx);
2539
    SigCleanSignatures(de_ctx);
2540
    DetectEngineCtxFree(de_ctx);
2541
2542
    return result;
2543
}
2544
2545
static int DetectByteExtractTest45(void)
2546
{
2547
    DetectEngineCtx *de_ctx = NULL;
2548
    int result = 0;
2549
    Signature *s = NULL;
2550
    SigMatch *sm = NULL;
2551
    DetectContentData *cd = NULL;
2552
    DetectByteExtractData *bed = NULL;
2553
2554
    de_ctx = DetectEngineCtxInit();
2555
    if (de_ctx == NULL)
2556
        goto end;
2557
2558
    de_ctx->flags |= DE_QUIET;
2559
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2560
                                   "(msg:\"Testing bytejump_body\"; "
2561
                                   "content:\"one\"; "
2562
                                   "byte_extract:4,0,two,string,hex; "
2563
                                   "content: \"three\"; depth:two; "
2564
                                   "sid:1;)");
2565
    if (de_ctx->sig_list == NULL) {
2566
        result = 0;
2567
        goto end;
2568
    }
2569
2570
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2571
        result = 0;
2572
        goto end;
2573
    }
2574
2575
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2576
    if (sm->type != DETECT_CONTENT) {
2577
        result = 0;
2578
        goto end;
2579
    }
2580
    cd = (DetectContentData *)sm->ctx;
2581
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2582
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2583
        cd->flags & DETECT_CONTENT_NOCASE ||
2584
        cd->flags & DETECT_CONTENT_WITHIN ||
2585
        cd->flags & DETECT_CONTENT_DISTANCE ||
2586
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2587
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2588
        cd->flags & DETECT_CONTENT_NEGATED ) {
2589
        printf("one failed\n");
2590
        result = 0;
2591
        goto end;
2592
    }
2593
2594
    sm = sm->next;
2595
    if (sm->type != DETECT_BYTE_EXTRACT) {
2596
        result = 0;
2597
        goto end;
2598
    }
2599
    bed = (DetectByteExtractData *)sm->ctx;
2600
    if (bed->nbytes != 4 ||
2601
        bed->offset != 0 ||
2602
        strcmp(bed->name, "two") != 0 ||
2603
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2604
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2605
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2606
        bed->align_value != 0 ||
2607
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2608
        goto end;
2609
    }
2610
    if (bed->local_id != 0) {
2611
        result = 0;
2612
        goto end;
2613
    }
2614
2615
    sm = sm->next;
2616
    if (sm->type != DETECT_CONTENT) {
2617
        result = 0;
2618
        goto end;
2619
    }
2620
    cd = (DetectContentData *)sm->ctx;
2621
    if (strncmp((char *)cd->content, "three", cd->content_len) != 0 ||
2622
        cd->flags != (DETECT_CONTENT_DEPTH_VAR |
2623
                      DETECT_CONTENT_DEPTH) ||
2624
        cd->depth != bed->local_id ||
2625
        cd->offset != 0) {
2626
        printf("three failed\n");
2627
        result = 0;
2628
        goto end;
2629
    }
2630
2631
    if (sm->next != NULL)
2632
        goto end;
2633
2634
    result = 1;
2635
2636
 end:
2637
    SigGroupCleanup(de_ctx);
2638
    SigCleanSignatures(de_ctx);
2639
    DetectEngineCtxFree(de_ctx);
2640
2641
    return result;
2642
}
2643
2644
static int DetectByteExtractTest46(void)
2645
{
2646
    DetectEngineCtx *de_ctx = NULL;
2647
    int result = 0;
2648
    Signature *s = NULL;
2649
    SigMatch *sm = NULL;
2650
    DetectContentData *cd = NULL;
2651
    DetectByteExtractData *bed1 = NULL;
2652
    DetectByteExtractData *bed2 = NULL;
2653
2654
    de_ctx = DetectEngineCtxInit();
2655
    if (de_ctx == NULL)
2656
        goto end;
2657
2658
    de_ctx->flags |= DE_QUIET;
2659
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2660
                                   "(msg:\"Testing bytejump_body\"; "
2661
                                   "content:\"one\"; "
2662
                                   "byte_extract:4,0,two,string,hex; "
2663
                                   "byte_extract:4,0,three,string,hex; "
2664
                                   "content: \"four\"; depth:two; "
2665
                                   "content: \"five\"; depth:three; "
2666
                                   "sid:1;)");
2667
    if (de_ctx->sig_list == NULL) {
2668
        result = 0;
2669
        goto end;
2670
    }
2671
2672
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2673
        result = 0;
2674
        goto end;
2675
    }
2676
2677
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2678
    if (sm->type != DETECT_CONTENT) {
2679
        result = 0;
2680
        goto end;
2681
    }
2682
    cd = (DetectContentData *)sm->ctx;
2683
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2684
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2685
        cd->flags & DETECT_CONTENT_NOCASE ||
2686
        cd->flags & DETECT_CONTENT_WITHIN ||
2687
        cd->flags & DETECT_CONTENT_DISTANCE ||
2688
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2689
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
2690
        cd->flags & DETECT_CONTENT_NEGATED ) {
2691
        printf("one failed\n");
2692
        result = 0;
2693
        goto end;
2694
    }
2695
2696
    sm = sm->next;
2697
    if (sm->type != DETECT_BYTE_EXTRACT) {
2698
        result = 0;
2699
        goto end;
2700
    }
2701
    bed1 = (DetectByteExtractData *)sm->ctx;
2702
    if (bed1->nbytes != 4 ||
2703
        bed1->offset != 0 ||
2704
        strcmp(bed1->name, "two") != 0 ||
2705
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2706
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2707
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2708
        bed1->align_value != 0 ||
2709
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2710
        goto end;
2711
    }
2712
    if (bed1->local_id != 0) {
2713
        result = 0;
2714
        goto end;
2715
    }
2716
2717
    sm = sm->next;
2718
    if (sm->type != DETECT_BYTE_EXTRACT) {
2719
        result = 0;
2720
        goto end;
2721
    }
2722
    bed2 = (DetectByteExtractData *)sm->ctx;
2723
2724
    sm = sm->next;
2725
    if (sm->type != DETECT_CONTENT) {
2726
        result = 0;
2727
        goto end;
2728
    }
2729
    cd = (DetectContentData *)sm->ctx;
2730
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
2731
        cd->flags != (DETECT_CONTENT_DEPTH_VAR |
2732
                      DETECT_CONTENT_DEPTH) ||
2733
        cd->depth != bed1->local_id) {
2734
        printf("four failed\n");
2735
        result = 0;
2736
        goto end;
2737
    }
2738
2739
    sm = sm->next;
2740
    if (sm->type != DETECT_CONTENT) {
2741
        result = 0;
2742
        goto end;
2743
    }
2744
    cd = (DetectContentData *)sm->ctx;
2745
    if (strncmp((char *)cd->content, "five", cd->content_len) != 0 ||
2746
        cd->flags != (DETECT_CONTENT_DEPTH_VAR |
2747
                      DETECT_CONTENT_DEPTH) ||
2748
        cd->depth != bed2->local_id) {
2749
        printf("five failed\n");
2750
        result = 0;
2751
        goto end;
2752
    }
2753
2754
    if (sm->next != NULL)
2755
        goto end;
2756
2757
    result = 1;
2758
2759
 end:
2760
    SigGroupCleanup(de_ctx);
2761
    SigCleanSignatures(de_ctx);
2762
    DetectEngineCtxFree(de_ctx);
2763
2764
    return result;
2765
}
2766
2767
static int DetectByteExtractTest47(void)
2768
{
2769
    DetectEngineCtx *de_ctx = NULL;
2770
    int result = 0;
2771
    Signature *s = NULL;
2772
    SigMatch *sm = NULL;
2773
    DetectContentData *cd = NULL;
2774
    DetectByteExtractData *bed = NULL;
2775
2776
    de_ctx = DetectEngineCtxInit();
2777
    if (de_ctx == NULL)
2778
        goto end;
2779
2780
    de_ctx->flags |= DE_QUIET;
2781
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2782
                                   "(msg:\"Testing bytejump_body\"; "
2783
                                   "content:\"one\"; "
2784
                                   "byte_extract:4,0,two,string,hex; "
2785
                                   "content: \"three\"; distance:two; "
2786
                                   "sid:1;)");
2787
    if (de_ctx->sig_list == NULL) {
2788
        result = 0;
2789
        goto end;
2790
    }
2791
2792
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2793
        result = 0;
2794
        goto end;
2795
    }
2796
2797
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2798
    if (sm->type != DETECT_CONTENT) {
2799
        result = 0;
2800
        goto end;
2801
    }
2802
    cd = (DetectContentData *)sm->ctx;
2803
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2804
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2805
        cd->flags & DETECT_CONTENT_NOCASE ||
2806
        cd->flags & DETECT_CONTENT_WITHIN ||
2807
        cd->flags & DETECT_CONTENT_DISTANCE ||
2808
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2809
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
2810
        cd->flags & DETECT_CONTENT_NEGATED ) {
2811
        printf("one failed\n");
2812
        result = 0;
2813
        goto end;
2814
    }
2815
2816
    sm = sm->next;
2817
    if (sm->type != DETECT_BYTE_EXTRACT) {
2818
        result = 0;
2819
        goto end;
2820
    }
2821
    bed = (DetectByteExtractData *)sm->ctx;
2822
    if (bed->nbytes != 4 ||
2823
        bed->offset != 0 ||
2824
        strcmp(bed->name, "two") != 0 ||
2825
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2826
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2827
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2828
        bed->align_value != 0 ||
2829
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2830
        goto end;
2831
    }
2832
    if (bed->local_id != 0) {
2833
        result = 0;
2834
        goto end;
2835
    }
2836
2837
    sm = sm->next;
2838
    if (sm->type != DETECT_CONTENT) {
2839
        result = 0;
2840
        goto end;
2841
    }
2842
    cd = (DetectContentData *)sm->ctx;
2843
    if (strncmp((char *)cd->content, "three", cd->content_len) != 0 ||
2844
        cd->flags != (DETECT_CONTENT_DISTANCE_VAR |
2845
                      DETECT_CONTENT_DISTANCE) ||
2846
        cd->distance != bed->local_id ||
2847
        cd->offset != 0 ||
2848
        cd->depth != 0) {
2849
        printf("three failed\n");
2850
        result = 0;
2851
        goto end;
2852
    }
2853
2854
    if (sm->next != NULL)
2855
        goto end;
2856
2857
    result = 1;
2858
2859
 end:
2860
    SigGroupCleanup(de_ctx);
2861
    SigCleanSignatures(de_ctx);
2862
    DetectEngineCtxFree(de_ctx);
2863
2864
    return result;
2865
}
2866
2867
static int DetectByteExtractTest48(void)
2868
{
2869
    DetectEngineCtx *de_ctx = NULL;
2870
    int result = 0;
2871
    Signature *s = NULL;
2872
    SigMatch *sm = NULL;
2873
    DetectContentData *cd = NULL;
2874
    DetectByteExtractData *bed1 = NULL;
2875
    DetectByteExtractData *bed2 = NULL;
2876
2877
    de_ctx = DetectEngineCtxInit();
2878
    if (de_ctx == NULL)
2879
        goto end;
2880
2881
    de_ctx->flags |= DE_QUIET;
2882
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2883
                                   "(msg:\"Testing bytejump_body\"; "
2884
                                   "content:\"one\"; "
2885
                                   "byte_extract:4,0,two,string,hex; "
2886
                                   "byte_extract:4,0,three,string,hex; "
2887
                                   "content: \"four\"; distance:two; "
2888
                                   "content: \"five\"; distance:three; "
2889
                                   "sid:1;)");
2890
    if (de_ctx->sig_list == NULL) {
2891
        result = 0;
2892
        goto end;
2893
    }
2894
2895
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
2896
        result = 0;
2897
        goto end;
2898
    }
2899
2900
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
2901
    if (sm->type != DETECT_CONTENT) {
2902
        result = 0;
2903
        goto end;
2904
    }
2905
    cd = (DetectContentData *)sm->ctx;
2906
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
2907
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
2908
        cd->flags & DETECT_CONTENT_NOCASE ||
2909
        cd->flags & DETECT_CONTENT_WITHIN ||
2910
        cd->flags & DETECT_CONTENT_DISTANCE ||
2911
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
2912
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
2913
        cd->flags & DETECT_CONTENT_NEGATED ) {
2914
        printf("one failed\n");
2915
        result = 0;
2916
        goto end;
2917
    }
2918
2919
    sm = sm->next;
2920
    if (sm->type != DETECT_BYTE_EXTRACT) {
2921
        result = 0;
2922
        goto end;
2923
    }
2924
    bed1 = (DetectByteExtractData *)sm->ctx;
2925
    if (bed1->nbytes != 4 ||
2926
        bed1->offset != 0 ||
2927
        strcmp(bed1->name, "two") != 0 ||
2928
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
2929
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
2930
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
2931
        bed1->align_value != 0 ||
2932
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
2933
        goto end;
2934
    }
2935
    if (bed1->local_id != 0) {
2936
        result = 0;
2937
        goto end;
2938
    }
2939
2940
    sm = sm->next;
2941
    if (sm->type != DETECT_BYTE_EXTRACT) {
2942
        result = 0;
2943
        goto end;
2944
    }
2945
    bed2 = (DetectByteExtractData *)sm->ctx;
2946
2947
    sm = sm->next;
2948
    if (sm->type != DETECT_CONTENT) {
2949
        result = 0;
2950
        goto end;
2951
    }
2952
    cd = (DetectContentData *)sm->ctx;
2953
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
2954
        cd->flags != (DETECT_CONTENT_DISTANCE_VAR |
2955
                      DETECT_CONTENT_DISTANCE |
2956
                      DETECT_CONTENT_DISTANCE_NEXT) ||
2957
        cd->distance != bed1->local_id ||
2958
        cd->depth != 0 ||
2959
        cd->offset != 0) {
2960
        printf("four failed\n");
2961
        result = 0;
2962
        goto end;
2963
    }
2964
2965
    sm = sm->next;
2966
    if (sm->type != DETECT_CONTENT) {
2967
        result = 0;
2968
        goto end;
2969
    }
2970
    cd = (DetectContentData *)sm->ctx;
2971
    if (strncmp((char *)cd->content, "five", cd->content_len) != 0 ||
2972
        cd->flags != (DETECT_CONTENT_DISTANCE_VAR |
2973
                      DETECT_CONTENT_DISTANCE) ||
2974
        cd->distance != bed2->local_id ||
2975
        cd->depth != 0 ||
2976
        cd->offset != 0) {
2977
        printf("five failed\n");
2978
        result = 0;
2979
        goto end;
2980
    }
2981
2982
    if (sm->next != NULL)
2983
        goto end;
2984
2985
    result = 1;
2986
2987
 end:
2988
    SigGroupCleanup(de_ctx);
2989
    SigCleanSignatures(de_ctx);
2990
    DetectEngineCtxFree(de_ctx);
2991
2992
    return result;
2993
}
2994
2995
static int DetectByteExtractTest49(void)
2996
{
2997
    DetectEngineCtx *de_ctx = NULL;
2998
    int result = 0;
2999
    Signature *s = NULL;
3000
    SigMatch *sm = NULL;
3001
    DetectContentData *cd = NULL;
3002
    DetectByteExtractData *bed = NULL;
3003
3004
    de_ctx = DetectEngineCtxInit();
3005
    if (de_ctx == NULL)
3006
        goto end;
3007
3008
    de_ctx->flags |= DE_QUIET;
3009
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3010
                                   "(msg:\"Testing bytejump_body\"; "
3011
                                   "content:\"one\"; "
3012
                                   "byte_extract:4,0,two,string,hex; "
3013
                                   "content: \"three\"; within:two; "
3014
                                   "sid:1;)");
3015
    if (de_ctx->sig_list == NULL) {
3016
        result = 0;
3017
        goto end;
3018
    }
3019
3020
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3021
        result = 0;
3022
        goto end;
3023
    }
3024
3025
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3026
    if (sm->type != DETECT_CONTENT) {
3027
        result = 0;
3028
        goto end;
3029
    }
3030
    cd = (DetectContentData *)sm->ctx;
3031
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3032
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3033
        cd->flags & DETECT_CONTENT_NOCASE ||
3034
        cd->flags & DETECT_CONTENT_WITHIN ||
3035
        cd->flags & DETECT_CONTENT_DISTANCE ||
3036
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3037
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
3038
        cd->flags & DETECT_CONTENT_NEGATED ) {
3039
        printf("one failed\n");
3040
        result = 0;
3041
        goto end;
3042
    }
3043
3044
    sm = sm->next;
3045
    if (sm->type != DETECT_BYTE_EXTRACT) {
3046
        result = 0;
3047
        goto end;
3048
    }
3049
    bed = (DetectByteExtractData *)sm->ctx;
3050
    if (bed->nbytes != 4 ||
3051
        bed->offset != 0 ||
3052
        strcmp(bed->name, "two") != 0 ||
3053
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3054
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3055
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3056
        bed->align_value != 0 ||
3057
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3058
        goto end;
3059
    }
3060
    if (bed->local_id != 0) {
3061
        result = 0;
3062
        goto end;
3063
    }
3064
3065
    sm = sm->next;
3066
    if (sm->type != DETECT_CONTENT) {
3067
        result = 0;
3068
        goto end;
3069
    }
3070
    cd = (DetectContentData *)sm->ctx;
3071
    if (strncmp((char *)cd->content, "three", cd->content_len) != 0 ||
3072
        cd->flags != (DETECT_CONTENT_WITHIN_VAR |
3073
                      DETECT_CONTENT_WITHIN) ||
3074
        cd->within != bed->local_id ||
3075
        cd->offset != 0 ||
3076
        cd->depth != 0 ||
3077
        cd->distance != 0) {
3078
        printf("three failed\n");
3079
        result = 0;
3080
        goto end;
3081
    }
3082
3083
    if (sm->next != NULL)
3084
        goto end;
3085
3086
    result = 1;
3087
3088
 end:
3089
    SigGroupCleanup(de_ctx);
3090
    SigCleanSignatures(de_ctx);
3091
    DetectEngineCtxFree(de_ctx);
3092
3093
    return result;
3094
}
3095
3096
static int DetectByteExtractTest50(void)
3097
{
3098
    DetectEngineCtx *de_ctx = NULL;
3099
    int result = 0;
3100
    Signature *s = NULL;
3101
    SigMatch *sm = NULL;
3102
    DetectContentData *cd = NULL;
3103
    DetectByteExtractData *bed1 = NULL;
3104
    DetectByteExtractData *bed2 = NULL;
3105
3106
    de_ctx = DetectEngineCtxInit();
3107
    if (de_ctx == NULL)
3108
        goto end;
3109
3110
    de_ctx->flags |= DE_QUIET;
3111
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3112
                                   "(msg:\"Testing bytejump_body\"; "
3113
                                   "content:\"one\"; "
3114
                                   "byte_extract:4,0,two,string,hex; "
3115
                                   "byte_extract:4,0,three,string,hex; "
3116
                                   "content: \"four\"; within:two; "
3117
                                   "content: \"five\"; within:three; "
3118
                                   "sid:1;)");
3119
    if (de_ctx->sig_list == NULL) {
3120
        result = 0;
3121
        goto end;
3122
    }
3123
3124
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3125
        result = 0;
3126
        goto end;
3127
    }
3128
3129
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3130
    if (sm->type != DETECT_CONTENT) {
3131
        result = 0;
3132
        goto end;
3133
    }
3134
    cd = (DetectContentData *)sm->ctx;
3135
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3136
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3137
        cd->flags & DETECT_CONTENT_NOCASE ||
3138
        cd->flags & DETECT_CONTENT_WITHIN ||
3139
        cd->flags & DETECT_CONTENT_DISTANCE ||
3140
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3141
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
3142
        cd->flags & DETECT_CONTENT_NEGATED ) {
3143
        printf("one failed\n");
3144
        result = 0;
3145
        goto end;
3146
    }
3147
3148
    sm = sm->next;
3149
    if (sm->type != DETECT_BYTE_EXTRACT) {
3150
        result = 0;
3151
        goto end;
3152
    }
3153
    bed1 = (DetectByteExtractData *)sm->ctx;
3154
    if (bed1->nbytes != 4 ||
3155
        bed1->offset != 0 ||
3156
        strcmp(bed1->name, "two") != 0 ||
3157
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3158
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3159
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3160
        bed1->align_value != 0 ||
3161
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3162
        goto end;
3163
    }
3164
    if (bed1->local_id != 0) {
3165
        result = 0;
3166
        goto end;
3167
    }
3168
3169
    sm = sm->next;
3170
    if (sm->type != DETECT_BYTE_EXTRACT) {
3171
        result = 0;
3172
        goto end;
3173
    }
3174
    bed2 = (DetectByteExtractData *)sm->ctx;
3175
3176
    sm = sm->next;
3177
    if (sm->type != DETECT_CONTENT) {
3178
        result = 0;
3179
        goto end;
3180
    }
3181
    cd = (DetectContentData *)sm->ctx;
3182
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
3183
        cd->flags != (DETECT_CONTENT_WITHIN_VAR |
3184
                      DETECT_CONTENT_WITHIN|
3185
                      DETECT_CONTENT_WITHIN_NEXT) ||
3186
        cd->within != bed1->local_id ||
3187
        cd->depth != 0 ||
3188
        cd->offset != 0 ||
3189
        cd->distance != 0) {
3190
        printf("four failed\n");
3191
        result = 0;
3192
        goto end;
3193
    }
3194
3195
    sm = sm->next;
3196
    if (sm->type != DETECT_CONTENT) {
3197
        result = 0;
3198
        goto end;
3199
    }
3200
    cd = (DetectContentData *)sm->ctx;
3201
    if (strncmp((char *)cd->content, "five", cd->content_len) != 0 ||
3202
        cd->flags != (DETECT_CONTENT_WITHIN_VAR |
3203
                      DETECT_CONTENT_WITHIN) ||
3204
        cd->within != bed2->local_id ||
3205
        cd->depth != 0 ||
3206
        cd->offset != 0 ||
3207
        cd->distance != 0) {
3208
        printf("five failed\n");
3209
        result = 0;
3210
        goto end;
3211
    }
3212
3213
    if (sm->next != NULL)
3214
        goto end;
3215
3216
    result = 1;
3217
3218
 end:
3219
    SigGroupCleanup(de_ctx);
3220
    SigCleanSignatures(de_ctx);
3221
    DetectEngineCtxFree(de_ctx);
3222
3223
    return result;
3224
}
3225
3226
static int DetectByteExtractTest51(void)
3227
{
3228
    DetectEngineCtx *de_ctx = NULL;
3229
    int result = 0;
3230
    Signature *s = NULL;
3231
    SigMatch *sm = NULL;
3232
    DetectContentData *cd = NULL;
3233
    DetectByteExtractData *bed = NULL;
3234
    DetectBytetestData *btd = NULL;
3235
3236
    de_ctx = DetectEngineCtxInit();
3237
    if (de_ctx == NULL)
3238
        goto end;
3239
3240
    de_ctx->flags |= DE_QUIET;
3241
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3242
                                   "(msg:\"Testing bytejump_body\"; "
3243
                                   "content:\"one\"; "
3244
                                   "byte_extract:4,0,two,string,hex; "
3245
                                   "byte_test: 2,=,10, two; "
3246
                                   "sid:1;)");
3247
    if (de_ctx->sig_list == NULL) {
3248
        result = 0;
3249
        goto end;
3250
    }
3251
3252
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3253
        result = 0;
3254
        goto end;
3255
    }
3256
3257
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3258
    if (sm->type != DETECT_CONTENT) {
3259
        result = 0;
3260
        goto end;
3261
    }
3262
    cd = (DetectContentData *)sm->ctx;
3263
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3264
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3265
        cd->flags & DETECT_CONTENT_NOCASE ||
3266
        cd->flags & DETECT_CONTENT_WITHIN ||
3267
        cd->flags & DETECT_CONTENT_DISTANCE ||
3268
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3269
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
3270
        cd->flags & DETECT_CONTENT_NEGATED ) {
3271
        printf("one failed\n");
3272
        result = 0;
3273
        goto end;
3274
    }
3275
3276
    sm = sm->next;
3277
    if (sm->type != DETECT_BYTE_EXTRACT) {
3278
        result = 0;
3279
        goto end;
3280
    }
3281
    bed = (DetectByteExtractData *)sm->ctx;
3282
    if (bed->nbytes != 4 ||
3283
        bed->offset != 0 ||
3284
        strcmp(bed->name, "two") != 0 ||
3285
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3286
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3287
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3288
        bed->align_value != 0 ||
3289
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3290
        goto end;
3291
    }
3292
    if (bed->local_id != 0) {
3293
        result = 0;
3294
        goto end;
3295
    }
3296
3297
    sm = sm->next;
3298
    if (sm->type != DETECT_BYTETEST) {
3299
        result = 0;
3300
        goto end;
3301
    }
3302
    btd = (DetectBytetestData *)sm->ctx;
3303
    if (btd->flags != DETECT_BYTETEST_OFFSET_VAR ||
3304
        btd->value != 10 ||
3305
        btd->offset != 0) {
3306
        printf("three failed\n");
3307
        result = 0;
3308
        goto end;
3309
    }
3310
3311
    if (sm->next != NULL)
3312
        goto end;
3313
3314
    result = 1;
3315
3316
 end:
3317
    SigGroupCleanup(de_ctx);
3318
    SigCleanSignatures(de_ctx);
3319
    DetectEngineCtxFree(de_ctx);
3320
3321
    return result;
3322
}
3323
3324
static int DetectByteExtractTest52(void)
3325
{
3326
    DetectEngineCtx *de_ctx = NULL;
3327
    int result = 0;
3328
    Signature *s = NULL;
3329
    SigMatch *sm = NULL;
3330
    DetectContentData *cd = NULL;
3331
    DetectByteExtractData *bed1 = NULL;
3332
    DetectBytetestData *btd = NULL;
3333
3334
    de_ctx = DetectEngineCtxInit();
3335
    if (de_ctx == NULL)
3336
        goto end;
3337
3338
    de_ctx->flags |= DE_QUIET;
3339
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3340
                                   "(msg:\"Testing bytejump_body\"; "
3341
                                   "content:\"one\"; "
3342
                                   "byte_extract:4,0,two,string,hex; "
3343
                                   "byte_extract:4,0,three,string,hex; "
3344
                                   "byte_test: 2,=,two,three; "
3345
                                   "byte_test: 3,=,10,three; "
3346
                                   "sid:1;)");
3347
    if (de_ctx->sig_list == NULL) {
3348
        result = 0;
3349
        goto end;
3350
    }
3351
3352
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3353
        result = 0;
3354
        goto end;
3355
    }
3356
3357
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3358
    if (sm->type != DETECT_CONTENT) {
3359
        result = 0;
3360
        goto end;
3361
    }
3362
    cd = (DetectContentData *)sm->ctx;
3363
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3364
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3365
        cd->flags & DETECT_CONTENT_NOCASE ||
3366
        cd->flags & DETECT_CONTENT_WITHIN ||
3367
        cd->flags & DETECT_CONTENT_DISTANCE ||
3368
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3369
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
3370
        cd->flags & DETECT_CONTENT_NEGATED ) {
3371
        printf("one failed\n");
3372
        result = 0;
3373
        goto end;
3374
    }
3375
3376
    sm = sm->next;
3377
    if (sm->type != DETECT_BYTE_EXTRACT) {
3378
        result = 0;
3379
        goto end;
3380
    }
3381
    bed1 = (DetectByteExtractData *)sm->ctx;
3382
    if (bed1->nbytes != 4 ||
3383
        bed1->offset != 0 ||
3384
        strcmp(bed1->name, "two") != 0 ||
3385
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3386
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3387
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3388
        bed1->align_value != 0 ||
3389
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3390
        goto end;
3391
    }
3392
    if (bed1->local_id != 0) {
3393
        result = 0;
3394
        goto end;
3395
    }
3396
3397
    sm = sm->next;
3398
    if (sm->type != DETECT_BYTE_EXTRACT) {
3399
        result = 0;
3400
        goto end;
3401
    }
3402
3403
    sm = sm->next;
3404
    if (sm->type != DETECT_BYTETEST) {
3405
        result = 0;
3406
        goto end;
3407
    }
3408
    btd = (DetectBytetestData *)sm->ctx;
3409
    if (btd->flags != (DETECT_BYTETEST_OFFSET_VAR |
3410
                       DETECT_BYTETEST_VALUE_VAR) ||
3411
        btd->value != 0 ||
3412
        btd->offset != 1) {
3413
        printf("three failed\n");
3414
        result = 0;
3415
        goto end;
3416
    }
3417
3418
    sm = sm->next;
3419
    if (sm->type != DETECT_BYTETEST) {
3420
        result = 0;
3421
        goto end;
3422
    }
3423
    btd = (DetectBytetestData *)sm->ctx;
3424
    if (btd->flags != DETECT_BYTETEST_OFFSET_VAR ||
3425
        btd->value != 10 ||
3426
        btd->offset != 1) {
3427
        printf("four failed\n");
3428
        result = 0;
3429
        goto end;
3430
    }
3431
3432
    if (sm->next != NULL)
3433
        goto end;
3434
3435
    result = 1;
3436
3437
 end:
3438
    SigGroupCleanup(de_ctx);
3439
    SigCleanSignatures(de_ctx);
3440
    DetectEngineCtxFree(de_ctx);
3441
3442
    return result;
3443
}
3444
3445
static int DetectByteExtractTest53(void)
3446
{
3447
    DetectEngineCtx *de_ctx = NULL;
3448
    int result = 0;
3449
    Signature *s = NULL;
3450
    SigMatch *sm = NULL;
3451
    DetectContentData *cd = NULL;
3452
    DetectByteExtractData *bed = NULL;
3453
    DetectBytejumpData *bjd = NULL;
3454
3455
    de_ctx = DetectEngineCtxInit();
3456
    if (de_ctx == NULL)
3457
        goto end;
3458
3459
    de_ctx->flags |= DE_QUIET;
3460
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3461
                                   "(msg:\"Testing bytejump_body\"; "
3462
                                   "content:\"one\"; "
3463
                                   "byte_extract:4,0,two,string,hex; "
3464
                                   "byte_jump: 2,two; "
3465
                                   "sid:1;)");
3466
    if (de_ctx->sig_list == NULL) {
3467
        result = 0;
3468
        goto end;
3469
    }
3470
3471
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3472
        result = 0;
3473
        goto end;
3474
    }
3475
3476
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3477
    if (sm->type != DETECT_CONTENT) {
3478
        result = 0;
3479
        goto end;
3480
    }
3481
    cd = (DetectContentData *)sm->ctx;
3482
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3483
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3484
        cd->flags & DETECT_CONTENT_NOCASE ||
3485
        cd->flags & DETECT_CONTENT_WITHIN ||
3486
        cd->flags & DETECT_CONTENT_DISTANCE ||
3487
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3488
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
3489
        cd->flags & DETECT_CONTENT_NEGATED ) {
3490
        printf("one failed\n");
3491
        result = 0;
3492
        goto end;
3493
    }
3494
3495
    sm = sm->next;
3496
    if (sm->type != DETECT_BYTE_EXTRACT) {
3497
        result = 0;
3498
        goto end;
3499
    }
3500
    bed = (DetectByteExtractData *)sm->ctx;
3501
    if (bed->nbytes != 4 ||
3502
        bed->offset != 0 ||
3503
        strcmp(bed->name, "two") != 0 ||
3504
        bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3505
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3506
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3507
        bed->align_value != 0 ||
3508
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3509
        goto end;
3510
    }
3511
    if (bed->local_id != 0) {
3512
        result = 0;
3513
        goto end;
3514
    }
3515
3516
    sm = sm->next;
3517
    if (sm->type != DETECT_BYTEJUMP) {
3518
        result = 0;
3519
        goto end;
3520
    }
3521
    bjd = (DetectBytejumpData *)sm->ctx;
3522
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 0) {
3523
        printf("three failed\n");
3524
        result = 0;
3525
        goto end;
3526
    }
3527
3528
    if (sm->next != NULL)
3529
        goto end;
3530
3531
    result = 1;
3532
3533
 end:
3534
    SigGroupCleanup(de_ctx);
3535
    SigCleanSignatures(de_ctx);
3536
    DetectEngineCtxFree(de_ctx);
3537
3538
    return result;
3539
}
3540
3541
static int DetectByteExtractTest54(void)
3542
{
3543
    DetectEngineCtx *de_ctx = NULL;
3544
    int result = 0;
3545
    Signature *s = NULL;
3546
    SigMatch *sm = NULL;
3547
    DetectContentData *cd = NULL;
3548
    DetectByteExtractData *bed1 = NULL;
3549
    DetectBytejumpData *bjd = NULL;
3550
3551
    de_ctx = DetectEngineCtxInit();
3552
    if (de_ctx == NULL)
3553
        goto end;
3554
3555
    de_ctx->flags |= DE_QUIET;
3556
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3557
                                   "(msg:\"Testing bytejump_body\"; "
3558
                                   "content:\"one\"; "
3559
                                   "byte_extract:4,0,two,string,hex; "
3560
                                   "byte_extract:4,0,three,string,hex; "
3561
                                   "byte_jump: 2,two; "
3562
                                   "byte_jump: 3,three; "
3563
                                   "sid:1;)");
3564
    if (de_ctx->sig_list == NULL) {
3565
        result = 0;
3566
        goto end;
3567
    }
3568
3569
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3570
        result = 0;
3571
        goto end;
3572
    }
3573
3574
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3575
    if (sm->type != DETECT_CONTENT) {
3576
        result = 0;
3577
        goto end;
3578
    }
3579
    cd = (DetectContentData *)sm->ctx;
3580
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3581
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3582
        cd->flags & DETECT_CONTENT_NOCASE ||
3583
        cd->flags & DETECT_CONTENT_WITHIN ||
3584
        cd->flags & DETECT_CONTENT_DISTANCE ||
3585
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3586
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
3587
        cd->flags & DETECT_CONTENT_NEGATED ) {
3588
        printf("one failed\n");
3589
        result = 0;
3590
        goto end;
3591
    }
3592
3593
    sm = sm->next;
3594
    if (sm->type != DETECT_BYTE_EXTRACT) {
3595
        result = 0;
3596
        goto end;
3597
    }
3598
    bed1 = (DetectByteExtractData *)sm->ctx;
3599
    if (bed1->nbytes != 4 ||
3600
        bed1->offset != 0 ||
3601
        strcmp(bed1->name, "two") != 0 ||
3602
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3603
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3604
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3605
        bed1->align_value != 0 ||
3606
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3607
        goto end;
3608
    }
3609
    if (bed1->local_id != 0) {
3610
        result = 0;
3611
        goto end;
3612
    }
3613
3614
    sm = sm->next;
3615
    if (sm->type != DETECT_BYTE_EXTRACT) {
3616
        result = 0;
3617
        goto end;
3618
    }
3619
3620
    sm = sm->next;
3621
    if (sm->type != DETECT_BYTEJUMP) {
3622
        result = 0;
3623
        goto end;
3624
    }
3625
    bjd = (DetectBytejumpData *)sm->ctx;
3626
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 0) {
3627
        printf("three failed\n");
3628
        result = 0;
3629
        goto end;
3630
    }
3631
3632
    sm = sm->next;
3633
    if (sm->type != DETECT_BYTEJUMP) {
3634
        result = 0;
3635
        goto end;
3636
    }
3637
    bjd = (DetectBytejumpData *)sm->ctx;
3638
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 1) {
3639
        printf("four failed\n");
3640
        result = 0;
3641
        goto end;
3642
    }
3643
3644
    if (sm->next != NULL)
3645
        goto end;
3646
3647
    result = 1;
3648
3649
 end:
3650
    SigGroupCleanup(de_ctx);
3651
    SigCleanSignatures(de_ctx);
3652
    DetectEngineCtxFree(de_ctx);
3653
3654
    return result;
3655
}
3656
3657
static int DetectByteExtractTest55(void)
3658
{
3659
    DetectEngineCtx *de_ctx = NULL;
3660
    int result = 0;
3661
    Signature *s = NULL;
3662
    SigMatch *sm = NULL;
3663
    DetectContentData *cd = NULL;
3664
    DetectByteExtractData *bed1 = NULL;
3665
    DetectByteExtractData *bed2 = NULL;
3666
3667
    de_ctx = DetectEngineCtxInit();
3668
    if (de_ctx == NULL)
3669
        goto end;
3670
3671
    de_ctx->flags |= DE_QUIET;
3672
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3673
                                   "(msg:\"Testing byte_extract\"; "
3674
                                   "content:\"one\"; "
3675
                                   "byte_extract:4,0,two,string,hex; "
3676
                                   "byte_extract:4,0,three,string,hex; "
3677
                                   "byte_extract:4,0,four,string,hex; "
3678
                                   "byte_extract:4,0,five,string,hex; "
3679
                                   "content: \"four\"; within:two; distance:three; "
3680
                                   "sid:1;)");
3681
    if (de_ctx->sig_list == NULL) {
3682
        goto end;
3683
    }
3684
3685
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3686
        goto end;
3687
    }
3688
3689
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3690
    if (sm->type != DETECT_CONTENT) {
3691
        goto end;
3692
    }
3693
    cd = (DetectContentData *)sm->ctx;
3694
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3695
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3696
        cd->flags & DETECT_CONTENT_NOCASE ||
3697
        cd->flags & DETECT_CONTENT_WITHIN ||
3698
        cd->flags & DETECT_CONTENT_DISTANCE ||
3699
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3700
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
3701
        cd->flags & DETECT_CONTENT_NEGATED ) {
3702
        printf("one failed: ");
3703
        goto end;
3704
    }
3705
3706
    sm = sm->next;
3707
    if (sm->type != DETECT_BYTE_EXTRACT) {
3708
        goto end;
3709
    }
3710
    bed1 = (DetectByteExtractData *)sm->ctx;
3711
    if (bed1->nbytes != 4 ||
3712
        bed1->offset != 0 ||
3713
        strcmp(bed1->name, "two") != 0 ||
3714
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3715
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3716
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3717
        bed1->align_value != 0 ||
3718
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3719
        goto end;
3720
    }
3721
    if (bed1->local_id != 0) {
3722
        goto end;
3723
    }
3724
3725
    sm = sm->next;
3726
    if (sm->type != DETECT_BYTE_EXTRACT) {
3727
        goto end;
3728
    }
3729
    bed2 = (DetectByteExtractData *)sm->ctx;
3730
3731
    sm = sm->next;
3732
    if (sm->type != DETECT_BYTE_EXTRACT) {
3733
        goto end;
3734
    }
3735
3736
    sm = sm->next;
3737
    if (sm->type != DETECT_BYTE_EXTRACT) {
3738
        goto end;
3739
    }
3740
3741
    sm = sm->next;
3742
    if (sm->type != DETECT_CONTENT) {
3743
        goto end;
3744
    }
3745
    cd = (DetectContentData *)sm->ctx;
3746
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
3747
        cd->flags != (DETECT_CONTENT_DISTANCE_VAR |
3748
                      DETECT_CONTENT_WITHIN_VAR |
3749
                      DETECT_CONTENT_DISTANCE |
3750
                      DETECT_CONTENT_WITHIN) ||
3751
        cd->within != bed1->local_id ||
3752
        cd->distance != bed2->local_id) {
3753
        printf("four failed: ");
3754
        goto end;
3755
    }
3756
3757
    if (sm->next != NULL) {
3758
        goto end;
3759
    }
3760
3761
    result = 1;
3762
3763
 end:
3764
    SigGroupCleanup(de_ctx);
3765
    SigCleanSignatures(de_ctx);
3766
    DetectEngineCtxFree(de_ctx);
3767
3768
    return result;
3769
}
3770
3771
static int DetectByteExtractTest56(void)
3772
{
3773
    DetectEngineCtx *de_ctx = NULL;
3774
    int result = 0;
3775
    Signature *s = NULL;
3776
    SigMatch *sm = NULL;
3777
    DetectContentData *cd = NULL;
3778
    DetectByteExtractData *bed1 = NULL;
3779
    DetectByteExtractData *bed2 = NULL;
3780
3781
    de_ctx = DetectEngineCtxInit();
3782
    if (de_ctx == NULL)
3783
        goto end;
3784
3785
    de_ctx->flags |= DE_QUIET;
3786
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3787
                                   "(msg:\"Testing bytejump_body\"; "
3788
                                   "uricontent:\"urione\"; "
3789
                                   "content:\"one\"; "
3790
                                   "byte_extract:4,0,two,string,hex; "
3791
                                   "byte_extract:4,0,three,string,hex; "
3792
                                   "byte_extract:4,0,four,string,hex; "
3793
                                   "byte_extract:4,0,five,string,hex; "
3794
                                   "content: \"four\"; within:two; distance:three; "
3795
                                   "sid:1;)");
3796
    if (de_ctx->sig_list == NULL) {
3797
        result = 0;
3798
        goto end;
3799
    }
3800
3801
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3802
        result = 0;
3803
        goto end;
3804
    }
3805
3806
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
3807
    if (sm->type != DETECT_CONTENT) {
3808
        result = 0;
3809
        goto end;
3810
    }
3811
    cd = (DetectContentData *)sm->ctx;
3812
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3813
        strncmp((char *)cd->content, "urione", cd->content_len) != 0 ||
3814
        cd->flags & DETECT_CONTENT_NOCASE ||
3815
        cd->flags & DETECT_CONTENT_WITHIN ||
3816
        cd->flags & DETECT_CONTENT_DISTANCE ||
3817
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3818
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
3819
        cd->flags & DETECT_CONTENT_NEGATED ) {
3820
        printf("one failed\n");
3821
        result = 0;
3822
        goto end;
3823
    }
3824
3825
    if (sm->next != NULL)
3826
        goto end;
3827
3828
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3829
    if (sm->type != DETECT_CONTENT) {
3830
        result = 0;
3831
        goto end;
3832
    }
3833
    cd = (DetectContentData *)sm->ctx;
3834
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3835
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3836
        cd->flags & DETECT_CONTENT_NOCASE ||
3837
        cd->flags & DETECT_CONTENT_WITHIN ||
3838
        cd->flags & DETECT_CONTENT_DISTANCE ||
3839
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3840
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
3841
        cd->flags & DETECT_CONTENT_NEGATED ) {
3842
        printf("one failed\n");
3843
        result = 0;
3844
        goto end;
3845
    }
3846
3847
    sm = sm->next;
3848
    if (sm->type != DETECT_BYTE_EXTRACT) {
3849
        result = 0;
3850
        goto end;
3851
    }
3852
    bed1 = (DetectByteExtractData *)sm->ctx;
3853
    if (bed1->nbytes != 4 ||
3854
        bed1->offset != 0 ||
3855
        strcmp(bed1->name, "two") != 0 ||
3856
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
3857
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
3858
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
3859
        bed1->align_value != 0 ||
3860
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
3861
        goto end;
3862
    }
3863
    if (bed1->local_id != 0) {
3864
        result = 0;
3865
        goto end;
3866
    }
3867
3868
    sm = sm->next;
3869
    if (sm->type != DETECT_BYTE_EXTRACT) {
3870
        result = 0;
3871
        goto end;
3872
    }
3873
    bed2 = (DetectByteExtractData *)sm->ctx;
3874
3875
    sm = sm->next;
3876
    if (sm->type != DETECT_BYTE_EXTRACT) {
3877
        result = 0;
3878
        goto end;
3879
    }
3880
3881
    sm = sm->next;
3882
    if (sm->type != DETECT_BYTE_EXTRACT) {
3883
        result = 0;
3884
        goto end;
3885
    }
3886
3887
    sm = sm->next;
3888
    if (sm->type != DETECT_CONTENT) {
3889
        result = 0;
3890
        goto end;
3891
    }
3892
    cd = (DetectContentData *)sm->ctx;
3893
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
3894
        cd->flags != (DETECT_CONTENT_DISTANCE_VAR |
3895
                      DETECT_CONTENT_WITHIN_VAR |
3896
                      DETECT_CONTENT_DISTANCE |
3897
                      DETECT_CONTENT_WITHIN) ||
3898
        cd->within != bed1->local_id ||
3899
        cd->distance != bed2->local_id ) {
3900
        printf("four failed\n");
3901
        result = 0;
3902
        goto end;
3903
    }
3904
3905
    if (sm->next != NULL) {
3906
        goto end;
3907
    }
3908
3909
    result = 1;
3910
3911
 end:
3912
    SigGroupCleanup(de_ctx);
3913
    SigCleanSignatures(de_ctx);
3914
    DetectEngineCtxFree(de_ctx);
3915
3916
    return result;
3917
}
3918
3919
static int DetectByteExtractTest57(void)
3920
{
3921
    DetectEngineCtx *de_ctx = NULL;
3922
    int result = 0;
3923
    Signature *s = NULL;
3924
    SigMatch *sm = NULL;
3925
    DetectContentData *cd = NULL;
3926
    DetectByteExtractData *bed1 = NULL;
3927
    DetectByteExtractData *bed2 = NULL;
3928
    DetectByteExtractData *bed3 = NULL;
3929
    DetectByteExtractData *bed4 = NULL;
3930
3931
    de_ctx = DetectEngineCtxInit();
3932
    if (de_ctx == NULL)
3933
        goto end;
3934
3935
    de_ctx->flags |= DE_QUIET;
3936
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
3937
                                   "(msg:\"Testing bytejump_body\"; "
3938
                                   "content:\"one\"; "
3939
                                   "uricontent: \"urione\"; "
3940
                                   "byte_extract:4,0,two,string,hex,relative; "
3941
                                   "byte_extract:4,0,three,string,hex,relative; "
3942
                                   "byte_extract:4,0,four,string,hex,relative; "
3943
                                   "byte_extract:4,0,five,string,hex,relative; "
3944
                                   "uricontent: \"four\"; within:two; distance:three; "
3945
                                   "sid:1;)");
3946
    if (de_ctx->sig_list == NULL) {
3947
        result = 0;
3948
        goto end;
3949
    }
3950
3951
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
3952
        result = 0;
3953
        goto end;
3954
    }
3955
3956
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
3957
    if (sm->type != DETECT_CONTENT) {
3958
        result = 0;
3959
        goto end;
3960
    }
3961
    cd = (DetectContentData *)sm->ctx;
3962
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3963
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
3964
        cd->flags & DETECT_CONTENT_NOCASE ||
3965
        cd->flags & DETECT_CONTENT_WITHIN ||
3966
        cd->flags & DETECT_CONTENT_DISTANCE ||
3967
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3968
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
3969
        cd->flags & DETECT_CONTENT_NEGATED ) {
3970
        printf("one failed\n");
3971
        result = 0;
3972
        goto end;
3973
    }
3974
3975
    if (sm->next != NULL)
3976
        goto end;
3977
3978
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
3979
    if (sm->type != DETECT_CONTENT) {
3980
        result = 0;
3981
        goto end;
3982
    }
3983
    cd = (DetectContentData *)sm->ctx;
3984
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
3985
        strncmp((char *)cd->content, "urione", cd->content_len) != 0 ||
3986
        cd->flags & DETECT_CONTENT_NOCASE ||
3987
        cd->flags & DETECT_CONTENT_WITHIN ||
3988
        cd->flags & DETECT_CONTENT_DISTANCE ||
3989
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
3990
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
3991
        cd->flags & DETECT_CONTENT_NEGATED ) {
3992
        printf("one failed\n");
3993
        result = 0;
3994
        goto end;
3995
    }
3996
3997
    sm = sm->next;
3998
    if (sm->type != DETECT_BYTE_EXTRACT) {
3999
        result = 0;
4000
        goto end;
4001
    }
4002
    bed1 = (DetectByteExtractData *)sm->ctx;
4003
    if (bed1->nbytes != 4 ||
4004
        bed1->offset != 0 ||
4005
        strcmp(bed1->name, "two") != 0 ||
4006
        bed1->flags != (DETECT_BYTE_EXTRACT_FLAG_STRING |
4007
                        DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
4008
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4009
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4010
        bed1->align_value != 0 ||
4011
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4012
        goto end;
4013
    }
4014
    if (bed1->local_id != 0) {
4015
        result = 0;
4016
        goto end;
4017
    }
4018
4019
    sm = sm->next;
4020
    if (sm->type != DETECT_BYTE_EXTRACT) {
4021
        result = 0;
4022
        goto end;
4023
    }
4024
    bed2 = (DetectByteExtractData *)sm->ctx;
4025
    if (bed2->local_id != 1) {
4026
        result = 0;
4027
        goto end;
4028
    }
4029
4030
    sm = sm->next;
4031
    if (sm->type != DETECT_BYTE_EXTRACT) {
4032
        result = 0;
4033
        goto end;
4034
    }
4035
    bed3 = (DetectByteExtractData *)sm->ctx;
4036
    if (bed3->local_id != 2) {
4037
        result = 0;
4038
        goto end;
4039
    }
4040
4041
    sm = sm->next;
4042
    if (sm->type != DETECT_BYTE_EXTRACT) {
4043
        result = 0;
4044
        goto end;
4045
    }
4046
    bed4 = (DetectByteExtractData *)sm->ctx;
4047
    if (bed4->local_id != 3) {
4048
        result = 0;
4049
        goto end;
4050
    }
4051
4052
    sm = sm->next;
4053
    if (sm->type != DETECT_CONTENT) {
4054
        result = 0;
4055
        goto end;
4056
    }
4057
    cd = (DetectContentData *)sm->ctx;
4058
    if (strncmp((char *)cd->content, "four", cd->content_len) != 0 ||
4059
        cd->flags != (DETECT_CONTENT_DISTANCE_VAR |
4060
                      DETECT_CONTENT_WITHIN_VAR |
4061
                      DETECT_CONTENT_DISTANCE |
4062
                      DETECT_CONTENT_WITHIN) ||
4063
        cd->within != bed1->local_id ||
4064
        cd->distance != bed2->local_id)  {
4065
        printf("four failed\n");
4066
        result = 0;
4067
        goto end;
4068
    }
4069
4070
    if (sm->next != NULL) {
4071
        goto end;
4072
    }
4073
4074
    result = 1;
4075
4076
 end:
4077
    SigGroupCleanup(de_ctx);
4078
    SigCleanSignatures(de_ctx);
4079
    DetectEngineCtxFree(de_ctx);
4080
4081
    return result;
4082
}
4083
4084
static int DetectByteExtractTest58(void)
4085
{
4086
    DetectEngineCtx *de_ctx = NULL;
4087
    int result = 0;
4088
    Signature *s = NULL;
4089
    SigMatch *sm = NULL;
4090
    DetectContentData *cd = NULL;
4091
    DetectByteExtractData *bed1 = NULL;
4092
    DetectBytejumpData *bjd = NULL;
4093
    DetectIsdataatData *isdd = NULL;
4094
4095
    de_ctx = DetectEngineCtxInit();
4096
    if (de_ctx == NULL)
4097
        goto end;
4098
4099
    de_ctx->flags |= DE_QUIET;
4100
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
4101
                                   "(msg:\"Testing bytejump_body\"; "
4102
                                   "content:\"one\"; "
4103
                                   "byte_extract:4,0,two,string,hex; "
4104
                                   "byte_extract:4,0,three,string,hex; "
4105
                                   "byte_jump: 2,two; "
4106
                                   "byte_jump: 3,three; "
4107
                                   "isdataat: three; "
4108
                                   "sid:1;)");
4109
    if (de_ctx->sig_list == NULL) {
4110
        result = 0;
4111
        goto end;
4112
    }
4113
4114
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
4115
        result = 0;
4116
        goto end;
4117
    }
4118
4119
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
4120
    if (sm->type != DETECT_CONTENT) {
4121
        result = 0;
4122
        goto end;
4123
    }
4124
    cd = (DetectContentData *)sm->ctx;
4125
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
4126
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
4127
        cd->flags & DETECT_CONTENT_NOCASE ||
4128
        cd->flags & DETECT_CONTENT_WITHIN ||
4129
        cd->flags & DETECT_CONTENT_DISTANCE ||
4130
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
4131
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
4132
        cd->flags & DETECT_CONTENT_NEGATED ) {
4133
        printf("one failed\n");
4134
        result = 0;
4135
        goto end;
4136
    }
4137
4138
    sm = sm->next;
4139
    if (sm->type != DETECT_BYTE_EXTRACT) {
4140
        result = 0;
4141
        goto end;
4142
    }
4143
    bed1 = (DetectByteExtractData *)sm->ctx;
4144
    if (bed1->nbytes != 4 ||
4145
        bed1->offset != 0 ||
4146
        strcmp(bed1->name, "two") != 0 ||
4147
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
4148
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4149
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4150
        bed1->align_value != 0 ||
4151
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4152
        goto end;
4153
    }
4154
    if (bed1->local_id != 0) {
4155
        result = 0;
4156
        goto end;
4157
    }
4158
4159
    sm = sm->next;
4160
    if (sm->type != DETECT_BYTE_EXTRACT) {
4161
        result = 0;
4162
        goto end;
4163
    }
4164
4165
    sm = sm->next;
4166
    if (sm->type != DETECT_BYTEJUMP) {
4167
        result = 0;
4168
        goto end;
4169
    }
4170
    bjd = (DetectBytejumpData *)sm->ctx;
4171
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 0) {
4172
        printf("three failed\n");
4173
        result = 0;
4174
        goto end;
4175
    }
4176
4177
    sm = sm->next;
4178
    if (sm->type != DETECT_BYTEJUMP) {
4179
        result = 0;
4180
        goto end;
4181
    }
4182
    bjd = (DetectBytejumpData *)sm->ctx;
4183
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 1) {
4184
        printf("four failed\n");
4185
        result = 0;
4186
        goto end;
4187
    }
4188
4189
    sm = sm->next;
4190
    if (sm->type != DETECT_ISDATAAT) {
4191
        result = 0;
4192
        goto end;
4193
    }
4194
    isdd = (DetectIsdataatData *)sm->ctx;
4195
    if (isdd->flags != ISDATAAT_OFFSET_VAR ||
4196
        isdd->dataat != 1) {
4197
        printf("isdataat failed\n");
4198
        result = 0;
4199
        goto end;
4200
    }
4201
4202
    if (sm->next != NULL)
4203
        goto end;
4204
4205
    result = 1;
4206
4207
 end:
4208
    SigGroupCleanup(de_ctx);
4209
    SigCleanSignatures(de_ctx);
4210
    DetectEngineCtxFree(de_ctx);
4211
4212
    return result;
4213
}
4214
4215
static int DetectByteExtractTest59(void)
4216
{
4217
    DetectEngineCtx *de_ctx = NULL;
4218
    int result = 0;
4219
    Signature *s = NULL;
4220
    SigMatch *sm = NULL;
4221
    DetectContentData *cd = NULL;
4222
    DetectByteExtractData *bed1 = NULL;
4223
    DetectBytejumpData *bjd = NULL;
4224
    DetectIsdataatData *isdd = NULL;
4225
4226
    de_ctx = DetectEngineCtxInit();
4227
    if (de_ctx == NULL)
4228
        goto end;
4229
4230
    de_ctx->flags |= DE_QUIET;
4231
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
4232
                                   "(msg:\"Testing bytejump_body\"; "
4233
                                   "content:\"one\"; "
4234
                                   "byte_extract:4,0,two,string,hex; "
4235
                                   "byte_extract:4,0,three,string,hex; "
4236
                                   "byte_jump: 2,two; "
4237
                                   "byte_jump: 3,three; "
4238
                                   "isdataat: three,relative; "
4239
                                   "sid:1;)");
4240
    if (de_ctx->sig_list == NULL) {
4241
        result = 0;
4242
        goto end;
4243
    }
4244
4245
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
4246
        result = 0;
4247
        goto end;
4248
    }
4249
4250
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
4251
    if (sm->type != DETECT_CONTENT) {
4252
        result = 0;
4253
        goto end;
4254
    }
4255
    cd = (DetectContentData *)sm->ctx;
4256
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
4257
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
4258
        cd->flags & DETECT_CONTENT_NOCASE ||
4259
        cd->flags & DETECT_CONTENT_WITHIN ||
4260
        cd->flags & DETECT_CONTENT_DISTANCE ||
4261
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
4262
        cd->flags & DETECT_CONTENT_RELATIVE_NEXT ||
4263
        cd->flags & DETECT_CONTENT_NEGATED ) {
4264
        printf("one failed\n");
4265
        result = 0;
4266
        goto end;
4267
    }
4268
4269
    sm = sm->next;
4270
    if (sm->type != DETECT_BYTE_EXTRACT) {
4271
        result = 0;
4272
        goto end;
4273
    }
4274
    bed1 = (DetectByteExtractData *)sm->ctx;
4275
    if (bed1->nbytes != 4 ||
4276
        bed1->offset != 0 ||
4277
        strcmp(bed1->name, "two") != 0 ||
4278
        bed1->flags != DETECT_BYTE_EXTRACT_FLAG_STRING ||
4279
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4280
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4281
        bed1->align_value != 0 ||
4282
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4283
        goto end;
4284
    }
4285
    if (bed1->local_id != 0) {
4286
        result = 0;
4287
        goto end;
4288
    }
4289
4290
    sm = sm->next;
4291
    if (sm->type != DETECT_BYTE_EXTRACT) {
4292
        result = 0;
4293
        goto end;
4294
    }
4295
4296
    sm = sm->next;
4297
    if (sm->type != DETECT_BYTEJUMP) {
4298
        result = 0;
4299
        goto end;
4300
    }
4301
    bjd = (DetectBytejumpData *)sm->ctx;
4302
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 0) {
4303
        printf("three failed\n");
4304
        result = 0;
4305
        goto end;
4306
    }
4307
4308
    sm = sm->next;
4309
    if (sm->type != DETECT_BYTEJUMP) {
4310
        result = 0;
4311
        goto end;
4312
    }
4313
    bjd = (DetectBytejumpData *)sm->ctx;
4314
    if (bjd->flags != DETECT_BYTEJUMP_OFFSET_VAR || bjd->offset != 1) {
4315
        printf("four failed\n");
4316
        result = 0;
4317
        goto end;
4318
    }
4319
4320
    sm = sm->next;
4321
    if (sm->type != DETECT_ISDATAAT) {
4322
        result = 0;
4323
        goto end;
4324
    }
4325
    isdd = (DetectIsdataatData *)sm->ctx;
4326
    if (isdd->flags != (ISDATAAT_OFFSET_VAR |
4327
                        ISDATAAT_RELATIVE) ||
4328
        isdd->dataat != 1) {
4329
        printf("isdataat failed\n");
4330
        result = 0;
4331
        goto end;
4332
    }
4333
4334
    if (sm->next != NULL)
4335
        goto end;
4336
4337
    result = 1;
4338
4339
 end:
4340
    SigGroupCleanup(de_ctx);
4341
    SigCleanSignatures(de_ctx);
4342
    DetectEngineCtxFree(de_ctx);
4343
4344
    return result;
4345
}
4346
4347
static int DetectByteExtractTest60(void)
4348
{
4349
    DetectEngineCtx *de_ctx = NULL;
4350
    int result = 0;
4351
    Signature *s = NULL;
4352
    SigMatch *sm = NULL;
4353
    DetectContentData *cd = NULL;
4354
    DetectByteExtractData *bed1 = NULL;
4355
    DetectIsdataatData *isdd = NULL;
4356
4357
    de_ctx = DetectEngineCtxInit();
4358
    if (de_ctx == NULL)
4359
        goto end;
4360
4361
    de_ctx->flags |= DE_QUIET;
4362
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
4363
                                   "(msg:\"Testing bytejump_body\"; "
4364
                                   "content:\"one\"; "
4365
                                   "byte_extract:4,0,two,string,hex,relative; "
4366
                                   "uricontent: \"three\"; "
4367
                                   "byte_extract:4,0,four,string,hex,relative; "
4368
                                   "isdataat: two; "
4369
                                   "sid:1;)");
4370
    if (de_ctx->sig_list == NULL) {
4371
        result = 0;
4372
        goto end;
4373
    }
4374
4375
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
4376
        result = 0;
4377
        goto end;
4378
    }
4379
4380
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
4381
    if (sm->type != DETECT_CONTENT) {
4382
        result = 0;
4383
        goto end;
4384
    }
4385
    cd = (DetectContentData *)sm->ctx;
4386
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
4387
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
4388
        cd->flags & DETECT_CONTENT_NOCASE ||
4389
        cd->flags & DETECT_CONTENT_WITHIN ||
4390
        cd->flags & DETECT_CONTENT_DISTANCE ||
4391
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
4392
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
4393
        cd->flags & DETECT_CONTENT_NEGATED ) {
4394
        printf("one failed\n");
4395
        result = 0;
4396
        goto end;
4397
    }
4398
4399
    sm = sm->next;
4400
    if (sm->type != DETECT_BYTE_EXTRACT) {
4401
        result = 0;
4402
        goto end;
4403
    }
4404
    bed1 = (DetectByteExtractData *)sm->ctx;
4405
    if (bed1->nbytes != 4 ||
4406
        bed1->offset != 0 ||
4407
        strcmp(bed1->name, "two") != 0 ||
4408
        bed1->flags != (DETECT_BYTE_EXTRACT_FLAG_STRING |
4409
                        DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
4410
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4411
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4412
        bed1->align_value != 0 ||
4413
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4414
        goto end;
4415
    }
4416
    if (bed1->local_id != 0) {
4417
        result = 0;
4418
        goto end;
4419
    }
4420
4421
    sm = sm->next;
4422
    if (sm->type != DETECT_ISDATAAT) {
4423
        result = 0;
4424
        goto end;
4425
    }
4426
    isdd = (DetectIsdataatData *)sm->ctx;
4427
    if (isdd->flags != (ISDATAAT_OFFSET_VAR) ||
4428
        isdd->dataat != bed1->local_id) {
4429
        printf("isdataat failed\n");
4430
        result = 0;
4431
        goto end;
4432
    }
4433
4434
    if (sm->next != NULL)
4435
        goto end;
4436
4437
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
4438
    if (sm == NULL) {
4439
        result = 0;
4440
        goto end;
4441
    }
4442
    if (sm->type != DETECT_CONTENT) {
4443
        result = 0;
4444
        goto end;
4445
    }
4446
    cd = (DetectContentData *)sm->ctx;
4447
    if (cd->flags != DETECT_CONTENT_RELATIVE_NEXT ||
4448
        strncmp((char *)cd->content, "three", cd->content_len) != 0) {
4449
        printf("one failed\n");
4450
        result = 0;
4451
        goto end;
4452
    }
4453
4454
    sm = sm->next;
4455
    if (sm->type != DETECT_BYTE_EXTRACT) {
4456
        result = 0;
4457
        goto end;
4458
    }
4459
    bed1 = (DetectByteExtractData *)sm->ctx;
4460
    if (bed1->nbytes != 4 ||
4461
        bed1->offset != 0 ||
4462
        strcmp(bed1->name, "four") != 0 ||
4463
        bed1->flags != (DETECT_BYTE_EXTRACT_FLAG_STRING |
4464
                        DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
4465
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4466
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4467
        bed1->align_value != 0 ||
4468
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4469
        goto end;
4470
    }
4471
    if (bed1->local_id != 0) {
4472
        result = 0;
4473
        goto end;
4474
    }
4475
4476
    if (sm->next != NULL)
4477
        goto end;
4478
4479
    result = 1;
4480
4481
 end:
4482
    SigGroupCleanup(de_ctx);
4483
    SigCleanSignatures(de_ctx);
4484
    DetectEngineCtxFree(de_ctx);
4485
4486
    return result;
4487
}
4488
4489
static int DetectByteExtractTest61(void)
4490
{
4491
    DetectEngineCtx *de_ctx = NULL;
4492
    int result = 0;
4493
    Signature *s = NULL;
4494
    SigMatch *sm = NULL;
4495
    DetectContentData *cd = NULL;
4496
    DetectByteExtractData *bed1 = NULL;
4497
    DetectIsdataatData *isdd = NULL;
4498
4499
    de_ctx = DetectEngineCtxInit();
4500
    if (de_ctx == NULL)
4501
        goto end;
4502
4503
    de_ctx->flags |= DE_QUIET;
4504
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
4505
                                   "(msg:\"Testing bytejump_body\"; "
4506
                                   "content:\"one\"; "
4507
                                   "byte_extract:4,0,two,string,hex,relative; "
4508
                                   "uricontent: \"three\"; "
4509
                                   "byte_extract:4,0,four,string,hex,relative; "
4510
                                   "isdataat: four, relative; "
4511
                                   "sid:1;)");
4512
    if (de_ctx->sig_list == NULL) {
4513
        result = 0;
4514
        goto end;
4515
    }
4516
4517
    if (s->init_data->smlists_tail[DETECT_SM_LIST_PMATCH] == NULL) {
4518
        result = 0;
4519
        goto end;
4520
    }
4521
4522
    sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
4523
    if (sm->type != DETECT_CONTENT) {
4524
        result = 0;
4525
        goto end;
4526
    }
4527
    cd = (DetectContentData *)sm->ctx;
4528
    if (cd->flags & DETECT_CONTENT_RAWBYTES ||
4529
        strncmp((char *)cd->content, "one", cd->content_len) != 0 ||
4530
        cd->flags & DETECT_CONTENT_NOCASE ||
4531
        cd->flags & DETECT_CONTENT_WITHIN ||
4532
        cd->flags & DETECT_CONTENT_DISTANCE ||
4533
        cd->flags & DETECT_CONTENT_FAST_PATTERN ||
4534
        !(cd->flags & DETECT_CONTENT_RELATIVE_NEXT) ||
4535
        cd->flags & DETECT_CONTENT_NEGATED ) {
4536
        printf("one failed\n");
4537
        result = 0;
4538
        goto end;
4539
    }
4540
4541
    sm = sm->next;
4542
    if (sm->type != DETECT_BYTE_EXTRACT) {
4543
        result = 0;
4544
        goto end;
4545
    }
4546
    bed1 = (DetectByteExtractData *)sm->ctx;
4547
    if (bed1->nbytes != 4 ||
4548
        bed1->offset != 0 ||
4549
        strcmp(bed1->name, "two") != 0 ||
4550
        bed1->flags != (DETECT_BYTE_EXTRACT_FLAG_STRING |
4551
                        DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
4552
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4553
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4554
        bed1->align_value != 0 ||
4555
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4556
        goto end;
4557
    }
4558
    if (bed1->local_id != 0) {
4559
        result = 0;
4560
        goto end;
4561
    }
4562
4563
    if (sm->next != NULL)
4564
        goto end;
4565
4566
    sm = DetectBufferGetFirstSigMatch(s, g_http_uri_buffer_id);
4567
    if (sm == NULL) {
4568
        result = 0;
4569
        goto end;
4570
    }
4571
    if (sm->type != DETECT_CONTENT) {
4572
        result = 0;
4573
        goto end;
4574
    }
4575
    cd = (DetectContentData *)sm->ctx;
4576
    if (cd->flags != DETECT_CONTENT_RELATIVE_NEXT ||
4577
        strncmp((char *)cd->content, "three", cd->content_len) != 0) {
4578
        printf("one failed\n");
4579
        result = 0;
4580
        goto end;
4581
    }
4582
4583
    sm = sm->next;
4584
    if (sm->type != DETECT_BYTE_EXTRACT) {
4585
        result = 0;
4586
        goto end;
4587
    }
4588
    bed1 = (DetectByteExtractData *)sm->ctx;
4589
    if (bed1->nbytes != 4 ||
4590
        bed1->offset != 0 ||
4591
        strcmp(bed1->name, "four") != 0 ||
4592
        bed1->flags != (DETECT_BYTE_EXTRACT_FLAG_STRING |
4593
                        DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
4594
        bed1->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4595
        bed1->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4596
        bed1->align_value != 0 ||
4597
        bed1->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4598
        goto end;
4599
    }
4600
    if (bed1->local_id != 0) {
4601
        result = 0;
4602
        goto end;
4603
    }
4604
4605
    sm = sm->next;
4606
    if (sm->type != DETECT_ISDATAAT) {
4607
        result = 0;
4608
        goto end;
4609
    }
4610
    isdd = (DetectIsdataatData *)sm->ctx;
4611
    if (isdd->flags != (ISDATAAT_OFFSET_VAR |
4612
                        ISDATAAT_RELATIVE) ||
4613
        isdd->dataat != bed1->local_id) {
4614
        printf("isdataat failed\n");
4615
        result = 0;
4616
        goto end;
4617
    }
4618
4619
    if (sm->next != NULL)
4620
        goto end;
4621
4622
    result = 1;
4623
4624
 end:
4625
    SigGroupCleanup(de_ctx);
4626
    SigCleanSignatures(de_ctx);
4627
    DetectEngineCtxFree(de_ctx);
4628
4629
    return result;
4630
}
4631
4632
static int DetectByteExtractTest62(void)
4633
{
4634
    DetectEngineCtx *de_ctx = NULL;
4635
    int result = 0;
4636
    Signature *s = NULL;
4637
    SigMatch *sm = NULL;
4638
    DetectByteExtractData *bed = NULL;
4639
4640
    de_ctx = DetectEngineCtxInit();
4641
    if (de_ctx == NULL)
4642
        goto end;
4643
4644
    de_ctx->flags |= DE_QUIET;
4645
    s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
4646
                                   "(file_data; byte_extract:4,2,two,relative,string,hex; "
4647
                                   "sid:1;)");
4648
    if (de_ctx->sig_list == NULL) {
4649
        goto end;
4650
    }
4651
4652
    sm = DetectBufferGetFirstSigMatch(s, g_file_data_buffer_id);
4653
    if (sm == NULL) {
4654
        goto end;
4655
    }
4656
    if (sm->type != DETECT_BYTE_EXTRACT) {
4657
        goto end;
4658
    }
4659
    bed = (DetectByteExtractData *)sm->ctx;
4660
    if (bed->nbytes != 4 ||
4661
        bed->offset != 2 ||
4662
        strncmp(bed->name, "two", 3) != 0 ||
4663
        bed->flags != (DETECT_BYTE_EXTRACT_FLAG_STRING | DETECT_BYTE_EXTRACT_FLAG_RELATIVE) ||
4664
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE ||
4665
        bed->base != DETECT_BYTE_EXTRACT_BASE_HEX ||
4666
        bed->align_value != 0 ||
4667
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4668
        goto end;
4669
    }
4670
4671
    result = 1;
4672
4673
 end:
4674
    SigGroupCleanup(de_ctx);
4675
    SigCleanSignatures(de_ctx);
4676
    DetectEngineCtxFree(de_ctx);
4677
4678
    return result;
4679
}
4680
4681
static int DetectByteExtractTest63(void)
4682
{
4683
    int result = 0;
4684
4685
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, -2, one");
4686
    if (bed == NULL)
4687
        goto end;
4688
4689
    if (bed->nbytes != 4 ||
4690
        bed->offset != -2 ||
4691
        strcmp(bed->name, "one") != 0 ||
4692
        bed->flags != 0 ||
4693
        bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT ||
4694
        bed->base != DETECT_BYTE_EXTRACT_BASE_NONE ||
4695
        bed->align_value != 0 ||
4696
        bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4697
        goto end;
4698
    }
4699
4700
    result = 1;
4701
 end:
4702
    if (bed != NULL)
4703
        DetectByteExtractFree(NULL, bed);
4704
    return result;
4705
}
4706
4707
static int DetectByteExtractTestParseNoBase(void)
4708
{
4709
    int result = 0;
4710
4711
    DetectByteExtractData *bed = DetectByteExtractParse(NULL, "4, 2, one, string");
4712
    if (bed == NULL)
4713
        goto end;
4714
4715
    if (bed->nbytes != 4) {
4716
        goto end;
4717
    }
4718
    if (bed->offset != 2) {
4719
        goto end;
4720
    }
4721
    if (strcmp(bed->name, "one") != 0) {
4722
        goto end;
4723
    }
4724
    if (bed->flags != DETECT_BYTE_EXTRACT_FLAG_STRING) {
4725
        goto end;
4726
    }
4727
    if (bed->endian != DETECT_BYTE_EXTRACT_ENDIAN_NONE) {
4728
        goto end;
4729
    }
4730
    if (bed->base != DETECT_BYTE_EXTRACT_BASE_DEC) {
4731
        goto end;
4732
    }
4733
    if (bed->align_value != 0) {
4734
        goto end;
4735
    }
4736
    if (bed->multiplier_value != DETECT_BYTE_EXTRACT_MULTIPLIER_DEFAULT) {
4737
        goto end;
4738
    }
4739
4740
    result = 1;
4741
 end:
4742
    if (bed != NULL)
4743
        DetectByteExtractFree(NULL, bed);
4744
    return result;
4745
}
4746
4747
static void DetectByteExtractRegisterTests(void)
4748
{
4749
    g_file_data_buffer_id = DetectBufferTypeGetByName("file_data");
4750
    g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
4751
4752
    UtRegisterTest("DetectByteExtractTest01", DetectByteExtractTest01);
4753
    UtRegisterTest("DetectByteExtractTest02", DetectByteExtractTest02);
4754
    UtRegisterTest("DetectByteExtractTest03", DetectByteExtractTest03);
4755
    UtRegisterTest("DetectByteExtractTest04", DetectByteExtractTest04);
4756
    UtRegisterTest("DetectByteExtractTest05", DetectByteExtractTest05);
4757
    UtRegisterTest("DetectByteExtractTest06", DetectByteExtractTest06);
4758
    UtRegisterTest("DetectByteExtractTest07", DetectByteExtractTest07);
4759
    UtRegisterTest("DetectByteExtractTest08", DetectByteExtractTest08);
4760
    UtRegisterTest("DetectByteExtractTest09", DetectByteExtractTest09);
4761
    UtRegisterTest("DetectByteExtractTest10", DetectByteExtractTest10);
4762
    UtRegisterTest("DetectByteExtractTest11", DetectByteExtractTest11);
4763
    UtRegisterTest("DetectByteExtractTest12", DetectByteExtractTest12);
4764
    UtRegisterTest("DetectByteExtractTest13", DetectByteExtractTest13);
4765
    UtRegisterTest("DetectByteExtractTest14", DetectByteExtractTest14);
4766
    UtRegisterTest("DetectByteExtractTest15", DetectByteExtractTest15);
4767
    UtRegisterTest("DetectByteExtractTest16", DetectByteExtractTest16);
4768
    UtRegisterTest("DetectByteExtractTest17", DetectByteExtractTest17);
4769
    UtRegisterTest("DetectByteExtractTest18", DetectByteExtractTest18);
4770
    UtRegisterTest("DetectByteExtractTest19", DetectByteExtractTest19);
4771
    UtRegisterTest("DetectByteExtractTest20", DetectByteExtractTest20);
4772
    UtRegisterTest("DetectByteExtractTest21", DetectByteExtractTest21);
4773
    UtRegisterTest("DetectByteExtractTest22", DetectByteExtractTest22);
4774
    UtRegisterTest("DetectByteExtractTest23", DetectByteExtractTest23);
4775
    UtRegisterTest("DetectByteExtractTest24", DetectByteExtractTest24);
4776
    UtRegisterTest("DetectByteExtractTest25", DetectByteExtractTest25);
4777
    UtRegisterTest("DetectByteExtractTest26", DetectByteExtractTest26);
4778
    UtRegisterTest("DetectByteExtractTest27", DetectByteExtractTest27);
4779
    UtRegisterTest("DetectByteExtractTest28", DetectByteExtractTest28);
4780
    UtRegisterTest("DetectByteExtractTest29", DetectByteExtractTest29);
4781
    UtRegisterTest("DetectByteExtractTest30", DetectByteExtractTest30);
4782
    UtRegisterTest("DetectByteExtractTest31", DetectByteExtractTest31);
4783
    UtRegisterTest("DetectByteExtractTest32", DetectByteExtractTest32);
4784
    UtRegisterTest("DetectByteExtractTest33", DetectByteExtractTest33);
4785
    UtRegisterTest("DetectByteExtractTest34", DetectByteExtractTest34);
4786
    UtRegisterTest("DetectByteExtractTest35", DetectByteExtractTest35);
4787
    UtRegisterTest("DetectByteExtractTest36", DetectByteExtractTest36);
4788
    UtRegisterTest("DetectByteExtractTest37", DetectByteExtractTest37);
4789
    UtRegisterTest("DetectByteExtractTest38", DetectByteExtractTest38);
4790
    UtRegisterTest("DetectByteExtractTest39", DetectByteExtractTest39);
4791
    UtRegisterTest("DetectByteExtractTest40", DetectByteExtractTest40);
4792
    UtRegisterTest("DetectByteExtractTest41", DetectByteExtractTest41);
4793
    UtRegisterTest("DetectByteExtractTest42", DetectByteExtractTest42);
4794
4795
    UtRegisterTest("DetectByteExtractTest43", DetectByteExtractTest43);
4796
    UtRegisterTest("DetectByteExtractTest44", DetectByteExtractTest44);
4797
4798
    UtRegisterTest("DetectByteExtractTest45", DetectByteExtractTest45);
4799
    UtRegisterTest("DetectByteExtractTest46", DetectByteExtractTest46);
4800
4801
    UtRegisterTest("DetectByteExtractTest47", DetectByteExtractTest47);
4802
    UtRegisterTest("DetectByteExtractTest48", DetectByteExtractTest48);
4803
4804
    UtRegisterTest("DetectByteExtractTest49", DetectByteExtractTest49);
4805
    UtRegisterTest("DetectByteExtractTest50", DetectByteExtractTest50);
4806
4807
    UtRegisterTest("DetectByteExtractTest51", DetectByteExtractTest51);
4808
    UtRegisterTest("DetectByteExtractTest52", DetectByteExtractTest52);
4809
4810
    UtRegisterTest("DetectByteExtractTest53", DetectByteExtractTest53);
4811
    UtRegisterTest("DetectByteExtractTest54", DetectByteExtractTest54);
4812
4813
    UtRegisterTest("DetectByteExtractTest55", DetectByteExtractTest55);
4814
    UtRegisterTest("DetectByteExtractTest56", DetectByteExtractTest56);
4815
    UtRegisterTest("DetectByteExtractTest57", DetectByteExtractTest57);
4816
4817
    UtRegisterTest("DetectByteExtractTest58", DetectByteExtractTest58);
4818
    UtRegisterTest("DetectByteExtractTest59", DetectByteExtractTest59);
4819
    UtRegisterTest("DetectByteExtractTest60", DetectByteExtractTest60);
4820
    UtRegisterTest("DetectByteExtractTest61", DetectByteExtractTest61);
4821
    UtRegisterTest("DetectByteExtractTest62", DetectByteExtractTest62);
4822
    UtRegisterTest("DetectByteExtractTest63", DetectByteExtractTest63);
4823
4824
    UtRegisterTest("DetectByteExtractTestParseNoBase",
4825
                   DetectByteExtractTestParseNoBase);
4826
}
4827
#endif /* UNITTESTS */