-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathflow.bro
More file actions
564 lines (514 loc) · 20.9 KB
/
flow.bro
File metadata and controls
564 lines (514 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
@load ./main
@load base/utils/directions-and-hosts
module Netbase;
export {
redef record Netbase::observation += {
## Unique ports communicated with internally
int_ports: set[string] &optional;
## Count of unique ports communicated with internally
int_port_cnt: count &default=0 &log;
## Unique hosts communicated with internally
int_hosts: set[string] &optional;
## Count of unique hosts communicated with internally
int_host_cnt: count &default=0 &log;
## Unique ports communicated with externally
ext_ports: set[string] &optional;
## Count of unique ports communicated with externally
ext_port_cnt: count &default=0 &log;
## Unique IP's communicated with externally
ext_hosts: set[string] &optional;
## Count of unique hosts communicated with externally
ext_host_cnt: count &default=0 &log;
## Unique internal clients communicating with this IP
int_clients: set[string] &optional;
## Count of unique internal clients communicating with this IP
int_client_cnt: count &default=0 &log;
## Unique external clients communicating with this IP
ext_clients: set[string] &optional;
## Count fo unique external clients communicating with this IP
ext_client_cnt: count &default=0 &log;
## Total count of connections this IP was involved in
total_conns: count &default=0 &log;
## Total count of external conns originated by this IP
out_orig_conns: count &default=0 &log;
## Count of outbound conns originated by this IP that were successful
out_succ_conns: count &default=0 &log;
## Count of outbound conns originated by this IP that were rejected
out_rej_conns: count &default=0 &log;
## Count of outbound conns originated by this IP to ports >= 1024
out_to_highports: count &default=0 &log;
## Count of outbound conns originated by this IP to ports < 1024
out_to_lowports: count &default=0 &log;
## Count of outbound conns to a recognized service (service field populated)
out_to_service: count &default=0 &log;
## Total count of internal conns originated by this host
int_orig_conns: count &default=0 &log;
## Count of internal conns originated by this host that were rejected
int_rej_conns: count &default=0 &log;
## Count of internal conns to ports >= 1024
int_to_highports: count &default=0 &log;
## Count of internal conns to ports < 1024
int_to_lowports: count &default=0 &log;
## Count of internal conns to recognized server (service field populated)
int_to_service: count &default=0 &log;
## Count of internal conns this IP responded to
int_resp_conns: count &default=0 &log;
## Sum of bytes sent as originator in internal conns
int_orig_bytes_sent: count &default=0 &log;
## Sum of bytes received as originator in internal conns
int_orig_bytes_rcvd: count &default=0 &log;
## Sum of bytes sent as originator in external conns
out_orig_bytes_sent: count &default=0 &log;
## Sum of bytes received as originator in external conns
out_orig_bytes_rcvd: count &default=0 &log;
## Count of packets sent in internal conns
int_orig_pkts_sent: count &default=0 &log;
## Count of packets recevied in internal conns
int_orig_pkts_recvd: count &default=0 &log;
## Count of packets sent as originator in outbound conns
out_orig_pkts_sent: count &default=0 &log;
## Count of packets received as originator in outbound conns
out_orig_pkts_recvd: count &default=0 &log;
## Count of SMB connections as a client
smb_client_conns: count &default=0 &log;
## Count of SMB connections as a server
smb_server_conns: count &default=0 &log;
# Container for smb-related PCR stats
pcr_smb: Netbase::numstats &default=Netbase::numstats();
## Avg pcr for smb connections
pcr_smb_avg: double &optional &log;
## Max pcr for smb connections
pcr_smb_max: double &optional &log;
## Min pcr for smb connections
pcr_smb_min: double &optional &log;
## Count of http connections as client
http_client_conns: count &default=0 &log;
## Count of http connections as server
http_server_conns: count &default=0 &log;
## Container for http pcr stats
pcr_http: Netbase::numstats &default=Netbase::numstats();
## Avg pcr for http connections
pcr_http_avg: double &optional &log;
## Max pcr for http connections
pcr_http_max: double &optional &log;
## Min pcr for http connections
pcr_http_min: double &optional &log;
## Count of dns connections as client
dns_client_conns: count &default=0 &log;
## Count of dns connections as server
dns_server_conns: count &default=0 &log;
## Container for dns pcr stats
pcr_dns: Netbase::numstats &default=Netbase::numstats();
## Avg pcr dns connections
pcr_dns_avg: double &optional &log;
## Max pcr for dns connections
pcr_dns_max: double &optional &log;
## Min pcr for dns connections
pcr_dns_min: double &optional &log;
## Count of ssl connection as client
ssl_client_conns: count &default=0 &log;
## Count of ssl connections as server
ssl_server_conns: count &default=0 &log;
## Container for ssl pcr stats
pcr_ssl: Netbase::numstats &default=Netbase::numstats();
## Avg pcr for ssl connections
pcr_ssl_avg: double &optional &log;
## Max pcr for ssl connections
pcr_ssl_max: double &optional &log;
## Min pcr for ssl connections
pcr_ssl_min: double &optional &log;
## Count of rdp connections as client
rdp_client_conns: count &default=0 &log;
## Count of rdp connections as server
rdp_server_conns: count &default=0 &log;
## Container for rdp pcr stats
pcr_rdp: Netbase::numstats &default=Netbase::numstats();
## Avg pcr for rdp connections
pcr_rdp_avg: double &optional &log;
## Max pcr for rdp connections
pcr_rdp_max: double &optional &log;
## Min pcr for rdp connections
pcr_rdp_min: double &optional &log;
};
}
# Function to gather flow stats for IPs in a given connection
function Netbase::get_flow_obs(c: connection, do_orig: bool, do_resp: bool)
{
if ( ! do_orig && ! do_resp )
return;
local orig = c$id$orig_h;
local resp = c$id$resp_h;
local pkg = observables();
if ( do_orig )
{
pkg[orig] = set([$name="total_conns"]);
}
if ( do_resp )
{
pkg[resp] = set([$name="total_conns"]);
}
local rp = port_to_count(c$id$resp_p);
# Internal -> external flow?
if ( id_matches_direction(c$id, OUTBOUND) && do_orig )
{
add pkg[orig][[$name="ext_ports", $val=cat(c$id$resp_p)]];
add pkg[orig][[$name="ext_hosts", $val=cat(c$id$resp_h)]];
add pkg[orig][[$name="out_orig_conns"]];
if ( c$orig?$size )
add pkg[orig][[$name="out_orig_bytes_sent", $val=cat(c$orig$size)]];
if ( c$resp?$size )
add pkg[orig][[$name="out_orig_bytes_rcvd", $val=cat(c$resp$size)]];
if ( c$orig?$num_pkts )
add pkg[orig][[$name="out_orig_pkts_sent", $val=cat(c$orig$num_pkts)]];
if ( c$resp?$num_pkts )
add pkg[orig][[$name="out_orig_pkts_recvd", $val=cat(c$resp$num_pkts)]];
if ( c$conn?$conn_state )
{
switch (c$conn$conn_state)
{
case "SF":
add pkg[orig][[$name="out_succ_conns"]];
break;
case "REJ":
add pkg[orig][[$name="out_rej_conns"]];
fallthrough;
}
}
if ( c?$service && |c$service| > 0 )
add pkg[orig][[$name="out_to_service"]];
if ( rp >= 1024)
add pkg[orig][[$name="out_to_highports"]];
else if ( rp < 1024 )
add pkg[orig][[$name="out_to_lowports"]];
}
# Internal -> internal flow?
else if ( addr_matches_host(orig,LOCAL_HOSTS) && addr_matches_host(resp,LOCAL_HOSTS) )
{
if ( do_resp )
{
add pkg[resp][[$name="int_clients", $val=cat(orig)]];
add pkg[resp][[$name="int_resp_conns"]];
}
if ( do_orig )
{
add pkg[orig][[$name="int_ports", $val=cat(c$id$resp_p)]];
add pkg[orig][[$name="int_hosts", $val=cat(resp)]];
add pkg[orig][[$name="int_orig_conns"]];
if ( c?$service && |c$service| > 0 )
add pkg[orig][[$name="int_to_service"]];
if ( c$orig?$size )
{
add pkg[orig][[$name="int_orig_bytes_sent", $val=cat(c$orig$size)]];
}
if ( c$resp?$size )
{
add pkg[orig][[$name="int_orig_bytes_rcvd", $val=cat(c$resp$size)]];
}
if ( c$orig?$num_pkts )
{
add pkg[orig][[$name="int_orig_pkts_sent", $val=cat(c$orig$num_pkts)]];
}
if ( c$resp?$num_pkts )
{
add pkg[orig][[$name="int_orig_pkts_recvd", $val=cat(c$resp$num_pkts)]];
}
if ( c$conn?$conn_state )
{
switch (c$conn$conn_state)
{
case "SF":
add pkg[orig][[$name="int_conns"]];
break;
case "REJ":
add pkg[orig][[$name="int_rej_conns"]];
fallthrough;
}
}
if ( rp >= 1024)
{
add pkg[orig][[$name="int_to_highports"]];
}
else if ( rp < 1024 )
{
add pkg[orig][[$name="int_to_lowports"]];
}
}
}
# External -> internal flow?
else if ( id_matches_direction(c$id, INBOUND) && do_resp )
{
add pkg[resp][[$name="server_conns"]];
add pkg[resp][[$name="ext_clients", $val=cat(orig)]];
}
# Now check the service field
for ( s in c$service )
{
switch s
{
case "DNS":
# found dns
if ( do_orig )
{
add pkg[orig][[$name="dns_client_conns"]];
if ( c$conn?$pcr )
add pkg[orig][[$name="pcr_dns", $val=cat(c$conn$pcr)]];
}
if ( do_resp )
{
add pkg[resp][[$name="dns_server_conns"]];
}
break;
case "SSL":
if ( do_orig )
{
add pkg[orig][[$name="ssl_client_conns"]];
if ( c$conn?$pcr )
add pkg[orig][[$name="pcr_ssl", $val=cat(c$conn$pcr)]];
}
if ( do_resp )
{
add pkg[resp][[$name="ssl_server_conns"]];
}
break;
case "RDP":
if ( do_orig )
{
add pkg[orig][[$name="rdp_client_conns"]];
if ( c$conn?$pcr )
add pkg[orig][[$name="pcr_rdp", $val=cat(c$conn$pcr)]];
}
if ( do_resp )
{
add pkg[resp][[$name="rdp_server_conns"]];
}
break;
case "SMB":
if ( do_orig )
{
add pkg[orig][[$name="smb_client_conns"]];
if ( c$conn?$pcr )
add pkg[orig][[$name="pcr_smb", $val=cat(c$conn$pcr)]];
}
if ( do_resp )
{
add pkg[resp][[$name="smb_server_conns"]];
}
break;
case "HTTP":
if ( do_orig )
{
add pkg[orig][[$name="http_client_conns"]];
if ( c$conn?$pcr )
add pkg[orig][[$name="pcr_http", $val=cat(c$conn$pcr)]];
}
if ( do_resp )
{
add pkg[resp][[$name="http_server_conns"]];
}
break;
}
}
# See if the observable pkgs need delivering
if ( do_orig )
{
Netbase::SEND(orig, pkg[orig]);
}
if ( do_resp )
{
Netbase::SEND(resp, pkg[resp]);
}
}
# Handler for grabbing unique value counts for logging
event Netbase::log_observation(obs: observation)
{
obs$int_port_cnt = |obs$int_ports|;
obs$int_host_cnt = |obs$int_hosts|;
obs$ext_port_cnt = |obs$ext_ports|;
obs$ext_host_cnt = |obs$ext_hosts|;
obs$int_client_cnt = |obs$int_clients|;
obs$ext_client_cnt = |obs$ext_clients|;
if ( obs$pcr_dns$cnt > 0 )
{
obs$pcr_dns_avg = obs$pcr_dns$avg;
obs$pcr_dns_max = obs$pcr_dns$max;
obs$pcr_dns_min = obs$pcr_dns$min;
}
if ( obs$pcr_http$cnt > 0 )
{
obs$pcr_http_avg = obs$pcr_http$avg;
obs$pcr_http_max = obs$pcr_http$max;
obs$pcr_http_min = obs$pcr_http$min;
}
if ( obs$pcr_ssl$cnt > 0 )
{
obs$pcr_ssl_avg = obs$pcr_ssl$avg;
obs$pcr_ssl_max = obs$pcr_ssl$max;
obs$pcr_ssl_min = obs$pcr_ssl$min;
}
if ( obs$pcr_smb$cnt > 0 )
{
obs$pcr_smb_avg = obs$pcr_smb$avg;
obs$pcr_smb_max = obs$pcr_smb$max;
obs$pcr_smb_min = obs$pcr_smb$min;
}
if ( obs$pcr_rdp$cnt > 0 )
{
obs$pcr_rdp_avg = obs$pcr_rdp$avg;
obs$pcr_rdp_max = obs$pcr_rdp$max;
obs$pcr_rdp_min = obs$pcr_rdp$min;
}
}
# Hook handler to initialize sets
hook Netbase::customize_obs(ip: addr, obs: table[addr] of observation)
{
obs[ip]$int_ports=set();
obs[ip]$int_hosts=set();
obs[ip]$ext_ports=set();
obs[ip]$ext_hosts=set();
obs[ip]$int_clients=set();
obs[ip]$ext_clients=set();
}
# Handler to load observables into the observations table
# This event is executed every time a node calls the SEND()
# function.
@if ( ! Cluster::is_enabled() || Cluster::local_node_type() == Cluster::PROXY )
event Netbase::add_observables(ip: addr, obs: set[observable])
{
for ( o in obs )
{
switch o$name
{
case "int_ports":
add observations[ip]$int_ports[o$val];
break;
case "int_hosts":
add observations[ip]$int_hosts[o$val];
break;
case "ext_ports":
add observations[ip]$ext_ports[o$val];
break;
case "ext_hosts":
add observations[ip]$ext_hosts[o$val];
break;
case "int_clients":
add observations[ip]$int_clients[o$val];
break;
case "ext_clients":
add observations[ip]$ext_clients[o$val];
break;
case "total_conns":
++observations[ip]$total_conns;
break;
case "out_succ_conns":
++observations[ip]$out_succ_conns;
break;
case "out_rej_conns":
++observations[ip]$out_rej_conns;
break;
case "out_to_highports":
++observations[ip]$out_to_highports;
break;
case "out_to_lowports":
++observations[ip]$out_to_lowports;
break;
case "out_to_service":
++observations[ip]$out_to_service;
break;
case "int_orig_conns":
++observations[ip]$int_orig_conns;
break;
case "int_rej_conns":
++observations[ip]$int_rej_conns;
break;
case "int_to_highports":
++observations[ip]$int_to_highports;
break;
case "int_to_lowports":
++observations[ip]$int_to_lowports;
break;
case "int_to_service":
++observations[ip]$int_to_service;
break;
case "int_resp_conns":
++observations[ip]$int_resp_conns;
break;
case "int_orig_bytes_sent":
observations[ip]$int_orig_bytes_sent = observations[ip]$int_orig_bytes_sent + to_count(o$val);
break;
case "int_orig_bytes_rcvd":
observations[ip]$int_orig_bytes_rcvd = observations[ip]$int_orig_bytes_rcvd + to_count(o$val);
break;
case "out_orig_bytes_sent":
observations[ip]$out_orig_bytes_sent = observations[ip]$out_orig_bytes_sent + to_count(o$val);
break;
case "out_orig_bytes_rcvd":
observations[ip]$out_orig_bytes_rcvd = observations[ip]$out_orig_bytes_rcvd + to_count(o$val);
break;
case "int_orig_pkts_sent":
observations[ip]$int_orig_pkts_sent = observations[ip]$int_orig_pkts_sent + to_count(o$val);
break;
case "int_orig_pkts_recvd":
observations[ip]$int_orig_pkts_recvd = observations[ip]$int_orig_pkts_recvd + to_count(o$val);
break;
case "out_orig_pkts_sent":
observations[ip]$out_orig_pkts_sent = observations[ip]$out_orig_pkts_sent + to_count(o$val);
break;
case "out_orig_pkts_recvd":
observations[ip]$out_orig_pkts_recvd = observations[ip]$out_orig_pkts_recvd + to_count(o$val);
break;
case "pcr_dns":
observations[ip]$pcr_dns = Netbase::update_numstats(observations[ip]$pcr_dns, to_double(o$val));
break;
case "pcr_ssl":
observations[ip]$pcr_ssl = Netbase::update_numstats(observations[ip]$pcr_ssl, to_double(o$val));
break;
case "pcr_http":
observations[ip]$pcr_http = Netbase::update_numstats(observations[ip]$pcr_http, to_double(o$val));
break;
case "pcr_smb":
observations[ip]$pcr_smb = Netbase::update_numstats(observations[ip]$pcr_smb, to_double(o$val));
break;
case "pcr_rdp":
observations[ip]$pcr_rdp = Netbase::update_numstats(observations[ip]$pcr_rdp, to_double(o$val));
break;
case "dns_server_conns":
++observations[ip]$dns_server_conns;
break;
case "dns_client_conns":
++observations[ip]$dns_client_conns;
break;
case "http_server_conns":
++observations[ip]$http_server_conns;
break;
case "http_client_conns":
++observations[ip]$http_client_conns;
break;
case "ssl_server_conns":
++observations[ip]$ssl_server_conns;
break;
case "ssl_client_conns":
++observations[ip]$ssl_client_conns;
break;
case "smb_server_conns":
++observations[ip]$smb_server_conns;
break;
case "smb_client_conns":
++observations[ip]$smb_client_conns;
break;
case "rdp_server_conns":
++observations[ip]$rdp_server_conns;
break;
case "rdp_client_conns":
++observations[ip]$rdp_client_conns;
break;
}
}
}
@endif
# Hanndler to vet the connection and start observations
event connection_state_remove(c: connection)
{
if ( ! c?$id )
return;
Netbase::get_flow_obs(c, Netbase::is_monitored(c$id$orig_h), Netbase::is_monitored(c$id$resp_h));
}