• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 22618661228

02 Mar 2026 09:33PM UTC coverage: 42.258% (-34.4%) from 76.611%
22618661228

push

github

victorjulien
github-actions: bump actions/download-artifact from 7.0.0 to 8.0.0

Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 7.0.0 to 8.0.0.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](https://github.com/actions/download-artifact/compare/37930b1c2...70fc10c6e)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-version: 8.0.0
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

91511 of 216553 relevant lines covered (42.26%)

3416852.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.87
/src/detect-engine-payload.c
1
/* Copyright (C) 2007-2021 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \file
20
 *
21
 * \author Victor Julien <victor@inliniac.net>
22
 *
23
 * Performs payload matching functions
24
 */
25

26
#include "suricata-common.h"
27
#include "suricata.h"
28
#include "rust.h"
29

30
#include "decode.h"
31

32
#include "detect.h"
33
#include "detect-engine.h"
34
#include "detect-parse.h"
35
#include "detect-engine-content-inspection.h"
36
#include "detect-engine-prefilter.h"
37
#include "detect-engine-state.h"
38
#include "detect-engine-payload.h"
39
#include "detect-engine-build.h"
40

41
#include "stream.h"
42
#include "stream-tcp.h"
43

44
#include "util-debug.h"
45
#include "util-print.h"
46

47
#include "util-unittest.h"
48
#include "util-unittest-helper.h"
49
#include "util-validate.h"
50
#include "util-profiling.h"
51
#include "util-mpm-ac.h"
52

53
struct StreamMpmData {
54
    DetectEngineThreadCtx *det_ctx;
55
    const MpmCtx *mpm_ctx;
56
};
57

58
static int StreamMpmFunc(
59
        void *cb_data, const uint8_t *data, const uint32_t data_len, const uint64_t _offset)
60
{
18,557✔
61
    struct StreamMpmData *smd = cb_data;
18,557✔
62
    if (data_len >= smd->mpm_ctx->minlen) {
18,558✔
63
#ifdef DEBUG
64
        smd->det_ctx->stream_mpm_cnt++;
65
        smd->det_ctx->stream_mpm_size += data_len;
66
#endif
67
        (void)mpm_table[smd->mpm_ctx->mpm_type].Search(
18,558✔
68
                smd->mpm_ctx, &smd->det_ctx->mtc, &smd->det_ctx->pmq, data, data_len);
18,558✔
69
        PREFILTER_PROFILING_ADD_BYTES(smd->det_ctx, data_len);
18,558✔
70
    }
18,558✔
71
    return 0;
18,557✔
72
}
18,557✔
73

74
static void PrefilterPktStream(DetectEngineThreadCtx *det_ctx,
75
        Packet *p, const void *pectx)
76
{
331,957✔
77
    SCEnter();
331,957✔
78

79
    const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
331,957✔
80

81
    /* for established packets inspect any stream we may have queued up */
82
    if (p->flags & PKT_DETECT_HAS_STREAMDATA) {
331,957✔
83
        SCLogDebug("PRE det_ctx->raw_stream_progress %"PRIu64,
19,113✔
84
                det_ctx->raw_stream_progress);
19,113✔
85
        struct StreamMpmData stream_mpm_data = { det_ctx, mpm_ctx };
19,113✔
86
        StreamReassembleRaw(p->flow->protoctx, p,
19,113✔
87
                StreamMpmFunc, &stream_mpm_data,
19,113✔
88
                &det_ctx->raw_stream_progress,
19,113✔
89
                false /* mpm doesn't use min inspect depth */);
19,113✔
90
        SCLogDebug("POST det_ctx->raw_stream_progress %"PRIu64,
19,113✔
91
                det_ctx->raw_stream_progress);
19,113✔
92

93
        /* packets that have not been added to the stream will be inspected as if they are stream
94
         * chunks */
95
    } else if ((p->flags & (PKT_NOPAYLOAD_INSPECTION | PKT_STREAM_ADD)) == 0) {
312,844✔
96
        if (p->payload_len >= mpm_ctx->minlen) {
9,192✔
97
#ifdef DEBUG
98
            det_ctx->payload_mpm_cnt++;
99
            det_ctx->payload_mpm_size += p->payload_len;
100
#endif
101
            (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
9,191✔
102
                    &det_ctx->mtc, &det_ctx->pmq,
9,191✔
103
                    p->payload, p->payload_len);
9,191✔
104
            PREFILTER_PROFILING_ADD_BYTES(det_ctx, p->payload_len);
9,191✔
105
        }
9,191✔
106
    }
9,192✔
107
}
331,957✔
108

109
int PrefilterPktStreamRegister(DetectEngineCtx *de_ctx,
110
        SigGroupHead *sgh, MpmCtx *mpm_ctx)
111
{
58✔
112
    return PrefilterAppendPayloadEngine(de_ctx, sgh,
58✔
113
            PrefilterPktStream, mpm_ctx, NULL, "stream");
58✔
114
}
58✔
115

116
static void PrefilterPktPayload(DetectEngineThreadCtx *det_ctx,
117
        Packet *p, const void *pectx)
118
{
782,599✔
119
    SCEnter();
782,599✔
120

121
    const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
782,599✔
122
    if (p->payload_len < mpm_ctx->minlen)
782,599✔
123
        SCReturn;
6,581✔
124

125
    (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
776,018✔
126
            &det_ctx->mtc, &det_ctx->pmq,
776,018✔
127
            p->payload, p->payload_len);
776,018✔
128

129
    PREFILTER_PROFILING_ADD_BYTES(det_ctx, p->payload_len);
776,018✔
130
}
776,018✔
131

132
int PrefilterPktPayloadRegister(DetectEngineCtx *de_ctx,
133
        SigGroupHead *sgh, MpmCtx *mpm_ctx)
134
{
120✔
135
    return PrefilterAppendPayloadEngine(de_ctx, sgh,
120✔
136
            PrefilterPktPayload, mpm_ctx, NULL, "payload");
120✔
137
}
120✔
138

139

140
/**
141
 *  \brief Do the content inspection & validation for a signature
142
 *
143
 *  \param de_ctx Detection engine context
144
 *  \param det_ctx Detection engine thread context
145
 *  \param s Signature to inspect
146
 *  \param f flow (for pcre flowvar storage)
147
 *  \param p Packet
148
 *
149
 *  \retval 0 no match
150
 *  \retval 1 match
151
 */
152
uint8_t DetectEngineInspectPacketPayload(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
153
        const Signature *s, Flow *f, Packet *p)
154
{
17,604✔
155
    SCEnter();
17,604✔
156

157
    if (s->sm_arrays[DETECT_SM_LIST_PMATCH] == NULL) {
17,604✔
158
        SCReturnInt(0);
×
159
    }
×
160
#ifdef DEBUG
161
    det_ctx->payload_persig_cnt++;
162
    det_ctx->payload_persig_size += p->payload_len;
163
#endif
164
    const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s,
17,604✔
165
            s->sm_arrays[DETECT_SM_LIST_PMATCH], p, f, p->payload, p->payload_len, 0,
17,604✔
166
            DETECT_CI_FLAGS_SINGLE, DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD);
17,604✔
167
    if (match) {
17,604✔
168
        SCReturnInt(1);
1,708✔
169
    }
1,708✔
170
    SCReturnInt(0);
17,604✔
171
}
17,604✔
172

