forked from gwen001/pentest-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apk-downloader.py
434 lines (328 loc) · 12.2 KB
/
apk-downloader.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import math
from multiprocessing import Process, Queue
import os
import os.path
import re
import sys
import time
try:
# Python 3
from queue import Empty as EmptyQueueException
from queue import Full as FullQueueException
except ImportError:
# Python 2
from Queue import Empty as EmptyQueueException
from Queue import Full as FullQueueException
from bs4 import BeautifulSoup
import requests
DOMAIN = "https://apkpure.com"
SEARCH_URL = DOMAIN + "/search?q=%s"
DOWNLOAD_DIR = "./downloaded/"
PACKAGE_NAMES_FILE = "package_names.txt"
OUTPUT_CSV = "output.csv"
CONCURRENT_DOWNLOADS = 4
CHUNK_SIZE = 128*1024 # 128 KiB
PROGRESS_UPDATE_DELAY = 0.25
PROCESS_TIMEOUT = 10.0
MSG_ERROR = -1
MSG_PAYLOAD = 0
MSG_START = 1
MSG_PROGRESS = 2
MSG_END = 3
class SplitProgBar(object):
@staticmethod
def center(text, base):
if len(text) <= len(base):
left = (len(base) - len(text)) // 2
return "%s%s%s" % (base[:left], text, base[left+len(text):])
else:
return base
def __init__(self, n, width):
self.n = n
self.sub_width = int(float(width-(n+1))/n)
self.width = n * (self.sub_width + 1) + 1
self.progress = [float("NaN")] * n
def __getitem__(self, ix):
return self.progress[ix]
def __setitem__(self, ix, value):
self.progress[ix] = value
def render(self):
bars = []
for prog in self.progress:
if math.isnan(prog) or prog < 0.0:
bars.append(" " * self.sub_width)
continue
bar = "=" * int(round(prog*self.sub_width))
bar += " " * (self.sub_width-len(bar))
bar = SplitProgBar.center(" %.2f%% " % (prog*100), bar)
bars.append(bar)
new_str = "|%s|" % "|".join(bars)
sys.stdout.write("\r%s" % new_str)
def clear(self):
sys.stdout.write("\r%s\r" % (" " * self.width))
class Counter(object):
def __init__(self, value = 0):
self.value = value
def inc(self, n = 1):
self.value += n
def dec(self, n = 1):
self.value -= n
@property
def empty(self):
return self.value == 0
def download_process(id_, qi, qo):
def send_progress(progress):
try:
qo.put_nowait((MSG_PROGRESS, (id_, progress)))
except FullQueueException:
pass
def send_error(msg):
qo.put((MSG_ERROR, (id_, msg)))
def send_start(pkg_name):
qo.put((MSG_START, (id_, pkg_name)))
def send_finished(pkg_name, app_name, size, path, already=False):
if already:
qo.put((MSG_END, (id_, pkg_name, app_name, size, path)))
else:
qo.put((MSG_PAYLOAD, (id_, pkg_name, app_name, size, path)))
while True:
message = qi.get()
if message[0] == MSG_PAYLOAD:
package_name, app_name, download_url = message[1]
elif message[0] == MSG_END:
break
try:
r = requests.get(download_url, stream=True)
except requests.exceptions.ConnectionError:
send_error("Connection error")
continue
if r.status_code != 200:
send_error("HTTP Error %d" % r.status_code)
r.close()
continue
content_disposition = r.headers.get("content-disposition", "")
content_length = int(r.headers.get('content-length', 0))
filename = re.search(r'filename="(.+)"', content_disposition)
if filename and filename.groups():
filename = filename.groups()[0]
else:
filename = "%s.apk" % (package_name.replace(".", "_"))
local_path = os.path.normpath(os.path.join(DOWNLOAD_DIR, filename))
if os.path.exists(local_path):
if not os.path.isfile(local_path):
# Not a file
send_error("%s is a directory." % local_path)
r.close()
continue
if os.path.getsize(local_path) == content_length:
# File has likely already been downloaded
send_finished(
package_name, app_name, content_length, local_path, True)
r.close()
continue
send_start(package_name)
size = 0
t = time.time()
with open(local_path, "wb+") as f:
for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
size += len(chunk)
f.write(chunk)
nt = time.time()
if nt - t >= PROGRESS_UPDATE_DELAY:
send_progress(float(size) / content_length)
t = nt
send_finished(package_name, app_name, size, local_path)
def search_process(qi, qo):
def send_error(msg):
qo.put((MSG_ERROR, msg))
def send_payload(pkg_name, app_name, dl_url):
qo.put((MSG_PAYLOAD, (pkg_name, app_name, dl_url)))
while True:
message = qi.get()
if message[0] == MSG_PAYLOAD:
package_name = message[1]
elif message[0] == MSG_END:
break
# Search page
url = SEARCH_URL % package_name
try:
r = requests.get(url)
except requests.exceptions.ConnectionError:
send_error("Connection error.")
continue
if r.status_code != 200:
send_error("Could not get search page for %s." % package_name)
continue
soup = BeautifulSoup(r.text, "html.parser")
first_result = soup.find("dl", class_="search-dl")
if first_result is None:
send_error("Could not find %s." % package_name)
continue
search_title = first_result.find("p", class_="search-title")
search_title_a = search_title.find("a")
app_name = search_title.text.strip()
app_url = search_title_a.attrs["href"]
# App page
url = DOMAIN + app_url
try:
r = requests.get(url)
except requests.exceptions.ConnectionError:
send_error("Connection error.")
continue
if r.status_code != 200:
send_error("Could not get app page for %s." % package_name)
continue
soup = BeautifulSoup(r.text, "html.parser")
download_button = soup.find("a", {"class":"da"})
if download_button is None:
send_error("%s is a paid app. Could not download." % package_name)
continue
download_url = download_button.attrs["href"]
# Download app page
url = DOMAIN + download_url
try:
r = requests.get(url)
except requests.exceptions.ConnectionError:
send_error("Connection error.")
continue
if r.status_code != 200:
send_error("Could not get app download page for %s." % package_name)
continue
soup = BeautifulSoup(r.text, "html.parser")
download_link = soup.find("a", {"id":"download_link"})
if download_link is None:
send_error("%s is a paid or region app. Could not download." % package_name)
continue
download_apk_url = download_link.attrs["href"]
send_payload(package_name, app_name, download_apk_url)
def main():
# Create the download directory
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
elif not os.path.isdir(DOWNLOAD_DIR):
print("%s is not a directory." % DOWNLOAD_DIR)
return -1
# Read the package names
if not os.path.isfile(PACKAGE_NAMES_FILE):
print("Could not find %s." % PACKAGE_NAMES_FILE)
return -1
with open(PACKAGE_NAMES_FILE, "r") as f:
package_names = [line.strip() for line in f.readlines()]
# CSV file header
with open(OUTPUT_CSV, "w+") as csv:
csv.write("App name,Package name,Size,Location\n")
# Message-passing queues
search_qi = Queue()
search_qo = Queue()
download_qi = Queue()
download_qo = Queue()
# Search Process
search_proc = Process(target=search_process, args=(search_qo, search_qi))
search_proc.start()
# Download Processes
download_procs = []
for i in range(CONCURRENT_DOWNLOADS):
download_proc = Process(target=download_process,
args=(i, download_qo, download_qi))
download_procs.append(download_proc)
download_proc.start()
active_tasks = Counter()
def new_search_query():
if package_names:
search_qo.put((MSG_PAYLOAD, package_names.pop(0)))
active_tasks.inc()
return True
return False
# Send some queries to the search process
for _ in range(CONCURRENT_DOWNLOADS + 1):
new_search_query()
prog_bars = SplitProgBar(CONCURRENT_DOWNLOADS, 80)
def log(msg, pb=True):
prog_bars.clear()
print(msg)
if pb:
prog_bars.render()
sys.stdout.flush()
last_message_time = time.time()
while True:
if active_tasks.empty:
log("Done!", False)
break
no_message = True
try:
# Messages from the search process
message = search_qi.get(block=False)
last_message_time = time.time()
no_message = False
if message[0] == MSG_PAYLOAD:
# Donwload URL found => Start a download
download_qo.put(message)
log(" Found app for %s." % message[1][0])
elif message[0] == MSG_ERROR:
# Error with search query
log("!!" + message[1])
active_tasks.dec()
# Search for another app
new_search_query()
except EmptyQueueException:
pass
try:
# Messages from the download processes
message = download_qi.get(block=False)
last_message_time = time.time()
no_message = False
if message[0] == MSG_PAYLOAD or message[0] == MSG_END:
# Download finished
id_, package_name, app_name, size, location = message[1]
prog_bars[id_] = float("NaN")
if message[0] == MSG_PAYLOAD:
log(" Finished downloading %s." % package_name)
elif message[0] == MSG_END:
log(" File already downloaded for %s." % package_name)
# Add row to CSV file
# with open(OUTPUT_CSV, "a") as csv:
# csv.write(",".join([
# '"%s"' % app_name.replace('"', '""'),
# '"%s"' % package_name.replace('"', '""'),
# "%d" % size,
# '"%s"' % location.replace('"', '""')]))
# csv.write("\n")
active_tasks.dec()
# Search for another app
new_search_query()
elif message[0] == MSG_START:
# Download started
id_, package_name = message[1]
prog_bars[id_] = 0.0
log(" Started downloading %s." % package_name)
elif message[0] == MSG_PROGRESS:
# Download progress
id_, progress = message[1]
prog_bars[id_] = progress
prog_bars.render()
elif message[0] == MSG_ERROR:
# Error during download
id_, msg = message[1]
log("!!" + msg)
prog_bars[id_] = 0.0
active_tasks.dec()
# Search for another app
new_search_query()
except EmptyQueueException:
pass
if no_message:
if time.time() - last_message_time > PROCESS_TIMEOUT:
log("!!Timed out after %.2f seconds." % (PROCESS_TIMEOUT), False)
break
time.sleep(PROGRESS_UPDATE_DELAY / 2.0)
# End processes
search_qo.put((MSG_END, ))
for _ in range(CONCURRENT_DOWNLOADS):
download_qo.put((MSG_END, ))
search_proc.join()
for download_proc in download_procs:
download_proc.join()
return 0
if __name__ == '__main__':
sys.exit(main())