LoginSignup
0
0

More than 5 years have passed since last update.

Jupyter NotebookでGoogle Safe Browsing API

Posted at

Google Safe Browsing APIは特定のURLやバイナリが安全か否かをチェックするAPI。さっと調べるときに便利なようにJupyterから使えるようにしてみた。

普通のURLを入れると何も値が出てこないので「安全でないURL」を探したのと、ヒアドキュメントにURLを埋め込むあたりに苦労の跡が。

import pycurl, io, json

url = 'http://malware.testing.google.test/testing/malware/'
api_key = 'your_api_key'
data = '''
 {{
      "client": {{
      "clientId":      "your_client_name",
      "clientVersion": "0.1"
    }},
    "threatInfo": {{
      "threatTypes":      ["THREAT_TYPE_UNSPECIFIED","MALWARE", "SOCIAL_ENGINEERING","UNWANTED_SOFTWARE","POTENTIALLY_HARMFUL_APPLICATION"],
      "platformTypes":    ["ALL_PLATFORMS"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [
        {{"url": "{}"}}
      ]
    }}
 }}
'''.format(url)
buffer = io.BytesIO()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key='+api_key)
curl.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json']);
curl.setopt(pycurl.CUSTOMREQUEST, 'POST')
curl.setopt(pycurl.POSTFIELDS, data)
curl.setopt(pycurl.WRITEDATA, buffer)
#curl.setopt(pycurl.VERBOSE, True)
curl.perform()

http_code = curl.getinfo(pycurl.HTTP_CODE)
ret = buffer.getvalue()

print(http_code)
print(ret.decode('iso-8859-1'))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0