173
/**
174
 *  \brief Do the content inspection & validation for a sigmatch list
175
 *
176
 *  \param de_ctx Detection engine context
177
 *  \param det_ctx Detection engine thread context
178
 *  \param s Signature to inspect
179
 *  \param smd array of matches to eval
180
 *  \param f flow (for pcre flowvar storage)
181
 *  \param p Packet
182
 *
183
 *  \retval 0 no match
184
 *  \retval 1 match
185
 */
186
static uint8_t DetectEngineInspectStreamUDPPayload(DetectEngineCtx *de_ctx,
187
        DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f,
188
        Packet *p)
189
{
×
190
    SCEnter();
×
191

192
    if (smd == NULL) {
×
193
        SCReturnInt(0);
×
194
    }
×
195
#ifdef DEBUG
196
    det_ctx->payload_persig_cnt++;
197
    det_ctx->payload_persig_size += p->payload_len;
198
#endif
199
    const bool match =
×
200
            DetectEngineContentInspection(de_ctx, det_ctx, s, smd, p, f, p->payload, p->payload_len,
×
201
                    0, DETECT_CI_FLAGS_SINGLE, DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD);
×
202
    if (match) {
×
203
        SCReturnInt(1);
×
204
    }
×
205
    SCReturnInt(0);
×
206
}
×
207

208
struct StreamContentInspectData {
209
    DetectEngineCtx *de_ctx;
210
    DetectEngineThreadCtx *det_ctx;
211
    const Signature *s;
212
    Flow *f;
213
};
214

215
static int StreamContentInspectFunc(
216
        void *cb_data, const uint8_t *data, const uint32_t data_len, const uint64_t _offset)
217
{
1,581✔
218
    SCEnter();
1,581✔
219
    struct StreamContentInspectData *smd = cb_data;
1,581✔
220
#ifdef DEBUG
221
    smd->det_ctx->stream_persig_cnt++;
222
    smd->det_ctx->stream_persig_size += data_len;
223
#endif
224

225
    const bool match = DetectEngineContentInspection(smd->de_ctx, smd->det_ctx, smd->s,
1,581✔
226
            smd->s->sm_arrays[DETECT_SM_LIST_PMATCH], NULL, smd->f, data, data_len, 0,
1,581✔
227
            0, // TODO
1,581✔
228
            DETECT_ENGINE_CONTENT_INSPECTION_MODE_STREAM);
1,581✔
229
    if (match) {
1,581✔
230
        SCReturnInt(1);
91✔
231
    }
91✔
232

233
    SCReturnInt(0);
1,581✔
234
}
1,581✔
235

236
/**
237
 *  \brief Do the content inspection & validation for a signature
238
 *         on the raw stream
239
 *
240
 *  \param de_ctx Detection engine context
241
 *  \param det_ctx Detection engine thread context
242
 *  \param s Signature to inspect
243
 *  \param f flow (for pcre flowvar storage)
244
 *
245
 *  \retval 0 no match
246
 *  \retval 1 match
247
 */
248
int DetectEngineInspectStreamPayload(DetectEngineCtx *de_ctx,
249
        DetectEngineThreadCtx *det_ctx, const Signature *s,
250
        Flow *f, Packet *p)
