summaryrefslogtreecommitdiffstats
path: root/src/tests/detect-ttl.c
blob: 74949313c438a626510e9efd497fb85e71913361 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (C) 2007-2018 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.
 */

#include "../util-unittest.h"
#include "../util-unittest-helper.h"
#include "detect-engine.h"
#include "detect-engine-alert.h"
#include "detect-engine-build.h"

/**
 * \test DetectTtlParseTest01 is a test for setting up an valid ttl value.
 */

static int DetectTtlParseTest01 (void)
{
    DetectU8Data *ttld = DetectU8Parse("10");
    FAIL_IF_NULL(ttld);
    FAIL_IF_NOT(ttld->arg1 == 10);
    FAIL_IF_NOT(ttld->mode == DETECT_UINT_EQ);
    DetectTtlFree(NULL, ttld);
    PASS;
}

/**
 * \test DetectTtlParseTest02 is a test for setting up an valid ttl value with
 *       "<" operator.
 */

static int DetectTtlParseTest02 (void)
{
    DetectU8Data *ttld = DetectU8Parse("<10");
    FAIL_IF_NULL(ttld);
    FAIL_IF_NOT(ttld->arg1 == 10);
    FAIL_IF_NOT(ttld->mode == DETECT_UINT_LT);
    DetectTtlFree(NULL, ttld);
    PASS;
}

/**
 * \test DetectTtlParseTest03 is a test for setting up an valid ttl values with
 *       "-" operator.
 */

static int DetectTtlParseTest03 (void)
{
    DetectU8Data *ttld = DetectU8Parse("1-3");
    FAIL_IF_NULL(ttld);
    FAIL_IF_NOT(ttld->arg1 == 1);
    FAIL_IF_NOT(ttld->arg2 == 3);
    FAIL_IF_NOT(ttld->mode == DETECT_UINT_RA);
    DetectTtlFree(NULL, ttld);
    PASS;
}

/**
 * \test DetectTtlParseTest04 is a test for setting up an valid ttl value with
 *       ">" operator and include spaces arround the given values.
 */

static int DetectTtlParseTest04 (void)
{
    DetectU8Data *ttld = DetectU8Parse(" > 10 ");
    FAIL_IF_NULL(ttld);
    FAIL_IF_NOT(ttld->arg1 == 10);
    FAIL_IF_NOT(ttld->mode == DETECT_UINT_GT);
    DetectTtlFree(NULL, ttld);
    PASS;
}

/**
 * \test DetectTtlParseTest05 is a test for setting up an valid ttl values with
 *       "-" operator and include spaces arround the given values.
 */

static int DetectTtlParseTest05 (void)
{
    DetectU8Data *ttld = DetectU8Parse(" 1 - 3 ");
    FAIL_IF_NULL(ttld);
    FAIL_IF_NOT(ttld->arg1 == 1);
    FAIL_IF_NOT(ttld->arg2 == 3);
    FAIL_IF_NOT(ttld->mode == DETECT_UINT_RA);
    DetectTtlFree(NULL, ttld);
    PASS;
}

/**
 * \test DetectTtlParseTest06 is a test for setting up an valid ttl values with
 *       invalid "=" operator and include spaces arround the given values.
 */

static int DetectTtlParseTest06 (void)
{
    DetectU8Data *ttld = DetectU8Parse(" 1 = 2 ");
    FAIL_IF_NOT_NULL(ttld);
    PASS;
}

/**
 * \test DetectTtlParseTest07 is a test for setting up an valid ttl values with
 *       invalid "<>" operator and include spaces arround the given values.
 */

static int DetectTtlParseTest07 (void)
{
    DetectU8Data *ttld = DetectU8Parse(" 1<>2 ");
    FAIL_IF_NOT_NULL(ttld);
    PASS;
}

/**
 * \test DetectTtlSetupTest01 is a test for setting up an valid ttl values with
 *       valid "-" operator and include spaces arround the given values. In the
 *       test the values are setup with initializing the detection engine context
 *       setting up the signature itself.
 */

