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