251
{
1,574✔
252
    SCEnter();
1,574✔
253
    SCLogDebug("FLUSH? %s", (s->flags & SIG_FLAG_FLUSH)?"true":"false");
1,574✔
254
    uint64_t unused;
1,574✔
255
    struct StreamContentInspectData inspect_data = { de_ctx, det_ctx, s, f };
1,574✔
256
    int r = StreamReassembleRaw(f->protoctx, p,
1,574✔
257
            StreamContentInspectFunc, &inspect_data,
1,574✔
258
            &unused, ((s->flags & SIG_FLAG_FLUSH) != 0));
1,574✔
259
    return r;
1,574✔
260
}
1,574✔
261

262
struct StreamContentInspectEngineData {
263
    DetectEngineCtx *de_ctx;
264
    DetectEngineThreadCtx *det_ctx;
265
    const Signature *s;
266
    const SigMatchData *smd;
267
    Flow *f;
268
};
269

270
static int StreamContentInspectEngineFunc(
271
        void *cb_data, const uint8_t *data, const uint32_t data_len, const uint64_t _offset)
272
{
3✔
273
    SCEnter();
3✔
274
    struct StreamContentInspectEngineData *smd = cb_data;
3✔
275
#ifdef DEBUG
276
    smd->det_ctx->stream_persig_cnt++;
277
    smd->det_ctx->stream_persig_size += data_len;
278
#endif
279

280
    const bool match = DetectEngineContentInspection(smd->de_ctx, smd->det_ctx, smd->s, smd->smd,
3✔
281
            NULL, smd->f, data, data_len, 0, 0, // TODO
3✔
282
            DETECT_ENGINE_CONTENT_INSPECTION_MODE_STREAM);
3✔
283
    if (match) {
3✔
284
        SCReturnInt(1);
3✔
285
    }
3✔
286

287
    SCReturnInt(0);
3✔
288
}
3✔
289

290
/**
291
 *  \brief inspect engine for stateful rules
292
 *
293
 *  Caches results as it may be called multiple times if we inspect
294
 *  multiple transactions in one packet.
295
 *
296
 *  Returns "can't match" if depth is reached.
297
 */
