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
53static 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
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 */
179static 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
229static 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
282static 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}
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
uint8_t proto
uint16_t type
uint16_t Port
Definition decode.h:218
#define IPPROTO_SCTP
Definition decode.h:1228
#define FLOW_IS_IPV6(f)
Definition flow.h:172
#define FLOW_DIR_REVERSED
Definition flow.h:112
#define FLOW_IS_IPV4(f)
Definition flow.h:170
ThreadVars * tv
@ LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOCLIENT
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
void JsonNetFlowLogRegister(void)
void EveTcpFlags(const uint8_t flags, SCJsonBuilder *js)
jsonify tcp flags field Only add 'true' fields in an attempt to keep things reasonably compact.
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
void CreateEveFlowId(SCJsonBuilder *js, const Flow *f)
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, SCJsonBuilder *js, enum SCOutputJsonLogDirection dir)
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:495
#define JB_SET_STRING(jb, key, val)
Definition rust.h:26
uint64_t ts
union FlowAddress_::@128 address
Flow data structure.
Definition flow.h:356
struct Flow_::@129::@135 icmp_s
AppProto alproto_ts
Definition flow.h:451
Port dp
Definition flow.h:372
uint8_t max_ttl_toclient
Definition flow.h:470
AppProto alproto_tc
Definition flow.h:452
uint8_t proto
Definition flow.h:378
uint32_t flags
Definition flow.h:421
uint8_t min_ttl_toserver
Definition flow.h:467
uint8_t max_ttl_toserver
Definition flow.h:468
void * alstate
Definition flow.h:479
uint8_t min_ttl_toclient
Definition flow.h:469
uint64_t tosrcbytecnt
Definition flow.h:498
uint8_t vlan_idx
Definition flow.h:382
void * protoctx
Definition flow.h:441
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition flow.h:380
uint64_t todstbytecnt
Definition flow.h:497
SCTime_t lastts
Definition flow.h:410
struct LiveDevice_ * livedev
Definition flow.h:398
struct Flow_::@131::@137 icmp_d
uint8_t code
Definition flow.h:364
struct Flow_::@129::@136 esp
uint32_t todstpktcnt
Definition flow.h:495
uint32_t spi
Definition flow.h:368
FlowAddress src
Definition flow.h:359
Port sp
Definition flow.h:361
FlowAddress dst
Definition flow.h:359
uint32_t tosrcpktcnt
Definition flow.h:496
uint8_t type
Definition flow.h:363
SCTime_t startts
Definition flow.h:493
OutputJsonCommonSettings cfg
Definition output-json.h:78
OutputJsonCtx * ctx
Definition output-json.h:84
Per thread variable structure.
Definition threadvars.h:58
@ LOGGER_JSON_NETFLOW
@ TM_ECODE_OK
#define SCEnter(...)
Definition util-debug.h:277
#define SCReturnInt(x)
Definition util-debug.h:281
#define unlikely(expr)
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition util-print.c:231
bool SCProtoNameValid(uint16_t proto)
Function to check if the received protocol number is valid and do we have corresponding name entry fo...
const char * known_proto[256]
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition util-time.c:209
SCTime_t TimeGet(void)
Definition util-time.c:152
#define SCTIME_SECS(t)
Definition util-time.h:57