suricata
output-json-netflow.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-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 Unidirectional NetFlow JSON logging portion of the engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "app-layer-parser.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-print.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-private.h"
44 #include "util-proto-name.h"
45 #include "util-logopenfile.h"
46 #include "output-json.h"
47 #include "output-json-netflow.h"
48 
49 #include "stream-tcp-private.h"
50 
51 static SCJsonBuilder *CreateEveHeaderFromNetFlow(
52  const Flow *f, int dir, OutputJsonCommonSettings *cfg)
53 {
54  char timebuf[64];
55  char srcip[46] = {0}, dstip[46] = {0};
56  Port sp, dp;
57 
58  SCJsonBuilder *js = SCJbNewObject();
59  if (unlikely(js == NULL))
60  return NULL;
61 
62  SCTime_t ts = TimeGet();
63 
64  CreateIsoTimeString(ts, timebuf, sizeof(timebuf));
65 
66  /* reverse header direction if the flow started out wrong */
67  dir ^= ((f->flags & FLOW_DIR_REVERSED) != 0);
68 
69  if (FLOW_IS_IPV4(f)) {
70  if (dir == 0) {
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 {
74  PrintInet(AF_INET, (const void *)&(f->dst.addr_data32[0]), srcip, sizeof(srcip));
75  PrintInet(AF_INET, (const void *)&(f->src.addr_data32[0]), dstip, sizeof(dstip));
76  }
77  } else if (FLOW_IS_IPV6(f)) {
78  if (dir == 0) {
80  (const void *)&(f->src.address), srcip, sizeof(srcip), cfg->compress_ipv6);
82  (const void *)&(f->dst.address), dstip, sizeof(dstip), cfg->compress_ipv6);
83  } else {
85  (const void *)&(f->dst.address), srcip, sizeof(srcip), cfg->compress_ipv6);
87  (const void *)&(f->src.address), dstip, sizeof(dstip), cfg->compress_ipv6);
88  }
89  }
90 
91  if (dir == 0) {
92  sp = f->sp;
93  dp = f->dp;
94  } else {
95  sp = f->dp;
96  dp = f->sp;
97  }
98 
99  /* time */
100  SCJbSetString(js, "timestamp", timebuf);
101 
102  CreateEveFlowId(js, (const Flow *)f);
103 
104 #if 0 // TODO
105  /* sensor id */
106  if (sensor_id >= 0)
107  json_object_set_new(js, "sensor_id", json_integer(sensor_id));
108 #endif
109 
110  /* input interface */
112  if (dev) {
113  SCJbSetString(js, "in_iface", dev->dev);
114  }
115 
116  JB_SET_STRING(js, "event_type", "netflow");
117 
118  /* vlan */
119  if (f->vlan_idx > 0) {
120  SCJbOpenArray(js, "vlan");
121  SCJbAppendUint(js, f->vlan_id[0]);
122  if (f->vlan_idx > 1) {
123  SCJbAppendUint(js, f->vlan_id[1]);
124  }
125  if (f->vlan_idx > 2) {
126  SCJbAppendUint(js, f->vlan_id[2]);
127  }
128  SCJbClose(js);
129  }
130 
131  /* tuple */
132  SCJbSetString(js, "src_ip", srcip);
133  switch(f->proto) {
134  case IPPROTO_ICMP:
135  break;
136  case IPPROTO_UDP:
137  case IPPROTO_TCP:
138  case IPPROTO_SCTP:
139  SCJbSetUint(js, "src_port", sp);
140  break;
141  }
142  SCJbSetString(js, "dest_ip", dstip);
143  switch(f->proto) {
144  case IPPROTO_ICMP:
145  break;
146  case IPPROTO_UDP:
147  case IPPROTO_TCP:
148  case IPPROTO_SCTP:
149  SCJbSetUint(js, "dest_port", dp);
150  break;
151  }
152 
153  if (SCProtoNameValid(f->proto)) {
154  SCJbSetString(js, "proto", known_proto[f->proto]);
155  } else {
156  char proto[4];
157  snprintf(proto, sizeof(proto), "%"PRIu8"", f->proto);
158  SCJbSetString(js, "proto", proto);
159  }
160 
161  switch (f->proto) {
162  case IPPROTO_ICMP:
163  case IPPROTO_ICMPV6: {
164  uint8_t type = f->icmp_s.type;
165  uint8_t code = f->icmp_s.code;
166  if (dir == 1) {
167  type = f->icmp_d.type;
168  code = f->icmp_d.code;
169 
170  }
171  SCJbSetUint(js, "icmp_type", type);
172  SCJbSetUint(js, "icmp_code", code);
173  break;
174  }
175  case IPPROTO_ESP:
176  SCJbSetUint(js, "spi", f->esp.spi);
177  break;
178  }
179  return js;
180 }
181 
182 /* JSON format logging */
183 static void NetFlowLogEveToServer(SCJsonBuilder *js, Flow *f)
184 {
185  SCJbSetString(js, "app_proto", AppProtoToString(f->alproto_ts ? f->alproto_ts : f->alproto));
186 
187  SCJbOpenObject(js, "netflow");
188 
189  SCJbSetUint(js, "pkts", f->todstpktcnt);
190  SCJbSetUint(js, "bytes", f->todstbytecnt);
191 
192  char timebuf1[64], timebuf2[64];
193 
194  CreateIsoTimeString(f->startts, timebuf1, sizeof(timebuf1));
195  CreateIsoTimeString(f->lastts, timebuf2, sizeof(timebuf2));
196 
197  SCJbSetString(js, "start", timebuf1);
198  SCJbSetString(js, "end", timebuf2);
199 
200  uint64_t age = (SCTIME_SECS(f->lastts) - SCTIME_SECS(f->startts));
201  SCJbSetUint(js, "age", age);
202 
203  SCJbSetUint(js, "min_ttl", f->min_ttl_toserver);
204  SCJbSetUint(js, "max_ttl", f->max_ttl_toserver);
205 
206  if (f->alstate) {
207  uint64_t tx_id = AppLayerParserGetTxCnt(f, f->alstate);
208  if (tx_id) {
209  SCJbSetUint(js, "tx_cnt", tx_id);
210  }
211  }
212 
213  /* Close netflow. */
214  SCJbClose(js);
215 
216  /* TCP */
217  if (f->proto == IPPROTO_TCP) {
218  SCJbOpenObject(js, "tcp");
219 
220  TcpSession *ssn = f->protoctx;
221 
222  char hexflags[3];
223  snprintf(hexflags, sizeof(hexflags), "%02x",
224  ssn ? ssn->client.tcp_flags : 0);
225  SCJbSetString(js, "tcp_flags", hexflags);
226 
227  EveTcpFlags(ssn ? ssn->client.tcp_flags : 0, js);
228 
229  SCJbClose(js);
230  }
231 }
232 
233 static void NetFlowLogEveToClient(SCJsonBuilder *js, Flow *f)
234 {
235  SCJbSetString(js, "app_proto", AppProtoToString(f->alproto_tc ? f->alproto_tc : f->alproto));
236 
237  SCJbOpenObject(js, "netflow");
238 
239  SCJbSetUint(js, "pkts", f->tosrcpktcnt);
240  SCJbSetUint(js, "bytes", f->tosrcbytecnt);
241 
242  char timebuf1[64], timebuf2[64];
243 
244  CreateIsoTimeString(f->startts, timebuf1, sizeof(timebuf1));
245  CreateIsoTimeString(f->lastts, timebuf2, sizeof(timebuf2));
246 
247  SCJbSetString(js, "start", timebuf1);
248  SCJbSetString(js, "end", timebuf2);
249 
250  uint64_t age = (SCTIME_SECS(f->lastts) - SCTIME_SECS(f->startts));
251  SCJbSetUint(js, "age", age);
252 
253  /* To client is zero if we did not see any packet */
254  if (f->tosrcpktcnt) {
255  SCJbSetUint(js, "min_ttl", f->min_ttl_toclient);
256  SCJbSetUint(js, "max_ttl", f->max_ttl_toclient);
257  }
258 
259  if (f->alstate) {
260  uint64_t tx_id = AppLayerParserGetTxCnt(f, f->alstate);
261  if (tx_id) {
262  SCJbSetUint(js, "tx_cnt", tx_id);
263  }
264  }
265 
266  /* Close netflow. */
267  SCJbClose(js);
268 
269  /* TCP */
270  if (f->proto == IPPROTO_TCP) {
271  SCJbOpenObject(js, "tcp");
272 
273  TcpSession *ssn = f->protoctx;
274 
275  char hexflags[3];
276  snprintf(hexflags, sizeof(hexflags), "%02x",
277  ssn ? ssn->server.tcp_flags : 0);
278  SCJbSetString(js, "tcp_flags", hexflags);
279 
280  EveTcpFlags(ssn ? ssn->server.tcp_flags : 0, js);
281 
282  SCJbClose(js);
283  }
284 }
285 
286 static int JsonNetFlowLogger(ThreadVars *tv, void *thread_data, Flow *f)
287 {
288  SCEnter();
289  OutputJsonThreadCtx *jhl = thread_data;
290 
291  SCJsonBuilder *jb = CreateEveHeaderFromNetFlow(f, 0, &jhl->ctx->cfg);
292  if (unlikely(jb == NULL))
293  return TM_ECODE_OK;
294  NetFlowLogEveToServer(jb, f);
295  EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW_TOSERVER);
296  OutputJsonBuilderBuffer(tv, NULL, f, jb, jhl);
297  SCJbFree(jb);
298 
299  /* only log a response record if we actually have seen response packets */
300  if (f->tosrcpktcnt) {
301  jb = CreateEveHeaderFromNetFlow(f, 1, &jhl->ctx->cfg);
302  if (unlikely(jb == NULL))
303  return TM_ECODE_OK;
304  NetFlowLogEveToClient(jb, f);
305  EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW_TOCLIENT);
306  OutputJsonBuilderBuffer(tv, NULL, f, jb, jhl);
307  SCJbFree(jb);
308  }
310 }
311 
313 {
314  /* register as child of eve-log */
315  OutputRegisterFlowSubModule(LOGGER_JSON_NETFLOW, "eve-log", "JsonNetFlowLog", "eve-log.netflow",
317 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
util-device-private.h
tm-threads.h
ts
uint64_t ts
Definition: source-erf-file.c:68
FLOW_IS_IPV6
#define FLOW_IS_IPV6(f)
Definition: flow.h:175
Flow_::flags
uint64_t flags
Definition: flow.h:408
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:86
Flow_::startts
SCTime_t startts
Definition: flow.h:498
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:80
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
Flow_::proto
uint8_t proto
Definition: flow.h:381
type
uint8_t type
Definition: decode-sctp.h:0
ssn
TcpSession ssn
Definition: fuzz_dataset.c:31
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:90
threads.h
PrintInetIPv6
const char * PrintInetIPv6(const void *src, char *dst, socklen_t size, bool compress_ipv6)
Definition: util-print.c:209
Flow_
Flow data structure.
Definition: flow.h:359
OutputJsonCommonSettings_
Definition: output-json.h:62
Flow_::esp
struct Flow_::@126::@133 esp
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its normalized string equivalent.
Definition: app-layer-protos.c:53
LiveDevice_
Definition: util-device-private.h:32
output-json-netflow.h
LiveDeviceGetById
LiveDevice * LiveDeviceGetById(const int id)
Definition: util-device.c:460
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:449
EveTcpFlags
void EveTcpFlags(const uint8_t flags, SCJsonBuilder *js)
jsonify tcp flags field Only add 'true' fields in an attempt to keep things reasonably compact.
Definition: output-json.c:444
LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOSERVER
Definition: output-eve-bindgen.h:37
util-privs.h
known_proto
const char * known_proto[256]
Definition: util-proto-name.c:39
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:1018
Flow_::max_ttl_toserver
uint8_t max_ttl_toserver
Definition: flow.h:473
proto
uint8_t proto
Definition: decode-template.h:0
Flow_::livedev_id
uint16_t livedev_id
Definition: flow.h:404
CreateEveFlowId
void CreateEveFlowId(SCJsonBuilder *js, const Flow *f)
Definition: output-json.c:682
Flow_::dp
Port dp
Definition: flow.h:375
Flow_::protoctx
void * protoctx
Definition: flow.h:438
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
OutputJsonThreadCtx_
Definition: output-json.h:85
Flow_::icmp_d
struct Flow_::@128::@134 icmp_d
Flow_::tosrcbytecnt
uint64_t tosrcbytecnt
Definition: flow.h:503
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:73
Flow_::dst
FlowAddress dst
Definition: flow.h:362
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:500
Flow_::min_ttl_toserver
uint8_t min_ttl_toserver
Definition: flow.h:472
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:36
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, SCJsonBuilder *js, enum SCOutputJsonLogDirection dir)
Definition: output-json.c:392
Flow_::todstpktcnt
uint32_t todstpktcnt
Definition: flow.h:500
output-json.h
Flow_::lastts
SCTime_t lastts
Definition: flow.h:423
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
PrintInet
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:238
pkt-var.h
LiveDevice_::dev
char * dev
Definition: util-device-private.h:33
app-layer-parser.h
Flow_::todstbytecnt
uint64_t todstbytecnt
Definition: flow.h:502
FLOW_IS_IPV4
#define FLOW_IS_IPV4(f)
Definition: flow.h:173
LOGGER_JSON_NETFLOW
@ LOGGER_JSON_NETFLOW
Definition: suricata-common.h:508
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
stream-tcp-private.h
conf.h
Port
uint16_t Port
Definition: decode.h:219
SCTime_t
Definition: util-time.h:40
Flow_::vlan_idx
uint8_t vlan_idx
Definition: flow.h:385
Flow_::min_ttl_toclient
uint8_t min_ttl_toclient
Definition: flow.h:474
util-proto-name.h
Flow_::src
FlowAddress src
Definition: flow.h:362
OutputJsonCommonSettings_::compress_ipv6
bool compress_ipv6
Definition: output-json.h:66
Flow_::icmp_s
struct Flow_::@126::@132 icmp_s
suricata-common.h
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
Flow_::max_ttl_toclient
uint8_t max_ttl_toclient
Definition: flow.h:475
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:297
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
threadvars.h
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:296
Flow_::alproto_ts
AppProto alproto_ts
Definition: flow.h:456
util-logopenfile.h
Flow_::alstate
void * alstate
Definition: flow.h:484
util-buffer.h
FlowAddress_::address
union FlowAddress_::@125 address
Flow_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:383
LOG_DIR_FLOW_TOCLIENT
@ LOG_DIR_FLOW_TOCLIENT
Definition: output-eve-bindgen.h:36
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1273
Flow_::sp
Port sp
Definition: flow.h:364
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_DIR_REVERSED
#define FLOW_DIR_REVERSED
Definition: flow.h:111
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
AppLayerParserGetTxCnt
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
Definition: app-layer-parser.c:1212
TcpStream_::tcp_flags
uint8_t tcp_flags
Definition: stream-tcp-private.h:111
Flow_::tosrcpktcnt
uint32_t tosrcpktcnt
Definition: flow.h:501
output.h
JsonNetFlowLogRegister
void JsonNetFlowLogRegister(void)
Definition: output-json-netflow.c:312
f
Flow f
Definition: fuzz_dataset.c:32