suricata
output-json-template.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-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  * TODO: Update \author in this file and in output-json-template.h.
20  * TODO: Remove SCLogNotice statements, or convert to debug.
21  * TODO: Implement your app-layers logging.
22  */
23 
24 /**
25  * \file
26  *
27  * \author FirstName LastName <yourname@domain>
28  *
29  * Implement JSON/eve logging app-layer Template.
30  */
31 
32 #include "suricata-common.h"
33 #include "detect.h"
34 #include "pkt-var.h"
35 #include "conf.h"
36 
37 #include "threads.h"
38 #include "threadvars.h"
39 #include "tm-threads.h"
40 
41 #include "util-unittest.h"
42 #include "util-buffer.h"
43 #include "util-debug.h"
44 #include "util-byte.h"
45 
46 #include "output.h"
47 #include "output-json.h"
48 
49 #include "app-layer.h"
50 #include "app-layer-parser.h"
51 
52 #include "app-layer-template.h"
53 #include "output-json-template.h"
54 
55 typedef struct LogTemplateFileCtx_ {
56  uint32_t flags;
59 
60 typedef struct LogTemplateLogThread_ {
64 
65 static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
66  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
67 {
68  TemplateTransaction *templatetx = tx;
69  LogTemplateLogThread *thread = thread_data;
70 
71  SCLogNotice("Logging template transaction %"PRIu64".", templatetx->tx_id);
72 
73  JsonBuilder *js =
74  CreateEveHeader(p, LOG_DIR_PACKET, "template", NULL, thread->templatelog_ctx->eve_ctx);
75  if (unlikely(js == NULL)) {
76  return TM_ECODE_FAILED;
77  }
78 
79  jb_open_object(js, "template");
80 
81  /* Log the request buffer. */
82  if (templatetx->request_buffer != NULL) {
83  jb_set_string_from_bytes(js, "request", templatetx->request_buffer,
84  templatetx->request_buffer_len);
85  }
86 
87  /* Log the response buffer. */
88  if (templatetx->response_buffer != NULL) {
89  jb_set_string_from_bytes(js, "response", templatetx->response_buffer,
90  templatetx->response_buffer_len);
91  }
92 
93  /* Close template. */
94  jb_close(js);
95 
96  OutputJsonBuilderBuffer(js, thread->ctx);
97 
98  jb_free(js);
99  return TM_ECODE_OK;
100 }
101 
102 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
103 {
104  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
105  SCFree(templatelog_ctx);
106  SCFree(output_ctx);
107 }
108 
109 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf,
110  OutputCtx *parent_ctx)
111 {
112  OutputInitResult result = { NULL, false };
113  OutputJsonCtx *ajt = parent_ctx->data;
114 
115  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
116  if (unlikely(templatelog_ctx == NULL)) {
117  return result;
118  }
119  templatelog_ctx->eve_ctx = ajt;
120 
121  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
122  if (unlikely(output_ctx == NULL)) {
123  SCFree(templatelog_ctx);
124  return result;
125  }
126  output_ctx->data = templatelog_ctx;
127  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
128 
129  SCLogNotice("Template log sub-module initialized.");
130 
132 
133  result.ctx = output_ctx;
134  result.ok = true;
135  return result;
136 }
137 
138 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
139 {
140  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
141  if (unlikely(thread == NULL)) {
142  return TM_ECODE_FAILED;
143  }
144 
145  if (initdata == NULL) {
146  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
147  goto error_exit;
148  }
149 
150  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
151  thread->ctx = CreateEveThreadCtx(t, thread->templatelog_ctx->eve_ctx);
152  if (!thread->ctx) {
153  goto error_exit;
154  }
155  *data = (void *)thread;
156 
157  return TM_ECODE_OK;
158 
159 error_exit:
160  SCFree(thread);
161  return TM_ECODE_FAILED;
162 }
163 
164 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
165 {
166  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
167  if (thread == NULL) {
168  return TM_ECODE_OK;
169  }
170  FreeEveThreadCtx(thread->ctx);
171  SCFree(thread);
172  return TM_ECODE_OK;
173 }
174 
176 {
177  /* TEMPLATE_START_REMOVE */
178  if (ConfGetNode("app-layer.protocols.template") == NULL) {
179  return;
180  }
181  /* TEMPLATE_END_REMOVE */
182  /* Register as an eve sub-module. */
183  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonTemplateLog", "eve-log.template",
184  OutputTemplateLogInitSub, ALPROTO_TEMPLATE, JsonTemplateLogger,
185  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
186 
187  SCLogNotice("Template JSON logger registered.");
188 }
LogTemplateLogThread_::templatelog_ctx
LogTemplateFileCtx * templatelog_ctx
Definition: output-json-template-rust.c:61
util-byte.h
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:357
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:934
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
LogTemplateLogThread
struct LogTemplateLogThread_ LogTemplateLogThread
LogTemplateFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-template-rust.c:57
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
LogTemplateLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-template-rust.c:62
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:477
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:796
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
Packet_
Definition: decode.h:428
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
OutputInitResult_
Definition: output.h:46
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
LogTemplateFileCtx_
Definition: output-json-template-rust.c:55
LogTemplateFileCtx_::flags
uint32_t flags
Definition: output-json-template-rust.c:56
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:411
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:456
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
ALPROTO_TEMPLATE
@ ALPROTO_TEMPLATE
Definition: app-layer-protos.h:59
LogTemplateLogThread_
Definition: output-json-template-rust.c:60
JsonTemplateLogRegister
void JsonTemplateLogRegister(void)
Definition: output-json-template.c:175
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:237
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
LogTemplateFileCtx
struct LogTemplateFileCtx_ LogTemplateFileCtx
app-layer.h