0

I am trying to implement Google Safe Browsing API into my python script but cannot get it to work properly. Code shown below

import urllib2
key = 'mykey'
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey={key}&appver=1.0&pver=3.0&url={url}"

def is_safe(key, url):
    response = urllib2.urlopen(url).read().decode("utf8")
    return reponse != 'malware'

print(is_safe(key, 'http://google.com')) #This should return True
print(is_safe(key, 'http://steam.com.co.in')) # This should return False

When I ran the code, it returned True for both queries which shouldn't because the second URL is definitely malware.

l3echod
  • 25
  • 5

1 Answers1

0

Try this code if you are using python3.

from urllib.request import urlopen
key = "mykey"
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey={key}&appver=1.0&pver=3.0&url={url}"

def is_safe(key, url):
    response = urlopen(URL.format(key=key, url=url))
    return response.read().decode("utf8") != 'malware'

print(is_safe(key, "http://www.gumblar.cn/599")) #This should return False

The mistake you are making is passing url to urlopen instead of URL.Also you are not using using .format to pass url and key to the URL string for python 2.7

import urllib2
key = "mykey"
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey={key}&appver=1.0&pver=3.0&url={url}"

def is_safe(key, url):
    response = urllib2.urlopen(URL.format(key=key, url=url))
    return response.read().decode("utf8") != 'malware'

print(is_safe(key, "http://www.gumblar.cn/599")) 
avinash pandey
  • 1,321
  • 2
  • 11
  • 15
  • the code gave me an error. return response.read().decode("utf8") ) != 'malware' SyntaxError: E0L while scanning string literal Does the code work with python 2.7? – l3echod Apr 01 '15 at 00:50
  • The error now changed to urllib2.HTTPError: HTTP Error 403: Forbidden.. any clue? Thank you – l3echod Apr 01 '15 at 00:56
  • what key you are using from google console or one from this link http://code.google.com/apis/safebrowsing/key_signup.html. Also did you enable safe browsing api in your console.The error is because it is not able to find the url – avinash pandey Apr 01 '15 at 01:04
  • I used the one from Google Developers Console (Enabled) where you have to put IP address in. Also, I have just tried one from the link but still did not work. Note that I ran the code on VM not host computer where I have all Google Developers Console set. Is that the problem? – l3echod Apr 01 '15 at 01:09
  • I am not sure about that but from this link it is working fine for me – avinash pandey Apr 01 '15 at 01:11
  • The problem is solved, I have to generate the key on the VM rather than from my host machine. Thank you – l3echod Apr 01 '15 at 01:15
  • one more thing, I have tried to put some valid phishing websites from phishtank database but the google safe browsing returned "False" to all of them. Any reasons why it did this? – l3echod Apr 01 '15 at 01:19
  • the function returns false when the site is either malware or fishing and true otherwise – avinash pandey Apr 01 '15 at 01:22
  • Yes that is what I expected but it returned "False" to all of valid phishing websites reported on phishtank. I sightly changed the code to == 'malware', so it is the other way round. – l3echod Apr 01 '15 at 01:25
  • it did behave weird for some of the cases even on my machine – avinash pandey Apr 01 '15 at 01:29
  • no i even tried it on browser... it is able to catch malware sites but i haven't tried the phishtank pages post the results along with the page – avinash pandey Apr 01 '15 at 01:49
  • It returned "False" to all valid phishing websites in the database. It only worked on your url (http://www.gumblar.cn/599). Have you found any working url?? – l3echod Apr 01 '15 at 01:53
  • I have analysed the python wrapper code for this api there in the test cases I found this example.It appears the api is not working correctly as since for non malware pages response of 'Ok' is not received.Take a look at this code https://github.com/juliensobrier/google-safe-browsing-lookup-python/blob/master/safebrowsinglookup.py – avinash pandey Apr 01 '15 at 02:02