Coverage Report

Created: 2025-11-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-depth.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 depth 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-depth.h"
38
39
#include "flow-var.h"
40
#include "app-layer.h"
41
42
#include "util-byte.h"
43
#include "util-debug.h"
44
45
static int DetectDepthSetup (DetectEngineCtx *, Signature *, const char *);
46
static int DetectStartsWithSetup (DetectEngineCtx *, Signature *, const char *);
47
48
void DetectDepthRegister (void)
49
73
{
50
73
    sigmatch_table[DETECT_DEPTH].name = "depth";
51
73
    sigmatch_table[DETECT_DEPTH].desc = "designate how many bytes from the beginning of the payload will be checked";
52
73
    sigmatch_table[DETECT_DEPTH].url = "/rules/payload-keywords.html#depth";
53
73
    sigmatch_table[DETECT_DEPTH].Match = NULL;
54
73
    sigmatch_table[DETECT_DEPTH].Setup = DetectDepthSetup;
55
73
    sigmatch_table[DETECT_DEPTH].Free  = NULL;
56
57
73
    sigmatch_table[DETECT_STARTS_WITH].name = "startswith";
58
73
    sigmatch_table[DETECT_STARTS_WITH].desc = "pattern must be at the start of a buffer (same as 'depth:<pattern len>')";
59
73
    sigmatch_table[DETECT_STARTS_WITH].url = "/rules/payload-keywords.html#startswith";
60
73
    sigmatch_table[DETECT_STARTS_WITH].Setup = DetectStartsWithSetup;
61
73
    sigmatch_table[DETECT_STARTS_WITH].flags |= SIGMATCH_NOOPT;
62
73
}
63
64
static int DetectDepthSetup (DetectEngineCtx *de_ctx, Signature *s, const char *depthstr)
65
77.2k
{
66
77.2k
    const char *str = depthstr;
67
77.2k
    SigMatch *pm = NULL;
68
77.2k
    int ret = -1;
69
70
    /* retrieve the sm to apply the depth against */
71
77.2k
    pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
72
77.2k
    if (pm == NULL) {
73
1.35k
        SCLogError("depth needs "
74
1.35k
                   "preceding content, uricontent option, http_client_body, "
75
1.35k
                   "http_server_body, http_header option, http_raw_header option, "
76
1.35k
                   "http_method option, http_cookie, http_raw_uri, "
77
1.35k
                   "http_stat_msg, http_stat_code, http_user_agent, "
78
1.35k
                   "http_host, http_raw_host or "
79
1.35k
                   "file_data/dce_stub_data sticky buffer options.");
80
1.35k
        goto end;
81
1.35k
    }
82
83
    /* verify other conditions. */
84
75.9k
    DetectContentData *cd = (DetectContentData *)pm->ctx;
85
86
75.9k
    if (cd->flags & DETECT_CONTENT_DEPTH) {
87
930
        SCLogError("can't use multiple depths for the same content.");
88
930
        goto end;
89
930
    }
90
75.0k
    if ((cd->flags & DETECT_CONTENT_WITHIN) || (cd->flags & DETECT_CONTENT_DISTANCE)) {
91
1.03k
        SCLogError("can't use a relative "
92
1.03k
                   "keyword like within/distance with a absolute "
93
1.03k
                   "relative keyword like depth/offset for the same "
94
1.03k
                   "content.");
95
1.03k
        goto end;
96
1.03k
    }
97
73.9k
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
98
2
        SCLogError("can't have a relative "
99
2
                   "negated keyword set along with 'fast_pattern'.");
100
2
        goto end;
101
2
    }
102
73.9k
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
103
2
        SCLogError("can't have a relative "
104
2
                   "keyword set along with 'fast_pattern:only;'.");
105
2
        goto end;
106
2
    }
107
73.9k
    if (str[0] != '-' && isalpha((unsigned char)str[0])) {
108
7.87k
        DetectByteIndexType index;
109
7.87k
        if (!DetectByteRetrieveSMVar(str, s, &index)) {
110
2.66k
            SCLogError("unknown byte_ keyword var "
111
2.66k
                       "seen in depth - %s.",
112
2.66k
                    str);
113
2.66k
            goto end;
114
2.66k
        }
115
5.20k
        cd->depth = index;
116
5.20k
        cd->flags |= DETECT_CONTENT_DEPTH_VAR;
117
66.0k
    } else {
118
66.0k
        if (StringParseUint16(&cd->depth, 0, 0, str) < 0)
119
746
        {
120
746
            SCLogError("invalid value for depth: %s.", str);
121
746
            goto end;
122
746
        }
123
124
65.3k
        if (cd->depth < cd->content_len) {
125
1.24k
            SCLogError("depth:%u smaller than "
126
1.24k
                       "content of len %u.",
127
1.24k
                    cd->depth, cd->content_len);
128
1.24k
            return -1;
129
1.24k
        }
130
        /* Now update the real limit, as depth is relative to the offset */
131
64.1k
        cd->depth += cd->offset;
132
64.1k
    }
133
69.3k
    cd->flags |= DETECT_CONTENT_DEPTH;
134
135
69.3k
    ret = 0;
136
76.0k
 end:
137
76.0k
    return ret;
138
69.3k
}
139
140
static int DetectStartsWithSetup (DetectEngineCtx *de_ctx, Signature *s, const char *unused)
141
26.0k
{
142
26.0k
    SigMatch *pm = NULL;
143
26.0k
    int ret = -1;
144
145
    /* retrieve the sm to apply the depth against */
146
26.0k
    pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
147
26.0k
    if (pm == NULL) {
148
160
        SCLogError("startswith needs a "
149
160
                   "preceding content option.");
150
160
        goto end;
151
160
    }
152
153
    /* verify other conditions. */
154
25.8k
    DetectContentData *cd = (DetectContentData *)pm->ctx;
155
156
25.8k
    if (cd->flags & DETECT_CONTENT_DEPTH) {
157
55
        SCLogError("can't use multiple "
158
55
                   "depth/startswith settings for the same content.");
159
55
        goto end;
160
55
    }
161
25.7k
    if ((cd->flags & DETECT_CONTENT_WITHIN) || (cd->flags & DETECT_CONTENT_DISTANCE)) {
162
184
        SCLogError("can't use a relative "
163
184
                   "keyword like within/distance with a absolute "
164
184
                   "relative keyword like depth/offset for the same "
165
184
                   "content.");
166
184
        goto end;
167
184
    }
168
25.6k
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
169
3
        SCLogError("can't have a relative "
170
3
                   "negated keyword set along with a 'fast_pattern'.");
171
3
        goto end;
172
3
    }
173
25.6k
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
174
3
        SCLogError("can't have a relative "
175
3
                   "keyword set along with 'fast_pattern:only;'.");
176
3
        goto end;
177
3
    }
178
25.6k
    if (cd->flags & DETECT_CONTENT_OFFSET) {
179
3
        SCLogError("can't mix offset "
180
3
                   "with startswith.");
181
3
        goto end;
182
3
    }
183
184
25.6k
    cd->depth = cd->content_len;
185
25.6k
    cd->flags |= DETECT_CONTENT_DEPTH;
186
25.6k
    cd->flags |= DETECT_CONTENT_STARTS_WITH;
187
188
25.6k
    ret = 0;
189
26.0k
 end:
190
26.0k
    return ret;
191
25.6k
}