summaryrefslogtreecommitdiffstats
path: root/src/detect-depth.c
blob: 1c4f94a396f074580c4bda5b7f38e25192edfe17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Copyright (C) 2007-2019 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \file
 *
 * \author Victor Julien <victor@inliniac.net>
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
 *
 * Implements the depth keyword.
 */

#include "suricata-common.h"

#include "decode.h"

#include "detect.h"
#include "detect-parse.h"
#include "detect-content.h"
#include "detect-uricontent.h"
#include "detect-byte.h"
#include "detect-byte-extract.h"
#include "detect-depth.h"

#include "flow-var.h"
#include "app-layer.h"

#include "util-byte.h"
#include "util-debug.h"

static int DetectDepthSetup (DetectEngineCtx *, Signature *, const char *);
static int DetectStartsWithSetup (DetectEngineCtx *, Signature *, const char *);

void DetectDepthRegister (void)
{
    sigmatch_table[DETECT_DEPTH].name = "depth";
    sigmatch_table[DETECT_DEPTH].desc = "designate how many bytes from the beginning of the payload will be checked";
    sigmatch_table[DETECT_DEPTH].url = "/rules/payload-keywords.html#depth";
    sigmatch_table[DETECT_DEPTH].Match = NULL;
    sigmatch_table[DETECT_DEPTH].Setup = DetectDepthSetup;
    sigmatch_table[DETECT_DEPTH].Free  = NULL;

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

static int DetectDepthSetup (DetectEngineCtx *de_ctx, Signature *s, const char *depthstr)
{
    const char *str = depthstr;
    SigMatch *pm = NULL;
    int ret = -1;

    /* retrieve the sm to apply the depth against */
    pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
    if (pm == NULL) {
        SCLogError("depth needs "
                   "preceding content, uricontent option, http_client_body, "
                   "http_server_body, http_header option, http_raw_header option, "
                   "http_method option, http_cookie, http_raw_uri, "
                   "http_stat_msg, http_stat_code, http_user_agent, "
                   "http_host, http_raw_host or "
                   "file_data/dce_stub_data sticky buffer options.");
        goto end;
    }

    /* verify other conditions. */
    DetectContentData *cd = (DetectContentData *)pm->ctx;

    if (cd->flags & DETECT_CONTENT_DEPTH) {
        SCLogError("can't use multiple depths for the same content.");
        goto end;
    }
    if ((cd->flags & DETECT_CONTENT_WITHIN) || (cd->flags & DETECT_CONTENT_DISTANCE)) {
        SCLogError("can't use a relative "
                   "keyword like within/distance with a absolute "
                   "relative keyword like depth/offset for the same "
                   "content.");
        goto end;
    }
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
        SCLogError("can't have a relative "
                   "negated keyword set along with 'fast_pattern'.");
        goto end;
    }
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
        SCLogError("can't have a relative "
                   "keyword set along with 'fast_pattern:only;'.");
        goto end;
    }
    if (str[0] != '-' && isalpha((unsigned char)str[0])) {
        DetectByteIndexType index;
        if (!DetectByteRetrieveSMVar(str, s, &index)) {
            SCLogError("unknown byte_ keyword var "
                       "seen in depth - %s.",
                    str);
            goto end;
        }
        cd->depth = index;
        cd->flags |= DETECT_CONTENT_DEPTH_VAR;
    } else {
        if (StringParseUint16(&cd->depth, 0, 0, str) < 0)
        {
            SCLogError("invalid value for depth: %s.", str);
            goto end;
        }

        if (cd->depth < cd->content_len) {
            SCLogError("depth:%u smaller than "
                       "content of len %u.",
                    cd->depth, cd->content_len);
            return -1;
        }
        /* Now update the real limit, as depth is relative to the offset */
        cd->depth += cd->offset;
    }
    cd->flags |= DETECT_CONTENT_DEPTH;

    ret = 0;
 end:
    return ret;
}

static int DetectStartsWithSetup (DetectEngineCtx *de_ctx, Signature *s, const char *unused)
{
    SigMatch *pm = NULL;
    int ret = -1;

    /* retrieve the sm to apply the depth against */
    pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
    if (pm == NULL) {
        SCLogError("startswith needs a "
                   "preceding content option.");
        goto end;
    }

    /* verify other conditions. */
    DetectContentData *cd = (DetectContentData *)pm->ctx;

    if (cd->flags & DETECT_CONTENT_DEPTH) {
        SCLogError("can't use multiple "
                   "depth/startswith settings for the same content.");
        goto end;
    }
    if ((cd->flags & DETECT_CONTENT_WITHIN) || (cd->flags & DETECT_CONTENT_DISTANCE)) {
        SCLogError("can't use a relative "
                   "keyword like within/distance with a absolute "
                   "relative keyword like depth/offset for the same "
                   "content.");
        goto end;
    }
    if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
        SCLogError("can't have a relative "
                   "negated keyword set along with a 'fast_pattern'.");
        goto end;
    }
    if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
        SCLogError("can't have a relative "
                   "keyword set along with 'fast_pattern:only;'.");
        goto end;
    }
    if (cd->flags & DETECT_CONTENT_OFFSET) {
        SCLogError("can't mix offset "
                   "with startswith.");
        goto end;
    }

    cd->depth = cd->content_len;
    cd->flags |= DETECT_CONTENT_DEPTH;
    cd->flags |= DETECT_CONTENT_STARTS_WITH;

    ret = 0;
 end:
    return ret;
}