質問編集履歴

1

プログラムの編集

2018/03/23 15:39

投稿

peromaron
peromaron

スコア7

test CHANGED
File without changes
test CHANGED
@@ -56,6 +56,74 @@
56
56
 
57
57
  ```ここに言語を入力
58
58
 
59
+ ```ここに言語を入力
60
+
61
+ import json as _json
62
+
63
+ import urllib.request, urllib.parse, urllib.error
64
+
65
+ import urllib.request, urllib.error, urllib.parse
66
+
67
+ import sys
68
+
69
+ import time
70
+
71
+ import mysql.connector
72
+
73
+ import datetime
74
+
75
+ import pprint
76
+
77
+
78
+
79
+ from urllib.request import Request, urlopen
80
+
81
+
82
+
83
+ #Apikeyを設定
84
+
85
+ class Apikey:
86
+
87
+ def __init__(self):
88
+
89
+ #database接続
90
+
91
+ self.connect = mysql.connector.connect(user='root', password='', host='localhost', database='kaiseki_development', charset='utf8')
92
+
93
+ self.connect.autocommit = True
94
+
95
+ self.cursor = self.connect.cursor()
96
+
97
+ self.cursor.execute('use kaiseki_development')
98
+
99
+
100
+
101
+ #virustotal_api_keysの中身を空に
102
+
103
+ self.cursor.execute('truncate table virustotal_api_keys')
104
+
105
+ #apikey
106
+
107
+ def set_apikeys(self):
108
+
109
+ apikeys = [ apikeyが入ってます。]
110
+
111
+
112
+
113
+ for n in range(len(apikeys)):
114
+
115
+ #table virustotal_api_keysのapi_key,use_time,use_numbers,created_at,updated_atに追加
116
+
117
+ self.cursor.execute('insert into virustotal_api_keys(api_key,use_time,use_numbers,created_at,updated_at) values(%s,now(),0,now(),now())' % (apikeys[n]))
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+ #Analysisを設定
126
+
59
127
  class Analysis:
60
128
 
61
129
  def __init__(self,analysis_id):
@@ -180,79 +248,97 @@
180
248
 
181
249
  url = self.analyse_url
182
250
 
183
-
184
-
185
- #keys = 4
186
-
187
- #time.sleep(15/keys)
188
-
189
-
190
-
191
- parameters = {"resource": targeturl,
192
-
193
- "apikey": self.apikey}
194
-
195
-
196
-
197
- data = urllib.parse.urlencode(parameters).encode("utf-8")
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
- #クライアントの要求処理
206
-
207
- request = urllib.request.Request(url)
208
-
209
-
210
-
211
- #リクエスト送信
212
-
213
- response = urllib.request.urlopen(request,data)
214
-
215
-
216
-
217
- #コンテンツを抽出
218
-
219
- json_str = response.read().decode('utf-8')
220
-
221
-
222
-
223
- #Json形式からpythonオブジェクトに変換
224
-
225
- decjson = json.loads(json_str)
226
-
227
-
228
-
229
- #print(str(decjson ["positives"])
230
-
231
-
232
-
233
-
234
-
235
- self.cursor.execute('update analysis_results set search_time =now(), analysis_result = %d, updated_at =now()') % str(decjson ["positives"])
236
-
237
-
238
-
239
- ```### 試したこと
240
-
241
- 2To3を使用した際同じエラーを似た構文で吐かれたという英語の質問箱のようなところで書かれていたので
242
-
243
-
244
-
245
- data = urllib.parse.urlencode(parameters).encode("utf-8")
246
-
247
- json_str = response.read().decode('utf-8')
248
-
249
- のように .encode("utf-8") .decode('utf-8')を追加した気がします。
250
-
251
- 少し前に検索したのでその記事が見つけることが出来なくて申し訳ありません。
252
-
253
-
254
-
255
-
251
+ apikey = self.apikey
252
+
253
+
254
+
255
+
256
+
257
+ parameters = urllib.parse.urlencode({"apikey": apikey,"targeturl": self.url}).encode("utf-8")
258
+
259
+
260
+
261
+ response = urllib.request.urlopen("https://www.virustotal.com/vtapi/v2/url/scan", data=parameters)
262
+
263
+
264
+
265
+ json = _json.loads(response.read().decode('utf-8'))
266
+
267
+
268
+
269
+ pprint.pprint(json)
270
+
271
+ if not json:
272
+
273
+ print("Error in calling /url/scan API")
274
+
275
+ return 3
276
+
277
+
278
+
279
+ scan_id = json["scan_id"]
280
+
281
+
282
+
283
+ print("Wait 10 seconds...")
284
+
285
+ time.sleep(10)
286
+
287
+
288
+
289
+ print("Waiting response 'GET /url/report'")
290
+
291
+
292
+
293
+ query = urllib.parse.urlencode({"apikey": self.apikey, "resource": scan_id})
294
+
295
+
296
+
297
+ response = urllib.request.urlopen("https://www.virustotal.com/vtapi/v2/url/report?" + query)
298
+
299
+
300
+
301
+ json = _json.loads(response.read().decode('utf-8'))
302
+
303
+
304
+
305
+ pprint.pprint(json)
306
+
307
+
308
+
309
+ #print (["positives"])
310
+
311
+
312
+
313
+ if not json:
314
+
315
+ print("Error in calling /url/report API")
316
+
317
+ return 4
318
+
319
+
320
+
321
+ return 0
322
+
323
+
324
+
325
+ if __name__ == "__main__":
326
+
327
+ sys.exit(main())
328
+
329
+ #print (["positives"])
330
+
331
+ #m2 = "url"
332
+
333
+ #print(decjson["m2"])
334
+
335
+
336
+
337
+ #self.cursor.execute('update analysis_results set search_time =now(), analysis_result = %s, updated_at =now()' % str(decjson ["positives"]))
338
+
339
+
340
+
341
+ ```
256
342
 
257
343
  ### 補足情報(FW/ツールのバージョンなど)
258
344