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

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

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

93.5
/src/detect-depth.c
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
{
3✔
50
    sigmatch_table[DETECT_DEPTH].name = "depth";
3✔
51
    sigmatch_table[DETECT_DEPTH].desc = "designate how many bytes from the beginning of the payload will be checked";
3✔
52
    sigmatch_table[DETECT_DEPTH].url = "/rules/payload-keywords.html#depth";
3✔
53
    sigmatch_table[DETECT_DEPTH].Match = NULL;
3✔
54
    sigmatch_table[DETECT_DEPTH].Setup = DetectDepthSetup;
3✔
55
    sigmatch_table[DETECT_DEPTH].Free  = NULL;
3✔
56

57
    sigmatch_table[DETECT_STARTS_WITH].name = "startswith";
3✔
58
    sigmatch_table[DETECT_STARTS_WITH].desc = "pattern must be at the start of a buffer (same as 'depth:<pattern len>')";
3✔
59
    sigmatch_table[DETECT_STARTS_WITH].url = "/rules/payload-keywords.html#startswith";
3✔
60
    sigmatch_table[DETECT_STARTS_WITH].Setup = DetectStartsWithSetup;
3✔
61
    sigmatch_table[DETECT_STARTS_WITH].flags |= SIGMATCH_NOOPT;
3✔
62
}
3✔
63

64
static int DetectDepthSetup (DetectEngineCtx *de_ctx, Signature *s, const char *depthstr)
65
{
44,262✔
66
    const char *str = depthstr;
44,262✔
67
    SigMatch *pm = NULL;
44,262✔
68
    int ret = -1;
44,262✔
69

70
    /* retrieve the sm to apply the depth against */
71
    pm = SCDetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
44,262✔
72
    if (pm == NULL) {
44,262✔
73
        SCLogError("depth needs "
839✔
74
                   "preceding content, uricontent option, http_client_body, "
839✔
75
                   "http_server_body, http_header option, http_raw_header option, "
839✔
76
                   "http_method option, http_cookie, http_raw_uri, "
839✔
77
                   "http_stat_msg, http_stat_code, http_user_agent, "
839✔
78
                   "http_host, http_raw_host or "
839✔
79
                   "file_data/dce_stub_data sticky buffer options.");
839✔
80
        goto end;
839✔
81
    }
839✔
82

83
    /* verify other conditions. */
84
    DetectContentData *cd = (DetectContentData *)pm->ctx;
43,423✔
85

86
    if (cd->flags & DETECT_CONTENT_DEPTH) {
43,423✔
87
        SCLogError("can't use multiple depths for the same content.");
235✔
88
        goto end;
235✔
89
    }
235✔
90
    if ((cd->flags & DETECT_CONTENT_WITHIN) || (cd->flags & DETECT_CONTENT_DISTANCE)) {
43,188✔
91
        SCLogError("can't use a relative "
150✔
92
                   "keyword like within/distance with a absolute "
150✔
93
                   "relative keyword like depth/offset for the same "
150✔
94
                   "content.");
150✔
95
        goto end;
150✔
96
    }
150✔
97
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
43,038✔
UNCOV
98
        SCLogError("can't have a relative "
×
UNCOV
99
                   "negated keyword set along with 'fast_pattern'.");
×
UNCOV
100
        goto end;
×
UNCOV
101
    }
×
102
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
43,038✔
103
        SCLogError("can't have a relative "
1✔
104
                   "keyword set along with 'fast_pattern:only;'.");
1✔
105
        goto end;
1✔
106
    }
1✔
107
    if (str[0] != '-' && isalpha((unsigned char)str[0])) {
43,037✔
108
        DetectByteIndexType index;
6,902✔
109
        if (!DetectByteRetrieveSMVar(str, s, -1, &index)) {
6,902✔
110
            SCLogError("unknown byte_ keyword var "
2,039✔
111
                       "seen in depth - %s.",
2,039✔
112
                    str);
2,039✔
113
            goto end;
2,039✔
114
        }
2,039✔
115
        cd->depth = index;
4,863✔
116
        cd->flags |= DETECT_CONTENT_DEPTH_VAR;
4,863✔
117
    } else {
36,135✔
118
        if (StringParseUint16(&cd->depth, 0, 0, str) < 0)
36,135✔
119
        {
381✔
120
            SCLogError("invalid value for depth: %s.", str);
381✔
121
            goto end;
381✔
122
        }
381✔
123

124
        if (cd->depth < cd->content_len) {
35,754✔
125
            SCLogError("depth:%u smaller than "
880✔
126
                       "content of len %u.",
880✔
127
                    cd->depth, cd->content_len);
880✔
128
            return -1;
880✔
129
        }
880✔
130
        /* Now update the real limit, as depth is relative to the offset */
131
        cd->depth += cd->offset;
34,874✔
132
    }
34,874✔
133
    cd->flags |= DETECT_CONTENT_DEPTH;
39,737✔
134

135
    ret = 0;
39,737✔
136
 end:
43,382✔
137
    return ret;
43,382✔
138
}
39,737✔
139

140
static int DetectStartsWithSetup (DetectEngineCtx *de_ctx, Signature *s, const char *unused)
141
{
23,052✔
142
    SigMatch *pm = NULL;
23,052✔
143
    int ret = -1;
23,052✔
144

145
    /* retrieve the sm to apply the depth against */
146
    pm = SCDetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
23,052✔
147
    if (pm == NULL) {
23,052✔
148
        SCLogError("startswith needs a "
178✔
149
                   "preceding content option.");
178✔
150
        goto end;
178✔
151
    }
178✔
152

153
    /* verify other conditions. */
154
    DetectContentData *cd = (DetectContentData *)pm->ctx;
22,874✔
155

156
    if (cd->flags & DETECT_CONTENT_DEPTH) {
22,874✔
157
        SCLogError("can't use multiple "
51✔
158
                   "depth/startswith settings for the same content.");
51✔
159
        goto end;
51✔
160
    }
51✔
161
    if ((cd->flags & DETECT_CONTENT_WITHIN) || (cd->flags & DETECT_CONTENT_DISTANCE)) {
22,823✔
162
        SCLogError("can't use a relative "
194✔
163
                   "keyword like within/distance with a absolute "
194✔
164
                   "relative keyword like depth/offset for the same "
194✔
165
                   "content.");
194✔
166
        goto end;
194✔
167
    }
194✔
168
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
22,629✔
169
        SCLogError("can't have a relative "
×
170
                   "negated keyword set along with a 'fast_pattern'.");
×
171
        goto end;
×
172
    }
×
173
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
22,629✔
174
        SCLogError("can't have a relative "
1✔
175
                   "keyword set along with 'fast_pattern:only;'.");
1✔
176
        goto end;
1✔
177
    }
1✔
178
    if (cd->flags & DETECT_CONTENT_OFFSET) {
22,628✔
179
        SCLogError("can't mix offset "
1✔
180
                   "with startswith.");
1✔
181
        goto end;
1✔
182
    }
1✔
183

184
    cd->depth = cd->content_len;
22,627✔
185
    cd->flags |= DETECT_CONTENT_DEPTH;
22,627✔
186
    cd->flags |= DETECT_CONTENT_STARTS_WITH;
22,627✔
187

188
    ret = 0;
22,627✔
189
 end:
23,052✔
190
    return ret;
23,052✔
191
}
22,627✔
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