| 1 | @load tcp |
|---|
| 2 | @load irc |
|---|
| 3 | |
|---|
| 4 | module IrcHosts; |
|---|
| 5 | |
|---|
| 6 | export { |
|---|
| 7 | # Define a NOTICE type for our alert. |
|---|
| 8 | redef enum Notice += { |
|---|
| 9 | ConnectionSeen |
|---|
| 10 | }; |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | # Set of all addresses seen on IRC. |
|---|
| 14 | global hosts: set[addr] &persistent &read_expire=7days; |
|---|
| 15 | |
|---|
| 16 | # Generated when a message is posted to a channel. |
|---|
| 17 | event irc_privmsg_message(c: connection, source: string, target: string, message: string) |
|---|
| 18 | { |
|---|
| 19 | # Find numerical addresses. |
|---|
| 20 | local num_addrs = find_all(message, /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/); |
|---|
| 21 | for ( a in num_addrs ) |
|---|
| 22 | { |
|---|
| 23 | print a; |
|---|
| 24 | local ip = to_addr(a); |
|---|
| 25 | add hosts[ip]; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | # Find DNS names |
|---|
| 29 | local dns_addrs = find_all(message, /[^[:space:]]*\.[A-Za-z][A-Za-z]*/); |
|---|
| 30 | for ( a in dns_addrs ) |
|---|
| 31 | { |
|---|
| 32 | print a; |
|---|
| 33 | when ( local ips = lookup_hostname(a) ) |
|---|
| 34 | { |
|---|
| 35 | for ( ip in ips ) |
|---|
| 36 | add hosts[ip]; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | # Generated when a connection has been fully established. |
|---|
| 42 | event connection_established(c: connection) |
|---|
| 43 | { |
|---|
| 44 | if ( c$id$resp_h !in hosts ) |
|---|
| 45 | return; |
|---|
| 46 | |
|---|
| 47 | NOTICE([$note=ConnectionSeen, $conn=c, |
|---|
| 48 | $msg=fmt("%s established connection to host %s seen on IRC", |
|---|
| 49 | c$id$orig_h, c$id$resp_h)]); |
|---|
| 50 | } |
|---|
| 51 | |
|---|