suricata
output-json-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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 
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_ {
73  uint32_t file_cnt;
77 
78 typedef struct JsonFileLogThread_ {
82 
83 JsonBuilder *JsonBuildFileInfoRecord(const Packet *p, const File *ff,
84  const bool stored, uint8_t dir, HttpXFFCfg *xff_cfg)
85 {
87 
88  switch(dir) {
89  case STREAM_TOCLIENT:
90  fdir = LOG_DIR_FLOW_TOCLIENT;
91  break;
92  case STREAM_TOSERVER:
93  fdir = LOG_DIR_FLOW_TOSERVER;
94  break;
95  default:
97  break;
98  }
99 
101  JsonAddrInfoInit(p, fdir, &addr);
102 
103  /* Overwrite address info with XFF if needed. */
104  int have_xff_ip = 0;
105  char xff_buffer[XFF_MAXLEN];
106  if ((xff_cfg != NULL) && !(xff_cfg->flags & XFF_DISABLED)) {
107  if (FlowGetAppProtocol(p->flow) == ALPROTO_HTTP) {
108  have_xff_ip = HttpXFFGetIPFromTx(p->flow, ff->txid, xff_cfg, xff_buffer, XFF_MAXLEN);
109  }
110  if (have_xff_ip && xff_cfg->flags & XFF_OVERWRITE) {
111  if (p->flowflags & FLOW_PKT_TOCLIENT) {
112  strlcpy(addr.dst_ip, xff_buffer, JSON_ADDR_LEN);
113  } else {
114  strlcpy(addr.src_ip, xff_buffer, JSON_ADDR_LEN);
115  }
116  have_xff_ip = 0;
117  }
118  }
119 
120  JsonBuilder *js = CreateEveHeader(p, fdir, "fileinfo", &addr);
121  if (unlikely(js == NULL))
122  return NULL;
123 
124  JsonBuilderMark mark = { 0, 0, 0 };
125  switch (p->flow->alproto) {
126  case ALPROTO_HTTP:
127  jb_open_object(js, "http");
128  EveHttpAddMetadata(p->flow, ff->txid, js);
129  jb_close(js);
130  break;
131  case ALPROTO_SMTP:
132  jb_get_mark(js, &mark);
133  jb_open_object(js, "smtp");
134  if (EveSMTPAddMetadata(p->flow, ff->txid, js)) {
135  jb_close(js);
136  } else {
137  jb_restore_mark(js, &mark);
138  }
139  jb_get_mark(js, &mark);
140  jb_open_object(js, "email");
141  if (EveEmailAddMetadata(p->flow, ff->txid, js)) {
142  jb_close(js);
143  } else {
144  jb_restore_mark(js, &mark);
145  }
146  break;
147  case ALPROTO_NFS:
148  /* rpc */
149  jb_get_mark(js, &mark);
150  jb_open_object(js, "rpc");
151  if (EveNFSAddMetadataRPC(p->flow, ff->txid, js)) {
152  jb_close(js);
153  } else {
154  jb_restore_mark(js, &mark);
155  }
156  /* nfs */
157  jb_get_mark(js, &mark);
158  jb_open_object(js, "nfs");
159  if (EveNFSAddMetadata(p->flow, ff->txid, js)) {
160  jb_close(js);
161  } else {
162  jb_restore_mark(js, &mark);
163  }
164  break;
165  case ALPROTO_SMB:
166  jb_get_mark(js, &mark);
167  jb_open_object(js, "smb");
168  if (EveSMBAddMetadata(p->flow, ff->txid, js)) {
169  jb_close(js);
170  } else {
171  jb_restore_mark(js, &mark);
172  }
173  break;
174  }
175 
176  jb_set_string(js, "app_proto", AppProtoToString(p->flow->alproto));
177 
178  jb_open_object(js, "fileinfo");
179  EveFileInfo(js, ff, stored);
180  jb_close(js);
181 
182  /* xff header */
183  if (have_xff_ip && xff_cfg->flags & XFF_EXTRADATA) {
184  jb_set_string(js, "xff", xff_buffer);
185  }
186 
187  return js;
188 }
189 
190 /**
191  * \internal
192  * \brief Write meta data on a single line json record
193  */
194 static void FileWriteJsonRecord(JsonFileLogThread *aft, const Packet *p,
195  const File *ff, uint32_t dir)
196 {
197  HttpXFFCfg *xff_cfg = aft->filelog_ctx->xff_cfg != NULL ?
199  JsonBuilder *js = JsonBuildFileInfoRecord(p, ff,
200  ff->flags & FILE_STORED ? true : false, dir, xff_cfg);
201  if (unlikely(js == NULL)) {
202  return;
203  }
204 
205  MemBufferReset(aft->buffer);
207  jb_free(js);
208 }
209 
210 static int JsonFileLogger(ThreadVars *tv, void *thread_data, const Packet *p,
211  const File *ff, uint8_t dir)
212 {
213  SCEnter();
214  JsonFileLogThread *aft = (JsonFileLogThread *)thread_data;
215 
216  BUG_ON(ff->flags & FILE_LOGGED);
217 
218  SCLogDebug("ff %p", ff);
219 
220  FileWriteJsonRecord(aft, p, ff, dir);
221  return 0;
222 }
223 
224 
225 static TmEcode JsonFileLogThreadInit(ThreadVars *t, const void *initdata, void **data)
226 {
228  if (unlikely(aft == NULL))
229  return TM_ECODE_FAILED;
230  memset(aft, 0, sizeof(JsonFileLogThread));
231 
232  if(initdata == NULL)
233  {
234  SCLogDebug("Error getting context for EveLogFile. \"initdata\" argument NULL");
235  SCFree(aft);
236  return TM_ECODE_FAILED;
237  }
238 
239  /* Use the Ouptut Context (file pointer and mutex) */
240  aft->filelog_ctx = ((OutputCtx *)initdata)->data;
241 
243  if (aft->buffer == NULL) {
244  SCFree(aft);
245  return TM_ECODE_FAILED;
246  }
247 
248  *data = (void *)aft;
249  return TM_ECODE_OK;
250 }
251 
252 static TmEcode JsonFileLogThreadDeinit(ThreadVars *t, void *data)
253 {
254  JsonFileLogThread *aft = (JsonFileLogThread *)data;
255  if (aft == NULL) {
256  return TM_ECODE_OK;
257  }
258 
259  MemBufferFree(aft->buffer);
260  /* clear memory */
261  memset(aft, 0, sizeof(JsonFileLogThread));
262 
263  SCFree(aft);
264  return TM_ECODE_OK;
265 }
266 
267 static void OutputFileLogDeinitSub(OutputCtx *output_ctx)
268 {
269  OutputFileCtx *ff_ctx = output_ctx->data;
270  if (ff_ctx->xff_cfg != NULL) {
271  SCFree(ff_ctx->xff_cfg);
272  }
273  SCFree(ff_ctx);
274  SCFree(output_ctx);
275 }
276 
277 /** \brief Create a new http log LogFileCtx.
278  * \param conf Pointer to ConfNode containing this loggers configuration.
279  * \return NULL if failure, LogFileCtx* to the file_ctx if succesful
280  * */
281 static OutputInitResult OutputFileLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
282 {
283  OutputInitResult result = { NULL, false };
284  OutputJsonCtx *ojc = parent_ctx->data;
285 
286  OutputFileCtx *output_file_ctx = SCCalloc(1, sizeof(OutputFileCtx));
287  if (unlikely(output_file_ctx == NULL))
288  return result;
289 
290  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
291  if (unlikely(output_ctx == NULL)) {
292  SCFree(output_file_ctx);
293  return result;
294  }
295 
296  output_file_ctx->file_ctx = ojc->file_ctx;
297 
298  if (conf) {
299  const char *force_filestore = ConfNodeLookupChildValue(conf, "force-filestore");
300  if (force_filestore != NULL && ConfValIsTrue(force_filestore)) {
302  SCLogConfig("forcing filestore of all files");
303  }
304 
305  const char *force_magic = ConfNodeLookupChildValue(conf, "force-magic");
306  if (force_magic != NULL && ConfValIsTrue(force_magic)) {
308  SCLogConfig("forcing magic lookup for logged files");
309  }
310 
311  FileForceHashParseCfg(conf);
312  }
313 
314  if (conf != NULL && ConfNodeLookupChild(conf, "xff") != NULL) {
315  output_file_ctx->xff_cfg = SCCalloc(1, sizeof(HttpXFFCfg));
316  if (output_file_ctx->xff_cfg != NULL) {
317  HttpXFFGetCfg(conf, output_file_ctx->xff_cfg);
318  }
319  } else if (ojc->xff_cfg) {
320  output_file_ctx->parent_xff_cfg = ojc->xff_cfg;
321  }
322 
323  output_ctx->data = output_file_ctx;
324  output_ctx->DeInit = OutputFileLogDeinitSub;
325 
327  result.ctx = output_ctx;
328  result.ok = true;
329  return result;
330 }
331 
333 {
334  /* register as child of eve-log */
335  OutputRegisterFileSubModule(LOGGER_JSON_FILE, "eve-log", "JsonFileLog",
336  "eve-log.files", OutputFileLogInitSub, JsonFileLogger,
337  JsonFileLogThreadInit, JsonFileLogThreadDeinit, NULL);
338 }
XFF_MAXLEN
#define XFF_MAXLEN
Definition: app-layer-htp-xff.h:39
util-byte.h
JsonBuildFileInfoRecord
JsonBuilder * JsonBuildFileInfoRecord(const Packet *p, const File *ff, const bool stored, uint8_t dir, HttpXFFCfg *xff_cfg)
Definition: output-json-file.c:83
EveSMTPAddMetadata
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-smtp.c:101
OutputJsonCtx_::xff_cfg
HttpXFFCfg * xff_cfg
Definition: output-json.h:112
XFF_EXTRADATA
#define XFF_EXTRADATA
Definition: app-layer-htp-xff.h:31
FileForceTrackingEnable
void FileForceTrackingEnable(void)
Definition: util-file.c:163
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:62
threads.h
OutputJsonCtx_
Definition: output-json.h:108
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:30
LogFileCtx_
Definition: util-logopenfile.h:52
JsonFileLogRegister
void JsonFileLogRegister(void)
Definition: output-json-file.c:332
EveNFSAddMetadata
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:63
json_addr_info_zero
const JsonAddrInfo json_addr_info_zero
Definition: output-json.c:86
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1429
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:468
tm-modules.h
JsonFileLogThread_::buffer
MemBuffer * buffer
Definition: output-json-file.c:80
util-privs.h
stream-tcp-reassemble.h
FileForceMagicEnable
void FileForceMagicEnable(void)
Definition: util-file.c:100
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:442
OutputFileCtx_::xff_cfg
HttpXFFCfg * xff_cfg
Definition: output-json-file.c:74
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
EveHttpAddMetadata
bool EveHttpAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-http.c:547
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
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
util-memcmp.h
app-layer-htp.h
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:1284
LOGGER_JSON_FILE
@ LOGGER_JSON_FILE
Definition: suricata-common.h:479
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
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:138
util-atomic.h
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
stream.h
JsonAddrInfo_
Definition: output-json.h:48
Packet_
Definition: decode.h:411
conf.h
XFF_OVERWRITE
#define XFF_OVERWRITE
Definition: app-layer-htp-xff.h:33
TmEcode
TmEcode
Definition: tm-threads-common.h:77
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:40
MemBuffer_
Definition: util-buffer.h:27
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:219
EveEmailAddMetadata
bool EveEmailAddMetadata(const Flow *f, uint32_t tx_id, JsonBuilder *js)
Definition: output-json-email-common.c:388
ConfNodeLookupChild
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:815
File_::flags
uint16_t flags
Definition: util-file.h:64
util-file.h
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
File_
Definition: util-file.h:63
OutputInitResult_
Definition: output.h:41
Packet_::flow
struct Flow_ * flow
Definition: decode.h:448
suricata-common.h
FileForceFilestoreEnable
void FileForceFilestoreEnable(void)
Definition: util-file.c:94
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
OutputFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-file.c:72
JSON_ADDR_LEN
#define JSON_ADDR_LEN
Definition: output-json.h:44
output-json-nfs.h
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
FILE_STORED
#define FILE_STORED
Definition: util-file.h:47
JsonFileLogThread_
Definition: output-json-file.c:78
FileForceHashParseCfg
void FileForceHashParseCfg(ConfNode *conf)
Function to parse forced file hashing configuration.
Definition: util-file.c:172
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
OutputJsonLogDirection
OutputJsonLogDirection
Definition: output-json.h:37
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
util-validate.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:39
FlowGetAppProtocol
AppProto FlowGetAppProtocol(const Flow *f)
Definition: flow.c:1075
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
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:49
HttpXFFCfg_::flags
uint8_t flags
Definition: app-layer-htp-xff.h:42
FILE_LOGGED
#define FILE_LOGGED
Definition: util-file.h:44
LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOSERVER
Definition: output-json.h:41
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:30
JsonAddrInfo_::dst_ip
char dst_ip[JSON_ADDR_LEN]
Definition: output-json.h:50
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
JsonFileLogThread
struct JsonFileLogThread_ JsonFileLogThread
JsonAddrInfoInit
void JsonAddrInfoInit(const Packet *p, enum OutputJsonLogDirection dir, JsonAddrInfo *addr)
Definition: output-json.c:780
File_::txid
uint64_t txid
Definition: util-file.h:68
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: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: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:425
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:111
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
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:843