298
uint8_t DetectEngineInspectStream(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
299
        const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
300
        uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
301
{
3✔
302
    Packet *p = det_ctx->p; /* TODO: get rid of this HACK */
3✔
303

304
    /* in certain sigs, e.g. 'alert dns', which apply to both tcp and udp
305
     * we can get called for UDP. Then we simply inspect the packet payload */
306
    if (p->proto == IPPROTO_UDP) {
3✔
307
        return DetectEngineInspectStreamUDPPayload(de_ctx, det_ctx, s, engine->smd, f, p);
×
308
        /* for other non-TCP protocols we assume match */
309
    } else if (p->proto != IPPROTO_TCP)
3✔
310
        return DETECT_ENGINE_INSPECT_SIG_MATCH;
×
311

312
    TcpSession *ssn = f->protoctx;
3✔
313
    if (ssn == NULL)
3✔
314
        return DETECT_ENGINE_INSPECT_SIG_CANT_MATCH;
×
315

316
    SCLogDebug("pre-inspect det_ctx->raw_stream_progress %"PRIu64" FLUSH? %s",
3✔
317
            det_ctx->raw_stream_progress,
3✔
318
            (s->flags & SIG_FLAG_FLUSH)?"true":"false");
3✔
319
    uint64_t unused;
3✔
320
    struct StreamContentInspectEngineData inspect_data = { de_ctx, det_ctx, s, engine->smd, f };
3✔
321
    int match = StreamReassembleRaw(f->protoctx, p,
3✔
322
            StreamContentInspectEngineFunc, &inspect_data,
3✔
323
            &unused, ((s->flags & SIG_FLAG_FLUSH) != 0));
3✔
324

325
    bool is_last = false;
3✔
326
    if (flags & STREAM_TOSERVER) {
3✔
327
        TcpStream *stream = &ssn->client;
3✔
328
        if (stream->flags & STREAMTCP_STREAM_FLAG_DEPTH_REACHED)
3✔
329
            is_last = true;
×
330
    } else {
3✔
331
        TcpStream *stream = &ssn->server;
×
332
        if (stream->flags & STREAMTCP_STREAM_FLAG_DEPTH_REACHED)
×
333
            is_last = true;
×
334
    }
×
335

336
    SCLogDebug("%s ran stream for sid %u on packet %" PRIu64 " and we %s",
3✔
337
            is_last ? "LAST:" : "normal:", s->id, PcapPacketCntGet(p),
3✔
338
            match ? "matched" : "didn't match");
3✔
339

340
    if (match) {
3✔
341
        return DETECT_ENGINE_INSPECT_SIG_MATCH;
3✔
342
    } else {
3✔
343
        if (is_last) {
×
344
            //SCLogNotice("last, so DETECT_ENGINE_INSPECT_SIG_CANT_MATCH");
345
            return DETECT_ENGINE_INSPECT_SIG_CANT_MATCH;
×
346
        }
×
347
        /* TODO maybe we can set 'CANT_MATCH' for EOF too? */
348
        return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
×
349
    }
×
350
}
3✔
351

352
#ifdef UNITTESTS
353
#include "detect-engine-alert.h"
354

355
/** \test Not the first but the second occurrence of "abc" should be used
356
 *       for the 2nd match */
357
static int PayloadTestSig01 (void)
358
{
359
    uint8_t *buf = (uint8_t *)
360
                    "abcabcd";
361
    uint16_t buflen = strlen((char *)buf);
362
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
363

364
    FAIL_IF_NULL(p);
365

366
    char sig[] = "alert tcp any any -> any any (content:\"abc\"; content:\"d\"; distance:0; within:1; sid:1;)";
367

368
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
369

370
    UTHFreePacket(p);
371

372
    PASS;
373
}
374

375
/** \test Nocase matching */
376
static int PayloadTestSig02 (void)
377
{
378
    uint8_t *buf = (uint8_t *)
379
                    "abcaBcd";
380
    uint16_t buflen = strlen((char *)buf);
381
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
382

383
    FAIL_IF_NULL(p);
384

385
    char sig[] = "alert tcp any any -> any any (content:\"abc\"; nocase; content:\"d\"; distance:0; within:1; sid:1;)";
386

387
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
388

389
    UTHFreePacket(p);
390

391
    PASS;
392
}
393

394
/** \test Negative distance matching */
395
static int PayloadTestSig03 (void)
396
{
397
    uint8_t *buf = (uint8_t *)
398
                    "abcaBcd";
399
    uint16_t buflen = strlen((char *)buf);
400
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
401

402
    FAIL_IF_NULL(p);
403

404
    char sig[] = "alert tcp any any -> any any (content:\"aBc\"; nocase; content:\"abca\"; distance:-10; within:4; sid:1;)";
405

406
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
407

408
    UTHFreePacket(p);
409

410
    PASS;
411
}
412

413
/**
414
 * \test Test multiple relative matches.
415
 */
416
static int PayloadTestSig04(void)
417
{
418
    uint8_t *buf = (uint8_t *)"now this is is big big string now";
419
    uint16_t buflen = strlen((char *)buf);
420
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
421

422
    FAIL_IF_NULL(p);
423

424
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
425
        "content:\"this\"; content:\"is\"; within:6; content:\"big\"; within:8; "
426
        "content:\"string\"; within:8; sid:1;)";
427

428
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
429

430
    UTHFreePacket(p);
431

432
    PASS;
433
}
434

435
/**
436
 * \test Test multiple relative matches.
437
 */
438
static int PayloadTestSig05(void)
439
{
440
    uint8_t *buf = (uint8_t *)"now this is is is big big big string now";
441
    uint16_t buflen = strlen((char *)buf);
442
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
443

444
    FAIL_IF_NULL(p);
445

446
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
447
        "content:\"this\"; content:\"is\"; within:9; content:\"big\"; within:12; "
448
        "content:\"string\"; within:8; sid:1;)";
449

450
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
451

452
    UTHFreePacket(p);
453

454
    PASS;
455
}
456

457
/**
458
 * \test Test multiple relative matches.
459
 */
460
static int PayloadTestSig06(void)
461
{
462
    uint8_t *buf = (uint8_t *)"this this now is is     big string now";
463
    uint16_t buflen = strlen((char *)buf);
464
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
465

466
    FAIL_IF_NULL(p);
467

468
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
469
        "content:\"now\"; content:\"this\"; content:\"is\"; within:12; content:\"big\"; within:8; "
470
        "content:\"string\"; within:8; sid:1;)";
471

472
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
473

474
    UTHFreePacket(p);
475

476
    PASS;
477
}
478

479
/**
480
 * \test Test multiple relative matches.
481
 */
482
static int PayloadTestSig07(void)
483
{
484
    uint8_t *buf = (uint8_t *)"         thus thus is a big";
485
    uint16_t buflen = strlen((char *)buf);
486
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
487

488
    FAIL_IF_NULL(p);
489

490
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
491
        "content:\"thus\"; offset:8; content:\"is\"; within:6; content:\"big\"; within:8; sid:1;)";
492

493
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
494

495
    UTHFreePacket(p);
496

497
    PASS;
498
}
499

500
/**
501
 * \test Test multiple relative matches with negative matches
502
 *       and show the need for det_ctx->discontinue_matching.
503
 */
504
static int PayloadTestSig08(void)
505
{
506
    uint8_t *buf = (uint8_t *)"we need to fix this and yes fix this now";
507
    uint16_t buflen = strlen((char *)buf);
508
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
509

510
    FAIL_IF_NULL(p);
511

512
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
513
        "content:\"fix\"; content:\"this\"; within:6; content:!\"and\"; distance:0; sid:1;)";
514

515
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) != 1);
516

517
    UTHFreePacket(p);
518

519
    PASS;
520
}
521

522
/**
523
 * \test Test pcre recursive matching.
524
 */
525
static int PayloadTestSig09(void)
526
{
527
    uint8_t *buf = (uint8_t *)"this is a super duper nova in super nova now";
528
    uint16_t buflen = strlen((char *)buf);
529
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
530

531
    FAIL_IF_NULL(p);
532

533
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
534
        "pcre:/super/; content:\"nova\"; within:7; sid:1;)";
535

536
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
537

538
    UTHFreePacket(p);
539

540
    PASS;
541
}
542

