blib/lib/Apache/Earlybird.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 38 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 176 CC 20 SCAR 56.4
CC
20
Cov
0%
handler
420.0
BEGIN
2.0
BEGIN
2.0
CRAP 280.7
1 package Apache::Earlybird;
2 use strict;
3  
4 # © 2001 Ronald Florence <ron@18james.com>
5 # last modified, 23 Dec 2001
6  
7 # The following line is local; remove or modify as
8 # needed for your Apache/mod_perl installation.
9 use lib qw(/usr/apache/perl5/5.005/sun4-solaris);
10  
11 use vars qw($VERSION);
12 use Apache::Constants qw(OK DECLINED FORBIDDEN);
13 use Mail::Sendmail;
14 use Net::DNS;
15 use Cache::FileCache;
16 use Time::Zone;
17 use HTTP::Request::Common;
18 use LWP::UserAgent;
19  
20 # ------------------------------------------------------------
21 $VERSION = 1.03;
22  
23 # URLs to recommend in notifications.
24 my %security_url = (
25 Nimda => 'http://www.microsoft.com/technet/treeview/default.asp?url=/technet/security/topics/Nimda.asp',
26 CodeRed => 'http://www.microsoft.com/technet/treeview/default.asp?url=/technet/security/topics/codealrt.asp'
27 );
28 # address for email to SecurityFocus.com
29 my $security_focus_address = 'aris-report@securityfocus.com';
30  
31 # base URL for arin whois queries
32 my $arin_base = 'http://www.arin.net/cgi-bin/whois.pl?queryinput=';
33  
34 # optional BCC
35 my $bcc_address = '';
36  
37 # default notification interval (24 hours)
38 my %cache_options = ('default_expires_in' => 86400 );
39  
40 # $debug = 0 for errors only, 1 for logging email, 2 for max debugging.
41 my $debug = 0;
42  
43 # $use_mx = 0 to use the SOA address only, 1 for MX addresses too.
44 # my $use_mx = 0;
45  
46 # end of configuration
47 # ------------------------------------------------------------
48  
49 sub handler
50 {
51 my $worm;
52 my $r = shift;
53 my $remote_ip = $r->connection->remote_ip;
54 my $uri = $r->uri;
55 if ($uri =~ /default\.ida/) { $worm = "CodeRed"; }
56 elsif ($uri =~ /\.exe/) { $worm = "Nimda"; }
57 else
58 {
59 $r->warn("Earlybird: unrecognized attack from [$remote_ip].");
60 return DECLINED;
61 }
62 $cache_options{'namespace'} = $worm;
63 my $file_cache = new Cache::FileCache(\%cache_options);
64 unless ($file_cache)
65 {
66 $r->log_error("$worm: Could not instantiate FileCache.");
67 return DECLINED;
68 }
69  
70 my $last_visited = $file_cache->get($remote_ip);
71 if ($last_visited)
72 {
73 $r->warn("$worm: Found cached IP [$remote_ip].") if ($debug > 1);
74 return FORBIDDEN;
75 }
76 $file_cache->purge;
77  
78 # Send e-mail to SecurityFocus.com
79 my $server_name = $r->server->server_hostname;
80 my $from_address = $r->server->server_admin;
81 my $now = scalar localtime;
82 my $time_zone_name = uc tz_name;
83 my $sf_message = "$remote_ip\t$now $time_zone_name
84  
85 --
86 Apache::Earlybird-$VERSION (mod_perl) on $server_name";
87  
88 my %sf_mail = (
89 To => $security_focus_address,
90 BCC => $bcc_address,
91 From => $from_address,
92 Subject => "$worm infection on '$remote_ip': Automatic report",
93 Message => $sf_message
94 );
95 if (sendmail %sf_mail)
96 {
97 $file_cache->set($remote_ip, 1);
98 $r->warn("$worm: sent e-mail to SecurityFocus about '$remote_ip'")
99 if ($debug);
100 }
101 else
102 {
103 $file_cache->set($remote_ip, 0);
104 $r->warn("$worm: sendmail returned: $Mail::Sendmail::error");
105 return DECLINED;
106 }
107  
108 my $admin_addr;
109 my $remote_hostname;
110  
111 # Get a hostname and an email address from the SOA of the domain.
112 my $res = new Net::DNS::Resolver;
113 my $query = $res->search($remote_ip);
114 if ($query)
115 {
116 foreach my $rr ($query->answer)
117 {
118 next unless $rr->type eq "PTR";
119 $remote_hostname = $rr->rdatastr;
120 }
121 chop $remote_hostname;
122 }
123 else
124 {
125 my $dns_error = $res->errorstring;
126 $r->warn("$worm: PTR lookup on [$remote_ip] failed: $dns_error")
127 if ($debug > 1);
128 }
129 if ($remote_hostname)
130 {
131 my @zone = split /\./, $remote_hostname;
132 my $SOA_query = 0;
133 while (!$SOA_query)
134 {
135 $SOA_query = $res->query(join('.', @zone), "SOA");
136 last if !shift @zone;
137 }
138 if ($SOA_query)
139 {
140 my $addr = ($SOA_query->answer)[0]->rname;
141 $addr =~ s/\./@/;
142 $admin_addr = $addr . ', ' . do { $addr =~ s/^[^@]+/abuse/; $addr };
143 }
144 # if ($use_mx)
145 # {
146 # my @mx;
147 # my @hostname_components = split /\./, $remote_hostname;
148 # while (!@mx)
149 # {
150 # my $host_for_mx_lookup = join '.', @hostname_components;
151 # @mx = mx($res, $host_for_mx_lookup);
152 # last if !shift @hostname_components;
153 # }
154 # if (!@mx)
155 # {
156 # my $dns_err = $res->errorstring;
157 # $r->warn("$worm: no MX record for '$remote_hostname' [$dns_err].")
158 # if ($debug > 1);
159 # }
160 # my $mx_host = $mx[0]->exchange;
161 # my @webmasters = ('webmaster', 'postmaster', 'abuse');
162 # $admin_addr .= ', ' . join ', ', map("$_\@$mx_host", @webmasters);
163 # }
164 }
165  
166 # If we cannot resolve the $remote_ip or the SOA address.
167 if (!$admin_addr)
168 {
169 $remote_hostname = $remote_ip;
170 my $ua = LWP::UserAgent->new;
171 $ua->agent("Apache::Earlybird/$VERSION");
172 $ua->env_proxy;
173 my $resp = $ua->request(GET $arin_base . $remote_ip);
174 my @arin = split '\n', $resp->content;
175 my @noc_urls;
176 my @addr;
177 for (@arin)
178 {
179 push @noc_urls, /queryinput=(\w*[A-Za-z][^"]+)/;
180 push @addr, /\s+([\w.-]+@[\w.-]+)/;
181 }
182 if (!@addr)
183 {
184 for (@noc_urls)
185 {
186 $resp = $ua->request(GET $arin_base . $_);
187 @arin = split '\n', $resp->content;
188 for (@arin) { push @addr, /\s+([\w.-]+@[\w.-]+)/; }
189 }
190 }
191 if (!@addr)
192 {
193 $r->warn("$worm: arin whois lookup failed for $remote_ip")
194 if ($debug > 1);
195 return FORBIDDEN;
196 }
197 $admin_addr = join ', ', @addr;
198 $admin_addr .= ', ' . join ', ', map { s/^[^@]+/abuse/; $_ } @addr;
199 }
200  
201 my $request = $r->the_request;
202 my $port = $r->get_server_port;
203 my $outgoing_message = <<END;
204 The Microsoft IIS server at
205  
206 $remote_hostname [$remote_ip]
207  
208 appears to be infected with the $worm worm. It attempted
209 to spread to our Apache server at $server_name port $port
210 on $now $time_zone_name with the request:
211  
212 $request
213  
214 Please investigate and apply the appropriate patch from
215 $security_url{$worm}
216  
217 --
218 Apache::Earlybird-$VERSION (mod_perl) on $server_name
219 END
220  
221 my %mail = (
222 To => $admin_addr,
223 BCC => $bcc_address,
224 From => $from_address,
225 Subject => "$worm infection on '$remote_hostname': Automatic report",
226 Message => $outgoing_message
227 );
228 if (sendmail %mail)
229 {
230 $r->warn("$worm: sent email to '$admin_addr'") if ($debug);
231 return FORBIDDEN;
232 }
233 else
234 {
235 $r->warn("$worm: sendmail returned: $Mail::Sendmail::error");
236 return DECLINED;
237 }
238 }
239 # All modules must return a true value
240 1;
241  
242 __END__