summaryrefslogtreecommitdiffstats
path: root/src/detect-ipaddr.c
blob: d2dd6ceb2ae258f14cc8bc2decc1d10d75e9f71a (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
/* Copyright (C) 2022 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 Eric Leblond <el@stamus-networks.com>
 *
 * Offer source or destination IP as a sticky buffer.
 */

#include "suricata-common.h"

#include "decode.h"
#include "conf.h"
#include "detect.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "detect-engine-prefilter.h"
#include "detect-ipaddr.h"

#define KEYWORD_NAME_SRC "ip.src"
#define KEYWORD_NAME_DST "ip.dst"

static int DetectSrcIPAddrBufferSetup(DetectEngineCtx *, Signature *, const char *);
static int DetectDestIPAddrBufferSetup(DetectEngineCtx *, Signature *, const char *);
static InspectionBuffer *GetDataSrc(DetectEngineThreadCtx *det_ctx,
        const DetectEngineTransforms *transforms, Packet *p, const int list_id);
static InspectionBuffer *GetDataDst(DetectEngineThreadCtx *det_ctx,
        const DetectEngineTransforms *transforms, Packet *p, const int list_id);

#ifdef UNITTESTS
static void DetectIPAddrRegisterTests(void);
#endif
static int g_src_ipaddr_buffer_id = 0;
static int g_dest_ipaddr_buffer_id = 0;

void DetectIPAddrBufferRegister(void)
{
    sigmatch_table[DETECT_IPADDR_SRC].name = KEYWORD_NAME_SRC;
    sigmatch_table[DETECT_IPADDR_SRC].desc = "Sticky buffer for src_ip";
    sigmatch_table[DETECT_IPADDR_SRC].url = "/rules/ipaddr.html#ip-src";
    sigmatch_table[DETECT_IPADDR_SRC].Setup = DetectSrcIPAddrBufferSetup;
#ifdef UNITTESTS
    sigmatch_table[DETECT_IPADDR_SRC].RegisterTests = DetectIPAddrRegisterTests;
#endif

    sigmatch_table[DETECT_IPADDR_SRC].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;

    g_src_ipaddr_buffer_id = DetectBufferTypeRegister(KEYWORD_NAME_SRC);
    BUG_ON(g_src_ipaddr_buffer_id < 0);

    DetectBufferTypeSupportsPacket(KEYWORD_NAME_SRC);

    DetectPktMpmRegister(KEYWORD_NAME_SRC, 2, PrefilterGenericMpmPktRegister, GetDataSrc);

    DetectPktInspectEngineRegister(
            KEYWORD_NAME_SRC, GetDataSrc, DetectEngineInspectPktBufferGeneric);

    sigmatch_table[DETECT_IPADDR_DST].name = KEYWORD_NAME_DST;
    sigmatch_table[DETECT_IPADDR_DST].desc = "Sticky buffer for dest_ip";
    sigmatch_table[DETECT_IPADDR_DST].url = "/rules/ipaddr.html#ip-dst";
    sigmatch_table[DETECT_IPADDR_DST].Setup = DetectDestIPAddrBufferSetup;

    sigmatch_table[DETECT_IPADDR_DST].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;

    g_dest_ipaddr_buffer_id = DetectBufferTypeRegister(KEYWORD_NAME_DST);
    BUG_ON(g_dest_ipaddr_buffer_id < 0);

    DetectBufferTypeSupportsPacket(KEYWORD_NAME_DST);

    DetectPktMpmRegister(KEYWORD_NAME_DST, 2, PrefilterGenericMpmPktRegister, GetDataDst);

    DetectPktInspectEngineRegister(
            KEYWORD_NAME_DST, GetDataDst, DetectEngineInspectPktBufferGeneric);

    SCLogDebug("IPAddr detect registered.");
}

static int DetectSrcIPAddrBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
{
    /* store list id. Content, pcre, etc will be added to the list at this
     * id. */
    s->init_data->list = g_src_ipaddr_buffer_id;

    return 0;
}

static int DetectDestIPAddrBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
{
    /* store list id. Content, pcre, etc will be added to the list at this
     * id. */
    s->init_data->list = g_dest_ipaddr_buffer_id;

    return 0;
}

/** \internal
 *  \brief get the data to inspect from the buffer
 *
 *  \retval buffer or NULL in case of error
 */
static InspectionBuffer *GetDataSrc(DetectEngineThreadCtx *det_ctx,
        const DetectEngineTransforms *transforms, Packet *p, const int list_id)
{
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
    if (buffer->inspect == NULL) {
        if (PKT_IS_IPV4(p)) {
            /* Suricata stores the IPv4 at the beginning of the field */
            InspectionBufferSetup(det_ctx, list_id, buffer, p->src.address.address_un_data8, 4);
        } else if (PKT_IS_IPV6(p)) {
            InspectionBufferSetup(det_ctx, list_id, buffer, p->src.address.address_un_data8, 16);
        } else {
            return NULL;
        }
        InspectionBufferApplyTransforms(buffer, transforms);
    }

    return buffer;
}

/** \internal
 *  \brief get the data to inspect from the buffer
 *
 *  \retval buffer or NULL in case of error
 */
static InspectionBuffer *GetDataDst(DetectEngineThreadCtx *det_ctx,
        const DetectEngineTransforms *transforms, Packet *p, const int list_id)
{
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
    if (buffer->inspect == NULL) {
        if (PKT_IS_IPV4(p)) {
            /* Suricata stores the IPv4 at the beginning of the field */
            InspectionBufferSetup(det_ctx, list_id, buffer, p->dst.address.address_un_data8, 4);
        } else if (PKT_IS_IPV6(p)) {
            InspectionBufferSetup(det_ctx, list_id, buffer, p->dst.address.address_un_data8, 16);
        } else {
            return NULL;
        }
        InspectionBufferApplyTransforms(buffer, transforms);
    }

    return buffer;
}

#ifdef UNITTESTS
#include "tests/detect-ipaddr.c"
#endif