LoginSignup
1
1

More than 5 years have passed since last update.

Google Safe BrowsingのAPI(V4)にRubyでLookupする

Posted at

Google Safe Browsingってありまして。
APIがあるのでRubyからきゃつらのルールで不正と判断しているドメイン(または実行ファイル)をチェックしてみようとしました。

事前準備

  • rubygems google-api-client をインストール
  • APIKEYを環境変数にいれておく

Lookupのサンプルコード

google-api-clientってほとんど動的生成なので、普通に使い方をソースから見ようとするとサッパリわかりません。

動的に生成されたらこうなるよというコードがリポジトリに一緒に上がっているので、そちらをドキュメント代わりにチェックします。

safebrowsing_v4はこのへんですね。

Lookupする場合、find_threat_matchesメソッドにGoogle::Apis::SafebrowsingV4::FindThreatMatchesRequestのオブジェクトを渡すだけです。

safebrowsing_lookup.rb
require 'google/apis/safebrowsing_v4'
sb_service = Google::Apis::SafebrowsingV4::SafebrowsingService.new
sb_service.key = ENV['APIKEY']

# タイプの詳細は https://developers.google.com/safe-browsing/v4/reference/rest/ で
tr = Google::Apis::SafebrowsingV4::ThreatInfo.new(
  threat_types: ["MALWARE", "SOCIAL_ENGINEERING"],
  platform_types:    ["ANY_PLATFORM"],
  threat_entry_types: ["URL"],
  threat_entries: [
    {"url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/"}
  ]
)

# client_idはなるべく一意にしてねとのことですが、サンプルコードなので適当に
opts = Google::Apis::SafebrowsingV4::FindThreatMatchesRequest.new(
  client: Google::Apis::SafebrowsingV4::ClientInfo.new(
    client_id: "google-api-client",
    client_version: "0.19.8"
  ),
  threat_info: tr
)

res = sb_service.find_threat_matches(opts)

puts res.to_json

レスポンスのmatchesに何かしら入っていれば引っかかったということです。
ちなみにthreat_entriesurlhttp://〜のようにスキーマを含めなくてもマッチします。

$ ruby safebrowsing_lookup.rb
{
  "matches": [
    {
      "cacheDuration": "300s",
      "platformType": "ANY_PLATFORM",
      "threat": {
        "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/"
      },
      "threatEntryType": "URL",
      "threatType": "MALWARE"
    }
  ]
}

余談

Google Safe Browsingの影響として身近なところでは、Let's Encryptでドメインの申請をする際、このAPIでマッチしたら所有確認の時点でお断りされるんですよ。

1
1
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
1
1