543
/**
544
 * \test Test invalid sig.
545
 */
546
static int PayloadTestSig10(void)
547
{
548
    uint8_t *buf = (uint8_t *)"this is a super duper nova in super nova now";
549
    uint16_t buflen = strlen((char *)buf);
550
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
551

552
    FAIL_IF_NULL(p);
553

554
    char sig[] = "alert udp any any -> any any (msg:\"crash\"; "
555
        "byte_test:4,>,2,0,relative; sid:11;)";
556

557
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 1);
558

559
    UTHFreePacket(p);
560

561
    PASS;
562
}
563

564
/**
565
 * \test Test invalid sig.
566
 */
567
static int PayloadTestSig11(void)
568
{
569
    uint8_t *buf = (uint8_t *)"this is a super duper nova in super nova now";
570
    uint16_t buflen = strlen((char *)buf);
571
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
572

573
    FAIL_IF_NULL(p);
574

575
    char sig[] = "alert udp any any -> any any (msg:\"crash\"; "
576
        "byte_jump:1,0,relative; sid:11;)";
577

578
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 1);
579

580
    UTHFreePacket(p);
581

582
    PASS;
583
}
584

585
/**
586
 * \test Test invalid sig.
587
 */
588
static int PayloadTestSig12(void)
589
{
590
    uint8_t *buf = (uint8_t *)"this is a super duper nova in super nova now";
591
    uint16_t buflen = strlen((char *)buf);
592
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
593

594
    FAIL_IF_NULL(p);
595

596
    char sig[] = "alert udp any any -> any any (msg:\"crash\"; "
597
        "isdataat:10,relative; sid:11;)";
598

599
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 1);
600

601
    UTHFreePacket(p);
602

603
    PASS;
604
}
605

606
/**
607
 * \test Used to check the working of recursion_limit counter.
608
 */
609
static int PayloadTestSig13(void)
610
{
611
    uint8_t *buf = (uint8_t *)"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
612
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
613
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
614
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
615
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
616
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
617
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
618
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
619
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
620
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
621
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
622
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
623
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
624
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
625
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
626
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
627
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
628
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
629
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
630
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
631

632
    uint16_t buflen = strlen((char *)buf);
633
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
634
    uint16_t mpm_type = mpm_default_matcher;
635

636
    FAIL_IF_NULL(p);
637

638
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
639
        "content:\"aa\"; content:\"aa\"; distance:0; content:\"aa\"; distance:0; "
640
        "byte_test:1,>,200,0,relative; sid:1;)";
641

642
    DecodeThreadVars dtv;
643
    ThreadVars th_v;
644
    DetectEngineThreadCtx *det_ctx = NULL;
645

646
    memset(&dtv, 0, sizeof(DecodeThreadVars));
647
    memset(&th_v, 0, sizeof(th_v));
648
    StatsThreadInit(&th_v.stats);
649

650
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
651
    FAIL_IF_NULL(de_ctx);
652
    de_ctx->inspection_recursion_limit = 3000;
653
    de_ctx->flags |= DE_QUIET;
654
    de_ctx->mpm_matcher = mpm_type;
655

656
    Signature *s = DetectEngineAppendSig(de_ctx, sig);
657
    FAIL_IF_NULL(s);
658

659
    SigGroupBuild(de_ctx);
660
    DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
661

662
    SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
663
    FAIL_IF_NOT(PacketAlertCheck(p, de_ctx->sig_list->id) != 1);
664

665
    UTHFreePacket(p);
666
    DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
667
    DetectEngineCtxFree(de_ctx);
668
    StatsThreadCleanup(&th_v.stats);
669
    PASS;
670
}
671

672
/**
673
 * \test normal & negated matching, both absolute and relative
674
 */
675
static int PayloadTestSig14(void)
676
{
677
    uint8_t *buf = (uint8_t *)"User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b4) Gecko/20090423 Firefox/3.6 GTB5";
678
    uint16_t buflen = strlen((char *)buf);
679
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
680

681
    FAIL_IF_NULL(p);
682

683
    char sig[] = "alert tcp any any -> any any (content:\"User-Agent|3A| Mozilla/5.0 |28|Macintosh|3B| \"; content:\"Firefox/3.\"; distance:0; content:!\"Firefox/3.6.12\"; distance:-10; content:!\"Mozilla/5.0 |28|Macintosh|3B| U|3B| Intel Mac OS X 10.5|3B| en-US|3B| rv|3A|1.9.1b4|29| Gecko/20090423 Firefox/3.6 GTB5\"; sid:1; rev:1;)";
684

685
    //char sig[] = "alert tcp any any -> any any (content:\"User-Agent: Mozilla/5.0 (Macintosh; \"; content:\"Firefox/3.\"; distance:0; content:!\"Firefox/3.6.12\"; distance:-10; content:!\"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b4) Gecko/20090423 Firefox/3.6 GTB5\"; sid:1; rev:1;)";
686

687
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 1);
688

689
    UTHFreePacket(p);
690

691
    PASS;
692
}
693

