フィッシング防止ツール Google Safe Browsing API は、従来使われていた「version 3」は、2017年の初め頃(early 2017)に廃止予定となり、Google Safe Browsing Team は「version 4」への移行を推奨しています。Googleは「Google Safe Browsing」のGo言語製APIクライアントを提供していますので、CentOS6 環境にセットアップしてみます。(2017/12/30作成)
lenalindell20 / Pixabay
Google Online Security Blog: Evolving the Safe Browsing API
https://security.googleblog.com/2016/05/evolving-safe-browsing-api.html
要約:Google Safe Browsing Team は 2016年5月20日(米国時間)、「Google Online Security Blog: Evolving the Safe Browsing API」において、Safe Browsing APIの最新版となる「Safe Browsing API バージョン4」を公開したと伝えた。
このAPIは既存のSafe Browsing API バージョン3を置き換えるもので、従来のバージョン(バージョン2とバージョン3)は非推奨になり始めている。Google Safe Browsing Teamは、古いバージョンを使っている場合、可能な限り迅速にアップグレードすることを推奨している。
1.APIキーの取得
まずはAPIキーを取得します。
Google API Console にアクセスします。
https://console.developers.google.com/
1)プロジェクトの作成
新しいプロジェクトを作成します。
⇒すでに「プロジェクト」作成されている場合は、新設は必要ありません。
2)APIとサービスの有効化
[+]APIとサービスの有効化 ⇒クリック!
検索ボックスに「safe」と入力して Google Safe Browsing API を探して、APIを有効にします。 ⇒Google Safe Browsing API
3)APIキーの取得
認証情報を設定して、APIアクセスに必要な「APIキー」を取得します。
2.サーバー側)Go言語の導入
1)epelリポジトリの追加
$ sudo yum install epel-release
2)golangのインストール
$ sudo yum install golang $ go version go version go1.9.2 linux/amd64
3).bash_profile に環境変数追加
$ vi ~/.bash_profile export GOPATH="$HOME/.go" export PATH="$PATH:$GOPATH/bin" $ source ~/.bash_profile
4)Goサンプルソース
vi hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
$ go run hello.go
hello, world
※ビルドして実行する場合
$ go build hello.go $ ./hello hello, world
3.サーバー側)Go言語製クライアント導入
1)セットアップ
$ go get github.com/google/safebrowsing $ go get github.com/google/safebrowsing/cmd/sbserver
2)起動方法
$ sbserver -apikey {Googleのコンソールで取得したAPIキー} -db="{sbserverデータファイル}" -srvaddr="localhost:8080"
◆リクエスト(サンプル)
$ curl --include --request POST \
--url http://localhost:8080/v4/threatMatches:find \
--header 'Content-Type: application/json' \
--data '{"threatInfo":{"threatEntries":[{"url":"http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/UNWANTED_SOFTWARE/URL/"}]}}'
◆レスポンス(サンプル)
HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 27 Dec 2017 06:41:12 GMT
Content-Length: 199
{"matches":[{"threatType":"UNWANTED_SOFTWARE","platformType":"ANY_PLATFORM","threatEntryType":"URL","threat":{"url":"http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/UNWANTED_SOFTWARE/URL/"}}]}
4.サーバー側)出力結果JSONの整形
1)出力結果は「JSON」形式となる
$ sudo yum install jq $ jq --version jq version 1.3
$ vi SafeCheck.sh
#!/bin/sh
MSG="Usage: $0 {URL}"
if [ $# -eq 0 ]; then
echo ${MSG} 1>&2
exit 0
fi
CURL=$(cat << EOS
curl -s --request POST \
--url http://localhost:8080/v4/threatMatches:find \
--header 'Content-Type: application/json' \
--data '{"threatInfo":{"threatEntries":[{"url":"${1}"}]}}'
EOS
)
eval ${CURL}
echo \
$ ./SafeCheck.sh http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/UNWANTED_SOFTWARE/URL/ | jq .
{
"matches": [
{
"threat": {
"url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/UNWANTED_SOFTWARE/URL/"
},
"threatEntryType": "URL",
"platformType": "ANY_PLATFORM",
"threatType": "UNWANTED_SOFTWARE"
}
]
}
5.サーバー側)Upstartでデーモン化
1)バージョン
$ initctl --version | head -n 1 initctl (upstart 0.6.5)
2)スクリプト(root以外ユーザーで実行)
$ sudo vi /etc/init/go-safe-browsing.conf
description "Go Safe Browsing"
start on runlevel [2345]
stop on runlevel [016]
respawn
exec `su {実行ユーザー名} -c "/PATH/TO/sbserver -apikey {Googleのコンソールで取得したAPIキー} -db="{sbserverデータファイル}" -srvaddr="localhost:8080" > /dev/null 2>&1"`
3)起動方法
$ sudo initctl reload-configuration $ sudo initctl start go-safe-browsing $ sudo initctl status go-safe-browsing go-safe-browsing start/running, process 7435
以上
Auto Amazon Links: プロダクトが見つかりません。
よしまさのブログをもっと見る
購読すると最新の投稿がメールで送信されます。