static int DetectTtlSetupTest01(void)
{
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
    FAIL_IF_NULL(de_ctx);
    de_ctx->flags |= DE_QUIET;

    Signature *s = DetectEngineAppendSig(
            de_ctx, "alert ip any any -> any any (msg:\"with in ttl limit\"; ttl:1 - 3; sid:1;)");
    FAIL_IF_NULL(s);
    SigGroupBuild(de_ctx);
    FAIL_IF_NULL(s->sm_arrays[DETECT_SM_LIST_MATCH]);
    FAIL_IF_NULL(s->sm_arrays[DETECT_SM_LIST_MATCH]->ctx);
    DetectU8Data *ttld = (DetectU8Data *)s->sm_arrays[DETECT_SM_LIST_MATCH]->ctx;

    FAIL_IF_NOT(ttld->arg1 == 1);
    FAIL_IF_NOT(ttld->arg2 == 3);
    FAIL_IF_NOT(ttld->mode == DETECT_UINT_RA);
    DetectEngineCtxFree(de_ctx);
    PASS;
}

/**
 * \test DetectTtlTestSig01 is a test for checking the working of ttl keyword
 *       by setting up the signature and later testing its working by matching
 *       the received packet against the sig.
 */

static int DetectTtlTestSig1(void)
{
    Packet *p = PacketGetFromAlloc();
    FAIL_IF_NULL(p);
    Signature *s = NULL;
    ThreadVars th_v;
    DetectEngineThreadCtx *det_ctx;
    IPV4Hdr ip4h;

    memset(&th_v, 0, sizeof(th_v));
    memset(&ip4h, 0, sizeof(ip4h));

    p->src.family = AF_INET;
    p->dst.family = AF_INET;
    p->proto = IPPROTO_TCP;
    ip4h.ip_ttl = 15;
    p->ip4h = &ip4h;

    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
    FAIL_IF_NULL(de_ctx);
    de_ctx->flags |= DE_QUIET;

    s = DetectEngineAppendSig(de_ctx,"alert ip any any -> any any (msg:\"with in ttl limit\"; ttl: >16; sid:1;)");
    FAIL_IF_NULL(s);

    s = DetectEngineAppendSig(de_ctx,"alert ip any any -> any any (msg:\"Less than 17\"; ttl: <17; sid:2;)");
    FAIL_IF_NULL(s);

    s = DetectEngineAppendSig(de_ctx,"alert ip any any -> any any (msg:\"Greater than 5\"; ttl:15; sid:3;)");
    FAIL_IF_NULL(s);

    s = DetectEngineAppendSig(de_ctx,"alert ip any any -> any any (msg:\"Equals tcp\"; ttl: 1-30; sid:4;)");
    FAIL_IF_NULL(s);

    SigGroupBuild(de_ctx);
    DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);

    SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
    FAIL_IF(PacketAlertCheck(p, 1));
    FAIL_IF_NOT(PacketAlertCheck(p, 2));
    FAIL_IF_NOT(PacketAlertCheck(p, 3));
    FAIL_IF_NOT(PacketAlertCheck(p, 4));

    DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
    DetectEngineCtxFree(de_ctx);

    SCFree(p);
    PASS;
}

/**
 * \brief this function registers unit tests for DetectTtl
 */
void DetectTtlRegisterTests(void)
{
    UtRegisterTest("DetectTtlParseTest01", DetectTtlParseTest01);
    UtRegisterTest("DetectTtlParseTest02", DetectTtlParseTest02);
    UtRegisterTest("DetectTtlParseTest03", DetectTtlParseTest03);
    UtRegisterTest("DetectTtlParseTest04", DetectTtlParseTest04);
    UtRegisterTest("DetectTtlParseTest05", DetectTtlParseTest05);
    UtRegisterTest("DetectTtlParseTest06", DetectTtlParseTest06);
    UtRegisterTest("DetectTtlParseTest07", DetectTtlParseTest07);
    UtRegisterTest("DetectTtlSetupTest01", DetectTtlSetupTest01);
    UtRegisterTest("DetectTtlTestSig1", DetectTtlTestSig1);
}