694
static int PayloadTestSig15(void)
695
{
696
    uint8_t *buf = (uint8_t *)"this is a super duper nova in super nova now";
697
    uint16_t buflen = strlen((char *)buf);
698
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
699

700
    FAIL_IF_NULL(p);
701

702
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
703
        "content:\"nova\"; isdataat:18,relative; sid:1;)";
704

705
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
706

707
    UTHFreePacket(p);
708

709
    PASS;
710
}
711

712
static int PayloadTestSig16(void)
713
{
714
    uint8_t *buf = (uint8_t *)"this is a super duper nova in super nova now";
715
    uint16_t buflen = strlen((char *)buf);
716
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
717

718
    FAIL_IF_NULL(p);
719

720
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
721
        "content:\"nova\"; isdataat:!20,relative; sid:1;)";
722

723
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
724

725
    UTHFreePacket(p);
726

727
    PASS;
728
}
729

730
static int PayloadTestSig17(void)
731
{
732
    uint8_t buf[] = { 0xEB, 0x29, 0x25, 0x38, 0x78, 0x25, 0x38, 0x78, 0x25 };
733
    uint16_t buflen = 9;
734
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
735

736
    FAIL_IF_NULL(p);
737

738
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
739
        "content:\"%\"; depth:4; offset:0; "
740
        "content:\"%\"; within:2; distance:1; sid:1;)";
741

742
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
743

744
    UTHFreePacket(p);
745

746
    PASS;
747
}
748

749
static int PayloadTestSig18(void)
750
{
751
    uint8_t buf[] = {
752
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x35, /* the last byte is 2 */
753
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
754
        0x0E, 0x0F,
755
    };
756
    uint16_t buflen = sizeof(buf);
757
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
758

759
    FAIL_IF_NULL(p);
760

761
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
762
        "content:\"|01 02 03 04|\"; "
763
        "byte_extract:1,2,one,string,dec,relative; "
764
        "content:\"|0C 0D 0E 0F|\"; distance:one; sid:1;)";
765

766
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
767

768
    UTHFreePacket(p);
769

770
    PASS;
771
}
772

773
static int PayloadTestSig19(void)
774
{
775
    uint8_t buf[] = {
776
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x35, /* the last byte is 2 */
777
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
778
        0x0E, 0x0F,
779
    };
780
    uint16_t buflen = sizeof(buf);
781
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
782

783
    FAIL_IF_NULL(p);
784

785
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
786
        "content:\"|01 02 03 04|\"; "
787
        "byte_extract:1,2,one,string,hex,relative; "
788
        "content:\"|0C 0D 0E 0F|\"; distance:one; sid:1;)";
789

790
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
791

792
    UTHFreePacket(p);
793

794
    PASS;
795
}
796

797
static int PayloadTestSig20(void)
798
{
799
    uint8_t buf[] = {
800
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x35, /* the last byte is 2 */
801
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
802
        0x0E, 0x0F,
803
    };
804
    uint16_t buflen = sizeof(buf);
805
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
806

807
    FAIL_IF_NULL(p);
808

809
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
810
        "content:\"|01 02 03 04|\"; "
811
        "byte_extract:1,2,one,string,dec,relative; "
812
        "content:\"|06 35 07 08|\"; offset:one; sid:1;)";
813

814
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
815

816
    UTHFreePacket(p);
817

818
    PASS;
819
}
820

821
static int PayloadTestSig21(void)
822
{
823
    uint8_t buf[] = {
824
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x36, /* the last byte is 2 */
825
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
826
        0x0E, 0x0F,
827
    };
828
    uint16_t buflen = sizeof(buf);
829
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
830

831
    FAIL_IF_NULL(p);
832

833
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
834
        "content:\"|01 02 03 04|\"; "
835
        "byte_extract:1,2,one,string,dec,relative; "
836
        "content:\"|03 04 05 06|\"; depth:one; sid:1;)";
837

838
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
839

840
    UTHFreePacket(p);
841

842
    PASS;
843
}
844

845
static int PayloadTestSig22(void)
846
{
847
    uint8_t buf[] = {
848
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x36, /* the last byte is 2 */
849
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
850
        0x0E, 0x0F,
851
    };
852
    uint16_t buflen = sizeof(buf);
853
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
854

855
    FAIL_IF_NULL(p);
856

857
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
858
        "content:\"|01 02 03 04|\"; "
859
        "byte_extract:1,2,one,string,dec,relative; "
860
        "content:\"|09 0A 0B 0C|\"; within:one; sid:1;)";
861

862
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
863

864
    UTHFreePacket(p);
865

866
    PASS;
867
}
868

869
static int PayloadTestSig23(void)
870
{
871
    uint8_t buf[] = {
872
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x32, /* the last byte is 2 */
873
        0x07, 0x08, 0x09, 0x33, 0x0B, 0x0C, 0x0D,
874
        0x32, 0x0F,
875
    };
876
    uint16_t buflen = sizeof(buf);
877
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
878

879
    FAIL_IF_NULL(p);
880

881
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
882
        "content:\"|01 02 03 04|\"; "
883
        "byte_extract:1,2,one,string,dec,relative; "
884
        "byte_extract:1,3,two,string,dec,relative; "
885
        "byte_test:1,=,one,two,string,dec,relative; sid:1;)";
886

887
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
888

889
    UTHFreePacket(p);
890

891
    PASS;
892
}
893

