Net::Google::SafeBrowsing2(3pm) | User Contributed Perl Documentation | Net::Google::SafeBrowsing2(3pm) |
Net::Google::SafeBrowsing2 - Perl extension for the Google Safe Browsing v2 API. (Google Safe Browsing v1 has been deprecated by Google.)
use Net::Google::SafeBrowsing2; use Net::Google::SafeBrowsing2::Sqlite; my $storage = Net::Google::SafeBrowsing2::Sqlite->new(file => 'google-v2.db'); my $gsb = Net::Google::SafeBrowsing2->new( key => "my key", storage => $storage, ); $gsb->update(); my $match = $gsb->lookup(url => 'http://www.gumblar.cn/'); if ($match eq MALWARE) { print "http://www.gumblar.cn/ is flagged as a dangerous site\n"; } $storage->close();
Net::Google::SafeBrowsing2 implements the Google Safe Browsing v2 API.
The library passes most of the unit tests listed in the API documentation. See the documentation (<http://code.google.com/apis/safebrowsing/developers_guide_v2.html>) for more details about the failed tests.
The Google Safe Browsing database must be stored and managed locally. Net::Google::SafeBrowsing2::Sqlite uses Sqlite as the storage back-end, Net::Google::SafeBrowsing2::MySQL uses MySQL. Other storage mechanisms (databases, memory, etc.) can be added and used transparently with this module.
You may want to look at "Google Safe Browsing v2: Implementation Notes" (<http://www.zscaler.com/research/Google%20Safe%20Browsing%20v2%20API.pdf>), a collection of notes and real-world numbers about the API. This is intended for people who want to learn more about the API, whether as a user or to make their own implementation.
The source code is available on github at https://github.com/juliensobrier/Net-Google-SafeBrowsing2 <https://github.com/juliensobrier/Net-Google-SafeBrowsing2>.
If you do not need to inspect more than 10,000 URLs a day, you can use Net::Google::SafeBrowsing2::Lookup with the Google Safe Browsing v2 Lookup API which does not require to store and maintain a local database.
IMPORTANT: If you start with an empty database, you will need to perform several updates to retrieve all the Google Safe Browsing information. This may require up to 24 hours. This is a limitation of the Google API, not of this module. See "Google Safe Browsing v2: Implementation Notes" at <http://www.zscaler.com/research/Google%20Safe%20Browsing%20v2%20API.pdf>.
Several constants are exported by this module:
Create a Net::Google::SafeBrowsing2 object
my $gsb = Net::Google::SafeBrowsing2->new( key => "my key", storage => Net::Google::SafeBrowsing2::Sqlite->new(file => 'google-v2.db'), debug => 0, mac => 0, list => MALWARE, );
Arguments
The debug output maybe quite large and can slow down significantly the update and lookup functions.
Perform a database update.
$gsb->update();
Return the status of the update (see the list of constants above): INTERNAL_ERROR, SERVER_ERROR, NO_UPDATE, NO_DATA or SUCCESSFUL
This function can handle two lists at the same time. If one of the list should not be updated, it will automatically skip it and update the other one. It is faster to update two lists at once rather than doing them one by one.
NOTE: If you start with an empty database, you will need to perform several updates to retrieve all the Google Safe Browsing information. This may require up to 24 hours. This is a limitation of the Google API, not of this module. See "Google Safe Browsing v2: Implementation Notes" at <http://www.zscaler.com/research/Google%20Safe%20Browsing%20v2%20API.pdf>.
Arguments
Be careful if you set this option to 1 as too frequent updates might result in the blacklisting of your API key.
Import add and sub chunks from a file.
my $result = $gsb->import_chunks(list => MALWARE, file => 'malware.dat');
Return the status of the import: INTERNAL_ERROR or SUCCESSFUL.
This function should be used to initialize an empty back-end storage.
Arguments
Lookup a URL against the Google Safe Browsing database.
my $match = $gsb->lookup(url => 'http://www.gumblar.cn');
Returns the name of the list if there is any match, returns an empty string otherwise.
Arguments
Returns the name of all the Google Safe Browsing lists
my $@lists = $gsb->get_lists();
NOTE: this function is useless in practice because Google includes some lists which cannot be used by the Google Safe Browsing API, like lists used by the Google toolbar.
Get/Set the last error message.
print "Last error: ", $gsb->last_error(), "\n"; $gsb->last_error(''); # Reset last error
NOTE: the last error message might not come from the last call. Returns an empty string if no errors.
These functions are not intended to be used externally.
Lookup a host prefix.
Lookup a host prefix in the local database only.
Lookup a URL against the local Google Safe Browsing database URL. This should be used for debugging purpose only. See the lookup for normal use.
my $match = $gsb->local_lookup(url => 'http://www.gumblar.cn');
Returns the name of the list if there is any match, returns an empty string otherwise.
Arguments
Request the Message Authentication Code (MAC) keys
Request the Message Authentication Code (MAC) keys from Google.
Validate data against the MAC keys.
Handle server errors during a database update.
Lookup a host prefix and suffix in the whitelist (s chunks)
Create LWP::UserAgent to make HTTP requests to Google.
Parse data from a rediration (add asnd sub chunk information).
Parse s chunks information for a database update.
Parse a chunks information for a database update.
Transform hexadecimal strings to printable ASCII strings. Used mainly for debugging.
print $gsb->hex_to_ascii('hex value');
Transform ASCII strings to hexadecimal strings.
Print debug output.
Print error message.
Find all suffixes for a domain.
Find all canonical domains a domain.
Find all canonical paths for a URL.
Find all canonical URLs for a URL.
Create a canonical URI.
NOTE: URI cannot handle all the test cases provided by Google. This method is a hack to pass most of the test. A few tests are still failing. The proper way to handle URL canonicalization according to Google would be to create a new module to handle URLs. However, I believe most real-life cases are handled correctly by this function.
Return all possible full hashes for a URL.
Return a hash prefix. The size of the prefix is set to 4 bytes.
Request full full hashes for specific prefixes from Google.
Process the request for full hashes from Google.
Get the list of a chunks ranges for a list update.
Get the list of s chunks ranges for a list update.
Create a list of ranges (1-3, 5, 7-11) from a list of numbers.
Explode list of ranges (1-3, 5, 7-11) into a list of numbers (1,2,3,5,7,8,9,10,11).
Remove dependency on Switch (thanks to Curtis Jewel).
Fix value of FULL_HASH_TIME.
See Net::Google::SafeBrowsing2::Storage, Net::Google::SafeBrowsing2::Sqlite and Net::Google::SafeBrowsing2::MySQL for information on storing and managing the Google Safe Browsing database.
Google Safe Browsing v2 API: <http://code.google.com/apis/safebrowsing/developers_guide_v2.html>
Net::Google::SafeBrowsing (Google Safe Browsing v1) is deprecated by Google since 12/01/2011.
Julien Sobrier, <jsobrier@zscaler.com> or <julien@sobrier.net>
Copyright (C) 2012 by Julien Sobrier
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
2013-04-08 | perl v5.14.2 |