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