-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathAPT_2_virustotal_analyser.py
executable file
·190 lines (147 loc) · 6.31 KB
/
APT_2_virustotal_analyser.py
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
import sys
import time
import shutil
import urllib
import os.path
import hashlib
import urllib2
import requests
import argparse
import simplejson
from tqdm import *
from os import listdir
from termcolor import colored
from os.path import isfile, join
from simplejson import JSONDecodeError
from argparse import RawTextHelpFormatter
from os.path import join as join_dir
# TODO CHECK WHAT HAPPENS WHEN AN ANALYSIS HAS NOT BEEN FOUND OR WHEN IT COULD NOT BE DOWNLOADED
os.path.dirname(os.path.abspath(__file__))
VT_ANALYSIS_DIRECTORY_NAME = "/../VT_ANALYSIS/"
VT_KEY = None
def print_message(message, with_color, color):
if with_color:
print colored(message, color)
else:
print message
def sha256(fname):
"""
Method to calculate the SHA256 hash of a file
:param fname: Path to file
:return: SHA256 of the input file
"""
hash_num = hashlib.sha256()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_num.update(chunk)
return hash_num.hexdigest()
def get_report_hash(hash_num):
"""
Calls the VirusTotal service with a specific hash
:param hash_num: Hash of the app whose VirusTotal report is required
:return: VirusTotal report in JSON format
"""
url = "https://www.virustotal.com/vtapi/v2/file/report"
parameters = {"resource": hash_num, "apikey": VT_KEY}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
except urllib2.URLError:
return ""
json = response.read()
return json
def main():
parser = argparse.ArgumentParser(
description="Script designed for analysing apks with the VirusTotal service\n\n" +
'[!] A VirusTotal API key must be provided in a file called vt_key inside '
'the info/directory',
formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', '--source', help='Source directory for APKs', required=True)
parser.add_argument('-o', '--output', help='Output directory for VirusTotal Analysis', required=False)
parser.add_argument('-so', '--samplesoutput', help='Output directory to move APKs after analysis', required=False)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
analyse_virustotal(args.source, vt_analysis_output_folder=args.output, output_samples_folder=args.samplesoutput,
with_color=True)
def analyse_virustotal(source_directory, vt_api_key, vt_analysis_output_folder=None, output_samples_folder=None,
with_color=True):
"""
Analyses a set of APK files with the VirusTotal service
Parameters
----------
:param source_directory: Folder containing apk files
:param vt_analysis_output_folder: Folder where VirusTotal reports are saved
:param output_samples_folder: Folder where apk files are saved after analysed with VirusTotal
:return:
"""
if len(vt_api_key) != 64:
print 'ERROR! - invalid vt_key file. Please, provide a virustotal key!'
sys.exit(0)
global VT_KEY
VT_KEY = vt_api_key
if vt_analysis_output_folder is None:
vt_analysis_output_folder = join_dir(source_directory, VT_ANALYSIS_DIRECTORY_NAME)
reports_not_received = 0
# TODO It is necessary to control when the directory could not be created (for instance if the folder is going to be
# TODO created in a non existing directory
if not os.path.exists(vt_analysis_output_folder):
os.makedirs(vt_analysis_output_folder)
if output_samples_folder is not None:
if not os.path.exists(output_samples_folder):
os.makedirs(output_samples_folder)
apks_found = [f for f in listdir(source_directory) if isfile(join(source_directory, f))
and f.endswith(".apk")]
count_positives = 0
for apk in tqdm(apks_found):
if isfile(join(source_directory, apk.replace(".apk", ".json"))):
print_message("APK WITH JSON. CONTINUE...", with_color, 'green')
continue
apk_path = source_directory + apk
hash_sha = sha256(apk_path)
report = ""
while report == "":
report = get_report_hash(hash_sha)
if report == "":
print_message("No report received. Waiting...", with_color, 'red')
time.sleep(1)
response_dict = simplejson.loads(report)
response_code = response_dict.get("response_code")
if response_code == 1: # Report generated
positives = response_dict.get("positives")
file_json = open(apk_path.replace(".apk", "") + ".json", "w")
file_json.write(report)
if positives > 0:
count_positives += 1
shutil.move(apk_path.replace(".apk", "") + ".json",
join_dir(vt_analysis_output_folder, apk.replace(".apk", "") + ".json"))
if output_samples_folder is not None:
shutil.move(join_dir(source_directory, apk), join_dir(output_samples_folder, apk))
if response_code == 0:
reports_not_received += 1
params = {'apikey': VT_KEY}
files = {'file': ("apk", open(apk_path, 'rb'))}
print "Uploading APK: " + apk
print "File not analysed yet. Uploading file..."
try:
response = requests.post('https://www.virustotal.com/vtapi/v2/file/scan', files=files, params=params)
except requests.exceptions.ConnectionError:
print_message("Connection error", with_color, 'red')
continue
print str(response)
try:
response.json()
except JSONDecodeError:
print_message("JSONDecodeError", with_color, 'red')
continue
print_message("SENT TO VIRUS-TOTAL", with_color, 'blue')
if reports_not_received > 0:
print "WARNING! " + str(reports_not_received) + " apks does not have yet a VT analysis. Please" \
", execute again this script after a while"
else:
print_message("SUCCESS!!", with_color, 'green')
print " All reports have been saved in the VT_ANALYSIS folder. APKS are in SAMPLES folder."
if __name__ == '__main__':
main()