suricata
output-json-flow.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 Victor Julien <victor@inliniac.net>
22  *
23  * Implements Flow JSON logging portion of the engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-print.h"
36 #include "util-unittest.h"
37 
38 #include "util-debug.h"
39 
40 #include "output.h"
41 #include "util-privs.h"
42 #include "util-buffer.h"
43 #include "util-device.h"
44 #include "util-proto-name.h"
45 #include "util-logopenfile.h"
46 #include "util-time.h"
47 #include "output-json.h"
48 #include "output-json-flow.h"
49 
50 #include "stream-tcp.h"
51 #include "stream-tcp-private.h"
52 #include "flow-storage.h"
53 
54 static JsonBuilder *CreateEveHeaderFromFlow(const Flow *f)
55 {
56  char timebuf[64];
57  char srcip[46] = {0}, dstip[46] = {0};
58  Port sp, dp;
59 
60  JsonBuilder *jb = jb_new_object();
61  if (unlikely(jb == NULL)) {
62  return NULL;
63  }
64 
65  SCTime_t ts = TimeGet();
66 
67  CreateIsoTimeString(ts, timebuf, sizeof(timebuf));
68 
69  if ((f->flags & FLOW_DIR_REVERSED) == 0) {
70  if (FLOW_IS_IPV4(f)) {
71  PrintInet(AF_INET, (const void *)&(f->src.addr_data32[0]), srcip, sizeof(srcip));
72  PrintInet(AF_INET, (const void *)&(f->dst.addr_data32[0]), dstip, sizeof(dstip));
73  } else if (FLOW_IS_IPV6(f)) {
74  PrintInet(AF_INET6, (const void *)&(f->src.address), srcip, sizeof(srcip));
75  PrintInet(AF_INET6, (const void *)&(f->dst.address), dstip, sizeof(dstip));
76  }
77  sp = f->sp;
78  dp = f->dp;
79  } else {
80  if (FLOW_IS_IPV4(f)) {
81  PrintInet(AF_INET, (const void *)&(f->dst.addr_data32[0]), srcip, sizeof(srcip));
82  PrintInet(AF_INET, (const void *)&(f->src.addr_data32[0]), dstip, sizeof(dstip));
83  } else if (FLOW_IS_IPV6(f)) {
84  PrintInet(AF_INET6, (const void *)&(f->dst.address), srcip, sizeof(srcip));
85  PrintInet(AF_INET6, (const void *)&(f->src.address), dstip, sizeof(dstip));
86  }
87  sp = f->dp;
88  dp = f->sp;
89  }
90 
91  /* time */
92  jb_set_string(jb, "timestamp", timebuf);
93 
94  CreateEveFlowId(jb, (const Flow *)f);
95 
96 #if 0 // TODO
97  /* sensor id */
98  if (sensor_id >= 0)
99  json_object_set_new(js, "sensor_id", json_integer(sensor_id));
100 #endif
101 
102  /* input interface */
103  if (f->livedev) {
104  jb_set_string(jb, "in_iface", f->livedev->dev);
105  }
106 
107  JB_SET_STRING(jb, "event_type", "flow");
108 
109  /* vlan */
110  if (f->vlan_idx > 0) {
111  jb_open_array(jb, "vlan");
112  jb_append_uint(jb, f->vlan_id[0]);
113  if (f->vlan_idx > 1) {
114  jb_append_uint(jb, f->vlan_id[1]);
115  }
116  if (f->vlan_idx > 2) {
117  jb_append_uint(jb, f->vlan_id[2]);
118  }
119  jb_close(jb);
120  }
121 
122  /* tuple */
123  jb_set_string(jb, "src_ip", srcip);
124  switch(f->proto) {
125  case IPPROTO_ICMP:
126  break;
127  case IPPROTO_UDP:
128  case IPPROTO_TCP:
129  case IPPROTO_SCTP:
130  jb_set_uint(jb, "src_port", sp);
131  break;
132  }
133  jb_set_string(jb, "dest_ip", dstip);
134  switch(f->proto) {
135  case IPPROTO_ICMP:
136  break;
137  case IPPROTO_UDP:
138  case IPPROTO_TCP:
139  case IPPROTO_SCTP:
140  jb_set_uint(jb, "dest_port", dp);
141  break;
142  }
143 
144  if (SCProtoNameValid(f->proto)) {
145  jb_set_string(jb, "proto", known_proto[f->proto]);
146  } else {
147  char proto[4];
148  snprintf(proto, sizeof(proto), "%"PRIu8"", f->proto);
149  jb_set_string(jb, "proto", proto);
150  }
151 
152  switch (f->proto) {
153  case IPPROTO_ICMP:
154  case IPPROTO_ICMPV6:
155  jb_set_uint(jb, "icmp_type", f->icmp_s.type);
156  jb_set_uint(jb, "icmp_code", f->icmp_s.code);
157  if (f->tosrcpktcnt) {
158  jb_set_uint(jb, "response_icmp_type", f->icmp_d.type);
159  jb_set_uint(jb, "response_icmp_code", f->icmp_d.code);
160  }
161  break;
162  case IPPROTO_ESP:
163  jb_set_uint(jb, "spi", f->esp.spi);
164  break;
165  }
166  return jb;
167 }
168 
169 void EveAddAppProto(Flow *f, JsonBuilder *js)
170 {
171  if (f->alproto) {
172  jb_set_string(js, "app_proto", AppProtoToString(f->alproto));
173  }
174  if (f->alproto_ts && f->alproto_ts != f->alproto) {
175  jb_set_string(js, "app_proto_ts", AppProtoToString(f->alproto_ts));
176  }
177  if (f->alproto_tc && f->alproto_tc != f->alproto) {
178  jb_set_string(js, "app_proto_tc", AppProtoToString(f->alproto_tc));
179  }
180  if (f->alproto_orig != f->alproto && f->alproto_orig != ALPROTO_UNKNOWN) {
181  jb_set_string(js, "app_proto_orig", AppProtoToString(f->alproto_orig));
182  }
183  if (f->alproto_expect != f->alproto && f->alproto_expect != ALPROTO_UNKNOWN) {
184  jb_set_string(js, "app_proto_expected",
186  }
187 
188 }
189 
190 void EveAddFlow(Flow *f, JsonBuilder *js)
191 {
193  if (fc) {
194  jb_set_uint(js, "pkts_toserver", f->todstpktcnt + fc->todstpktcnt);
195  jb_set_uint(js, "pkts_toclient", f->tosrcpktcnt + fc->tosrcpktcnt);
196  jb_set_uint(js, "bytes_toserver", f->todstbytecnt + fc->todstbytecnt);
197  jb_set_uint(js, "bytes_toclient", f->tosrcbytecnt + fc->tosrcbytecnt);
198 
199  jb_open_object(js, "bypassed");
200  jb_set_uint(js, "pkts_toserver", fc->todstpktcnt);
201  jb_set_uint(js, "pkts_toclient", fc->tosrcpktcnt);
202  jb_set_uint(js, "bytes_toserver", fc->todstbytecnt);
203  jb_set_uint(js, "bytes_toclient", fc->tosrcbytecnt);
204  jb_close(js);
205  } else {
206  jb_set_uint(js, "pkts_toserver", f->todstpktcnt);
207  jb_set_uint(js, "pkts_toclient", f->tosrcpktcnt);
208  jb_set_uint(js, "bytes_toserver", f->todstbytecnt);
209  jb_set_uint(js, "bytes_toclient", f->tosrcbytecnt);
210  }
211 
212  char timebuf1[64];
213  CreateIsoTimeString(f->startts, timebuf1, sizeof(timebuf1));
214  jb_set_string(js, "start", timebuf1);
215 }
216 
217 /* Eve format logging */
218 static void EveFlowLogJSON(OutputJsonThreadCtx *aft, JsonBuilder *jb, Flow *f)
219 {
220  EveAddAppProto(f, jb);
221  jb_open_object(jb, "flow");
222  EveAddFlow(f, jb);
223 
224  char timebuf2[64];
225  CreateIsoTimeString(f->lastts, timebuf2, sizeof(timebuf2));
226  jb_set_string(jb, "end", timebuf2);
227 
228  uint64_t age = (SCTIME_SECS(f->lastts) - SCTIME_SECS(f->startts));
229  jb_set_uint(jb, "age", age);
230 
232  JB_SET_TRUE(jb, "emergency");
233 
234  const int flow_state = f->flow_state;
235  switch (flow_state) {
236  case FLOW_STATE_NEW:
237  JB_SET_STRING(jb, "state", "new");
238  break;
240  JB_SET_STRING(jb, "state", "established");
241  break;
242  case FLOW_STATE_CLOSED:
243  JB_SET_STRING(jb, "state", "closed");
244  break;
246  JB_SET_STRING(jb, "state", "bypassed");
247  JB_SET_STRING(jb, "bypass", "local");
248  break;
249 #ifdef CAPTURE_OFFLOAD
250  case FLOW_STATE_CAPTURE_BYPASSED:
251  JB_SET_STRING(jb, "state", "bypassed");
252  JB_SET_STRING(jb, "bypass", "capture");
253  break;
254 #endif
255  case FLOW_STATE_SIZE:
257  SCLogDebug("invalid flow state: %d, contact developers", flow_state);
258  }
259 
260  const char *reason = NULL;
262  reason = "forced";
264  reason = "shutdown";
266  reason = "timeout";
267  else
268  reason = "unknown";
269 
270  jb_set_string(jb, "reason", reason);
271 
272  jb_set_bool(jb, "alerted", FlowHasAlerts(f));
273  if (f->flags & FLOW_WRONG_THREAD)
274  JB_SET_TRUE(jb, "wrong_thread");
275 
276  if (f->flags & FLOW_ACTION_DROP) {
277  JB_SET_STRING(jb, "action", "drop");
278  } else if (f->flags & FLOW_ACTION_PASS) {
279  JB_SET_STRING(jb, "action", "pass");
280  }
281 
282  /* Close flow. */
283  jb_close(jb);
284 
285  EveAddCommonOptions(&aft->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW);
286 
287  /* TCP */
288  if (f->proto == IPPROTO_TCP) {
289  jb_open_object(jb, "tcp");
290 
291  TcpSession *ssn = f->protoctx;
292 
293  char hexflags[3];
294  snprintf(hexflags, sizeof(hexflags), "%02x",
295  ssn ? ssn->tcp_packet_flags : 0);
296  jb_set_string(jb, "tcp_flags", hexflags);
297 
298  snprintf(hexflags, sizeof(hexflags), "%02x",
299  ssn ? ssn->client.tcp_flags : 0);
300  jb_set_string(jb, "tcp_flags_ts", hexflags);
301 
302  snprintf(hexflags, sizeof(hexflags), "%02x",
303  ssn ? ssn->server.tcp_flags : 0);
304  jb_set_string(jb, "tcp_flags_tc", hexflags);
305 
306  EveTcpFlags(ssn ? ssn->tcp_packet_flags : 0, jb);
307 
308  if (ssn) {
309  const char *tcp_state = StreamTcpStateAsString(ssn->state);
310  if (tcp_state != NULL)
311  jb_set_string(jb, "state", tcp_state);
313  JB_SET_TRUE(jb, "tc_gap");
314  }
316  JB_SET_TRUE(jb, "ts_gap");
317  }
318 
319  jb_set_uint(jb, "ts_max_regions", ssn->client.sb.max_regions);
320  jb_set_uint(jb, "tc_max_regions", ssn->server.sb.max_regions);
321  }
322 
323  /* Close tcp. */
324  jb_close(jb);
325  }
326 }
327 
328 static int JsonFlowLogger(ThreadVars *tv, void *thread_data, Flow *f)
329 {
330  SCEnter();
331  OutputJsonThreadCtx *thread = thread_data;
332 
333  /* reset */
334  MemBufferReset(thread->buffer);
335 
336  JsonBuilder *jb = CreateEveHeaderFromFlow(f);
337  if (unlikely(jb == NULL)) {
339  }
340 
341  EveFlowLogJSON(thread, jb, f);
342 
343  OutputJsonBuilderBuffer(tv, NULL, f, jb, thread);
344  jb_free(jb);
345 
347 }
348 
350 {
351  /* register as child of eve-log */
352  OutputRegisterFlowSubModule(LOGGER_JSON_FLOW, "eve-log", "JsonFlowLog", "eve-log.flow",
354 }
tm-threads.h
ts
uint64_t ts
Definition: source-erf-file.c:55
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:73
FLOW_IS_IPV6
#define FLOW_IS_IPV6(f)
Definition: flow.h:171
FLOW_STATE_ESTABLISHED
@ FLOW_STATE_ESTABLISHED
Definition: flow.h:507
CreateIsoTimeString
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:209
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:90
Flow_::startts
SCTime_t startts
Definition: flow.h:495
stream-tcp.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:84
GetFlowBypassInfoID
FlowStorageId GetFlowBypassInfoID(void)
Definition: flow-util.c:214
FlowBypassInfo_
Definition: flow.h:531
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_::proto
uint8_t proto
Definition: flow.h:378
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:90
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:75
Flow_::alproto_orig
AppProto alproto_orig
Definition: flow.h:461
SCProtoNameValid
bool SCProtoNameValid(uint16_t proto)
Function to check if the received protocol number is valid and do we have corresponding name entry fo...
Definition: util-proto-name.c:453
StreamingBuffer_::max_regions
uint16_t max_regions
Definition: util-streaming-buffer.h:114
FlowBypassInfo_::tosrcbytecnt
uint64_t tosrcbytecnt
Definition: flow.h:536
util-privs.h
Flow_::icmp_d
struct Flow_::@116::@122 icmp_d
known_proto
const char * known_proto[256]
Definition: util-proto-name.c:40
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:69
TcpStream_::flags
uint16_t flags
Definition: stream-tcp-private.h:107
proto
uint8_t proto
Definition: decode-template.h:0
Flow_::dp
Port dp
Definition: flow.h:372
Flow_::protoctx
void * protoctx
Definition: flow.h:446
Flow_::icmp_s
struct Flow_::@114::@120 icmp_s
JsonFlowLogRegister
void JsonFlowLogRegister(void)
Definition: output-json-flow.c:349
util-unittest.h
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
Flow_::flow_state
FlowStateType flow_state
Definition: flow.h:417
OutputJsonThreadCtx_
Definition: output-json.h:89
LOGGER_JSON_FLOW
@ LOGGER_JSON_FLOW
Definition: suricata-common.h:486
Flow_::tosrcbytecnt
uint64_t tosrcbytecnt
Definition: flow.h:500
FLOW_ACTION_PASS
#define FLOW_ACTION_PASS
Definition: flow.h:116
Flow_::dst
FlowAddress dst
Definition: flow.h:359
OutputRegisterFlowSubModule
void OutputRegisterFlowSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, FlowLogger FlowLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Register a flow output sub-module.
Definition: output.c:466
Flow_::esp
struct Flow_::@114::@121 esp
FLOW_STATE_LOCAL_BYPASSED
@ FLOW_STATE_LOCAL_BYPASSED
Definition: flow.h:509
util-device.h
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:26
output-json-flow.h
FlowBypassInfo_::todstbytecnt
uint64_t todstbytecnt
Definition: flow.h:538
Flow_::todstpktcnt
uint32_t todstpktcnt
Definition: flow.h:497
output-json.h
Flow_::lastts
SCTime_t lastts
Definition: flow.h:415
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
PrintInet
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:230
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js, enum OutputJsonLogDirection dir)
Definition: output-json.c:398
Flow_::flow_end_flags
uint8_t flow_end_flags
Definition: flow.h:452
pkt-var.h
EveTcpFlags
void EveTcpFlags(const uint8_t flags, JsonBuilder *js)
jsonify tcp flags field Only add 'true' fields in an attempt to keep things reasonably compact.
Definition: output-json.c:439
TcpSession_::state
uint8_t state
Definition: stream-tcp-private.h:285
FlowBypassInfo_::todstpktcnt
uint64_t todstpktcnt
Definition: flow.h:537
FLOW_WRONG_THREAD
#define FLOW_WRONG_THREAD
Definition: flow.h:109
util-time.h
JB_SET_TRUE
#define JB_SET_TRUE(jb, key)
Definition: rust.h:27
LiveDevice_::dev
char * dev
Definition: util-device.h:51
TcpSession_::tcp_packet_flags
uint8_t tcp_packet_flags
Definition: stream-tcp-private.h:290
Flow_::todstbytecnt
uint64_t todstbytecnt
Definition: flow.h:499
FLOW_IS_IPV4
#define FLOW_IS_IPV4(f)
Definition: flow.h:169
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
stream-tcp-private.h
conf.h
Port
uint16_t Port
Definition: decode.h:214
FLOW_END_FLAG_EMERGENCY
#define FLOW_END_FLAG_EMERGENCY
Definition: flow.h:243
SCTime_t
Definition: util-time.h:40
STREAMTCP_STREAM_FLAG_HAS_GAP
#define STREAMTCP_STREAM_FLAG_HAS_GAP
Definition: stream-tcp-private.h:217
Flow_::vlan_idx
uint8_t vlan_idx
Definition: flow.h:382
util-proto-name.h
FlowBypassInfo_::tosrcpktcnt
uint64_t tosrcpktcnt
Definition: flow.h:535
Flow_::alproto_expect
AppProto alproto_expect
Definition: flow.h:464
FLOW_STATE_SIZE
#define FLOW_STATE_SIZE
Definition: flow.h:517
FlowGetStorageById
void * FlowGetStorageById(const Flow *f, FlowStorageId id)
Definition: flow-storage.c:40
Flow_::src
FlowAddress src
Definition: flow.h:359
flow-storage.h
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:92
FLOW_STATE_NEW
@ FLOW_STATE_NEW
Definition: flow.h:506
suricata-common.h
TcpStream_::sb
StreamingBuffer sb
Definition: stream-tcp-private.h:135
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:958
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
EveAddAppProto
void EveAddAppProto(Flow *f, JsonBuilder *js)
Definition: output-json-flow.c:169
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:295
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
Flow_::livedev
struct LiveDevice_ * livedev
Definition: flow.h:403
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
CreateEveFlowId
void CreateEveFlowId(JsonBuilder *js, const Flow *f)
Definition: output-json.c:689
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:294
FLOW_STATE_CLOSED
@ FLOW_STATE_CLOSED
Definition: flow.h:508
Flow_::alproto_ts
AppProto alproto_ts
Definition: flow.h:456
util-logopenfile.h
Flow_::flags
uint32_t flags
Definition: flow.h:426
util-buffer.h
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
FLOW_END_FLAG_SHUTDOWN
#define FLOW_END_FLAG_SHUTDOWN
Definition: flow.h:246
StreamTcpStateAsString
const char * StreamTcpStateAsString(const enum TcpState state)
Definition: stream-tcp.c:7076
Flow_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:380
FlowHasAlerts
int FlowHasAlerts(const Flow *f)
Check if flow has alerts.
Definition: flow.c:172
FLOW_END_FLAG_TIMEOUT
#define FLOW_END_FLAG_TIMEOUT
Definition: flow.h:244
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1190
Flow_::sp
Port sp
Definition: flow.h:361
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:123
TcpSession_
Definition: stream-tcp-private.h:283
Flow_::alproto_tc
AppProto alproto_tc
Definition: flow.h:457
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:455
FLOW_END_FLAG_FORCED
#define FLOW_END_FLAG_FORCED
Definition: flow.h:245
FLOW_DIR_REVERSED
#define FLOW_DIR_REVERSED
Definition: flow.h:111
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
TcpStream_::tcp_flags
uint8_t tcp_flags
Definition: stream-tcp-private.h:111
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
FlowAddress_::address
union FlowAddress_::@113 address
Flow_::tosrcpktcnt
uint32_t tosrcpktcnt
Definition: flow.h:498
output.h
EveAddFlow
void EveAddFlow(Flow *f, JsonBuilder *js)
Definition: output-json-flow.c:190