894
static int PayloadTestSig24(void)
895
{
896
    uint8_t buf[] = {
897
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x32, /* the last byte is 2 */
898
        0x07, 0x08, 0x33, 0x0A, 0x0B, 0x0C, 0x0D,
899
        0x0E, 0x0F,
900
    };
901
    uint16_t buflen = sizeof(buf);
902
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
903

904
    FAIL_IF_NULL(p);
905

906
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
907
        "content:\"|01 02 03 04|\"; "
908
        "byte_extract:1,2,one,string,dec,relative; "
909
        "byte_jump:1,one,string,dec,relative; "
910
        "content:\"|0D 0E 0F|\"; distance:0; sid:1;)";
911

912
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
913

914
    UTHFreePacket(p);
915

916
    PASS;
917
}
918

919
/*
920
 * \test Test negative byte extract.
921
 */
922
static int PayloadTestSig25(void)
923
{
924
    uint8_t buf[] = {
925
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x35, /* the last byte is 2 */
926
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
927
        0x0E, 0x0F,
928
    };
929
    uint16_t buflen = sizeof(buf);
930
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
931

932
    FAIL_IF_NULL(p);
933

934
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
935
        "content:\"|35 07 08 09|\"; "
936
        "byte_extract:1,-4,one,string,dec,relative; "
937
        "content:\"|0C 0D 0E 0F|\"; distance:one; sid:1;)";
938

939
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
940

941
    UTHFreePacket(p);
942

943
    PASS;
944
}
945

946
/*
947
 * \test Test negative byte extract.
948
 */
949
static int PayloadTestSig26(void)
950
{
951
    uint8_t buf[] = {
952
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x35, /* the last byte is 2 */
953
        0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
954
        0x0E, 0x0F,
955
    };
956
    uint16_t buflen = sizeof(buf);
957
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
958

959
    FAIL_IF_NULL(p);
960

961
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
962
        "content:\"|35 07 08 09|\"; "
963
        "byte_extract:1,-3000,one,string,dec,relative; "
964
        "content:\"|0C 0D 0E 0F|\"; distance:one; sid:1;)";
965

966
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) != 0);
967

968
    UTHFreePacket(p);
969

970
    PASS;
971
}
972

973
/*
974
 * \test Test packet/stream sigs
975
 */
976
static int PayloadTestSig27(void)
977
{
978
    uint8_t buf[] = "dummypayload";
979
    uint16_t buflen = sizeof(buf) - 1;
980
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
981

982
    FAIL_IF_NULL(p);
983

984
    char sig[] = "alert tcp any any -> any any (content:\"dummy\"; "
985
        "depth:5; sid:1;)";
986

987
    p->flags |= PKT_STREAM_ADD;
988
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) != 1);
989

990
    UTHFreePacket(p);
991

992
    PASS;
993
}
994

995
/*
996
 * \test Test packet/stream sigs
997
 */
998
static int PayloadTestSig28(void)
999
{
1000
    uint8_t buf[] = "dummypayload";
1001
    uint16_t buflen = sizeof(buf) - 1;
1002
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1003

1004
    FAIL_IF_NULL(p);
1005

1006
    char sig[] = "alert tcp any any -> any any (content:\"payload\"; "
1007
        "offset:4; depth:12; sid:1;)";
1008

1009
    p->flags |= PKT_STREAM_ADD;
1010
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) != 1);
1011

1012
    UTHFreePacket(p);
1013

1014
    PASS;
1015
}
1016

1017
/**
1018
 * \test Test pcre recursive matching - bug #529
1019
 */
1020
static int PayloadTestSig29(void)
1021
{
1022
    uint8_t *buf = (uint8_t *)"this is a super dupernova in super nova now";
1023
    uint16_t buflen = strlen((char *)buf);
1024
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1025

1026
    FAIL_IF_NULL(p);
1027

1028
    char sig[] = "alert tcp any any -> any any (msg:\"dummy\"; "
1029
        "pcre:/^.{4}/; content:\"nova\"; within:4; sid:1;)";
1030

1031
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 1);
1032

1033
    UTHFreePacket(p);
1034

1035
    PASS;
1036
}
1037

1038
static int PayloadTestSig30(void)
1039
{
1040
    uint8_t *buf = (uint8_t *)
1041
                    "xyonexxxxxxtwojunkonetwo";
1042
    uint16_t buflen = strlen((char *)buf);
1043
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1044

1045
    FAIL_IF_NULL(p);
1046

1047
    char sig[] = "alert tcp any any -> any any (content:\"one\"; pcre:\"/^two/R\"; sid:1;)";
1048

1049
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
1050

1051
    UTHFreePacket(p);
1052

1053
    PASS;
1054
}
1055

1056
static int PayloadTestSig31(void)
1057
{
1058
    uint8_t *buf = (uint8_t *)
1059
                    "xyonexxxxxxtwojunkonetwo";
1060
    uint16_t buflen = strlen((char *)buf);
1061
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1062

1063
    FAIL_IF_NULL(p);
1064

1065
    char sig[] = "alert tcp any any -> any any (content:\"one\"; pcre:\"/(fiv|^two)/R\"; sid:1;)";
1066

1067
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
1068

1069
    UTHFreePacket(p);
1070

1071
    PASS;
1072
}
1073

