| 1 | package Net::Google::SafeBrowsing2::Lookup; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use URI; | ||
| 9 | use Text::Trim; | ||
| 10 | |||
| 11 | our $VERSION = '0.2'; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Net::Google::SafeBrowsing2::Lookup - Perl extension for the Google Safe Browsing v2 Lookup API. | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use Net::Google::SafeBrowsing2::Lookup; | ||
| 20 | |||
| 21 | my $gsb = Net::Google::SafeBrowsing2::Lookup->new( | ||
| 22 | key => "my key", | ||
| 23 | ); | ||
| 24 | |||
| 25 | my %match = $gsb->lookup(url => 'http://www.gumblar.cn/'); | ||
| 26 | |||
| 27 | if ($match{'http://www.gumblar.cn/'} eq 'malware') { | ||
| 28 | print "http://www.gumblar.cn/ is flagged as a dangerous site\n"; | ||
| 29 | } | ||
| 30 | |||
| 31 | my %matches = $gsb->lookup(urls => ['http://www.gumblar.cn/', 'http://flahupdate.co.cc']); | ||
| 32 | foreach my $url (keys %matches) { | ||
| 33 | print $url, " is ", $matches{$url}, "\n"; | ||
| 34 | } | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | Net::Google::SafeBrowsing2::Lookup implements the Google Safe Browsing v2 Lookup API. See the API documentation at L<http://code.google.com/apis/safebrowsing/lookup_guide.html>. | ||
| 39 | |||
| 40 | If you need to check more than 10,000 URLs a day, you need to use L<Net::Google::SafeBrowsing2>. | ||
| 41 | |||
| 42 | The source code is available on github at L<https://github.com/juliensobrier/Net-Google-SafeBrowsing2>. | ||
| 43 | |||
| 44 | =head1 CONSTRUCTOR | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =back | ||
| 49 | |||
| 50 | =head2 new() | ||
| 51 | |||
| 52 | Create a Net::Google::SafeBrowsing2::Lookup object | ||
| 53 | |||
| 54 | my $gsb = Net::Google::SafeBrowsing2::Lookup->new( | ||
| 55 | key => "my key", | ||
| 56 | debug => 0, | ||
| 57 | ); | ||
| 58 | |||
| 59 | Arguments | ||
| 60 | |||
| 61 | =over 4 | ||
| 62 | |||
| 63 | =item key | ||
| 64 | |||
| 65 | Required. Your Google Safe Browsing API key | ||
| 66 | |||
| 67 | =item debug | ||
| 68 | |||
| 69 | Optional. Set to 1 to enable debugging. 0 (disabled) by default. | ||
| 70 | |||
| 71 | The debug output maybe quite large and can slow down significantly the update and lookup functions. | ||
| 72 | |||
| 73 | =item errors | ||
| 74 | |||
| 75 | Optional. Set to 1 to show errors to STDOUT. 0 (disabled by default). | ||
| 76 | |||
| 77 | =item version | ||
| 78 | |||
| 79 | Optional. Google Safe Browsing version. 3.0 by default | ||
| 80 | |||
| 81 | =item delay | ||
| 82 | |||
| 83 | Optional. Delay, in seconds, between 2 requests to the Google server. See the C<lookup> function for more details. 0 (no delay) by default | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub new { | ||
| 90 | my ($class, %args) = @_; | ||
| 91 | |||
| 92 | my $self = { # default arguments | ||
| 93 | key => '', | ||
| 94 | version => '3.0', | ||
| 95 | debug => 0, | ||
| 96 | delay => 0, | ||
| 97 | # errors => 0, | ||
| 98 | # last_error => '', | ||
| 99 | |||
| 100 | %args, | ||
| 101 | }; | ||
| 102 | |||
| 103 | bless $self, $class or croak "Can't bless $class: $!"; | ||
| 104 | return $self; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | =head1 PUBLIC FUNCTIONS | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =back | ||
| 113 | |||
| 114 | =head2 lookup() | ||
| 115 | |||
| 116 | Lookup a list URLs against the Google Safe Browsing v2 lists. | ||
| 117 | |||
| 118 | my %match = $gsb->lookup(url => 'http://www.gumblar.cn'); | ||
| 119 | |||
| 120 | Returns a hash C<url> => C<Google match>. The possible list of values for C<Google match> are: "ok" (no match), "malware", "phishing", "malware,phishing" (match both lists) and "error". | ||
| 121 | |||
| 122 | Arguments | ||
| 123 | |||
| 124 | =over 4 | ||
| 125 | |||
| 126 | =item url | ||
| 127 | |||
| 128 | Optional. Single URL to lookup. | ||
| 129 | |||
| 130 | =item urls | ||
| 131 | |||
| 132 | Optional. List of URLs to lookup. | ||
| 133 | |||
| 134 | The Lookup API allows only 10,000 URL checks a day. if you need more, use the L<Net::Google::SafeBrowsing2> library. | ||
| 135 | |||
| 136 | Each requests must contain 500 URLs at most. The lookup() method will split the list of URLS in blocks of 500 URLs if needed. | ||
| 137 | |||
| 138 | =item delay | ||
| 139 | |||
| 140 | Optional. If more than 500 URLs are checked, wait C<delay> seconds between consecutive requests to avoid rate limiting by Google. | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub lookup { | ||
| 147 | my ($self, %args) = @_; | ||
| 148 | my $url = $args{url} || ''; | ||
| 149 | my @inputs = @{ $args{urls} || []}; | ||
| 150 | my $delay = $args{delay} || $self->{delay} || 0; | ||
| 151 | |||
| 152 | |||
| 153 | if ($url ne '') { | ||
| 154 | push(@inputs, $url); | ||
| 155 | } | ||
| 156 | |||
| 157 | # Max is 500 URLs per request | ||
| 158 | my %results = (); | ||
| 159 | |||
| 160 | my $count = 0; | ||
| 161 | while (scalar @inputs > 0) { | ||
| 162 | my @urls = splice(@inputs, 0, 500); | ||
| 163 | |||
| 164 | my $body = scalar(@urls); | ||
| 165 | foreach my $input (@urls) { | ||
| 166 | my $canonical = $self->canonical_uri($input); | ||
| 167 | $body .= "\n$canonical"; | ||
| 168 | $self->debug("$input => $canonical\n"); | ||
| 169 | } | ||
| 170 | $self->debug("BODY:\n$body\n\n"); | ||
| 171 | |||
| 172 | my $url = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=perl&apikey=" . $self->{key} . "&appver=$VERSION&pver=" . $self->{version}; | ||
| 173 | sleep $delay if ($delay > 0 && $count > 0); | ||
| 174 | my $res = $self->ua->post($url, Content => $body); | ||
| 175 | |||
| 176 | if ($res->code == 400) { | ||
| 177 | $self->error("Invalid request"); | ||
| 178 | %results = ( %results, $self->errors(@urls) ); | ||
| 179 | } | ||
| 180 | elsif ($res->code == 401) { | ||
| 181 | $self->error("Invalid API key"); | ||
| 182 | %results = ( %results, $self->errors(@urls) ); | ||
| 183 | } | ||
| 184 | elsif ($res->code == 503) { | ||
| 185 | $self->error("Server error, client may have sent too many requests"); | ||
| 186 | %results = ( %results, $self->errors(@urls) ); | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | %results = ( %results, $self->parse(response => $res, urls => [@urls]) ); | ||
| 190 | } | ||
| 191 | |||
| 192 | $count++; | ||
| 193 | } | ||
| 194 | |||
| 195 | return %results; | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | sub parse { | ||
| 200 | my ($self, %args) = @_; | ||
| 201 | my $response = $args{response} || croak "Missing response\n";; | ||
| 202 | my @urls = @{ $args{urls} || []}; | ||
| 203 | |||
| 204 | |||
| 205 | if ($response->code == 204) { | ||
| 206 | $self->debug("No match\n"); | ||
| 207 | return map { $_ => 'ok' } @urls; | ||
| 208 | } | ||
| 209 | |||
| 210 | my %results = (); | ||
| 211 | my @lines = split /\n/, $response->content; | ||
| 212 | |||
| 213 | if (scalar @urls != scalar @lines) { | ||
| 214 | $self->error("Number of URLs in the reponse does not match the number of URLs in the request"); | ||
| 215 | $self->error( scalar(@urls) . "/" . scalar(@lines)); | ||
| 216 | $self->error($response->content); | ||
| 217 | return $self->errors(@urls); | ||
| 218 | } | ||
| 219 | |||
| 220 | for(my $i = 0; $i < scalar(@urls); $i++) { | ||
| 221 | $results{$urls[$i]} = $lines[$i]; | ||
| 222 | } | ||
| 223 | |||
| 224 | return %results; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub errors { | ||
| 228 | my ($self, @urls) = @_; | ||
| 229 | |||
| 230 | return map { $_ => 'error' } @urls; | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | sub ua { | ||
| 235 | my ($self, %args) = @_; | ||
| 236 | |||
| 237 | if (! exists $self->{ua}) { | ||
| 238 | my $ua = LWP::UserAgent->new; | ||
| 239 | $ua->timeout(60); | ||
| 240 | |||
| 241 | $self->{ua} = $ua; | ||
| 242 | } | ||
| 243 | |||
| 244 | return $self->{ua}; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | sub debug { | ||
| 249 | my ($self, $message) = @_; | ||
| 250 | |||
| 251 | print $message if ($self->{debug} > 0); | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub error { | ||
| 256 | my ($self, $message) = @_; | ||
| 257 | |||
| 258 | print "ERROR - ", $message, "\n" if ($self->{debug} > 0 || $self->{errors} > 0); | ||
| 259 | $self->{last_error} = $message; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub canonical_uri { | ||
| 263 | my ($self, $url) = @_; | ||
| 264 | |||
| 265 | $url = trim $url; | ||
| 266 | |||
| 267 | # Special case for \t \r \n | ||
| 268 | while ($url =~ s/^([^?]+)[\r\t\n]/$1/sgi) { } | ||
| 269 | |||
| 270 | my $uri = URI->new($url)->canonical; # does not deal with directory traversing | ||
| 271 | |||
| 272 | # $self->debug("0. $url => " . $uri->as_string . "\n"); | ||
| 273 | |||
| 274 | |||
| 275 | if (! $uri->scheme() || $uri->scheme() eq '') { | ||
| 276 | $uri = URI->new("http://$url")->canonical; | ||
| 277 | } | ||
| 278 | |||
| 279 | $uri->fragment(''); | ||
| 280 | |||
| 281 | my $escape = $uri->as_string; | ||
| 282 | |||
| 283 | # Reduce double // to single / in path | ||
| 284 | while ($escape =~ s/^([a-z]+:\/\/[^?]+)\/\//$1\//sgi) { } | ||
| 285 | |||
| 286 | |||
| 287 | # Remove empty fragment | ||
| 288 | $escape =~ s/#$//; | ||
| 289 | |||
| 290 | # canonial does not handle ../ | ||
| 291 | # $self->debug("\t$escape\n"); | ||
| 292 | while($escape =~ s/([^\/])\/([^\/]+)\/\.\.([\/?].*)?$/$1$3/gi) { } | ||
| 293 | |||
| 294 | # May have removed ending / | ||
| 295 | # $self->debug("\t$escape\n"); | ||
| 296 | $escape .= "/" if ($escape =~ /^[a-z]+:\/\/[^\/\?]+$/); | ||
| 297 | $escape =~ s/^([a-z]+:\/\/[^\/]+)(\?.*)$/$1\/$2/gi; | ||
| 298 | # $self->debug("\t$escape\n"); | ||
| 299 | |||
| 300 | # other weird case if domain = digits only, try to translte it to IP address | ||
| 301 | if ((my $domain = URI->new($escape)->host) =~/^\d+$/) { | ||
| 302 | my $ip = num2ip($domain); | ||
| 303 | |||
| 304 | if (validaddr($ip)) { | ||
| 305 | $uri = URI->new($escape); | ||
| 306 | $uri->host($ip); | ||
| 307 | |||
| 308 | $escape = $uri->as_string; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | # $self->debug("1. $url => $escape\n"); | ||
| 313 | |||
| 314 | # Try to escape the path again | ||
| 315 | $url = $escape; | ||
| 316 | while (($escape = URI::Escape::uri_unescape($url)) ne $escape) { # wrong for %23 -> # | ||
| 317 | $url = $escape; | ||
| 318 | } | ||
| 319 | # while (($escape = URI->new($url)->canonical->as_string) ne $escape) { # breask more unit tests than previous | ||
| 320 | # $url = $escape; | ||
| 321 | # } | ||
| 322 | |||
| 323 | # Fix for %23 -> # | ||
| 324 | while($escape =~ s/#/%23/sgi) { } | ||
| 325 | |||
| 326 | # $self->debug("2. $url => $escape\n"); | ||
| 327 | |||
| 328 | # Fix over escaping | ||
| 329 | while($escape =~ s/^([^?]+)%%(%.*)?$/$1%25%25$2/sgi) { } | ||
| 330 | |||
| 331 | # URI has issues with % in domains, it gets the host wrong | ||
| 332 | |||
| 333 | # 1. fix the host | ||
| 334 | # $self->debug("Domain: " . URI->new($escape)->host . "\n"); | ||
| 335 | my $exception = 0; | ||
| 336 | while ($escape =~ /^[a-z]+:\/\/[^\/]*([^a-z0-9%_.-\/:])[^\/]*(\/.*)$/) { | ||
| 337 | my $source = $1; | ||
| 338 | my $target = sprintf("%02x", ord($source)); | ||
| 339 | |||
| 340 | $escape =~ s/^([a-z]+:\/\/[^\/]*)\Q$source\E/$1%\Q$target\E/; | ||
| 341 | |||
| 342 | $exception = 1; | ||
| 343 | } | ||
| 344 | |||
| 345 | # 2. need to parse the path again | ||
| 346 | if ($exception && $escape =~ /^[a-z]+:\/\/[^\/]+\/(.+)/) { | ||
| 347 | my $source = $1; | ||
| 348 | my $target = URI::Escape::uri_unescape($source); | ||
| 349 | |||
| 350 | # print "Source: $source\n"; | ||
| 351 | while ($target ne URI::Escape::uri_unescape($target)) { | ||
| 352 | $target = URI::Escape::uri_unescape($target); | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | $escape =~ s/\/\Q$source\E/\/$target/; | ||
| 357 | |||
| 358 | while ($escape =~ s/#/%23/sgi) { } # fragement has been removed earlier | ||
| 359 | while ($escape =~ s/^([a-z]+:\/\/[^\/]+\/.*)%5e/$1\&/sgi) { } # not in the host name | ||
| 360 | # while ($escape =~ s/%5e/&/sgi) { } | ||
| 361 | |||
| 362 | while ($escape =~ s/%([^0-9a-f]|.[^0-9a-f])/%25$1/sgi) { } | ||
| 363 | } | ||
| 364 | |||
| 365 | # $self->debug("$url => $escape\n"); | ||
| 366 | # $self->debug(URI->new($escape)->as_string . "\n"); | ||
| 367 | |||
| 368 | return URI->new($escape); | ||
| 369 | } | ||
| 370 | |||
| 371 | |||
| 372 | =head1 CHANGELOG | ||
| 373 | |||
| 374 | =over 4 | ||
| 375 | |||
| 376 | =item 0.2 | ||
| 377 | |||
| 378 | Documentation update. | ||
| 379 | |||
| 380 | =back | ||
| 381 | |||
| 382 | =head1 SEE ALSO | ||
| 383 | |||
| 384 | See L<Net::Google::SafeBrowsing2> for the implementation of Google Safe Browsing v2 API. | ||
| 385 | |||
| 386 | =head1 AUTHOR | ||
| 387 | |||
| 388 | Julien Sobrier, E<lt>jsobrier@zscaler.comE<gt> or E<lt>julien@sobrier.netE<gt> | ||
| 389 | |||
| 390 | =head1 COPYRIGHT AND LICENSE | ||
| 391 | |||
| 392 | Copyright (C) 2012 by Julien Sobrier | ||
| 393 | |||
| 394 | This library is free software; you can redistribute it and/or modify | ||
| 395 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 396 | at your option, any later version of Perl 5 you may have available. | ||
| 397 | |||
| 398 | |||
| 399 | =cut | ||
| 400 | |||
| 401 | 1; |