suricata
output-json-template.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2022 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 "output-json-template.h"
53 #include "rust.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, const Packet *p, Flow *f,
66  void *state, void *tx, uint64_t tx_id)
67 {
68  SCLogNotice("JsonTemplateLogger");
69  LogTemplateLogThread *thread = thread_data;
70 
71  JsonBuilder *js =
72  CreateEveHeader(p, LOG_DIR_PACKET, "template", NULL, thread->templatelog_ctx->eve_ctx);
73  if (unlikely(js == NULL)) {
74  return TM_ECODE_FAILED;
75  }
76 
77  if (!rs_template_logger_log(tx, js)) {
78  goto error;
79  }
80 
81  OutputJsonBuilderBuffer(js, thread->ctx);
82  jb_free(js);
83 
84  return TM_ECODE_OK;
85 
86 error:
87  jb_free(js);
88  return TM_ECODE_FAILED;
89 }
90 
91 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
92 {
93  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
94  SCFree(templatelog_ctx);
95  SCFree(output_ctx);
96 }
97 
98 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
99 {
100  OutputInitResult result = { NULL, false };
101  OutputJsonCtx *ajt = parent_ctx->data;
102 
103  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
104  if (unlikely(templatelog_ctx == NULL)) {
105  return result;
106  }
107  templatelog_ctx->eve_ctx = ajt;
108 
109  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
110  if (unlikely(output_ctx == NULL)) {
111  SCFree(templatelog_ctx);
112  return result;
113  }
114  output_ctx->data = templatelog_ctx;
115  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
116 
117  SCLogNotice("Template log sub-module initialized.");
118 
120 
121  result.ctx = output_ctx;
122  result.ok = true;
123  return result;
124 }
125 
126 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
127 {
128  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
129  if (unlikely(thread == NULL)) {
130  return TM_ECODE_FAILED;
131  }
132 
133  if (initdata == NULL) {
134  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
135  goto error_exit;
136  }
137 
138  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
139  thread->ctx = CreateEveThreadCtx(t, thread->templatelog_ctx->eve_ctx);
140  if (!thread->ctx) {
141  goto error_exit;
142  }
143  *data = (void *)thread;
144 
145  return TM_ECODE_OK;
146 
147 error_exit:
148  SCFree(thread);
149  return TM_ECODE_FAILED;
150 }
151 
152 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
153 {
154  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
155  if (thread == NULL) {
156  return TM_ECODE_OK;
157  }
158  FreeEveThreadCtx(thread->ctx);
159  SCFree(thread);
160  return TM_ECODE_OK;
161 }
162 
164 {
165  /* TEMPLATE_START_REMOVE */
166  if (ConfGetNode("app-layer.protocols.template") == NULL) {
167  return;
168  }
169  /* TEMPLATE_END_REMOVE */
170  /* Register as an eve sub-module. */
171  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonTemplateLog", "eve-log.template",
172  OutputTemplateLogInitSub, ALPROTO_TEMPLATE, JsonTemplateLogger,
173  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
174 
175  SCLogNotice("Template JSON logger registered.");
176 }
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:79
Flow_
Flow data structure.
Definition: flow.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:928
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
LogTemplateLogThread
struct LogTemplateLogThread_ LogTemplateLogThread
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:88
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:85
OutputJsonThreadCtx_
Definition: output-json.h:87
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json-template.h
output-json.h
LogTemplateLogThread_::templatelog_ctx
LogTemplateFileCtx * templatelog_ctx
Definition: output-json-template.c:61
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:475
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:787
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:436
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
LogTemplateLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-template.c:62
LogTemplateFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-template.c:57
OutputInitResult_
Definition: output.h:46
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:37
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:91
LogTemplateFileCtx_
Definition: output-json-template.c:55
LogTemplateFileCtx_::flags
uint32_t flags
Definition: output-json-template.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:413
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:467
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.c:60
JsonTemplateLogRegister
void JsonTemplateLogRegister(void)
Definition: output-json-template.c:163
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