-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapk_grabber.py
More file actions
executable file
·80 lines (58 loc) · 2.45 KB
/
apk_grabber.py
File metadata and controls
executable file
·80 lines (58 loc) · 2.45 KB
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
#!/usr/bin/env python
# coding=utf-8
"""
Grab apks
"""
import argparse
import os
import re
import subprocess
import sys
import urllib
import zipfile
def execute(cmd):
print "running shell command:\n " + cmd
try:
return subprocess.check_output(cmd, shell=True).strip()
except subprocess.CalledProcessError, e:
print "Error output:\n ", e.output
raise e
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
description="apk grabber!", parents=())
parser.add_argument('patterns', metavar='pattern', type=str, nargs='+',
help='pattern(s) to match package names on the device')
parser.add_argument('--output_dir', dest='output', action='store', default='output',
help="output directory of the apk and jar files")
args = parser.parse_args()
print args.patterns
# grab them from device
packages = execute("adb shell pm list packages".format(**locals()))
# print packages
matching_packages = []
print "matching packages to {args.patterns}".format(**locals())
# for each pattern, grep the name and store in a big list
for pattern in args.patterns:
for line in packages.splitlines():
if re.search(pattern, line):
matching_packages.append(line.split(':')[1])
print "matched these packages on the device:" + str(matching_packages)
for matching_package in matching_packages:
# find path to apk
apk_path = execute("adb shell pm path {matching_package}".format(**locals())).split(':')[1]
apk_base_file_name = os.path.join(args.output, matching_package)
output_apk_path = apk_base_file_name + ".apk"
# pull apk off device
execute("adb pull {apk_path} {output_apk_path}".format(**locals()))
# unzip apk contents (gets everything but dex)
execute("rm -rf {apk_base_file_name}".format(**locals()))
execute("mkdir {apk_base_file_name}".format(**locals()))
execute("unzip -x {output_apk_path} -d {apk_base_file_name}".format(**locals()))
classes_files = execute("ls {apk_base_file_name}/classes*.dex".format(**locals())).split()
for dex_class_file in classes_files:
# decompile apk into jar file(s)
execute("dex2jar-2.0/d2j-dex2jar.sh -f -o {dex_class_file}.jar {dex_class_file}".format(**locals()))
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))