-
Notifications
You must be signed in to change notification settings - Fork 31
/
gdrive_download.py
236 lines (202 loc) · 9.92 KB
/
gdrive_download.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
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
#!/usr/bin/env python
# coding: utf-8
##############################################################################
# #
# Code for the USENIX Security '22 paper: #
# How Machine Learning Is Solving the Binary Function Similarity Problem. #
# #
# MIT License #
# #
# Copyright (c) 2019-2022 Cisco Talos #
# #
# Permission is hereby granted, free of charge, to any person obtaining #
# a copy of this software and associated documentation files (the #
# "Software"), to deal in the Software without restriction, including #
# without limitation the rights to use, copy, modify, merge, publish, #
# distribute, sublicense, and/or sell copies of the Software, and to #
# permit persons to whom the Software is furnished to do so, subject to #
# the following conditions: #
# #
# The above copyright notice and this permission notice shall be #
# included in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF #
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE #
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION #
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION #
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
# #
# gdrive_download.py - Download the dataset from Google Drive #
# #
##############################################################################
import click
import gdown
import hashlib
import os
import zipfile
BINARIES_DICT = {
"Dataset-1.zip": "1QpRgVJZTM52bfB6PvCKwVnmkKdHenUai",
"Dataset-2.zip": "1m3zocusSER3Hgt5Q7C1et_dOT44oi8hX",
"Dataset-Vulnerability.zip": "1i9CEJ7IGwyFF_3VlQWVsJRVW1XMXy96Z"
}
FEATURES_DICT = {
"Dataset-1/features.zip": "1gu7ZEhpg3JkznX3_VV2SBCyc6LO7sX2q",
"Dataset-1-CodeCMR/features.zip": "1LoXaMSyDFkHlAzMEuZslE_mMi2yo2LZz",
"Dataset-2/features.zip": "1Dp3MvTfIAG4iakjlAGqbsX_Y3Na_c_t8",
"Dataset-Vulnerability/features.zip": "1NOL5DBem1TbI2Lcc0R64L7JM1BnJMDSp"
}
RESULTS_DICT = {
"Dataset-1-CodeCMR-results.zip": "1vQD4MBNR32VSARmLQE4c7GaBG5vYZPgN",
"Dataset-1-results.zip": "1D_7QUiGhr8c2aFdAwZEGYyVZR3DIdfbH",
"Dataset-2-results.zip": "1dJ9aXDvzQLWUWz0vDBVM5XG8Y33kFHwS",
"Dataset-Vulnerability-results.zip": "17Hv0ZhSwnzUMgWpXyXM2zaEYPrv21igZ",
"raw_results-results.zip": "1T5BfUnjn287VCNCit4W2-ggmckUl9JO7"
}
SHA256_DICT = {
"Dataset-1.zip":
"f45edac9a7414c3bef77b271bcba083656e148d08d2da8ed5d667d887af35e46",
"Dataset-2.zip":
"2157fe91b374cb0cc5cd35eacfcb331671b9058839ad9fdff5e44ccb9661ac35",
"Dataset-Vulnerability.zip":
"0b916bd0fc5107e34d5d06c0e7037337b6e0dc5042b475ca3be5ccba7d7d1bd7",
"Dataset-1/features.zip":
"23a154929ac0600ac5a5893689088f166e1b34fd526f1fc45b6d9cf06ff987f5",
"Dataset-1-CodeCMR/features.zip":
"dc51c60189f65fa0a000bd2a1afa1fbc278e4a7164eebe5d1b47c90afaf2d98b",
"Dataset-2/features.zip":
"a3a919af846a5dee24280fbeb071b0d715f4499ee3daf1b6826d9be533ad1543",
"Dataset-Vulnerability/features.zip":
"3494a8d3af91fc8fd00e7e017c2208e3d6b47b1103bd7946553774b547135d2a",
"Dataset-1-CodeCMR-results.zip":
"e0a26cb782c505a3f30b53aef1e1d84acccc1e2ebd559d1afe68e6ae3f5b767a",
"Dataset-1-results.zip":
"706a760348e3467f76313c6b07bf01e6c942002ce903dcb02b32ae5c89929bff",
"Dataset-2-results.zip":
"0f1041391491cb2abd013db240b5bdcbb48300a194e79cf098339489ee7494fb",
"Dataset-Vulnerability-results.zip":
"7350576e115605aaf9da667ef14a14d5d307487b48ca1391c6fbb7ef9ea1dbbf",
"raw_results-results.zip":
"6759a38f42440b46f5607148d0b66d9f5ab1b7854a7203a87b39e7f08580d9b3"
}
REPO_PATH = (os.path.dirname(os.path.abspath(__file__)))
BINARIES_FOLDER = os.path.join(REPO_PATH, "Binaries")
DATASET_FOLDER = os.path.join(REPO_PATH, "DBs")
MODELS_FOLDER = os.path.join(REPO_PATH, "Models")
RESULTS_FOLDER = os.path.join(REPO_PATH, "Results", "data")
def compute_sha256(file_path):
"""Compute the sha256 for the file in file_path."""
blocksize = 65536
sha = hashlib.sha256()
with open(file_path, 'rb') as f_in:
file_buffer = f_in.read(blocksize)
while len(file_buffer) > 0:
sha.update(file_buffer)
file_buffer = f_in.read(blocksize)
return sha.hexdigest()
def download_binaries():
"""Download and unzip the archives containing the binaries for the exp."""
try:
if not os.path.isdir(BINARIES_FOLDER):
os.mkdir(BINARIES_FOLDER)
for zip_name, gid in BINARIES_DICT.items():
temp_dir = os.path.join(BINARIES_FOLDER, zip_name.split(".")[0])
if os.path.isdir(temp_dir):
print("[W] {} already exists".format(temp_dir))
continue
zip_path = os.path.join(BINARIES_FOLDER, zip_name)
print("Downloading {} ...".format(zip_name))
gdown.download(id=gid, output=zip_path, quiet=False)
if not os.path.isfile(zip_path):
print("[!] Error: file {} not found".format(zip_path))
continue
print("Checking checksum {} ...".format(zip_name))
sha = compute_sha256(zip_path)
if sha != SHA256_DICT[zip_name]:
print("[!] Checksum error: {} != {}".format(
sha, SHA256_DICT[zip_name]))
continue
print("Extracting archive to {}...".format(BINARIES_FOLDER))
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
if zip_name == "Dataset-2.zip":
zip_ref.extractall(BINARIES_FOLDER, pwd=b'notasecret')
else:
zip_ref.extractall(BINARIES_FOLDER)
os.remove(zip_path)
print()
except Exception as e:
print("[!] Exception in download_binaries\n{}".format(e))
def download_features():
"""Download and unzip the archives containing the features for the exp."""
try:
if not os.path.isdir(DATASET_FOLDER):
os.mkdir(DATASET_FOLDER)
for zip_name, gid in FEATURES_DICT.items():
temp_dir = os.path.join(DATASET_FOLDER, zip_name.split(".")[0])
if os.path.isdir(temp_dir):
print("[W] {} already exists".format(temp_dir))
continue
zip_path = os.path.join(DATASET_FOLDER, zip_name)
print("Downloading {} ...".format(zip_name))
gdown.download(id=gid, output=zip_path, quiet=False)
if not os.path.isfile(zip_path):
print("[!] Error: file {} not found".format(zip_path))
continue
print("Checking checksum {} ...".format(zip_name))
sha = compute_sha256(zip_path)
if sha != SHA256_DICT[zip_name]:
print("[!] Checksum error: {} != {}".format(
sha, SHA256_DICT[zip_name]))
continue
print("Extracting archive to {}...".format(DATASET_FOLDER))
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(os.path.dirname(zip_path))
os.remove(zip_path)
print()
except Exception as e:
print("[!] Exception in download_features\n{}".format(e))
def download_results_data():
"""Download and unzip the archives containing the results of the exp."""
try:
if not os.path.isdir(RESULTS_FOLDER):
os.mkdir(RESULTS_FOLDER)
for zip_name, gid in RESULTS_DICT.items():
temp_dir = os.path.join(RESULTS_FOLDER, zip_name.split(".")[0])
if os.path.isdir(temp_dir):
print("[W] {} already exists".format(temp_dir))
continue
zip_path = os.path.join(RESULTS_FOLDER, zip_name)
print("Downloading {} ...".format(zip_name))
gdown.download(id=gid, output=zip_path, quiet=False)
if not os.path.isfile(zip_path):
print("[!] Error: file {} not found".format(zip_path))
continue
print("Checking checksum {} ...".format(zip_name))
sha = compute_sha256(zip_path)
if sha != SHA256_DICT[zip_name]:
print("[!] Checksum error: {} != {}".format(
sha, SHA256_DICT[zip_name]))
continue
print("Extracting archive to {}...".format(RESULTS_FOLDER))
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(os.path.dirname(zip_path))
os.remove(zip_path)
print()
except Exception as e:
print("[!] Exception in download_results_data\n{}".format(e))
@click.command()
@click.option('--binaries', is_flag=True)
@click.option('--features', is_flag=True)
@click.option('--results', is_flag=True)
def main(binaries, features, results):
"""Download the dataset from Google Drive."""
if binaries:
download_binaries()
if features:
download_features()
if results:
download_results_data()
print("That's all")
if __name__ == "__main__":
main()