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