Coverage Report

Created: 2025-11-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-offset.c
Line
Count
Source
1
/* Copyright (C) 2007-2019 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
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23
 *
24
 * Implements the offset keyword.
25
 */
26
27
#include "suricata-common.h"
28
29
#include "decode.h"
30
31
#include "detect.h"
32
#include "detect-parse.h"
33
#include "detect-content.h"
34
#include "detect-uricontent.h"
35
#include "detect-byte.h"
36
#include "detect-byte-extract.h"
37
#include "detect-offset.h"
38
39
#include "flow-var.h"
40
41
#include "util-byte.h"
42
#include "util-debug.h"
43
44
static int DetectOffsetSetup(DetectEngineCtx *, Signature *, const char *);
45
46
void DetectOffsetRegister (void)
47
73
{
48
73
    sigmatch_table[DETECT_OFFSET].name = "offset";
49
73
    sigmatch_table[DETECT_OFFSET].desc = "designate from which byte in the payload will be checked to find a match";
50
73
    sigmatch_table[DETECT_OFFSET].url = "/rules/payload-keywords.html#offset";
51
73
    sigmatch_table[DETECT_OFFSET].Setup = DetectOffsetSetup;
52
73
}
53
54
int DetectOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, const char *offsetstr)
55
51.0k
{
56
51.0k
    const char *str = offsetstr;
57
58
    /* retrieve the sm to apply the offset against */
59
51.0k
    SigMatch *pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
60
51.0k
    if (pm == NULL) {
61
463
        SCLogError("offset needs preceding content option.");
62
463
        return -1;
63
463
    }
64
65
    /* verify other conditions */
66
50.6k
    DetectContentData *cd = (DetectContentData *)pm->ctx;
67
68
50.6k
    if (cd->flags & DETECT_CONTENT_STARTS_WITH) {
69
1
        SCLogError("can't use offset with startswith.");
70
1
        return -1;
71
1
    }
72
50.6k
    if (cd->flags & DETECT_CONTENT_OFFSET) {
73
545
        SCLogError("can't use multiple offsets for the same content.");
74
545
        return -1;
75
545
    }
76
50.0k
    if (cd->flags & (DETECT_CONTENT_WITHIN | DETECT_CONTENT_DISTANCE)) {
77
670
        SCLogError("can't use a relative "
78
670
                   "keyword like within/distance with a absolute "
79
670
                   "relative keyword like depth/offset for the same "
80
670
                   "content.");
81
670
        return -1;
82
670
    }
83
49.3k
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
84
6
        SCLogError("can't have a relative "
85
6
                   "negated keyword set along with 'fast_pattern'.");
86
6
        return -1;
87
6
    }
88
49.3k
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
89
3
        SCLogError("can't have a relative "
90
3
                   "keyword set along with 'fast_pattern:only;'.");
91
3
        return -1;
92
3
    }
93
49.3k
    if (str[0] != '-' && isalpha((unsigned char)str[0])) {
94
20.1k
        DetectByteIndexType index;
95
20.1k
        if (!DetectByteRetrieveSMVar(str, s, &index)) {
96
4.10k
            SCLogError("unknown byte_ keyword var "
97
4.10k
                       "seen in offset - %s.",
98
4.10k
                    str);
99
4.10k
            return -1;
100
4.10k
        }
101
16.0k
        cd->offset = index;
102
16.0k
        cd->flags |= DETECT_CONTENT_OFFSET_VAR;
103
29.2k
    } else {
104
29.2k
        if (StringParseUint16(&cd->offset, 0, 0, str) < 0)
105
3.46k
        {
106
3.46k
            SCLogError("invalid value for offset: %s.", str);
107
3.46k
            return -1;
108
3.46k
        }
109
25.7k
        if (cd->depth != 0) {
110
4.70k
            if (cd->depth < cd->content_len) {
111
0
                SCLogDebug("depth increased to %"PRIu32" to match pattern len",
112
0
                           cd->content_len);
113
0
                cd->depth = cd->content_len;
114
0
            }
115
            /* Updating the depth as is relative to the offset */
116
4.70k
            cd->depth += cd->offset;
117
4.70k
        }
118
25.7k
    }
119
41.8k
    cd->flags |= DETECT_CONTENT_OFFSET;
120
41.8k
    return 0;
121
49.3k
}
122