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-helper.h"
38 
39 static int DetectSameipMatch(DetectEngineThreadCtx *, Packet *,
40  const Signature *, const SigMatchCtx *);
41 static int DetectSameipSetup(DetectEngineCtx *, Signature *, const char *);
42 #ifdef UNITTESTS
43 static void DetectSameipRegisterTests(void);
44 #endif
45 
46 /**
47  * \brief Registration function for sameip: keyword
48  */
50 {
51  sigmatch_table[DETECT_SAMEIP].name = "sameip";
52  sigmatch_table[DETECT_SAMEIP].desc = "check if the IP address of the source is the same as the IP address of the destination";
53  sigmatch_table[DETECT_SAMEIP].url = "/rules/header-keywords.html#sameip";
54  sigmatch_table[DETECT_SAMEIP].Match = DetectSameipMatch;
55  sigmatch_table[DETECT_SAMEIP].Setup = DetectSameipSetup;
56 #ifdef UNITTESTS
57  sigmatch_table[DETECT_SAMEIP].RegisterTests = DetectSameipRegisterTests;
58 #endif
60 }
61 
62 /**
63  * \internal
64  * \brief This function is used to match packets with same src/dst IPs
65  *
66  * \param t pointer to thread vars
67  * \param det_ctx pointer to the pattern matcher thread
68  * \param p pointer to the current packet
69  * \param m pointer to the sigmatch that we will cast into DetectSameipData
70  *
71  * \retval 0 no match
72  * \retval 1 match
73  */
74 static int DetectSameipMatch(DetectEngineThreadCtx *det_ctx,
75  Packet *p, const Signature *s, const SigMatchCtx *ctx)
76 {
77  return CMP_ADDR(&p->src, &p->dst) ? 1 : 0;
78 }
79 
80 /**
81  * \internal
82  * \brief this function is used to add the sameip option into the signature
83  *
84  * \param de_ctx pointer to the Detection Engine Context
85  * \param s pointer to the Current Signature
86  * \param optstr pointer to the user provided options
87  *
88  * \retval 0 on Success
89  * \retval -1 on Failure
90  */
91 static int DetectSameipSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
92 {
93  /* Get this into a SigMatch and put it in the Signature. */
95  return -1;
96  }
98  return 0;
99 }
100 
101 #ifdef UNITTESTS
102 #include "detect-engine-alert.h"
103 
104 /* NOTE: No parameters, so no parse tests */
105 
106 /**
107  * \internal
108  * \brief This test tests sameip success and failure.
109  */
110 static int DetectSameipSigTest01(void)
111 {
112  uint8_t *buf = (uint8_t *)
113  "GET / HTTP/1.0\r\n"
114  "\r\n";
115  uint16_t buflen = strlen((char *)buf);
117  DetectEngineThreadCtx *det_ctx;
118 
119  memset(&th_v, 0, sizeof(th_v));
121 
122  /* First packet has same IPs */
123  Packet *p1 = UTHBuildPacketSrcDst(buf, buflen, IPPROTO_TCP, "1.2.3.4", "1.2.3.4");
124 
125  /* Second packet does not have same IPs */
126  Packet *p2 = UTHBuildPacketSrcDst(buf, buflen, IPPROTO_TCP, "1.2.3.4", "4.3.2.1");
127 
130  de_ctx->flags |= DE_QUIET;
131 
132  Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
133  "(msg:\"Testing sameip\"; sameip; sid:1;)");
134  FAIL_IF_NULL(s);
135 
137  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
138 
139  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
140  FAIL_IF(PacketAlertCheck(p1, 1) == 0);
141 
142  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
143  FAIL_IF(PacketAlertCheck(p2, 1) != 0);
144 
145  UTHFreePacket(p1);
146  UTHFreePacket(p2);
147 
148  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
151  PASS;
152 }
153 
154 /**
155  * \internal
156  * \brief This function registers unit tests for DetectSameip
157  */
158 static void DetectSameipRegisterTests(void)
159 {
160  UtRegisterTest("DetectSameipSigTest01", DetectSameipSigTest01);
161 }
162 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1545
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:308
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::name
const char * name
Definition: detect.h:1542
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:144
UTHBuildPacketSrcDst
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.
Definition: util-unittest-helper.c:403
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectSameipRegister
void DetectSameipRegister(void)
Registration function for sameip: keyword.
Definition: detect-sameip.c:49
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
DE_QUIET
#define DE_QUIET
Definition: detect.h:334
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3305
p
Packet * p
Definition: fuzz_dataset.c:30
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3965
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
util-unittest-helper.h
detect-sameip.h
decode.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:424
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3660
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
Signature_::flags
uint32_t flags
Definition: detect.h:693
Packet_
Definition: decode.h:516
detect-engine-build.h
DETECT_SAMEIP
@ DETECT_SAMEIP
Definition: detect-engine-register.h:112
detect-engine-alert.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1504
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2300
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1332
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
th_v
ThreadVars * th_v
Definition: fuzz_dataset.c:29
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
CMP_ADDR
#define CMP_ADDR(a1, a2)
Definition: decode.h:223
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3905
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:470
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
Packet_::dst
Address dst
Definition: decode.h:521
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:997
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1428
Packet_::src
Address src
Definition: decode.h:520
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:257