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 static void DetectTemplateRustBufferRegisterTests(void);
51 static int g_template_rust_id = 0;
52 
54 {
55  /* TEMPLATE_START_REMOVE */
56  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
57  return;
58  }
59  /* TEMPLATE_END_REMOVE */
61  "template_rust_buffer";
63  "Template content modififier to match on the template buffers";
65  DetectTemplateRustBufferSetup;
67  DetectTemplateRustBufferRegisterTests;
68 
70 
71  /* register inspect engines */
72  DetectAppLayerInspectEngineRegister("template_rust_buffer",
74  DetectEngineInspectTemplateRustBuffer);
75  DetectAppLayerInspectEngineRegister("template_rust_buffer",
77  DetectEngineInspectTemplateRustBuffer);
78 
79  g_template_rust_id = DetectBufferTypeGetByName("template_rust_buffer");
80 
81  SCLogNotice("Template application layer detect registered.");
82 }
83 
84 static int DetectTemplateRustBufferSetup(DetectEngineCtx *de_ctx, Signature *s,
85  const char *str)
86 {
87  s->init_data->list = g_template_rust_id;
88 
90  return -1;
91 
92  return 0;
93 }
94 
95 static int DetectEngineInspectTemplateRustBuffer(ThreadVars *tv,
97  const Signature *s, const SigMatchData *smd,
98  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
99 {
100  int ret = 0;
101  const uint8_t *data = NULL;
102  uint32_t data_len = 0;
103 
104  if (flags & STREAM_TOSERVER) {
105  rs_template_get_request_buffer(txv, &data, &data_len);
106  } else if (flags & STREAM_TOCLIENT) {
107  rs_template_get_response_buffer(txv, &data, &data_len);
108  }
109 
110  if (data != NULL) {
111  ret = DetectEngineContentInspection(de_ctx, det_ctx, s, smd,
112  NULL, f, (uint8_t *)data, data_len, 0, DETECT_CI_FLAGS_SINGLE,
114  }
115 
116  SCLogNotice("Returning %d.", ret);
117  return ret;
118 }
119 
120 #ifdef UNITTESTS
121 
122 #include "util-unittest.h"
123 #include "util-unittest-helper.h"
124 #include "app-layer-parser.h"
125 #include "detect-engine.h"
126 #include "detect-parse.h"
127 #include "flow-util.h"
128 #include "stream-tcp.h"
129 
130 static int DetectTemplateRustBufferTest(void)
131 {
133  DetectEngineThreadCtx *det_ctx = NULL;
134  DetectEngineCtx *de_ctx = NULL;
135  Flow f;
136  Packet *p;
137  TcpSession tcp;
138  ThreadVars tv;
139  Signature *s;
140 
141  uint8_t request[] = "12:Hello World!";
142 
143  /* Setup flow. */
144  memset(&f, 0, sizeof(Flow));
145  memset(&tcp, 0, sizeof(TcpSession));
146  memset(&tv, 0, sizeof(ThreadVars));
147  p = UTHBuildPacket(request, sizeof(request), IPPROTO_TCP);
148  FLOW_INITIALIZE(&f);
150  f.protoctx = (void *)&tcp;
151  f.proto = IPPROTO_TCP;
152  f.flags |= FLOW_IPV4;
153  p->flow = &f;
157 
160 
161  /* This rule should match. */
163  "alert tcp any any -> any any ("
164  "msg:\"TEMPLATE Test Rule\"; "
165  "template_rust_buffer; content:\"World!\"; "
166  "sid:1; rev:1;)");
167  FAIL_IF_NULL(s);
168 
169  /* This rule should not match. */
171  "alert tcp any any -> any any ("
172  "msg:\"TEMPLATE Test Rule\"; "
173  "template_rust_buffer; content:\"W0rld!\"; "
174  "sid:2; rev:1;)");
175  FAIL_IF_NULL(s);
176 
178  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
179 
180  FLOWLOCK_WRLOCK(&f);
182  STREAM_TOSERVER, request, sizeof(request));
183  FLOWLOCK_UNLOCK(&f);
184 
185  /* Check that we have app-layer state. */
187 
188  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
189  FAIL_IF(!PacketAlertCheck(p, 1));
190  FAIL_IF(PacketAlertCheck(p, 2));
191 
192  /* Cleanup. */
193  if (alp_tctx != NULL)
195  if (det_ctx != NULL)
196  DetectEngineThreadCtxDeinit(&tv, det_ctx);
197  if (de_ctx != NULL)
199  if (de_ctx != NULL)
202  FLOW_DESTROY(&f);
203  UTHFreePacket(p);
204 
205  PASS;
206 }
207 
208 #endif
209 
210 static void DetectTemplateRustBufferRegisterTests(void)
211 {
212 #ifdef UNITTESTS
213  UtRegisterTest("DetectTemplateRustBufferTest",
214  DetectTemplateRustBufferTest);
215 #endif /* UNITTESTS */
216 }
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1459
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:1203
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1077
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1201
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
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:35
Flow_::proto
uint8_t proto
Definition: flow.h:361
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:444
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
Flow_
Flow data structure.
Definition: flow.h:342
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:761
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2030
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:279
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:218
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:262
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:103
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:440
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
Flow_::protoctx
void * protoctx
Definition: flow.h:416
SigMatchData_
Data needed for Match()
Definition: detect.h:327
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1187
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:94
util-unittest.h
util-unittest-helper.h
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:260
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:860
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:16
DetectEngineThreadCtx_
Definition: detect.h:1004
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:257
SignatureInitData_::list
int list
Definition: detect.h:498
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
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:1665
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:1947
Packet_
Definition: decode.h:408
StreamTcpFreeConfig
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:669
conf.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:591
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1876
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:253
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:2316
Packet_::flow
struct Flow_ * flow
Definition: decode.h:446
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:2726
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:2
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:1181
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:2934
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:32
DETECT_AL_TEMPLATE_RUST_BUFFER
@ DETECT_AL_TEMPLATE_RUST_BUFFER
Definition: detect-engine-register.h:250
str
#define str(s)
Definition: suricata-common.h:256
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:410
Flow_::alstate
void * alstate
Definition: flow.h:454
Flow_::flags
uint32_t flags
Definition: flow.h:396
detect-parse.h
Signature_
Signature container.
Definition: detect.h:522
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:55
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:220
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:1985
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1370
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:85
DetectTemplateRustBufferRegister
void DetectTemplateRustBufferRegister(void)
Definition: detect-template-rust-buffer.c:53
TcpSession_
Definition: stream-tcp-private.h:260
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:170
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:425
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:121
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1075
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
ALPROTO_TEMPLATE_RUST
@ ALPROTO_TEMPLATE_RUST
Definition: app-layer-protos.h:56