新年あけましておめでとうございます!

VirusTotalは、疑わしいファイルや URL を分析する無料のサービスです。ウイルス、ワーム、トロイの木馬、あらゆる種類のマルウェアを素早く検出できます。Pythonを使ってVirusTotalのAPIでMD5ハッシュについてリポートを返事するシンプルなクライエントを作りましょう。

始まる前に、ソースコードは]こちらです

ワークフロー

クライエントはこのようにする:

  1. クライエントを初めてfile/report APIに疑わしいハッシュ(MD5、SHA1,SHA256)とAPIキーを供給します。VirusTotalからAPIキーをいただきました。(ドキュメンテーションはこちら)。
  2. サイバーセキュリティ専門家にとってハッシュが悪意のあるとのかどうかAPIからリポートをいただきます。

インポート

まず、モジュールは3つ要ります:

1
import requests, sys, json

リクエストのモジュールはPythonに含まれていないので(履いてないから)、別にインストールします。

1
$ pip3 install requests

コマンドラインのアーギュメント

エンドユーザーは二つのアーギュメント、とVirusTotalからのAPIキーと分かりたいハッシュを入れます。Pythonでコマンドラインのアーギュメントをもらうようにこれをする:

1
str(sys.argv)

コマンドラインのアーギュメントはリストになって、str()ファンクションはリストのものをストリングにかえります。

1
2
user_api_key = str(sys.argv[1])
user_hash = str(sys.argv[2])

sys.argv[0]はスクリプトの名前です:

1
$ python3 myscript.py arg1 arg2

だから、sys.argv[1]から始まります。

APIキーとハッシュの使い方

それからAPIキーとハッシュを割り当てます。

1
2
3
4
params = {
'apikey': user_api_key,
'resource': user_hash
}

きれいにJSONをかえて

もっと簡単にするように、APIの返事をコマンドラインにプリントします(もしくはファイルにプリントも出来ます)。 アウトプートは読みやすくなるから、Stack Overflowからこのヘルパーファンクションを使います:

1
2
3
4
5
6
def pp_json(json_thing, sort=True, indents=4):
if type(json_thing) is str:
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))
else:
print(json.dumps(json_thing, sort_keys=sort, indent=indents))
return None

APIを使う

このようにAPIを使います:

1
response = requests.get('https://www.virustotal.com/vtapi/v2/file/report', params=params)

シンプルと読みやすくてJAVAはこんなによかったらいいですね (JAVAよりも遥かにシンプルで読みやすくなります)。

アウトプットをもらってプリントします

APIの返事はJSONにかえて:

1
json_response = response.json()

便利なファンクションを使ってきれいにして:

1
pretty_json = pp_json(json_response)

プリントします:

1
print(pretty_json)

こんなに簡単です!

スクリプトを実行する

APIキーとハッシュとスクリプトを実行します:

1
$ python3 vtscript.py 18092yeu91hd9u02hdi1jd1jkldn 7657fcb7d772448a6d8504e4b20168b8

