suricata
detect-sameip.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 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 Brian Rectanus <brectanu@gmail.com>
22 *
23 * Implements the sameip keyword.
24 */
25
26#include "suricata-common.h"
27#include "decode.h"
28#include "detect.h"
29
30#include "detect-parse.h"
31#include "detect-engine.h"
32#include "detect-engine-mpm.h"
33#include "detect-engine-build.h"
34
35#include "detect-sameip.h"
36
37#include "util-unittest.h"
39
40static int DetectSameipMatch(DetectEngineThreadCtx *, Packet *,
41 const Signature *, const SigMatchCtx *);
42static int DetectSameipSetup(DetectEngineCtx *, Signature *, const char *);
43#ifdef UNITTESTS
44static void DetectSameipRegisterTests(void);
45#endif
46
47/**
48 * \brief Registration function for sameip: keyword
49 * \todo add support for no_stream and stream_only
50 */
52{
54 sigmatch_table[DETECT_SAMEIP].desc = "check if the IP address of the source is the same as the IP address of the destination";
55 sigmatch_table[DETECT_SAMEIP].url = "/rules/header-keywords.html#sameip";
56 sigmatch_table[DETECT_SAMEIP].Match = DetectSameipMatch;
57 sigmatch_table[DETECT_SAMEIP].Setup = DetectSameipSetup;
58#ifdef UNITTESTS
59 sigmatch_table[DETECT_SAMEIP].RegisterTests = DetectSameipRegisterTests;
60#endif
62}
63
64/**
65 * \internal
66 * \brief This function is used to match packets with same src/dst IPs
67 *
68 * \param t pointer to thread vars
69 * \param det_ctx pointer to the pattern matcher thread
70 * \param p pointer to the current packet
71 * \param m pointer to the sigmatch that we will cast into DetectSameipData
72 *
73 * \retval 0 no match
74 * \retval 1 match
75 */
76static int DetectSameipMatch(DetectEngineThreadCtx *det_ctx,
77 Packet *p, const Signature *s, const SigMatchCtx *ctx)
78{
79 return CMP_ADDR(&p->src, &p->dst) ? 1 : 0;
80}
81
82/**
83 * \internal
84 * \brief this function is used to add the sameip option into the signature
85 *
86 * \param de_ctx pointer to the Detection Engine Context
87 * \param s pointer to the Current Signature
88 * \param optstr pointer to the user provided options
89 *
90 * \retval 0 on Success
91 * \retval -1 on Failure
92 */
93static int DetectSameipSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
94{
95
96 /* Get this into a SigMatch and put it in the Signature. */
97
99 goto error;
100 }
102
103 return 0;
104
105error:
106 return -1;
107}
108
109#ifdef UNITTESTS
110#include "detect-engine-alert.h"
111
112/* NOTE: No parameters, so no parse tests */
113
114/**
115 * \internal
116 * \brief This test tests sameip success and failure.
117 */
118static int DetectSameipSigTest01(void)
119{
120 uint8_t *buf = (uint8_t *)
121 "GET / HTTP/1.0\r\n"
122 "\r\n";
123 uint16_t buflen = strlen((char *)buf);
124 Packet *p1 = NULL;
125 Packet *p2 = NULL;
126 ThreadVars th_v;
127 DetectEngineThreadCtx *det_ctx;
128
129 memset(&th_v, 0, sizeof(th_v));
130
131 /* First packet has same IPs */
132 p1 = UTHBuildPacketSrcDst(buf, buflen, IPPROTO_TCP, "1.2.3.4", "1.2.3.4");
133
134 /* Second packet does not have same IPs */
135 p2 = UTHBuildPacketSrcDst(buf, buflen, IPPROTO_TCP, "1.2.3.4", "4.3.2.1");
136
139
141
143 "alert tcp any any -> any any "
144 "(msg:\"Testing sameip\"; sameip; sid:1;)");
146
148 DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
149
150 SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
151 FAIL_IF(PacketAlertCheck(p1, 1) == 0);
152
153 SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
154 FAIL_IF(PacketAlertCheck(p2, 1) != 0);
155
156 DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
158
159 PASS;
160}
161
162/**
163 * \internal
164 * \brief This function registers unit tests for DetectSameip
165 */
166static void DetectSameipRegisterTests(void)
167{
168 UtRegisterTest("DetectSameipSigTest01", DetectSameipSigTest01);
169}
170#endif /* UNITTESTS */
#define CMP_ADDR(a1, a2)
Definition decode.h:222
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
void DetectSameipRegister(void)
Registration function for sameip: keyword.
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition detect.c:2420
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define DE_QUIET
Definition detect.h:330
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
@ DETECT_SM_LIST_MATCH
Definition detect.h:117
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
uint8_t flags
Definition detect.h:934
Signature * sig_list
Definition detect.h:941
Address src
Definition decode.h:505
Address dst
Definition decode.h:506
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
Per thread variable structure.
Definition threadvars.h:58
Packet * UTHBuildPacketSrcDst(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst)
UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs and defaulting ports.