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 "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threadvars.h"
33 #include "tm-modules.h"
34 
35 #include "threads.h"
36 
37 #include "app-layer-parser.h"
38 
39 #include "detect-filemagic.h"
40 
41 #include "stream.h"
42 
43 #include "util-print.h"
44 #include "util-unittest.h"
45 #include "util-privs.h"
46 #include "util-debug.h"
47 #include "util-atomic.h"
48 #include "util-file.h"
49 #include "util-time.h"
50 #include "util-buffer.h"
51 #include "util-byte.h"
52 #include "util-validate.h"
53 
54 #include "util-logopenfile.h"
55 
56 #include "output.h"
57 #include "output-json.h"
58 #include "output-json-file.h"
59 #include "output-json-http.h"
60 #include "output-json-smtp.h"
62 #include "output-json-nfs.h"
63 #include "output-json-smb.h"
64 #include "output-json-http2.h"
65 
66 #include "app-layer-htp.h"
67 #include "app-layer-htp-xff.h"
68 #include "util-memcmp.h"
69 #include "stream-tcp-reassemble.h"
70 
71 typedef struct OutputFileCtx_ {
72  uint32_t file_cnt;
77 
78 typedef struct JsonFileLogThread_ {
82 
83 JsonBuilder *JsonBuildFileInfoRecord(const Packet *p, const File *ff, void *tx,
84  const uint64_t tx_id, const bool stored, uint8_t dir, HttpXFFCfg *xff_cfg,
85  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)) {
108  if (FlowGetAppProtocol(p->flow) == ALPROTO_HTTP1) {
109  have_xff_ip = HttpXFFGetIPFromTx(p->flow, tx_id, 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 };
127  switch (p->flow->alproto) {
128  case ALPROTO_HTTP1:
129  jb_open_object(js, "http");
130  EveHttpAddMetadata(p->flow, tx_id, js);
131  jb_close(js);
132  break;
133  case ALPROTO_SMTP:
134  jb_get_mark(js, &mark);
135  jb_open_object(js, "smtp");
136  if (EveSMTPAddMetadata(p->flow, tx_id, js)) {
137  jb_close(js);
138  } else {
139  jb_restore_mark(js, &mark);
140  }
141  jb_get_mark(js, &mark);
142  jb_open_object(js, "email");
143  if (EveEmailAddMetadata(p->flow, tx_id, js)) {
144  jb_close(js);
145  } else {
146  jb_restore_mark(js, &mark);
147  }
148  break;
149  case ALPROTO_NFS:
150  /* rpc */
151  jb_get_mark(js, &mark);
152  jb_open_object(js, "rpc");
153  if (EveNFSAddMetadataRPC(p->flow, tx_id, js)) {
154  jb_close(js);
155  } else {
156  jb_restore_mark(js, &mark);
157  }
158  /* nfs */
159  jb_get_mark(js, &mark);
160  jb_open_object(js, "nfs");
161  if (EveNFSAddMetadata(p->flow, tx_id, js)) {
162  jb_close(js);
163  } else {
164  jb_restore_mark(js, &mark);
165  }
166  break;
167  case ALPROTO_SMB:
168  jb_get_mark(js, &mark);
169  jb_open_object(js, "smb");
170  if (EveSMBAddMetadata(p->flow, tx_id, js)) {
171  jb_close(js);
172  } else {
173  jb_restore_mark(js, &mark);
174  }
175  break;
176  default:
178  if (al && al->LogTx) {
179  void *state = FlowGetAppState(p->flow);
180  if (state) {
181  tx = AppLayerParserGetTx(p->flow->proto, p->flow->alproto, state, tx_id);
182  if (tx) {
183  jb_get_mark(js, &mark);
184  if (!al->LogTx(tx, js)) {
185  jb_restore_mark(js, &mark);
186  }
187  }
188  }
189  }
190  break;
191  }
192 
193  jb_set_string(js, "app_proto", AppProtoToString(p->flow->alproto));
194 
195  jb_open_object(js, "fileinfo");
196  if (stored) {
197  // the file has just been stored on disk cf OUTPUT_FILEDATA_FLAG_CLOSE
198  // but the flag is not set until the loggers have been called
199  EveFileInfo(js, ff, tx_id, ff->flags | FILE_STORED);
200  } else {
201  EveFileInfo(js, ff, tx_id, ff->flags);
202  }
203  jb_close(js);
204 
205  /* xff header */
206  if (have_xff_ip && xff_cfg->flags & XFF_EXTRADATA) {
207  jb_set_string(js, "xff", xff_buffer);
208  }
209 
210  return js;
211 }
212 
213 /**
214  * \internal
215  * \brief Write meta data on a single line json record
216  */
217 static void FileWriteJsonRecord(JsonFileLogThread *aft, const Packet *p, const File *ff, void *tx,
218  const uint64_t tx_id, uint8_t dir, OutputJsonCtx *eve_ctx)
219 {
220  HttpXFFCfg *xff_cfg = aft->filelog_ctx->xff_cfg != NULL ? aft->filelog_ctx->xff_cfg
221  : aft->filelog_ctx->parent_xff_cfg;
222  JsonBuilder *js = JsonBuildFileInfoRecord(p, ff, tx, tx_id, false, dir, xff_cfg, eve_ctx);
223  if (unlikely(js == NULL)) {
224  return;
225  }
226 
227  OutputJsonBuilderBuffer(js, aft->ctx);
228  jb_free(js);
229 }
230 
231 static int JsonFileLogger(ThreadVars *tv, void *thread_data, const Packet *p, const File *ff,
232  void *tx, const uint64_t tx_id, uint8_t dir)
233 {
234  SCEnter();
235  JsonFileLogThread *aft = (JsonFileLogThread *)thread_data;
236 
237  BUG_ON(ff->flags & FILE_LOGGED);
238 
239  SCLogDebug("ff %p", ff);
240 
241  FileWriteJsonRecord(aft, p, ff, tx, tx_id, dir, aft->filelog_ctx->eve_ctx);
242  return 0;
243 }
244 
245 
246 static TmEcode JsonFileLogThreadInit(ThreadVars *t, const void *initdata, void **data)
247 {
248  JsonFileLogThread *aft = SCCalloc(1, sizeof(JsonFileLogThread));
249  if (unlikely(aft == NULL))
250  return TM_ECODE_FAILED;
251 
252  if(initdata == NULL)
253  {
254  SCLogDebug("Error getting context for EveLogFile. \"initdata\" argument NULL");
255  goto error_exit;
256  }
257 
258  /* Use the Output Context (file pointer and mutex) */
259  aft->filelog_ctx = ((OutputCtx *)initdata)->data;
260  aft->ctx = CreateEveThreadCtx(t, aft->filelog_ctx->eve_ctx);
261  if (!aft->ctx) {
262  goto error_exit;
263  }
264 
265  *data = (void *)aft;
266  return TM_ECODE_OK;
267 
268 error_exit:
269  SCFree(aft);
270  return TM_ECODE_FAILED;
271 }
272 
273 static TmEcode JsonFileLogThreadDeinit(ThreadVars *t, void *data)
274 {
275  JsonFileLogThread *aft = (JsonFileLogThread *)data;
276  if (aft == NULL) {
277  return TM_ECODE_OK;
278  }
279 
280  FreeEveThreadCtx(aft->ctx);
281 
282  /* clear memory */
283  memset(aft, 0, sizeof(JsonFileLogThread));
284 
285  SCFree(aft);
286  return TM_ECODE_OK;
287 }
288 
289 static void OutputFileLogDeinitSub(OutputCtx *output_ctx)
290 {
291  OutputFileCtx *ff_ctx = output_ctx->data;
292  if (ff_ctx->xff_cfg != NULL) {
293  SCFree(ff_ctx->xff_cfg);
294  }
295  SCFree(ff_ctx);
296  SCFree(output_ctx);
297 }
298 
299 /** \brief Create a new http log LogFileCtx.
300  * \param conf Pointer to ConfNode containing this loggers configuration.
301  * \return NULL if failure, LogFileCtx* to the file_ctx if succesful
302  * */
303 static OutputInitResult OutputFileLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
304 {
305  OutputInitResult result = { NULL, false };
306  OutputJsonCtx *ojc = parent_ctx->data;
307 
308  OutputFileCtx *output_file_ctx = SCCalloc(1, sizeof(OutputFileCtx));
309  if (unlikely(output_file_ctx == NULL))
310  return result;
311 
312  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
313  if (unlikely(output_ctx == NULL)) {
314  SCFree(output_file_ctx);
315  return result;
316  }
317 
318  if (conf) {
319  const char *force_filestore = ConfNodeLookupChildValue(conf, "force-filestore");
320  if (force_filestore != NULL && ConfValIsTrue(force_filestore)) {
322  SCLogConfig("forcing filestore of all files");
323  }
324 
325  const char *force_magic = ConfNodeLookupChildValue(conf, "force-magic");
326  if (force_magic != NULL && ConfValIsTrue(force_magic)) {
328  SCLogConfig("forcing magic lookup for logged files");
329  }
330 
331  FileForceHashParseCfg(conf);
332  }
333 
334  if (conf != NULL && ConfNodeLookupChild(conf, "xff") != NULL) {
335  output_file_ctx->xff_cfg = SCCalloc(1, sizeof(HttpXFFCfg));
336  if (output_file_ctx->xff_cfg != NULL) {
337  HttpXFFGetCfg(conf, output_file_ctx->xff_cfg);
338  }
339  } else if (ojc->xff_cfg) {
340  output_file_ctx->parent_xff_cfg = ojc->xff_cfg;
341  }
342 
343  output_file_ctx->eve_ctx = ojc;
344  output_ctx->data = output_file_ctx;
345  output_ctx->DeInit = OutputFileLogDeinitSub;
346 
348  result.ctx = output_ctx;
349  result.ok = true;
350  return result;
351 }
352 
354 {
355  /* register as child of eve-log */
356  OutputRegisterFileSubModule(LOGGER_JSON_FILE, "eve-log", "JsonFileLog",
357  "eve-log.files", OutputFileLogInitSub, JsonFileLogger,
358  JsonFileLogThreadInit, JsonFileLogThreadDeinit, NULL);
359 }
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:96
OutputJsonCtx_::xff_cfg
HttpXFFCfg * xff_cfg
Definition: output-json.h:83
XFF_EXTRADATA
#define XFF_EXTRADATA
Definition: app-layer-htp-xff.h:31
FileForceTrackingEnable
void FileForceTrackingEnable(void)
Definition: util-file.c:161
OutputFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-file.c:75
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonFileLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-file.c:80
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
Flow_::proto
uint8_t proto
Definition: flow.h:373
SCEveJsonSimpleGetLogger
EveJsonSimpleAppLayerLogger * SCEveJsonSimpleGetLogger(AppProto alproto)
Definition: output.c:1173
threads.h
OutputJsonCtx_
Definition: output-json.h:79
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:74
JsonFileLogRegister
void JsonFileLogRegister(void)
Definition: output-json-file.c:353
EveNFSAddMetadata
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:62
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
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:470
tm-modules.h
EveFileInfo
void EveFileInfo(JsonBuilder *jb, const File *ff, const uint64_t tx_id, const uint16_t flags)
Definition: output-json.c:132
util-privs.h
stream-tcp-reassemble.h
FileForceMagicEnable
void FileForceMagicEnable(void)
Definition: util-file.c:98
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:468
OutputFileCtx_::xff_cfg
HttpXFFCfg * xff_cfg
Definition: output-json-file.c:73
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
EveHttpAddMetadata
bool EveHttpAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-http.c:503
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:537
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
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:87
util-memcmp.h
app-layer-htp.h
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
LOGGER_JSON_FILE
@ LOGGER_JSON_FILE
Definition: suricata-common.h:483
output-json.h
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:32
output-json-file.h
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:787
util-print.h
detect-filemagic.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
util-atomic.h
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
stream.h
JsonAddrInfo_
Definition: output-json.h:47
Packet_
Definition: decode.h:437
conf.h
XFF_OVERWRITE
#define XFF_OVERWRITE
Definition: app-layer-htp-xff.h:33
JsonBuildFileInfoRecord
JsonBuilder * JsonBuildFileInfoRecord(const Packet *p, const File *ff, void *tx, const uint64_t tx_id, const bool stored, uint8_t dir, HttpXFFCfg *xff_cfg, OutputJsonCtx *eve_ctx)
Definition: output-json-file.c:83
TmEcode
TmEcode
Definition: tm-threads-common.h:83
EveNFSAddMetadataRPC
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:50
HttpXFFCfg_
Definition: app-layer-htp-xff.h:41
LOG_DIR_FLOW_TOCLIENT
@ LOG_DIR_FLOW_TOCLIENT
Definition: output-json.h:39
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:224
EveJsonSimpleAppLayerLogger
Definition: output.h:213
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1125
EveEmailAddMetadata
bool EveEmailAddMetadata(const Flow *f, uint32_t tx_id, JsonBuilder *js)
Definition: output-json-email-common.c:359
ConfNodeLookupChild
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:786
File_::flags
uint16_t flags
Definition: util-file.h:80
util-file.h
File_
Definition: util-file.h:79
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:476
suricata-common.h
FileForceFilestoreEnable
void FileForceFilestoreEnable(void)
Definition: util-file.c:92
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:91
EveJsonSimpleAppLayerLogger::LogTx
EveJsonSimpleTxLogFunc LogTx
Definition: output.h:215
JSON_ADDR_LEN
#define JSON_ADDR_LEN
Definition: output-json.h:43
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:78
FileForceHashParseCfg
void FileForceHashParseCfg(ConfNode *conf)
Function to parse forced file hashing configuration.
Definition: util-file.c:170
OutputJsonLogDirection
OutputJsonLogDirection
Definition: output-json.h:36
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
util-validate.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
output-json-http2.h
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:48
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:40
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:49
JsonFileLogThread
struct JsonFileLogThread_ JsonFileLogThread
JsonAddrInfoInit
void JsonAddrInfoInit(const Packet *p, enum OutputJsonLogDirection dir, JsonAddrInfo *addr)
Definition: output-json.c:477
HttpXFFGetCfg
void HttpXFFGetCfg(ConfNode *conf, HttpXFFCfg *result)
Function to return XFF configuration from a configuration node.
Definition: app-layer-htp-xff.c:205
output-json-smb.h
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
OutputFileCtx_
Definition: output-json-file.c:71
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:116
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:34
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
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:79
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:103
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
OutputFileCtx_::file_cnt
uint32_t file_cnt
Definition: output-json-file.c:72
output.h
OutputFileCtx_::parent_xff_cfg
HttpXFFCfg * parent_xff_cfg
Definition: output-json-file.c:74
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:814