1074
/**
1075
 * \test Test byte_jump.
1076
 */
1077
static int PayloadTestSig32(void)
1078
{
1079
    uint8_t *buf = (uint8_t *)"dummy2xxcardmessage";
1080
    uint16_t buflen = strlen((char *)buf);
1081
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1082

1083
    FAIL_IF_NULL(p);
1084

1085
    char sig[] = "alert tcp any any -> any any (msg:\"crash\"; "
1086
        "content:\"message\"; byte_jump:2,-14,string,dec,relative; content:\"card\"; within:4; sid:1;)";
1087

1088
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
1089

1090
    UTHFreePacket(p);
1091

1092
    PASS;
1093
}
1094

1095
/**
1096
 * \test Test byte_test.
1097
 */
1098
static int PayloadTestSig33(void)
1099
{
1100
    uint8_t *buf = (uint8_t *)"dummy2xxcardmessage";
1101
    uint16_t buflen = strlen((char *)buf);
1102
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1103

1104
    FAIL_IF_NULL(p);
1105

1106
    char sig[] = "alert tcp any any -> any any (msg:\"crash\"; "
1107
        "content:\"message\"; byte_test:1,=,2,-14,string,dec,relative; sid:1;)";
1108

1109
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
1110

1111
    UTHFreePacket(p);
1112

1113
    PASS;
1114
}
1115

1116
/**
1117
 * \test Test byte_extract.
1118
 */
1119
static int PayloadTestSig34(void)
1120
{
1121
    uint8_t *buf = (uint8_t *)"dummy2xxcardmessage";
1122
    uint16_t buflen = strlen((char *)buf);
1123
    Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1124

1125
    FAIL_IF_NULL(p);
1126

1127
    char sig[] = "alert tcp any any -> any any (msg:\"crash\"; "
1128
        "content:\"message\"; byte_extract:1,-14,boom,string,dec,relative; sid:1;)";
1129

1130
    FAIL_IF(UTHPacketMatchSigMpm(p, sig, mpm_default_matcher) == 0);
1131

1132
    UTHFreePacket(p);
1133

1134
    PASS;
1135
}
1136

1137
#endif /* UNITTESTS */
1138

1139
void PayloadRegisterTests(void)
1140
{
×
1141
#ifdef UNITTESTS
1142
    UtRegisterTest("PayloadTestSig01", PayloadTestSig01);
1143
    UtRegisterTest("PayloadTestSig02", PayloadTestSig02);
1144
    UtRegisterTest("PayloadTestSig03", PayloadTestSig03);
1145
    UtRegisterTest("PayloadTestSig04", PayloadTestSig04);
1146
    UtRegisterTest("PayloadTestSig05", PayloadTestSig05);
1147
    UtRegisterTest("PayloadTestSig06", PayloadTestSig06);
1148
    UtRegisterTest("PayloadTestSig07", PayloadTestSig07);
1149
    UtRegisterTest("PayloadTestSig08", PayloadTestSig08);
1150
    UtRegisterTest("PayloadTestSig09", PayloadTestSig09);
1151
    UtRegisterTest("PayloadTestSig10", PayloadTestSig10);
1152
    UtRegisterTest("PayloadTestSig11", PayloadTestSig11);
1153
    UtRegisterTest("PayloadTestSig12", PayloadTestSig12);
1154
    UtRegisterTest("PayloadTestSig13", PayloadTestSig13);
1155
    UtRegisterTest("PayloadTestSig14", PayloadTestSig14);
1156
    UtRegisterTest("PayloadTestSig15", PayloadTestSig15);
1157
    UtRegisterTest("PayloadTestSig16", PayloadTestSig16);
1158
    UtRegisterTest("PayloadTestSig17", PayloadTestSig17);
1159

1160
    UtRegisterTest("PayloadTestSig18", PayloadTestSig18);
1161
    UtRegisterTest("PayloadTestSig19", PayloadTestSig19);
1162
    UtRegisterTest("PayloadTestSig20", PayloadTestSig20);
1163
    UtRegisterTest("PayloadTestSig21", PayloadTestSig21);
1164
    UtRegisterTest("PayloadTestSig22", PayloadTestSig22);
1165
    UtRegisterTest("PayloadTestSig23", PayloadTestSig23);
1166
    UtRegisterTest("PayloadTestSig24", PayloadTestSig24);
1167
    UtRegisterTest("PayloadTestSig25", PayloadTestSig25);
1168
    UtRegisterTest("PayloadTestSig26", PayloadTestSig26);
1169
    UtRegisterTest("PayloadTestSig27", PayloadTestSig27);
1170
    UtRegisterTest("PayloadTestSig28", PayloadTestSig28);
1171
    UtRegisterTest("PayloadTestSig29", PayloadTestSig29);
1172

1173
    UtRegisterTest("PayloadTestSig30", PayloadTestSig30);
1174
    UtRegisterTest("PayloadTestSig31", PayloadTestSig31);
1175
    UtRegisterTest("PayloadTestSig32", PayloadTestSig32);
1176
    UtRegisterTest("PayloadTestSig33", PayloadTestSig33);
1177
    UtRegisterTest("PayloadTestSig34", PayloadTestSig34);
1178
#endif /* UNITTESTS */
1179
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc