- 印刷する
- PDF
Hash Filter の開発
- 印刷する
- PDF
Classic/VPC環境で利用できます。
Hash Filterの REST APIを使用して Hash Filterコードを作成する方法について説明します。
Hash Filterの開発に先立ち、File Saferのご利用の申し込み、API Gatewayのご利用の申し込みおよび API Keyの作成が必要です。詳細は、File Safer を開始するをご参照ください。
Hash Filterの実装シナリオ
ユーザーがアップロードおよびダウンロードするファイルに対する Hash Filterの適用シナリオは、次の通りです。
1. マルウェアと疑われるファイルを区分
2. 区分されたファイルから Hash値を抽出
3. 認証値作成
4. リクエストパラメータ追加
5. APIリクエスト
6. レスポンス値確認および遮断有無の判断
Hash Filterコード作成
Hash Filterの実装シナリオに従ってコードを作成します。
1. マルウェアと疑われるファイルを区分
アップロードおよびダウンロードされるファイルのうち、マルウェア検査が必要なファイルを切り分けます。
2. 区分されたファイルから Hash値を抽出
ファイルから Hash値を抽出します。以下は Hash値を抽出する JAVAのサンプルコードです。
Hashタイプ別の Hash値の表記形式は、次の通りです。
- SHA-1: 16進法40byteの文字列で表記
例) 3ec625cb2277d7334406ab712b69c4ceaf38bd82 - MD5: 16進法32byteの文字列で表記
例) 8072fd6da170738c905cf362f787442b
public static String getHash(String filename, String algorithm) throws Exception
{
MessageDigest md = MessageDigest.getInstance(algorithm);
// file hashing with DigestInputStream
DigestInputStream dis = new DigestInputStream(new FileInputStream(new File(filename)), md);
// empty loop to clear the data
while(dis.read() != -1);
dis.close();
// Get the hash's bytes
byte[] hashBytes = md.digest();
// bytes to hex
StringBuffer sb = new StringBuffer();
for(byte h : hashBytes) {
sb.append(String.format("%02x", h));
}
return sb.toString();
}
public static void main(String[] args)
{
try {
// Get hash value of a file with SHA-1 Algorithm
String sha1 = getHash("./test.exe", "sha-1");
// Get hash value of a file MD5 Algorithm
String md5 = getHash("./test.exe", "md5");
} catch (Exception e) {
e.printStackTrace();
}
}
3. 認証値作成
NAVERクラウドプラットフォーム APIの使用認証に必要な IAM認証値を作成します。認証に必要なヘッダは、次の通りです。
x-ncp-iam-access-key
: NAVERクラウドプラットフォームポータルのマイページ > API認証キー管理メニューから Access Keyを発行x-ncp-apigw-signature-v2
: Access Keyとマッピングされる Secret Keyを HmacSHA256暗号化して signature値を作成(改行文字は\nを使用)x-ncp-apigw-api-key
: API Gatewayを通じて API Keyを発行x-ncp-apigw-timestamp
: 認証値ではないが、リクエストヘッダに含まれる。API明細を参照。
signature値を作成する方法とサンプルコードは、NAVERクラウドプラットフォーム APIガイド > API の概要 > 基本 API > Ncloud API > 2.ヘッダ作成をご参照ください。
4. リクエストパラメータ追加
Hash Filter APIのリクエストパラメータ値を追加します。リクエストパラメータは次の通りです。
- hashCode: Hash値を文字列に追加
- hashType: Hash値のアルゴリズムを表記(MD5または SHA-1)
以下は作成されたパラメータの例です。
hashCode=00010efc49b7174d18400710eed1642a7d4b12db&hashType=sha1
Hash Filterの REST API明細は、checkHash(Hash Filter)をご参照ください。
5. APIリクエスト
上記の3番と4番の手続きで作成したヘッダとパラメータを使って、Hash Filter URIでリクエストします。以下は Hash Filter APIリクエストのサンプルコードです。
public static void main(String[] args) throws Exception
{
String accessKey = "ACCESS_KEY"; // access key (from portal or sub account)
String apikey = API_GATEWAY_KEY;
String apiDomain = "https://filesafer.apigw.ntruss.com";
String method = "GET";
long timestamp = System.currentTimeMillis();
// step 2, 3: Extract hash vlaue of a identified file then add parameter value including hash value and hash type
String param = "hashCode=" + getHash("./test.exe") + "&hashType=sha1";
// The apiURL is the value obtained by appending parameter to the end of URI string provided by Hash Filter.
String apiURL = "/hashfilter/v1/checkHash?" + param;
// step 4: Create authentication value
String signature = makeSignature(method, apiURL, timestamp);
// step 5: Request API
URL url = new URL(apiDomain + apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod(method);
con.setRequestProperty("x-ncp-apigw-timestamp", Long.toString(timestamp));
con.setRequestProperty("x-ncp-iam-access-key", accessKey);
con.setRequestProperty("x-ncp-apigw-signature-v2", signature);
con.setRequestProperty("x-ncp-apigw-api-key", apikey);
con.setRequestProperty("accept", "application/json");
// step 6: Check the response value and determine whether to block
int httpResponseCode = con.getResponseCode();
BufferedReader br = null;
if (httpResponseCode == 200) {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String readLine;
StringBuffer httpResponse = new StringBuffer();
while((readLine = br.readLine())! = null) {
httpResponse.append(readLine);
}
// Do next step (ex, determine whether to block)
...
br.close();
}
}
jdk20以上の場合、URLメソッドの代わりに URIメソッドを使用できます。
6. レスポンス値確認および遮断有無の判断
API呼び出しレスポンス値の hashCheckResultList
の object
値を確認して、マルウェア有無を判断してファイルを処理します。
hashCheckResultList
に object
がある場合はマルウェアが検出されたことを意味し、そうでない場合はマルウェアがないことを意味します。