-
Notifications
You must be signed in to change notification settings - Fork 43
/
google.py
79 lines (63 loc) · 2.2 KB
/
google.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
#!/usr/bin/env python
# @Author: lnxg33k
# @Date: 2014-10-08 03:47:26
# @Last Modified by: lnxg33k
# @Last Modified time: 2014-10-12 20:35:28
"""
In order to interact with the Safe Browsing lookup server,
you need an API key to authenticate as an API user.
You will pass this key as a CGI parameter
1. Create a project in the Google Developers Console.
2. In your project, click on APIs & Auth > APIs.
3. Scroll down to the Safe Browsing API and turn it ON.
4. Click on APIs & Auth > Credentials.
5. Click on Create new key and create a browser or server key.
"""
from urllib2 import Request, urlopen
from sys import argv
from json import dumps
key = "--- INSERT YOUR KEY HERE ----"
url = "https://sb-ssl.google.com/safebrowsing/api"
url += "/lookup?client=lnxg33k_client&key=%s&appver=1.5.2&pver=3.1" % key
def chunks(l, n=500):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def lookUp(domains=[]):
result = {}
chunked_domains = chunks(domains)
for chunk in chunked_domains:
data = "%s\n%s" % (len(chunk), "\n".join(chunk))
try:
req = Request(url, data)
r = urlopen(req)
if r.code == 200:
result.update(dict(zip(
chunk,
map(lambda n: {'google': n}, map(str.strip, r.readlines())))
))
except:
pass
return result
def main():
if len(argv) != 3:
exit("[+] Usage: %s <input> <output>" % argv[0])
list_of_domains = argv[1]
output = argv[2]
print "[-] Loading domains from %s ..." % list_of_domains
with open(list_of_domains, 'r') as f:
domains = map(str.strip, f.readlines())
domains = list(set(domains))
print "[-] Got %s unique domains from the list" % len(domains)
print (
"\n[-] Sending domains to Google lookup API" +
" (will take some time depending on the size of the lisy)..."
)
result = lookUp(domains)
print "\n[-] Dumping data to %s ..." % output
with open(output, 'w') as output_file:
output_file.write(dumps(result, indent=2))
print "[-] Successfully dumped data !!"
if __name__ == '__main__':
main()