Debian Codegraph @

examples/prism-bro.html

raw symbols SCIP
1 <h2>Comments</h2>
2 <pre><code># Single line comment
3 </code></pre>
4
5 <h2>Strings</h2>
6 <pre><code>
7 "a", "b"
8 </code></pre>
9
10 <h2>Numbers</h2>
11 <pre><code>123
12 123.456
13 -123.456
14 </code></pre>
15
16 <h2>Misc</h2>
17 <pre><code>
18 @ifndef ourexp
19 @load-sigs somesigs
20 </code></pre>
21
22 <h2>Full example</h2>
23 <pre><code>
24 ##! Scan detector ported from Bro 1.x.
25 ##!
26 ##! This script has evolved over many years and is quite a mess right now. We
27 ##! have adapted it to work with Bro 2.x, but eventually Bro 2.x will
28 ##! get its own rewritten and generalized scan detector.
29
30 @load base/frameworks/notice/main
31
32 module Scan;
33
34 export {
35 redef enum Notice::Type += {
36 ## The source has scanned a number of ports.
37 PortScan,
38 ## The source has scanned a number of addresses.
39 AddressScan,
40 ## Apparent flooding backscatter seen from source.
41 BackscatterSeen,
42
43 ## Summary of scanning activity.
44 ScanSummary,
45 ## Summary of distinct ports per scanner.
46 PortScanSummary,
47 ## Summary of distinct low ports per scanner.
48 LowPortScanSummary,
49
50 ## Source reached :bro:id:`Scan::shut_down_thresh`
51 ShutdownThresh,
52 ## Source touched privileged ports.
53 LowPortTrolling,
54 };
55
56 # Whether to consider UDP "connections" for scan detection.
57 # Can lead to false positives due to UDP fanout from some P2P apps.
58 const suppress_UDP_scan_checks = F &amp;redef;
59
60 const activate_priv_port_check = T &amp;redef;
61 const activate_landmine_check = F &amp;redef;
62 const landmine_thresh_trigger = 5 &amp;redef;
63
64 const landmine_address: set[addr] &amp;redef;
65
66 const scan_summary_trigger = 25 &amp;redef;
67 const port_summary_trigger = 20 &amp;redef;
68 const lowport_summary_trigger = 10 &amp;redef;
69
70 # Raise ShutdownThresh after this many failed attempts
71 const shut_down_thresh = 100 &amp;redef;
72
73 # Which services should be analyzed when detecting scanning
74 # (not consulted if analyze_all_services is set).
75 const analyze_services: set[port] &amp;redef;
76 const analyze_all_services = T &amp;redef;
77
78 # Track address scaners only if at least these many hosts contacted.
79 const addr_scan_trigger = 0 &amp;redef;
80
81 # Ignore address scanners for further scan detection after
82 # scanning this many hosts.
83 # 0 disables.
84 const ignore_scanners_threshold = 0 &amp;redef;
85
86 # Report a scan of peers at each of these points.
87 const report_peer_scan: vector of count = {
88 20, 100, 1000, 10000, 50000, 100000, 250000, 500000, 1000000,
89 } &amp;redef;
90
91 const report_outbound_peer_scan: vector of count = {
92 100, 1000, 10000,
93 } &amp;redef;
94
95 # Report a scan of ports at each of these points.
96 const report_port_scan: vector of count = {
97 50, 250, 1000, 5000, 10000, 25000, 65000,
98 } &amp;redef;
99
100 # Once a source has scanned this many different ports (to however many
101 # different remote hosts), start tracking its per-destination access.
102 const possible_port_scan_thresh = 20 &amp;redef;
103
104 # Threshold for scanning privileged ports.
105 const priv_scan_trigger = 5 &amp;redef;
106 const troll_skip_service = {
107 25/tcp, 21/tcp, 22/tcp, 20/tcp, 80/tcp,
108 } &amp;redef;
109
110 const report_accounts_tried: vector of count = {
111 20, 100, 1000, 10000, 100000, 1000000,
112 } &amp;redef;
113
114 const report_remote_accounts_tried: vector of count = {
115 100, 500,
116 } &amp;redef;
117
118 # Report a successful password guessing if the source attempted
119 # at least this many.
120 const password_guessing_success_threshhold = 20 &amp;redef;
121
122 const skip_accounts_tried: set[addr] &amp;redef;
123
124 const addl_web = {
125 81/tcp, 443/tcp, 8000/tcp, 8001/tcp, 8080/tcp, }
126 &amp;redef;
127
128 const skip_services = { 113/tcp, } &amp;redef;
129 const skip_outbound_services = { 21/tcp, addl_web, }
130 &amp;redef;
131
132 const skip_scan_sources = {
133 255.255.255.255, # who knows why we see these, but we do
134 } &amp;redef;
135
136 const skip_scan_nets: set[subnet] = {} &amp;redef;
137
138 # List of well known local server/ports to exclude for scanning
139 # purposes.
140 const skip_dest_server_ports: set[addr, port] = {} &amp;redef;
141
142 # Reverse (SYN-ack) scans seen from these ports are considered
143 # to reflect possible SYN-flooding backscatter, and not true
144 # (stealth) scans.
145 const backscatter_ports = {
146 80/tcp, 8080/tcp, 53/tcp, 53/udp, 179/tcp, 6666/tcp, 6667/tcp,
147 } &amp;redef;
148
149 const report_backscatter: vector of count = {
150 20,
151 } &amp;redef;
152
153 global check_scan:
154 function(c: connection, established: bool, reverse: bool): bool;
155
156 # The following tables are defined here so that we can redef
157 # the expire timeouts.
158 # FIXME: should we allow redef of attributes on IDs which
159 # are not exported?
160
161 # How many different hosts connected to with a possible
162 # backscatter signature.
163 global distinct_backscatter_peers: table[addr] of table[addr] of count
164 &read_expire = 15 min;
165
166 # Expire functions that trigger summaries.
167 global scan_summary:
168 function(t: table[addr] of set[addr], orig: addr): interval;
169 global port_summary:
170 function(t: table[addr] of set[port], orig: addr): interval;
171 global lowport_summary:
172 function(t: table[addr] of set[port], orig: addr): interval;
173
174 # Indexed by scanner address, yields # distinct peers scanned.
175 # pre_distinct_peers tracks until addr_scan_trigger hosts first.
176 global pre_distinct_peers: table[addr] of set[addr]
177 &read_expire = 15 mins &amp;redef;
178
179 global distinct_peers: table[addr] of set[addr]
180 &read_expire = 15 mins &expire_func=scan_summary &amp;redef;
181 global distinct_ports: table[addr] of set[port]
182 &read_expire = 15 mins &expire_func=port_summary &amp;redef;
183 global distinct_low_ports: table[addr] of set[port]
184 &read_expire = 15 mins &expire_func=lowport_summary &amp;redef;
185
186 # Indexed by scanner address, yields a table with scanned hosts
187 # (and ports).
188 global scan_triples: table[addr] of table[addr] of set[port];
189
190 global remove_possible_source:
191 function(s: set[addr], idx: addr): interval;
192 global possible_scan_sources: set[addr]
193 &expire_func=remove_possible_source &read_expire = 15 mins;
194
195 # Indexed by source address, yields user name & password tried.
196 global accounts_tried: table[addr] of set[string, string]
197 &read_expire = 1 days;
198
199 global ignored_scanners: set[addr] &create_expire = 1 day &amp;redef;
200
201 # These tables track whether a threshold has been reached.
202 # More precisely, the counter is the next index of threshold vector.
203 global shut_down_thresh_reached: table[addr] of bool &default=F;
204 global rb_idx: table[addr] of count
205 &default=1 &read_expire = 1 days &amp;redef;
206 global rps_idx: table[addr] of count
207 &default=1 &read_expire = 1 days &amp;redef;
208 global rops_idx: table[addr] of count
209 &default=1 &read_expire = 1 days &amp;redef;
210 global rpts_idx: table[addr,addr] of count
211 &default=1 &read_expire = 1 days &amp;redef;
212 global rat_idx: table[addr] of count
213 &default=1 &read_expire = 1 days &amp;redef;
214 global rrat_idx: table[addr] of count
215 &default=1 &read_expire = 1 days &amp;redef;
216 }
217
218 global thresh_check: function(v: vector of count, idx: table[addr] of count,
219 orig: addr, n: count): bool;
220 global thresh_check_2: function(v: vector of count,
221 idx: table[addr,addr] of count, orig: addr,
222 resp: addr, n: count): bool;
223
224 function scan_summary(t: table[addr] of set[addr], orig: addr): interval
225 {
226 local num_distinct_peers = orig in t ? |t[orig]| : 0;
227
228 if ( num_distinct_peers >= scan_summary_trigger )
229 NOTICE([$note=ScanSummary, $src=orig, $n=num_distinct_peers,
230 $identifier=fmt("%s", orig),
231 $msg=fmt("%s scanned a total of %d hosts",
232 orig, num_distinct_peers)]);
233
234 return 0 secs;
235 }
236
237 function port_summary(t: table[addr] of set[port], orig: addr): interval
238 {
239 local num_distinct_ports = orig in t ? |t[orig]| : 0;
240
241 if ( num_distinct_ports >= port_summary_trigger )
242 NOTICE([$note=PortScanSummary, $src=orig, $n=num_distinct_ports,
243 $identifier=fmt("%s", orig),
244 $msg=fmt("%s scanned a total of %d ports",
245 orig, num_distinct_ports)]);
246
247 return 0 secs;
248 }
249
250 function lowport_summary(t: table[addr] of set[port], orig: addr): interval
251 {
252 local num_distinct_lowports = orig in t ? |t[orig]| : 0;
253
254 if ( num_distinct_lowports >= lowport_summary_trigger )
255 NOTICE([$note=LowPortScanSummary, $src=orig,
256 $n=num_distinct_lowports,
257 $identifier=fmt("%s", orig),
258 $msg=fmt("%s scanned a total of %d low ports",
259 orig, num_distinct_lowports)]);
260
261 return 0 secs;
262 }
263
264 function clear_addr(a: addr)
265 {
266 delete distinct_peers[a];
267 delete distinct_ports[a];
268 delete distinct_low_ports[a];
269 delete scan_triples[a];
270 delete possible_scan_sources[a];
271 delete distinct_backscatter_peers[a];
272 delete pre_distinct_peers[a];
273 delete rb_idx[a];
274 delete rps_idx[a];
275 delete rops_idx[a];
276 delete rat_idx[a];
277 delete rrat_idx[a];
278 delete shut_down_thresh_reached[a];
279 delete ignored_scanners[a];
280 }
281
282 function ignore_addr(a: addr)
283 {
284 clear_addr(a);
285 add ignored_scanners[a];
286 }
287
288 function check_scan(c: connection, established: bool, reverse: bool): bool
289 {
290 local id = c$id;
291
292 local service = "ftp-data" in c$service ? 20/tcp
293 : (reverse ? id$orig_p : id$resp_p);
294 local rev_service = reverse ? id$resp_p : id$orig_p;
295 local orig = reverse ? id$resp_h : id$orig_h;
296 local resp = reverse ? id$orig_h : id$resp_h;
297 local outbound = Site::is_local_addr(orig);
298
299 # The following works better than using get_conn_transport_proto()
300 # because c might not correspond to an active connection (which
301 # causes the function to fail).
302 if ( suppress_UDP_scan_checks &&
303 service >= 0/udp && service &lt;= 65535/udp )
304 return F;
305
306 if ( service in skip_services && ! outbound )
307 return F;
308
309 if ( outbound && service in skip_outbound_services )
310 return F;
311
312 if ( orig in skip_scan_sources )
313 return F;
314
315 if ( orig in skip_scan_nets )
316 return F;
317
318 # Don't include well known server/ports for scanning purposes.
319 if ( ! outbound && [resp, service] in skip_dest_server_ports )
320 return F;
321
322 if ( orig in ignored_scanners)
323 return F;
324
325 if ( ! established &&
326 # not established, service not expressly allowed
327
328 # not known peer set
329 (orig !in distinct_peers || resp !in distinct_peers[orig]) &&
330
331 # want to consider service for scan detection
332 (analyze_all_services || service in analyze_services) )
333 {
334 if ( reverse && rev_service in backscatter_ports &&
335 # reverse, non-priv backscatter port
336 service >= 1024/tcp )
337 {
338 if ( orig !in distinct_backscatter_peers )
339 {
340 local empty_bs_table:
341 table[addr] of count &default=0;
342 distinct_backscatter_peers[orig] =
343 empty_bs_table;
344 }
345
346 if ( ++distinct_backscatter_peers[orig][resp] &lt;= 2 &&
347 # The test is &lt;= 2 because we get two check_scan()
348 # calls, once on connection attempt and once on
349 # tear-down.
350
351 distinct_backscatter_peers[orig][resp] == 1 &&
352
353 # Looks like backscatter, and it's not scanning
354 # a privileged port.
355
356 thresh_check(report_backscatter, rb_idx, orig,
357 |distinct_backscatter_peers[orig]|)
358 )
359 {
360 NOTICE([$note=BackscatterSeen, $src=orig,
361 $p=rev_service,
362 $identifier=fmt("%s", orig),
363 $msg=fmt("backscatter seen from %s (%d hosts; %s)",
364 orig, |distinct_backscatter_peers[orig]|, rev_service)]);
365 }
366
367 if ( ignore_scanners_threshold > 0 &&
368 |distinct_backscatter_peers[orig]| >
369 ignore_scanners_threshold )
370 ignore_addr(orig);
371 }
372
373 else
374 { # done with backscatter check
375 local ignore = F;
376
377 if ( orig !in distinct_peers && addr_scan_trigger > 0 )
378 {
379 if ( orig !in pre_distinct_peers )
380 pre_distinct_peers[orig] = set();
381
382 add pre_distinct_peers[orig][resp];
383 if ( |pre_distinct_peers[orig]| &lt; addr_scan_trigger )
384 ignore = T;
385 }
386
387 if ( ! ignore )
388 { # XXXXX
389
390 if ( orig !in distinct_peers )
391 distinct_peers[orig] = set() &amp;mergeable;
392
393 if ( resp !in distinct_peers[orig] )
394 add distinct_peers[orig][resp];
395
396 local n = |distinct_peers[orig]|;
397
398 # Check for threshold if not outbound.
399 if ( ! shut_down_thresh_reached[orig] &&
400 n >= shut_down_thresh &&
401 ! outbound && orig !in Site::neighbor_nets )
402 {
403 shut_down_thresh_reached[orig] = T;
404 local msg = fmt("shutdown threshold reached for %s", orig);
405 NOTICE([$note=ShutdownThresh, $src=orig,
406 $identifier=fmt("%s", orig),
407 $p=service, $msg=msg]);
408 }
409
410 else
411 {
412 local address_scan = F;
413 if ( outbound &&
414 # inside host scanning out?
415 thresh_check(report_outbound_peer_scan, rops_idx, orig, n) )
416 address_scan = T;
417
418 if ( ! outbound &&
419 thresh_check(report_peer_scan, rps_idx, orig, n) )
420 address_scan = T;
421
422 if ( address_scan )
423 NOTICE([$note=AddressScan,
424 $src=orig, $p=service,
425 $n=n,
426 $identifier=fmt("%s-%d", orig, n),
427 $msg=fmt("%s has scanned %d hosts (%s)",
428 orig, n, service)]);
429
430 if ( address_scan &&
431 ignore_scanners_threshold > 0 &&
432 n > ignore_scanners_threshold )
433 ignore_addr(orig);
434 }
435 }
436 } # XXXX
437 }
438
439 if ( established )
440 # Don't consider established connections for port scanning,
441 # it's too easy to be mislead by FTP-like applications that
442 # legitimately gobble their way through the port space.
443 return F;
444
445 # Coarse search for port-scanning candidates: those that have made
446 # connections (attempts) to possible_port_scan_thresh or more
447 # distinct ports.
448 if ( orig !in distinct_ports || service !in distinct_ports[orig] )
449 {
450 if ( orig !in distinct_ports )
451 distinct_ports[orig] = set() &amp;mergeable;
452
453 if ( service !in distinct_ports[orig] )
454 add distinct_ports[orig][service];
455
456 if ( |distinct_ports[orig]| >= possible_port_scan_thresh &&
457 orig !in scan_triples )
458 {
459 scan_triples[orig] = table() &amp;mergeable;
460 add possible_scan_sources[orig];
461 }
462 }
463
464 # Check for low ports.
465 if ( activate_priv_port_check && ! outbound && service &lt; 1024/tcp &&
466 service !in troll_skip_service )
467 {
468 if ( orig !in distinct_low_ports ||
469 service !in distinct_low_ports[orig] )
470 {
471 if ( orig !in distinct_low_ports )
472 distinct_low_ports[orig] = set() &amp;mergeable;
473
474 add distinct_low_ports[orig][service];
475
476 if ( |distinct_low_ports[orig]| == priv_scan_trigger &&
477 orig !in Site::neighbor_nets )
478 {
479 local svrc_msg = fmt("low port trolling %s %s", orig, service);
480 NOTICE([$note=LowPortTrolling, $src=orig,
481 $identifier=fmt("%s", orig),
482 $p=service, $msg=svrc_msg]);
483 }
484
485 if ( ignore_scanners_threshold > 0 &&
486 |distinct_low_ports[orig]| >
487 ignore_scanners_threshold )
488 ignore_addr(orig);
489 }
490 }
491
492 # For sources that have been identified as possible scan sources,
493 # keep track of per-host scanning.
494 if ( orig in possible_scan_sources )
495 {
496 if ( orig !in scan_triples )
497 scan_triples[orig] = table() &amp;mergeable;
498
499 if ( resp !in scan_triples[orig] )
500 scan_triples[orig][resp] = set() &amp;mergeable;
501
502 if ( service !in scan_triples[orig][resp] )
503 {
504 add scan_triples[orig][resp][service];
505
506 if ( thresh_check_2(report_port_scan, rpts_idx,
507 orig, resp,
508 |scan_triples[orig][resp]|) )
509 {
510 local m = |scan_triples[orig][resp]|;
511 NOTICE([$note=PortScan, $n=m, $src=orig,
512 $p=service,
513 $identifier=fmt("%s-%d", orig, n),
514 $msg=fmt("%s has scanned %d ports of %s",
515 orig, m, resp)]);
516 }
517 }
518 }
519
520 return T;
521 }
522
523
524 # Hook into the catch&release dropping. When an address gets restored, we reset
525 # the source to allow dropping it again.
526 event Drop::address_restored(a: addr)
527 {
528 clear_addr(a);
529 }
530
531 event Drop::address_cleared(a: addr)
532 {
533 clear_addr(a);
534 }
535
536 # When removing a possible scan source, we automatically delete its scanned
537 # hosts and ports. But we do not want the deletion propagated, because every
538 # peer calls the expire_function on its own (and thus applies the delete
539 # operation on its own table).
540 function remove_possible_source(s: set[addr], idx: addr): interval
541 {
542 suspend_state_updates();
543 delete scan_triples[idx];
544 resume_state_updates();
545
546 return 0 secs;
547 }
548
549 # To recognize whether a certain threshhold vector (e.g. report_peer_scans)
550 # has been transgressed, a global variable containing the next vector index
551 # (idx) must be incremented. This cumbersome mechanism is necessary because
552 # values naturally don't increment by one (e.g. replayed table merges).
553 function thresh_check(v: vector of count, idx: table[addr] of count,
554 orig: addr, n: count): bool
555 {
556 if ( ignore_scanners_threshold > 0 && n > ignore_scanners_threshold )
557 {
558 ignore_addr(orig);
559 return F;
560 }
561
562 if ( idx[orig] &lt;= |v| && n >= v[idx[orig]] )
563 {
564 ++idx[orig];
565 return T;
566 }
567 else
568 return F;
569 }
570
571 # Same as above, except the index has a different type signature.
572 function thresh_check_2(v: vector of count, idx: table[addr, addr] of count,
573 orig: addr, resp: addr, n: count): bool
574 {
575 if ( ignore_scanners_threshold > 0 && n > ignore_scanners_threshold )
576 {
577 ignore_addr(orig);
578 return F;
579 }
580
581 if ( idx[orig,resp] &lt;= |v| && n >= v[idx[orig, resp]] )
582 {
583 ++idx[orig,resp];
584 return T;
585 }
586 else
587 return F;
588 }
589
590 event connection_established(c: connection)
591 {
592 local is_reverse_scan = (c$orig$state == TCP_INACTIVE);
593 Scan::check_scan(c, T, is_reverse_scan);
594 }
595
596 event partial_connection(c: connection)
597 {
598 Scan::check_scan(c, T, F);
599 }
600
601 event connection_attempt(c: connection)
602 {
603 Scan::check_scan(c, F, c$orig$state == TCP_INACTIVE);
604 }
605
606 event connection_half_finished(c: connection)
607 {
608 # Half connections never were "established", so do scan-checking here.
609 Scan::check_scan(c, F, F);
610 }
611
612 event connection_rejected(c: connection)
613 {
614 local is_reverse_scan = c$orig$state == TCP_RESET;
615
616 Scan::check_scan(c, F, is_reverse_scan);
617 }
618
619 event connection_reset(c: connection)
620 {
621 if ( c$orig$state == TCP_INACTIVE || c$resp$state == TCP_INACTIVE )
622 # We never heard from one side - that looks like a scan.
623 Scan::check_scan(c, c$orig$size + c$resp$size > 0,
624 c$orig$state == TCP_INACTIVE);
625 }
626
627 event connection_pending(c: connection)
628 {
629 if ( c$orig$state == TCP_PARTIAL && c$resp$state == TCP_INACTIVE )
630 Scan::check_scan(c, F, F);
631 }
632
633 # Report the remaining entries in the tables.
634 event bro_done()
635 {
636 for ( orig in distinct_peers )
637 scan_summary(distinct_peers, orig);
638
639 for ( orig in distinct_ports )
640 port_summary(distinct_ports, orig);
641
642 for ( orig in distinct_low_ports )
643 lowport_summary(distinct_low_ports, orig);
644 }
645 </code></pre>