suricata
detect-template-rust-buffer.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2017 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  * TODO: Update the \author in this file and detect-template.h.
20  * TODO: Update description in the \file section below.
21  * TODO: Remove SCLogNotice statements or convert to debug.
22  */
23 
24 /**
25  * \file
26  *
27  * \author FirstName LastName <yourname@domain>
28  *
29  * Set up of the "template_rust" keyword to allow content
30  * inspections on the decoded template application layer buffers.
31  */
32 
33 #include "suricata-common.h"
34 #include "conf.h"
35 #include "detect.h"
36 #include "detect-parse.h"
37 #include "detect-engine.h"
40 #include "app-layer-parser.h"
41 #include "rust.h"
42 
43 
44 static int DetectTemplateRustBufferSetup(DetectEngineCtx *, Signature *,
45  const char *);
46 static int DetectEngineInspectTemplateRustBuffer(ThreadVars *tv,
48  const Signature *s, const SigMatchData *smd,
49  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
50 #ifdef UNITTESTS
51 static void DetectTemplateRustBufferRegisterTests(void);
52 #endif
53 static int g_template_rust_id = 0;
54 
56 {
57  /* TEMPLATE_START_REMOVE */
58  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
59  return;
60  }
61  /* TEMPLATE_END_REMOVE */
63  "template_rust_buffer";
65  "Template content modififier to match on the template buffers";
67  DetectTemplateRustBufferSetup;
68 #ifdef UNITTESTS
70  DetectTemplateRustBufferRegisterTests;
71 #endif
73 
74  /* register inspect engines */
75  DetectAppLayerInspectEngineRegister("template_rust_buffer",
77  DetectEngineInspectTemplateRustBuffer);
78  DetectAppLayerInspectEngineRegister("template_rust_buffer",
80  DetectEngineInspectTemplateRustBuffer);
81 
82  g_template_rust_id = DetectBufferTypeGetByName("template_rust_buffer");
83 
84  SCLogNotice("Template application layer detect registered.");
85 }
86 
87 static int DetectTemplateRustBufferSetup(DetectEngineCtx *de_ctx, Signature *s,
88  const char *str)
89 {
90  s->init_data->list = g_template_rust_id;
91 
93  return -1;
94 
95  return 0;
96 }
97 
98 static int DetectEngineInspectTemplateRustBuffer(ThreadVars *tv,
100  const Signature *s, const SigMatchData *smd,
101  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
102 {
103  int ret = 0;
104  const uint8_t *data = NULL;
105  uint32_t data_len = 0;
106 
107  if (flags & STREAM_TOSERVER) {
108  rs_template_get_request_buffer(txv, &data, &data_len);
109  } else if (flags & STREAM_TOCLIENT) {
110  rs_template_get_response_buffer(txv, &data, &data_len);
111  }
112 
113  if (data != NULL) {
114  ret = DetectEngineContentInspection(de_ctx, det_ctx, s, smd,
115  NULL, f, (uint8_t *)data, data_len, 0, DETECT_CI_FLAGS_SINGLE,
117  }
118 
119  SCLogNotice("Returning %d.", ret);
120  return ret;
121 }
122 
123 #ifdef UNITTESTS
124 
125 #include "util-unittest.h"
126 #include "util-unittest-helper.h"
127 #include "app-layer-parser.h"
128 #include "detect-engine.h"
129 #include "detect-parse.h"
130 #include "flow-util.h"
131 #include "stream-tcp.h"
132 
133 static int DetectTemplateRustBufferTest(void)
134 {
136  DetectEngineThreadCtx *det_ctx = NULL;
137  DetectEngineCtx *de_ctx = NULL;
138  Flow f;
139  Packet *p;
140  TcpSession tcp;
141  ThreadVars tv;
142  Signature *s;
143 
144  uint8_t request[] = "12:Hello World!";
145 
146  /* Setup flow. */
147  memset(&f, 0, sizeof(Flow));
148  memset(&tcp, 0, sizeof(TcpSession));
149  memset(&tv, 0, sizeof(ThreadVars));
150  p = UTHBuildPacket(request, sizeof(request), IPPROTO_TCP);
151  FLOW_INITIALIZE(&f);
153  f.protoctx = (void *)&tcp;
154  f.proto = IPPROTO_TCP;
155  f.flags |= FLOW_IPV4;
156  p->flow = &f;
160 
163 
164  /* This rule should match. */
166  "alert tcp any any -> any any ("
167  "msg:\"TEMPLATE Test Rule\"; "
168  "template_rust_buffer; content:\"World!\"; "
169  "sid:1; rev:1;)");
170  FAIL_IF_NULL(s);
171 
172  /* This rule should not match. */
174  "alert tcp any any -> any any ("
175  "msg:\"TEMPLATE Test Rule\"; "
176  "template_rust_buffer; content:\"W0rld!\"; "
177  "sid:2; rev:1;)");
178  FAIL_IF_NULL(s);
179 
181  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
182 
183  FLOWLOCK_WRLOCK(&f);
185  STREAM_TOSERVER, request, sizeof(request));
186  FLOWLOCK_UNLOCK(&f);
187 
188  /* Check that we have app-layer state. */
190 
191  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
192  FAIL_IF(!PacketAlertCheck(p, 1));
193  FAIL_IF(PacketAlertCheck(p, 2));
194 
195  /* Cleanup. */
196  if (alp_tctx != NULL)
198  if (det_ctx != NULL)
199  DetectEngineThreadCtxDeinit(&tv, det_ctx);
200  if (de_ctx != NULL)
202  if (de_ctx != NULL)
205  FLOW_DESTROY(&f);
206  UTHFreePacket(p);
207 
208  PASS;
209 }
210 
211 static void DetectTemplateRustBufferRegisterTests(void)
212 {
213  UtRegisterTest("DetectTemplateRustBufferTest",
214  DetectTemplateRustBufferTest);
215 }
216 #endif /* UNITTESTS */
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1475
detect-engine.h
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:1212
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1104
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1210
stream-tcp.h
detect-template-rust-buffer.h
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_::proto
uint8_t proto
Definition: flow.h:365
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:138
Packet_::flags
uint32_t flags
Definition: decode.h:447
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
Flow_
Flow data structure.
Definition: flow.h:347
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1204
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:278
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:219
rust.h
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:337
DetectEngineContentInspection
int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
Run the actual payload match functions.
Definition: detect-engine-content-inspection.c:104
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:443
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
Flow_::protoctx
void * protoctx
Definition: flow.h:441
SigMatchData_
Data needed for Match()
Definition: detect.h:328
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:95
util-unittest.h
util-unittest-helper.h
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:264
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1009
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:19
FLOWLOCK_WRLOCK
#define FLOWLOCK_WRLOCK(fb)
Definition: flow.h:261
SignatureInitData_::list
int list
Definition: detect.h:504
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
app-layer-parser.h
TRUE
#define TRUE
Definition: suricata-common.h:33
SigMatchSignatures
void SigMatchSignatures(ThreadVars *th_v, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1688
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:1943
Packet_
Definition: decode.h:412
StreamTcpFreeConfig
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:668
conf.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:596
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1878
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:252
detect-engine-content-inspection.h
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2344
Packet_::flow
struct Flow_ * flow
Definition: decode.h:449
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:2797
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:46
flags
uint8_t flags
Definition: decode-gre.h:0
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1179
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3005
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
DETECT_AL_TEMPLATE_RUST_BUFFER
@ DETECT_AL_TEMPLATE_RUST_BUFFER
Definition: detect-engine-register.h:266
str
#define str(s)
Definition: suricata-common.h:273
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:485
Flow_::alstate
void * alstate
Definition: flow.h:476
Flow_::flags
uint32_t flags
Definition: flow.h:421
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
StreamTcpInitConfig
void StreamTcpInitConfig(char)
To initialize the stream global configuration data.
Definition: stream-tcp.c:365
ALPROTO_TEMPLATE
@ ALPROTO_TEMPLATE
Definition: app-layer-protos.h:56
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:221
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1379
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:87
DetectTemplateRustBufferRegister
void DetectTemplateRustBufferRegister(void)
Definition: detect-template-rust-buffer.c:55
TcpSession_
Definition: stream-tcp-private.h:261
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
Definition: detect-engine.c:171
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:232
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:35
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:130
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1102
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1202
ALPROTO_TEMPLATE_RUST
@ ALPROTO_TEMPLATE_RUST
Definition: app-layer-protos.h:57