-
Notifications
You must be signed in to change notification settings - Fork 12
/
safebrowsinglookup.py
executable file
·177 lines (130 loc) · 6.49 KB
/
safebrowsinglookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding: utf-8 -*-
# Copyright 2015 Julien Sobrier
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# modified by Alexander Bikadorov, 2012 (abiku@cs.tu-berlin.de)
""" Version 0.2.0
Google Safe Browsing Lookup library for Python.
If you need to check less than 10,000 URLs a day against the Google Safe Browsing v2 API (http://code.google.com/apis/safebrowsing/), you can use the Lookup API (http://code.google.com/apis/safebrowsing/lookup_guide.html) as a lighter alternative to the more complex API (http://code.google.com/apis/safebrowsing/developers_guide_v2.html).
You need to get an API key from Google at http://code.google.com/apis/safebrowsing/key_signup.html """
import urllib
import urllib2
import re
import httplib
class SafebrowsinglookupClient(object):
def __init__(self, key='', debug=0, error=0):
""" Create a new client. You must pass your Google API key (http://code.google.com/apis/safebrowsing/key_signup.html).
Arguments:
key: API key.
debug: Set to 1 to print debug & error output to the standard output. 0 (disabled) by default.
error: Set to 1 to print error output to the standard output. 0 (disabled) by default.
"""
self.key = key
self.debug = debug
self.error = error
self.last_error = ''
self.version = '0.2'
self.api_version = '3.1'
if self.key == '':
raise ValueError("Missing API key")
def lookup(self, *urls):
""" Lookup a list of URLs against the Google Safe Browsing v2 lists.
Returns a hash <url>: <Gooogle match>. The possible values for <Gooogle match> are: "ok" (no match), "malware", "phishing", "malware,phishing" (match both lists) and "error".
Arguments:
urls: List of URLs to lookup. The Lookup API allows only 10,000 URL checks a day. If you need more, use the official Google Safe Browsing v2 API implementation (http://code.google.com/p/google-safe-browsing/downloads/list). Each requests must contain 500 URLs at most. The lookup() method will split the list of URLS in blocks of 500 URLs if needed.
"""
results = {}
count = 0
while count * 500 < len(urls):
inputs = urls[count * 500 : (count + 1) * 500]
body = len(inputs)
for url in inputs:
body = str(body) + "\n" + self.__canonical(str(url))
self.__debug("BODY:\n" + body + "\n\n")
url = 'https://sb-ssl.google.com/safebrowsing/api/lookup?client=%s&key=%s&appver=%s&pver=%s' % ('python', self.key, self.version, self.api_version)
self.__debug("URL: %s" % (url))
response = ''
try:
response = urllib2.urlopen(url, body)
except Exception, e:
if hasattr(e, 'code') and e.code == httplib.NO_CONTENT: # 204
self.__debug("No match\n")
results.update( self.__ok(inputs) )
elif hasattr(e, 'code') and e.code == httplib.BAD_REQUEST: # 400
self.__error("Invalid request")
results.update( self.__errors(inputs) )
elif hasattr(e, 'code') and e.code == httplib.UNAUTHORIZED: # 401
self.__error("Invalid API key")
results.update( self.__errors(inputs) )
elif hasattr(e, 'code') and e.code == httplib.FORBIDDEN: # 403 (should be 401)
self.__error("Invalid API key")
results.update( self.__errors(inputs) )
elif hasattr(e, 'code') and e.code == httplib.SERVICE_UNAVAILABLE: # 503
self.__error("Server error, client may have sent too many requests")
results.update( self.__errors(inputs) )
else:
self.__error("Unexpected server response")
self.__debug(e)
results.update( self.__errors(inputs) )
else:
response_read = response.read()
if not response_read:
self.__debug("No match\n")
results.update( self.__ok(inputs) )
else:
self.__debug("At least 1 match\n")
results.update( self.__parse(response_read.strip(), inputs) )
count = count + 1
return results
# Private methods
# Not much is actually done, full URL canonicalization is not required with the Lookup library according to the API documentation
def __canonical(self, url=''):
# remove leading/ending white spaces
url = url.strip()
# Remove any embedded tabs and CR/LF characters which aren't escaped.
url = url.replace('\t', '').replace('\r', '').replace('\n', '')
# make sure whe have a scheme
scheme = re.compile("https?\:\/\/", re.IGNORECASE)
if scheme.match(url) is None:
url = "http://" + url
return url
def __parse(self, response, urls):
lines = response.splitlines()
if (len(urls) != len(lines)):
self.__error("Number of URLs in the response does not match the number of URLs in the request");
self.__debug( str(len(urls)) + " / " + str(len(lines)) )
self.__debug(response);
return self.__errors(urls);
results = { }
for i in range(0, len(lines)):
results.update({urls[i] : lines[i]})
return results
def __errors(self, urls):
results = {}
for url in urls:
results.update({url: 'error'})
return results
def __ok(self, urls):
results = {}
for url in urls:
results.update({url: 'ok'})
return results
def __debug(self, message=''):
if self.debug == 1:
print message
def __error(self, message=''):
if self.debug == 1 or self.error == 1:
print message + "\n"
self.last_error = message