きれいなJSONになりました:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
{
"md5": "7657fcb7d772448a6d8504e4b20168b8",
"permalink": "https://www.virustotal.com/file/54bc950d46a0d1aa72048a17c8275743209e6c17bdacfc4cb9601c9ce3ec9a71/analysis/1519298675/",
"positives": 64,
"resource": "7657fcb7d772448a6d8504e4b20168b8",
"response_code": 1,
"scan_date": "2018-02-22 11:24:35",
"scan_id": "54bc950d46a0d1aa72048a17c8275743209e6c17bdacfc4cb9601c9ce3ec9a71-1519298675",
"scans": {
"ALYac": {
"detected": true,
"result": "Gen:Variant.Kazy.8782",
"update": "20180222",
"version": "1.1.1.5"
},
"AVG": {
"detected": true,
"result": "Win32:Kryptik-JOV [Trj]",
"update": "20180222",
"version": "18.1.3800.0"
},
"AVware": {
"detected": true,
"result": "Trojan.Win32.Generic!BT",
"update": "20180222",
"version": "1.5.0.42"
},
"Ad-Aware": {
"detected": true,
"result": "Gen:Variant.Kazy.8782",
"update": "20180222",
"version": "3.0.3.1010"
},
"AhnLab-V3": {
"detected": true,
"result": "Trojan/Win32.Zbot.R19508",
"update": "20180222",
"version": "3.11.3.19504"
},
"Antiy-AVL": {
"detected": true,
"result": "Worm/Win32.Autorun.icp",
"update": "20180222",
"version": "3.0.0.1"
},
"Arcabit": {
"detected": true,
"result": "Trojan.Kazy.D224E",
"update": "20180222",
"version": "1.0.0.830"
},
"Avast": {
"detected": true,
"result": "Win32:Kryptik-JOV [Trj]",
"update": "20180222",
"version": "18.1.3800.0"
},
"Avast-Mobile": {
"detected": false,
"result": null,
"update": "20180221",
"version": "180221-04"
},
"Avira": {
"detected": true,
"result": "TR/Drop.Liks.A",
"update": "20180222",
"version": "8.3.3.6"
},
"Baidu": {
"detected": true,
"result": "Win32.Worm.Autorun.f",
"update": "20180208",
"version": "1.0.0.2"
},
"BitDefender": {
"detected": true,
"result": "Gen:Variant.Kazy.8782",
"update": "20180222",
"version": "7.2"
},
"Bkav": {
"detected": true,
"result": "W32.ZeustrackerZS.Trojan",
"update": "20180212",
"version": "1.3.0.9466"
},
"CAT-QuickHeal": {
"detected": true,
"result": "Trojan.Ramnit.A",
"update": "20180222",
"version": "14.00"
},
"CMC": {
"detected": true,
"result": "Trojan.Win32.Lebag!O",
"update": "20180222",
"version": "1.1.0.977"
},
"ClamAV": {
"detected": true,
"result": "Win.Trojan.Ramnit-7847",
"update": "20180222",
"version": "0.99.2.0"
},
"Comodo": {
"detected": true,
"result": "TrojWare.Win32.Kryptik.KLV",
"update": "20180222",
"version": "28564"
},
"CrowdStrike": {
"detected": true,
"result": "malicious_confidence_100% (W)",
"update": "20170201",
"version": "1.0"
},
"Cybereason": {
"detected": true,
"result": "malicious.7d7724",
"update": "20180205",
"version": "1.2.27"
},
"Cylance": {
"detected": true,
"result": "Unsafe",
"update": "20180222",
"version": "2.3.1.101"
},
"Cyren": {
"detected": true,
"result": "W32/Ramnit.K.gen!Eldorado",
"update": "20180222",
"version": "5.4.30.7"
},
"DrWeb": {
"detected": true,
"result": "Win32.HLLW.Tazebama.235",
"update": "20180222",
"version": "7.0.28.2020"
},
"ESET-NOD32": {
"detected": true,
"result": "Win32/Ramnit.A",
"update": "20180222",
"version": "16946"
},
"Emsisoft": {
"detected": true,
"result": "Gen:Variant.Kazy.8782 (B)",
"update": "20180222",
"version": "4.0.2.899"
},
"Endgame": {
"detected": true,
"result": "malicious (high confidence)",
"update": "20180216",
"version": "1.2.1"
},
"F-Prot": {
"detected": true,
"result": "W32/Ramnit.K.gen!Eldorado",
"update": "20180222",
"version": "4.7.1.166"
},
"F-Secure": {
"detected": true,
"result": "Gen:Variant.Kazy.8782",
"update": "20180222",
"version": "11.0.19100.45"
},
"Fortinet": {
"detected": true,
"result": "W32/Kryptik.KLV!tr",
"update": "20180222",
"version": "5.4.247.0"
},
"GData": {
"detected": true,
"result": "Gen:Variant.Kazy.8782",
"update": "20180222",
"version": "A:25.16111B:25.11644"
},
"Ikarus": {
"detected": true,
"result": "Virus.Win32.Virtob",
"update": "20180222",
"version": "0.1.5.2"
},
"Invincea": {
"detected": true,
"result": "heuristic",
"update": "20180121",
"version": "6.3.4.26036"
},
"Jiangmin": {
"detected": true,
"result": "Trojan/Generic.dkmt",
"update": "20180222",
"version": "16.0.100"
},
"K7AntiVirus": {
"detected": true,
"result": "Riskware ( 0015e4f11 )",
"update": "20180222",
"version": "10.40.26289"
},
"K7GW": {
"detected": true,
"result": "Riskware ( 0015e4f11 )",
"update": "20180222",
"version": "10.40.26290"
},
"Kaspersky": {
"detected": true,
"result": "Worm.Win32.Autorun.icp",
"update": "20180222",
"version": "15.0.1.13"
},
"Kingsoft": {
"detected": false,
"result": null,
"update": "20180222",
"version": "2013.8.14.323"
},
"MAX": {
"detected": true,
"result": "malware (ai score=100)",
"update": "20180222",
"version": "2017.11.15.1"
},
"Malwarebytes": {
"detected": true,
"result": "Spyware.Zbot",
"update": "20180222",
"version": "2.1.1.1115"
},
"McAfee": {
"detected": true,
"result": "PWS-Zbot.gen.cy",
"update": "20180221",
"version": "6.0.6.653"
},
"McAfee-GW-Edition": {
"detected": true,
"result": "BehavesLike.Win32.PWSZbot.ch",
"update": "20180222",
"version": "v2015"
},
"MicroWorld-eScan": {
"detected": true,
"result": "Gen:Variant.Kazy.8782",
"update": "20180222",
"version": "14.0.297.0"
},
"Microsoft": {
"detected": true,
"result": "Trojan:Win32/Ramnit",
"update": "20180222",
"version": "1.1.14500.5"
},
"NANO-Antivirus": {
"detected": true,
"result": "Trojan.Win32.DownLoad2.csmmu",
"update": "20180222",
"version": "1.0.100.21498"
},
"Paloalto": {
"detected": true,
"result": "generic.ml",
"update": "20180222",
"version": "1.0"
},
"Panda": {
"detected": true,
"result": "Trj/Ramnit.F",
"update": "20180221",
"version": "4.6.4.2"
},
"Qihoo-360": {
"detected": true,
"result": "Win32/Trojan.544",
"update": "20180222",
"version": "1.0.0.1120"
},
"Rising": {
"detected": true,
"result": "Malware.XPACK!1.64E1 (CLASSIC)",
"update": "20180222",
"version": "25.0.0.1"
},
"SUPERAntiSpyware": {
"detected": true,
"result": "Trojan.Agent/Gen-FakeSecurity",
"update": "20180221",
"version": "5.6.0.1032"
},
"SentinelOne": {
"detected": true,
"result": "static engine - malicious",
"update": "20180115",
"version": "1.0.12.202"
},
"Sophos": {
"detected": true,
"result": "Troj/ZXC-G",
"update": "20180222",
"version": "4.98.0"
},
"Symantec": {
"detected": true,
"result": "W32.Ramnit",
"update": "20180222",
"version": "1.5.0.0"
},
"Tencent": {
"detected": true,
"result": "Worm.Win32.AutoRun.aaa",
"update": "20180222",
"version": "1.0.0.1"
},
"TheHacker": {
"detected": true,
"result": "Trojan/Lebag.agu",
"update": "20180219",
"version": "6.8.0.5.2426"
},
"TotalDefense": {
"detected": true,
"result": "Win32/Ramnit.B!Dropper",
"update": "20180222",
"version": "37.1.62.1"
},
"TrendMicro": {
"detected": true,
"result": "TSPY_ZBOT.SMHA",
"update": "20180222",
"version": "9.862.0.1074"
},
"TrendMicro-HouseCall": {
"detected": true,
"result": "TSPY_ZBOT.SMHA",
"update": "20180222",
"version": "9.950.0.1006"
},
"VBA32": {
"detected": true,
"result": "Worm.AutoRun",
"update": "20180221",
"version": "3.12.28.0"
},
"VIPRE": {
"detected": true,
"result": "Trojan.Win32.Generic!BT",
"update": "20180222",
"version": "64782"
},
"ViRobot": {
"detected": true,
"result": "Trojan.Win32.Agent.109056.CR",
"update": "20180222",
"version": "2014.3.20.0"
},
"Webroot": {
"detected": true,
"result": "Trojan:Win32/Eyestye.H",
"update": "20180222",
"version": "1.0.0.207"
},
"WhiteArmor": {
"detected": false,
"result": null,
"update": "20180205",
"version": null
},
"Yandex": {
"detected": true,
"result": "Trojan.Ramnit!cLbJ7UZPdfE",
"update": "20180222",
"version": "5.5.1.3"
},
"Zillya": {
"detected": true,
"result": "Trojan.Zbot.Win32.81569",
"update": "20180221",
"version": "2.0.0.3497"
},
"ZoneAlarm": {
"detected": true,
"result": "Worm.Win32.Autorun.icp",
"update": "20180222",
"version": "1.0"
},
"Zoner": {
"detected": true,
"result": "Win32.Ramnit.A",
"update": "20180222",
"version": "1.0"
},
"eGambit": {
"detected": true,
"result": "Unsafe.AI_Score_70%",
"update": "20180222",
"version": "v4.3.5"
},
"nProtect": {
"detected": true,
"result": "Trojan-Spy/W32.ZBot.109056.AR",
"update": "20180222",
"version": "2018-02-22.02"
}
},
"sha1": "84c7201f7e59cb416280fd69a2e7f2e349ec8242",
"sha256": "54bc950d46a0d1aa72048a17c8275743209e6c17bdacfc4cb9601c9ce3ec9a71",
"total": 67,
"verbose_msg": "Scan finished, information embedded"
}

これはスクリプト:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import requests, sys,json

user_api_key = str(sys.argv[1])
user_hash = str(sys.argv[2])

params = {
'apikey': user_api_key,
'resource': user_hash
}

def pp_json(json_thing, sort=True, indents=4):
if type(json_thing) is str:
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))
else:
print(json.dumps(json_thing, sort_keys=sort, indent=indents))
return None

response = requests.get('https://www.virustotal.com/vtapi/v2/file/report', params=params)

json_response = response.json()
pretty_json = pp_json(json_response)

print(pretty_json)