suricata
output-json-template.c
Go to the documentation of this file.
1 /* Copyright (C) 2015 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 #ifdef HAVE_LIBJANSSON
57 
58 typedef struct LogTemplateFileCtx_ {
59  LogFileCtx *file_ctx;
60  uint32_t flags;
61 } LogTemplateFileCtx;
62 
63 typedef struct LogTemplateLogThread_ {
64  LogTemplateFileCtx *templatelog_ctx;
65  uint32_t count;
66  MemBuffer *buffer;
67 } LogTemplateLogThread;
68 
69 static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
70  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
71 {
72  TemplateTransaction *templatetx = tx;
73  LogTemplateLogThread *thread = thread_data;
74 
75  SCLogNotice("Logging template transaction %"PRIu64".", templatetx->tx_id);
76 
77  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "template");
78  if (unlikely(js == NULL)) {
79  return TM_ECODE_FAILED;
80  }
81 
82  json_t *templatejs = json_object();
83  if (unlikely(templatejs == NULL)) {
84  goto error;
85  }
86 
87  /* Convert the request buffer to a string then log. */
88  char *request_buffer = BytesToString(templatetx->request_buffer,
89  templatetx->request_buffer_len);
90  if (request_buffer != NULL) {
91  json_object_set_new(templatejs, "request", json_string(request_buffer));
92  SCFree(request_buffer);
93  }
94 
95  /* Convert the response buffer to a string then log. */
96  char *response_buffer = BytesToString(templatetx->response_buffer,
97  templatetx->response_buffer_len);
98  if (response_buffer != NULL) {
99  json_object_set_new(templatejs, "response",
100  json_string(response_buffer));
101  SCFree(response_buffer);
102  }
103 
104  json_object_set_new(js, "template", templatejs);
105 
106  MemBufferReset(thread->buffer);
107  OutputJSONBuffer(js, thread->templatelog_ctx->file_ctx, &thread->buffer);
108 
109  json_decref(js);
110  return TM_ECODE_OK;
111 
112 error:
113  json_decref(js);
114  return TM_ECODE_FAILED;
115 }
116 
117 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
118 {
119  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
120  SCFree(templatelog_ctx);
121  SCFree(output_ctx);
122 }
123 
124 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf,
125  OutputCtx *parent_ctx)
126 {
127  OutputInitResult result = { NULL, false };
128  OutputJsonCtx *ajt = parent_ctx->data;
129 
130  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
131  if (unlikely(templatelog_ctx == NULL)) {
132  return result;
133  }
134  templatelog_ctx->file_ctx = ajt->file_ctx;
135 
136  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
137  if (unlikely(output_ctx == NULL)) {
138  SCFree(templatelog_ctx);
139  return result;
140  }
141  output_ctx->data = templatelog_ctx;
142  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
143 
144  SCLogNotice("Template log sub-module initialized.");
145 
147 
148  result.ctx = output_ctx;
149  result.ok = true;
150  return result;
151 }
152 
153 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
154 {
155  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
156  if (unlikely(thread == NULL)) {
157  return TM_ECODE_FAILED;
158  }
159 
160  if (initdata == NULL) {
161  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
162  SCFree(thread);
163  return TM_ECODE_FAILED;
164  }
165 
166  thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
167  if (unlikely(thread->buffer == NULL)) {
168  SCFree(thread);
169  return TM_ECODE_FAILED;
170  }
171 
172  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
173  *data = (void *)thread;
174 
175  return TM_ECODE_OK;
176 }
177 
178 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
179 {
180  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
181  if (thread == NULL) {
182  return TM_ECODE_OK;
183  }
184  if (thread->buffer != NULL) {
185  MemBufferFree(thread->buffer);
186  }
187  SCFree(thread);
188  return TM_ECODE_OK;
189 }
190 
191 void JsonTemplateLogRegister(void)
192 {
193  /* TEMPLATE_START_REMOVE */
194  if (ConfGetNode("app-layer.protocols.template") == NULL) {
195  return;
196  }
197  /* TEMPLATE_END_REMOVE */
198  /* Register as an eve sub-module. */
199  OutputRegisterTxSubModule(LOGGER_JSON_TEMPLATE, "eve-log", "JsonTemplateLog",
200  "eve-log.template", OutputTemplateLogInitSub, ALPROTO_TEMPLATE,
201  JsonTemplateLogger, JsonTemplateLogThreadInit,
202  JsonTemplateLogThreadDeinit, NULL);
203 
204  SCLogNotice("Template JSON logger registered.");
205 }
206 
207 #else /* No JSON support. */
208 
210 {
211 }
212 
213 #endif /* HAVE_LIBJANSSON */
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
#define SCCalloc(nm, a)
Definition: util-mem.h:253
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
void JsonTemplateLogRegister(void)
char * BytesToString(const uint8_t *bytes, size_t nbytes)
Turn byte array into string.
Definition: util-byte.c:41
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:400
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
uint16_t tx_id
void * data
Definition: tm-modules.h:81
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82