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 */
113  if (f->livedev) {
114  SCJbSetString(js, "in_iface", f->livedev->dev);
115  }
116 
117  JB_SET_STRING(js, "event_type", "netflow");
118 
119  /* vlan */
120  if (f->vlan_idx > 0) {
121  SCJbOpenArray(js, "vlan");
122  SCJbAppendUint(js, f->vlan_id[0]);
123  if (f->vlan_idx > 1) {
124  SCJbAppendUint(js, f->vlan_id[1]);
125  }
126  if (f->vlan_idx > 2) {
127  SCJbAppendUint(js, f->vlan_id[2]);
128  }
129  SCJbClose(js);
130  }
131 
132  /* tuple */
133  SCJbSetString(js, "src_ip", srcip);
134  switch(f->proto) {
135  case IPPROTO_ICMP:
136  break;
137  case IPPROTO_UDP:
138  case IPPROTO_TCP:
139  case IPPROTO_SCTP:
140  SCJbSetUint(js, "src_port", sp);
141  break;
142  }
143  SCJbSetString(js, "dest_ip", dstip);
144  switch(f->proto) {
145  case IPPROTO_ICMP:
146  break;
147  case IPPROTO_UDP:
148  case IPPROTO_TCP:
149  case IPPROTO_SCTP:
150  SCJbSetUint(js, "dest_port", dp);
151  break;
152  }
153 
154  if (SCProtoNameValid(f->proto)) {
155  SCJbSetString(js, "proto", known_proto[f->proto]);
156  } else {
157  char proto[4];
158  snprintf(proto, sizeof(proto), "%"PRIu8"", f->proto);
159  SCJbSetString(js, "proto", proto);
160  }
161 
162  switch (f->proto) {
163  case IPPROTO_ICMP:
164  case IPPROTO_ICMPV6: {
165  uint8_t type = f->icmp_s.type;
166  uint8_t code = f->icmp_s.code;
167  if (dir == 1) {
168  type = f->icmp_d.type;
169  code = f->icmp_d.code;
170 
171  }
172  SCJbSetUint(js, "icmp_type", type);
173  SCJbSetUint(js, "icmp_code", code);
174  break;
175  }
176  case IPPROTO_ESP:
177  SCJbSetUint(js, "spi", f->esp.spi);
178  break;
179  }
180  return js;
181 }
182 
183 /* JSON format logging */
184 static void NetFlowLogEveToServer(SCJsonBuilder *js, Flow *f)
185 {
186  SCJbSetString(js, "app_proto", AppProtoToString(f->alproto_ts ? f->alproto_ts : f->alproto));
187 
188  SCJbOpenObject(js, "netflow");
189 
190  SCJbSetUint(js, "pkts", f->todstpktcnt);
191  SCJbSetUint(js, "bytes", f->todstbytecnt);
192 
193  char timebuf1[64], timebuf2[64];
194 
195  CreateIsoTimeString(f->startts, timebuf1, sizeof(timebuf1));
196  CreateIsoTimeString(f->lastts, timebuf2, sizeof(timebuf2));
197 
198  SCJbSetString(js, "start", timebuf1);
199  SCJbSetString(js, "end", timebuf2);
200 
201  uint64_t age = (SCTIME_SECS(f->lastts) - SCTIME_SECS(f->startts));
202  SCJbSetUint(js, "age", age);
203 
204  SCJbSetUint(js, "min_ttl", f->min_ttl_toserver);
205  SCJbSetUint(js, "max_ttl", f->max_ttl_toserver);
206 
207  if (f->alstate) {
208  uint64_t tx_id = AppLayerParserGetTxCnt(f, f->alstate);
209  if (tx_id) {
210  SCJbSetUint(js, "tx_cnt", tx_id);
211  }
212  }
213 
214  /* Close netflow. */
215  SCJbClose(js);
216 
217  /* TCP */
218  if (f->proto == IPPROTO_TCP) {
219  SCJbOpenObject(js, "tcp");
220 
221  TcpSession *ssn = f->protoctx;
222 
223  char hexflags[3];
224  snprintf(hexflags, sizeof(hexflags), "%02x",
225  ssn ? ssn->client.tcp_flags : 0);
226  SCJbSetString(js, "tcp_flags", hexflags);
227 
228  EveTcpFlags(ssn ? ssn->client.tcp_flags : 0, js);
229 
230  SCJbClose(js);
231  }
232 }
233 
234 static void NetFlowLogEveToClient(SCJsonBuilder *js, Flow *f)
235 {
236  SCJbSetString(js, "app_proto", AppProtoToString(f->alproto_tc ? f->alproto_tc : f->alproto));
237 
238  SCJbOpenObject(js, "netflow");
239 
240  SCJbSetUint(js, "pkts", f->tosrcpktcnt);
241  SCJbSetUint(js, "bytes", f->tosrcbytecnt);
242 
243  char timebuf1[64], timebuf2[64];
244 
245  CreateIsoTimeString(f->startts, timebuf1, sizeof(timebuf1));
246  CreateIsoTimeString(f->lastts, timebuf2, sizeof(timebuf2));
247 
248  SCJbSetString(js, "start", timebuf1);
249  SCJbSetString(js, "end", timebuf2);
250 
251  uint64_t age = (SCTIME_SECS(f->lastts) - SCTIME_SECS(f->startts));
252  SCJbSetUint(js, "age", age);
253 
254  /* To client is zero if we did not see any packet */
255  if (f->tosrcpktcnt) {
256  SCJbSetUint(js, "min_ttl", f->min_ttl_toclient);
257  SCJbSetUint(js, "max_ttl", f->max_ttl_toclient);
258  }
259 
260  if (f->alstate) {
261  uint64_t tx_id = AppLayerParserGetTxCnt(f, f->alstate);
262  if (tx_id) {
263  SCJbSetUint(js, "tx_cnt", tx_id);
264  }
265  }
266 
267  /* Close netflow. */
268  SCJbClose(js);
269 
270  /* TCP */
271  if (f->proto == IPPROTO_TCP) {
272  SCJbOpenObject(js, "tcp");
273 
274  TcpSession *ssn = f->protoctx;
275 
276  char hexflags[3];
277  snprintf(hexflags, sizeof(hexflags), "%02x",
278  ssn ? ssn->server.tcp_flags : 0);
279  SCJbSetString(js, "tcp_flags", hexflags);
280 
281  EveTcpFlags(ssn ? ssn->server.tcp_flags : 0, js);
282 
283  SCJbClose(js);
284  }
285 }
286 
287 static int JsonNetFlowLogger(ThreadVars *tv, void *thread_data, Flow *f)
288 {
289  SCEnter();
290  OutputJsonThreadCtx *jhl = thread_data;
291 
292  SCJsonBuilder *jb = CreateEveHeaderFromNetFlow(f, 0, &jhl->ctx->cfg);
293  if (unlikely(jb == NULL))
294  return TM_ECODE_OK;
295  NetFlowLogEveToServer(jb, f);
296  EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW_TOSERVER);
297  OutputJsonBuilderBuffer(tv, NULL, f, jb, jhl);
298  SCJbFree(jb);
299 
300  /* only log a response record if we actually have seen response packets */
301  if (f->tosrcpktcnt) {
302  jb = CreateEveHeaderFromNetFlow(f, 1, &jhl->ctx->cfg);
303  if (unlikely(jb == NULL))
304  return TM_ECODE_OK;
305  NetFlowLogEveToClient(jb, f);
306  EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW_TOCLIENT);
307  OutputJsonBuilderBuffer(tv, NULL, f, jb, jhl);
308  SCJbFree(jb);
309  }
311 }
312 
314 {
315  /* register as child of eve-log */
316  OutputRegisterFlowSubModule(LOGGER_JSON_NETFLOW, "eve-log", "JsonNetFlowLog", "eve-log.netflow",
318 }
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:163
Flow_::flags
uint64_t flags
Definition: flow.h:396
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:486
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:369
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:99
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:347
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
output-json-netflow.h
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:450
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:1003
Flow_::max_ttl_toserver
uint8_t max_ttl_toserver
Definition: flow.h:461
proto
uint8_t proto
Definition: decode-template.h:0
CreateEveFlowId
void CreateEveFlowId(SCJsonBuilder *js, const Flow *f)
Definition: output-json.c:688
Flow_::dp
Port dp
Definition: flow.h:363
Flow_::protoctx
void * protoctx
Definition: flow.h:426
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:491
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:82
Flow_::dst
FlowAddress dst
Definition: flow.h:350
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:494
Flow_::min_ttl_toserver
uint8_t min_ttl_toserver
Definition: flow.h:460
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:35
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, SCJsonBuilder *js, enum SCOutputJsonLogDirection dir)
Definition: output-json.c:398
Flow_::todstpktcnt
uint32_t todstpktcnt
Definition: flow.h:488
output-json.h
Flow_::lastts
SCTime_t lastts
Definition: flow.h:411
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:490
FLOW_IS_IPV4
#define FLOW_IS_IPV4(f)
Definition: flow.h:161
LOGGER_JSON_NETFLOW
@ LOGGER_JSON_NETFLOW
Definition: suricata-common.h:504
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:373
Flow_::min_ttl_toclient
uint8_t min_ttl_toclient
Definition: flow.h:462
util-proto-name.h
Flow_::src
FlowAddress src
Definition: flow.h:350
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:463
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:297
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
Flow_::livedev
struct LiveDevice_ * livedev
Definition: flow.h:392
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:444
util-logopenfile.h
Flow_::alstate
void * alstate
Definition: flow.h:472
util-buffer.h
Flow_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:371
LOG_DIR_FLOW_TOCLIENT
@ LOG_DIR_FLOW_TOCLIENT
Definition: output-eve-bindgen.h:36
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1255
Flow_::icmp_s
struct Flow_::@121::@127 icmp_s
Flow_::sp
Port sp
Definition: flow.h:352
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:132
TcpSession_
Definition: stream-tcp-private.h:283
Flow_::alproto_tc
AppProto alproto_tc
Definition: flow.h:445
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:443
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:489
output.h
JsonNetFlowLogRegister
void JsonNetFlowLogRegister(void)
Definition: output-json-netflow.c:313