| 1 | package ThreatNet::Filter::ThreatCache; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | ThreatNet::Filter::ThreatCache - A Threat Cache implementated as a filter | ||||||||||
| 8 | |||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||
| 10 | |||||||||||
| 11 | A C<ThreatNet::Filter::ThreatCache> is a basic filter-based implementation | ||||||||||
| 12 | of a I<Threat Cache>, as defined in the ThreatNet concept paper. | ||||||||||
| 13 | (L<http://ali.as/threatnet/>) | ||||||||||
| 14 | |||||||||||
| 15 | The consistent use of Threat Caches by all nodes is the key to keeping | ||||||||||
| 16 | message quantities to a minimum, and allows the entire network to safely | ||||||||||
| 17 | run without any canonical state. | ||||||||||
| 18 | |||||||||||
| 19 | As each message is provided to the filter, it stores an IPs that it sees, | ||||||||||
| 20 | filtering out any ips that have already been seen in the last hour (or | ||||||||||
| 21 | custom period if provided). | ||||||||||
| 22 | |||||||||||
| 23 | =head1 METHODS | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 3 | ▶ | use Params::Util '_INSTANCE'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 3 | ▶ | use base 'ThreatNet::Filter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 3 | ▶ | use vars qw{$VERSION}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 3 | $VERSION = '0.20'; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | ##################################################################### | ||||||||||
| 41 | # ThreatNet::Filter Interface | ||||||||||
| 42 | |||||||||||
| 43 | =pod | ||||||||||
| 44 | |||||||||||
| 45 | =head2 new [ $param => $value, ... ] | ||||||||||
| 46 | |||||||||||
| 47 | The C<new> method creates a new Threat Cache object. | ||||||||||
| 48 | |||||||||||
| 49 | It takes a single optional parameter C<timeout> which should the | ||||||||||
| 50 | positive integer number of seconds the channel dictates as the mimumum | ||||||||||
| 51 | time before an event can be rementioned. The default value is 3600 | ||||||||||
| 52 | (1 hour). | ||||||||||
| 53 | |||||||||||
| 54 | Returns a new C<ThreatNet::Filter::ThreatCache> object. | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 59 | 2 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 2 | my %args = @_; | |||||||||
| 61 | |||||||||||
| 62 | # Add the cache attributes to the base filter object | ||||||||||
| 63 | 2 | my $self = $class->SUPER::new; | |||||||||
| 64 | 2 | $self->{cache_ip} = {}; | |||||||||
| 65 | 2 | $self->{cache_time} = []; | |||||||||
| 66 | |||||||||||
| 67 | # When we will be syncronised? | ||||||||||
| 68 | # Add a couple of seconds to allow for small initialization delays. | ||||||||||
| 69 | 2 | my $t = time(); | |||||||||
| 70 | 2 | ▶ | $self->{timeout} = defined $args{timeout} ? $args{timeout} : 3600; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 2 | $self->{sync_at} = $self->{timeout} + $t + 2; | |||||||||
| 72 | |||||||||||
| 73 | # Collect some very basic statistic | ||||||||||
| 74 | 2 | $self->{stats} = { time_start => $t, => seen => 0, kept => 0 }; | |||||||||
| 75 | |||||||||||
| 76 | 2 | $self; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =pod | ||||||||||
| 80 | |||||||||||
| 81 | =head2 keep $Message | ||||||||||
| 82 | |||||||||||
| 83 | As in the parent L<ThreatNet::Filter> class, the C<keep> method takes as | ||||||||||
| 84 | argument a single C<ThreatNet::Message> object. | ||||||||||
| 85 | |||||||||||
| 86 | It returns true if the message can be kept, or false if the message should | ||||||||||
| 87 | be dropped. | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||
| 90 | |||||||||||
| 91 | 8 | ▶ | sub keep { | ||||||||
|
Subroutine
keep: called CC=8 SCAR=21.6
Pod
documented
| |||||||||||
| 92 | 8 | my $self = shift; | |||||||||
| 93 | 8 | ▶ | my $Message = _INSTANCE(shift, 'ThreatNet::Message') or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 6 | ▶ | $Message->can('ip') or return undef; # WTF is it if it can't? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | # Flush old events out of the cache | ||||||||||
| 97 | 6 | ▶ | while ( my $event = $self->{cache_time}->[0] ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 4 | ▶ | if ( time() > $event->{time} + $self->{timeout} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | # Block has expired | ||||||||||
| 100 | 0 | shift @{$self->{cache_time}}; | |||||||||
| 101 | 0 | delete $self->{cache_ip}->{$event->{ip}}; | |||||||||
| 102 | } else { | ||||||||||
| 103 | # Because the events are added (we assume) in creation | ||||||||||
| 104 | # order, as soon as we encounter one that has not expired, | ||||||||||
| 105 | # we can assume the rest have not expired. | ||||||||||
| 106 | 4 | last; | |||||||||
| 107 | } | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | # We only want the time and IP | ||||||||||
| 111 | ### Convert this to ->event_time later? | ||||||||||
| 112 | 6 | ▶ | my $created = $Message->created or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 6 | ▶ | my $ip = $Message->ip or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | |||||||||||
| 115 | # At this point, we've officially "seen" the message | ||||||||||
| 116 | 6 | $self->{stats}->{seen}++; | |||||||||
| 117 | |||||||||||
| 118 | # If the ip is in the cache, don't keep it | ||||||||||
| 119 | 6 | ▶ | return '' if $self->{cache_ip}->{$ip}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | |||||||||||
| 121 | # Add to the cache and signal to keep. | ||||||||||
| 122 | ### Stricly speaking, just dropping it onto the end of the | ||||||||||
| 123 | ### queue is not good enough. Convert this to something that | ||||||||||
| 124 | ### ensures correct order once there is a chance that the events | ||||||||||
| 125 | ### may come in out of order, such as if we change from object | ||||||||||
| 126 | ### creation time to event time. | ||||||||||
| 127 | 4 | $self->{cache_ip}->{$ip} = $created; | |||||||||
| 128 | 4 | push @{$self->{cache_time}}, { time => $created, ip => $ip }; | |||||||||
| 129 | 4 | $self->{stats}->{kept}++; | |||||||||
| 130 | |||||||||||
| 131 | 4 | 1; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | =pod | ||||||||||
| 135 | |||||||||||
| 136 | =head2 synced | ||||||||||
| 137 | |||||||||||
| 138 | The C<synced> method checks to see if the Threat Cache has synchronised | ||||||||||
| 139 | with the channel. | ||||||||||
| 140 | |||||||||||
| 141 | Returns true if the current time is past the sync time, or false if not. | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | sub synced { | ||||||||
|
Subroutine
synced: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 146 | 0 | my $self = shift; | |||||||||
| 147 | 0 | !! (time() > $self->{sync_at}); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | =pod | ||||||||||
| 151 | |||||||||||
| 152 | =head2 stats | ||||||||||
| 153 | |||||||||||
| 154 | The C<stats> method returns a hash with a variety of statistics from | ||||||||||
| 155 | the Threat Cache. | ||||||||||
| 156 | |||||||||||
| 157 | Returns the stats as a C<HASH> reference. | ||||||||||
| 158 | |||||||||||
| 159 | =cut | ||||||||||
| 160 | |||||||||||
| 161 | 2 | ▶ | sub stats { | ||||||||
|
Subroutine
stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 162 | 2 | my $self = shift; | |||||||||
| 163 | 2 | my %stats = (); | |||||||||
| 164 | |||||||||||
| 165 | # Generate the general statistics | ||||||||||
| 166 | 2 | $stats{size} = scalar @{$self->{cache_time}}; | |||||||||
| 167 | 2 | $stats{seen} = $self->{stats}->{seen}; | |||||||||
| 168 | 2 | $stats{kept} = $self->{stats}->{kept}; | |||||||||
| 169 | 2 | $stats{discard} = $stats{seen} - $stats{kept}; | |||||||||
| 170 | 2 | $stats{expired} = $stats{kept} - $stats{size}; | |||||||||
| 171 | |||||||||||
| 172 | # Add the time statistics | ||||||||||
| 173 | 2 | $stats{time_start} = $self->{stats}->{time_start}; | |||||||||
| 174 | 2 | $stats{time_current} = time(); | |||||||||
| 175 | 2 | $stats{time_running} = $stats{time_current} - $stats{time_start}; | |||||||||
| 176 | |||||||||||
| 177 | # Percentages | ||||||||||
| 178 | 2 | $stats{percent_kept} = $self->_perc($stats{kept}, $stats{seen}); | |||||||||
| 179 | 2 | $stats{percent_discard} = $self->_perc($stats{discard}, $stats{seen}); | |||||||||
| 180 | |||||||||||
| 181 | # Rates | ||||||||||
| 182 | 2 | $stats{rate_seen} = $self->_rate($stats{seen}, $stats{time_running}); | |||||||||
| 183 | 2 | $stats{rate_kept} = $self->_rate($stats{kept}, $stats{time_running}); | |||||||||
| 184 | |||||||||||
| 185 | 2 | \%stats; | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 4 | ▶ | sub _perc { | ||||||||
|
Subroutine
_perc: called CC=2 SCAR=7.0
| |||||||||||
| 189 | 4 | my (undef, $items, $total) = @_; | |||||||||
| 190 | 4 | ▶ | my $perc = $total ? ($items / $total) : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 4 | $perc = $perc * 100; | |||||||||
| 192 | 4 | sprintf("%0.1f", $perc) . '%'; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | 4 | ▶ | sub _rate { | ||||||||
|
Subroutine
_rate: called CC=2 SCAR=7.1
| |||||||||||
| 196 | 4 | my (undef, $items, $interval) = @_; | |||||||||
| 197 | 4 | ▶ | my $rate = $interval ? ($items / $interval) : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 198 | 4 | sprintf("%0.1f", $rate); | |||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | 3 | 1; | |||||||||
| 202 | |||||||||||
| 203 | =pod | ||||||||||
| 204 | |||||||||||
| 205 | =head1 SUPPORT | ||||||||||
| 206 | |||||||||||
| 207 | All bugs should be filed via the bug tracker at | ||||||||||
| 208 | |||||||||||
| 209 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ThreatNet-Filter> | ||||||||||
| 210 | |||||||||||
| 211 | For other issues, or commercial enhancement and support, contact the author | ||||||||||
| 212 | |||||||||||
| 213 | =head1 AUTHORS | ||||||||||
| 214 | |||||||||||
| 215 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||
| 216 | |||||||||||
| 217 | =head1 SEE ALSO | ||||||||||
| 218 | |||||||||||
| 219 | L<http://ali.as/devel/threatnetwork.html>, L<ThreatNet::Filter> | ||||||||||
| 220 | |||||||||||
| 221 | =head1 COPYRIGHT | ||||||||||
| 222 | |||||||||||
| 223 | Copyright (c) 2005 Adam Kennedy. All rights reserved. | ||||||||||
| 224 | This program is free software; you can redistribute | ||||||||||
| 225 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 226 | |||||||||||
| 227 | The full text of the license can be found in the | ||||||||||
| 228 | LICENSE file included with this module. | ||||||||||
| 229 | |||||||||||
| 230 | =cut | ||||||||||