suricata
output-json-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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  * \file
20  *
21  * \author Tom DeCanio <td@npulsetech.com>
22  *
23  * Log files we track.
24  *
25  */
26 
27 #include "suricata-common.h"
28 #include "debug.h"
29 #include "detect.h"
30 #include "pkt-var.h"
31 #include "conf.h"
32 
33 #include "threadvars.h"
34 #include "tm-modules.h"
35 
36 #include "threads.h"
37 
38 #include "app-layer-parser.h"
39 
40 #include "detect-filemagic.h"
41 
42 #include "stream.h"
43 
44 #include "util-print.h"
45 #include "util-unittest.h"
46 #include "util-privs.h"
47 #include "util-debug.h"
48 #include "util-atomic.h"
49 #include "util-file.h"
50 #include "util-time.h"
51 #include "util-buffer.h"
52 #include "util-byte.h"
53 #include "util-validate.h"
54 
55 #include "util-logopenfile.h"
56 
57 #include "output.h"
58 #include "output-json.h"
59 #include "output-json-file.h"
60 #include "output-json-http.h"
61 #include "output-json-smtp.h"
63 #include "output-json-nfs.h"
64 #include "output-json-smb.h"
65 #include "output-json-http2.h"
66 
67 #include "app-layer-htp.h"
68 #include "app-layer-htp-xff.h"
69 #include "util-memcmp.h"
70 #include "stream-tcp-reassemble.h"
71 
72 typedef struct OutputFileCtx_ {
73  uint32_t file_cnt;
78 
79 typedef struct JsonFileLogThread_ {
83 
84 JsonBuilder *JsonBuildFileInfoRecord(const Packet *p, const File *ff, const bool stored,
85  uint8_t dir, HttpXFFCfg *xff_cfg, OutputJsonCtx *eve_ctx)
86 {
88 
89  switch(dir) {
90  case STREAM_TOCLIENT:
91  fdir = LOG_DIR_FLOW_TOCLIENT;
92  break;
93  case STREAM_TOSERVER:
94  fdir = LOG_DIR_FLOW_TOSERVER;
95  break;
96  default:
98  break;
99  }
100 
102  JsonAddrInfoInit(p, fdir, &addr);
103 
104  /* Overwrite address info with XFF if needed. */
105  int have_xff_ip = 0;
106  char xff_buffer[XFF_MAXLEN];
107  if ((xff_cfg != NULL) && !(xff_cfg->flags & XFF_DISABLED)) {
109  have_xff_ip = HttpXFFGetIPFromTx(p->flow, ff->txid, xff_cfg, xff_buffer, XFF_MAXLEN);
110  }
111  if (have_xff_ip && xff_cfg->flags & XFF_OVERWRITE) {
112  if (p->flowflags & FLOW_PKT_TOCLIENT) {
113  strlcpy(addr.dst_ip, xff_buffer, JSON_ADDR_LEN);
114  } else {
115  strlcpy(addr.src_ip, xff_buffer, JSON_ADDR_LEN);
116  }
117  have_xff_ip = 0;
118  }
119  }
120 
121  JsonBuilder *js = CreateEveHeader(p, fdir, "fileinfo", &addr, eve_ctx);
122  if (unlikely(js == NULL))
123  return NULL;
124 
125  JsonBuilderMark mark = { 0, 0, 0 };
126  switch (p->flow->alproto) {
127  case ALPROTO_HTTP1:
128  jb_open_object(js, "http");
129  EveHttpAddMetadata(p->flow, ff->txid, js);
130  jb_close(js);
131  break;
132  case ALPROTO_SMTP:
133  jb_get_mark(js, &mark);
134  jb_open_object(js, "smtp");
135  if (EveSMTPAddMetadata(p->flow, ff->txid, js)) {
136  jb_close(js);
137  } else {
138  jb_restore_mark(js, &mark);
139  }
140  jb_get_mark(js, &mark);
141  jb_open_object(js, "email");
142  if (EveEmailAddMetadata(p->flow, ff->txid, js)) {
143  jb_close(js);
144  } else {
145  jb_restore_mark(js, &mark);
146  }
147  break;
148  case ALPROTO_NFS:
149  /* rpc */
150  jb_get_mark(js, &mark);
151  jb_open_object(js, "rpc");
152  if (EveNFSAddMetadataRPC(p->flow, ff->txid, js)) {
153  jb_close(js);
154  } else {
155  jb_restore_mark(js, &mark);
156  }
157  /* nfs */
158  jb_get_mark(js, &mark);
159  jb_open_object(js, "nfs");
160  if (EveNFSAddMetadata(p->flow, ff->txid, js)) {
161  jb_close(js);
162  } else {
163  jb_restore_mark(js, &mark);
164  }
165  break;
166  case ALPROTO_SMB:
167  jb_get_mark(js, &mark);
168  jb_open_object(js, "smb");
169  if (EveSMBAddMetadata(p->flow, ff->txid, js)) {
170  jb_close(js);
171  } else {
172  jb_restore_mark(js, &mark);
173  }
174  break;
175  case ALPROTO_HTTP2:
176  jb_get_mark(js, &mark);
177  jb_open_object(js, "http2");
178  if (EveHTTP2AddMetadata(p->flow, ff->txid, js)) {
179  jb_close(js);
180  } else {
181  jb_restore_mark(js, &mark);
182  }
183  break;
184  }
185 
186  jb_set_string(js, "app_proto", AppProtoToString(p->flow->alproto));
187 
188  jb_open_object(js, "fileinfo");
189  EveFileInfo(js, ff, stored);
190  jb_close(js);
191 
192  /* xff header */
193  if (have_xff_ip && xff_cfg->flags & XFF_EXTRADATA) {
194  jb_set_string(js, "xff", xff_buffer);
195  }
196 
197  return js;
198 }
199 
200 /**
201  * \internal
202  * \brief Write meta data on a single line json record
203  */
204 static void FileWriteJsonRecord(JsonFileLogThread *aft, const Packet *p, const File *ff,
205  uint32_t dir, OutputJsonCtx *eve_ctx)
206 {
207  HttpXFFCfg *xff_cfg = aft->filelog_ctx->xff_cfg != NULL ?
209  JsonBuilder *js = JsonBuildFileInfoRecord(
210  p, ff, ff->flags & FILE_STORED ? true : false, dir, xff_cfg, eve_ctx);
211  if (unlikely(js == NULL)) {
212  return;
213  }
214 
215  OutputJsonBuilderBuffer(js, aft->ctx);
216  jb_free(js);
217 }
218 
219 static int JsonFileLogger(ThreadVars *tv, void *thread_data, const Packet *p,
220  const File *ff, uint8_t dir)
221 {
222  SCEnter();
223  JsonFileLogThread *aft = (JsonFileLogThread *)thread_data;
224 
225  BUG_ON(ff->flags & FILE_LOGGED);
226 
227  SCLogDebug("ff %p", ff);
228 
229  FileWriteJsonRecord(aft, p, ff, dir, aft->filelog_ctx->eve_ctx);
230  return 0;
231 }
232 
233 
234 static TmEcode JsonFileLogThreadInit(ThreadVars *t, const void *initdata, void **data)
235 {
236  JsonFileLogThread *aft = SCCalloc(1, sizeof(JsonFileLogThread));
237  if (unlikely(aft == NULL))
238  return TM_ECODE_FAILED;
239 
240  if(initdata == NULL)
241  {
242  SCLogDebug("Error getting context for EveLogFile. \"initdata\" argument NULL");
243  goto error_exit;
244  }
245 
246  /* Use the Ouptut Context (file pointer and mutex) */
247  aft->filelog_ctx = ((OutputCtx *)initdata)->data;
248  aft->ctx = CreateEveThreadCtx(t, aft->filelog_ctx->eve_ctx);
249  if (!aft->ctx) {
250  goto error_exit;
251  }
252 
253  *data = (void *)aft;
254  return TM_ECODE_OK;
255 
256 error_exit:
257  SCFree(aft);
258  return TM_ECODE_FAILED;
259 }
260 
261 static TmEcode JsonFileLogThreadDeinit(ThreadVars *t, void *data)
262 {
263  JsonFileLogThread *aft = (JsonFileLogThread *)data;
264  if (aft == NULL) {
265  return TM_ECODE_OK;
266  }
267 
268  FreeEveThreadCtx(aft->ctx);
269 
270  /* clear memory */
271  memset(aft, 0, sizeof(JsonFileLogThread));
272 
273  SCFree(aft);
274  return TM_ECODE_OK;
275 }
276 
277 static void OutputFileLogDeinitSub(OutputCtx *output_ctx)
278 {
279  OutputFileCtx *ff_ctx = output_ctx->data;
280  if (ff_ctx->xff_cfg != NULL) {
281  SCFree(ff_ctx->xff_cfg);
282  }
283  SCFree(ff_ctx);
284  SCFree(output_ctx);
285 }
286 
287 /** \brief Create a new http log LogFileCtx.
288  * \param conf Pointer to ConfNode containing this loggers configuration.
289  * \return NULL if failure, LogFileCtx* to the file_ctx if succesful
290  * */
291 static OutputInitResult OutputFileLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
292 {
293  OutputInitResult result = { NULL, false };
294  OutputJsonCtx *ojc = parent_ctx->data;
295 
296  OutputFileCtx *output_file_ctx = SCCalloc(1, sizeof(OutputFileCtx));
297  if (unlikely(output_file_ctx == NULL))
298  return result;
299 
300  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
301  if (unlikely(output_ctx == NULL)) {
302  SCFree(output_file_ctx);
303  return result;
304  }
305 
306  if (conf) {
307  const char *force_filestore = ConfNodeLookupChildValue(conf, "force-filestore");
308  if (force_filestore != NULL && ConfValIsTrue(force_filestore)) {
310  SCLogConfig("forcing filestore of all files");
311  }
312 
313  const char *force_magic = ConfNodeLookupChildValue(conf, "force-magic");
314  if (force_magic != NULL && ConfValIsTrue(force_magic)) {
316  SCLogConfig("forcing magic lookup for logged files");
317  }
318 
319  FileForceHashParseCfg(conf);
320  }
321 
322  if (conf != NULL && ConfNodeLookupChild(conf, "xff") != NULL) {
323  output_file_ctx->xff_cfg = SCCalloc(1, sizeof(HttpXFFCfg));
324  if (output_file_ctx->xff_cfg != NULL) {
325  HttpXFFGetCfg(conf, output_file_ctx->xff_cfg);
326  }
327  } else if (ojc->xff_cfg) {
328  output_file_ctx->parent_xff_cfg = ojc->xff_cfg;
329  }
330 
331  output_file_ctx->eve_ctx = ojc;
332  output_ctx->data = output_file_ctx;
333  output_ctx->DeInit = OutputFileLogDeinitSub;
334 
336  result.ctx = output_ctx;
337  result.ok = true;
338  return result;
339 }
340 
342 {
343  /* register as child of eve-log */
344  OutputRegisterFileSubModule(LOGGER_JSON_FILE, "eve-log", "JsonFileLog",
345  "eve-log.files", OutputFileLogInitSub, JsonFileLogger,
346  JsonFileLogThreadInit, JsonFileLogThreadDeinit, NULL);
347 }
XFF_MAXLEN
#define XFF_MAXLEN
Definition: app-layer-htp-xff.h:39
util-byte.h
EveSMTPAddMetadata
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-smtp.c:98
OutputJsonCtx_::xff_cfg
HttpXFFCfg * xff_cfg
Definition: output-json.h:85
XFF_EXTRADATA
#define XFF_EXTRADATA
Definition: app-layer-htp-xff.h:31
FileForceTrackingEnable
void FileForceTrackingEnable(void)
Definition: util-file.c:162
OutputFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-file.c:76
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonFileLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-file.c:81
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
threads.h
OutputJsonCtx_
Definition: output-json.h:81
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:30
JsonFileLogRegister
void JsonFileLogRegister(void)
Definition: output-json-file.c:341
EveNFSAddMetadata
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:63
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:974
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
json_addr_info_zero
const JsonAddrInfo json_addr_info_zero
Definition: output-json.c:89
OutputRegisterFileSubModule
void OutputRegisterFileSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, FileLogger FileLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a file output sub-module.
Definition: output.c:467
tm-modules.h
util-privs.h
stream-tcp-reassemble.h
FileForceMagicEnable
void FileForceMagicEnable(void)
Definition: util-file.c:99
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:447
OutputFileCtx_::xff_cfg
HttpXFFCfg * xff_cfg
Definition: output-json-file.c:74
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
EveHttpAddMetadata
bool EveHttpAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-http.c:521
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:565
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
app-layer-htp-xff.h
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
OutputJsonThreadCtx_
Definition: output-json.h:89
util-memcmp.h
app-layer-htp.h
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
LOGGER_JSON_FILE
@ LOGGER_JSON_FILE
Definition: suricata-common.h:480
output-json.h
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:32
output-json-file.h
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:838
util-print.h
detect-filemagic.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
EveFileInfo
void EveFileInfo(JsonBuilder *jb, const File *ff, const bool stored)
Definition: output-json.c:127
util-atomic.h
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:277
stream.h
JsonAddrInfo_
Definition: output-json.h:49
Packet_
Definition: decode.h:416
conf.h
EveHTTP2AddMetadata
bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-http2.c:67
XFF_OVERWRITE
#define XFF_OVERWRITE
Definition: app-layer-htp-xff.h:33
TmEcode
TmEcode
Definition: tm-threads-common.h:79
EveNFSAddMetadataRPC
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:51
HttpXFFCfg_
Definition: app-layer-htp-xff.h:41
LOG_DIR_FLOW_TOCLIENT
@ LOG_DIR_FLOW_TOCLIENT
Definition: output-json.h:41
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:59
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:223
EveEmailAddMetadata
bool EveEmailAddMetadata(const Flow *f, uint32_t tx_id, JsonBuilder *js)
Definition: output-json-email-common.c:374
JsonBuildFileInfoRecord
JsonBuilder * JsonBuildFileInfoRecord(const Packet *p, const File *ff, const bool stored, uint8_t dir, HttpXFFCfg *xff_cfg, OutputJsonCtx *eve_ctx)
Definition: output-json-file.c:84
ConfNodeLookupChild
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:814
File_::flags
uint16_t flags
Definition: util-file.h:73
util-file.h
File_
Definition: util-file.h:72
OutputInitResult_
Definition: output.h:43
Packet_::flow
struct Flow_ * flow
Definition: decode.h:453
suricata-common.h
FileForceFilestoreEnable
void FileForceFilestoreEnable(void)
Definition: util-file.c:93
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
JSON_ADDR_LEN
#define JSON_ADDR_LEN
Definition: output-json.h:45
output-json-nfs.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
FILE_STORED
#define FILE_STORED
Definition: util-file.h:56
JsonFileLogThread_
Definition: output-json-file.c:79
FileForceHashParseCfg
void FileForceHashParseCfg(ConfNode *conf)
Function to parse forced file hashing configuration.
Definition: util-file.c:171
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
OutputJsonLogDirection
OutputJsonLogDirection
Definition: output-json.h:38
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
util-validate.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
output-json-http2.h
FlowGetAppProtocol
AppProto FlowGetAppProtocol(const Flow *f)
Definition: flow.c:1097
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
JsonAddrInfo_::src_ip
char src_ip[JSON_ADDR_LEN]
Definition: output-json.h:50
HttpXFFCfg_::flags
uint8_t flags
Definition: app-layer-htp-xff.h:42
FILE_LOGGED
#define FILE_LOGGED
Definition: util-file.h:53
LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOSERVER
Definition: output-json.h:42
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
JsonAddrInfo_::dst_ip
char dst_ip[JSON_ADDR_LEN]
Definition: output-json.h:51
JsonFileLogThread
struct JsonFileLogThread_ JsonFileLogThread
JsonAddrInfoInit
void JsonAddrInfoInit(const Packet *p, enum OutputJsonLogDirection dir, JsonAddrInfo *addr)
Definition: output-json.c:491
File_::txid
uint64_t txid
Definition: util-file.h:77
HttpXFFGetCfg
void HttpXFFGetCfg(ConfNode *conf, HttpXFFCfg *result)
Function to return XFF configuration from a configuration node.
Definition: app-layer-htp-xff.c:202
output-json-smb.h
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
OutputFileCtx_
Definition: output-json-file.c:72
HttpXFFGetIPFromTx
int HttpXFFGetIPFromTx(const Flow *f, uint64_t tx_id, HttpXFFCfg *xff_cfg, char *dstbuf, int dstbuflen)
Function to return XFF IP if any in the selected transaction. The caller needs to lock the flow.
Definition: app-layer-htp-xff.c:113
output-json-smtp.h
output-json-http.h
EveSMBAddMetadata
bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-smb.c:50
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:457
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
OutputFileCtx
struct OutputFileCtx_ OutputFileCtx
XFF_DISABLED
#define XFF_DISABLED
Definition: app-layer-htp-xff.h:29
JsonFileLogThread_::filelog_ctx
OutputFileCtx * filelog_ctx
Definition: output-json-file.c:80
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
debug.h
OutputFileCtx_::file_cnt
uint32_t file_cnt
Definition: output-json-file.c:73
output.h
OutputFileCtx_::parent_xff_cfg
HttpXFFCfg * parent_xff_cfg
Definition: output-json-file.c:75
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:842