-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapk_decompiler.py
138 lines (97 loc) · 3.74 KB
/
apk_decompiler.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import os
import shutil
import zipfile
from subprocess import call, DEVNULL
tools_path = "tools"
dex2jar_path = tools_path + "/dex2jar/d2j-dex2jar.sh"
jd_path = tools_path + "/jd-cli/jd-cli"
apktool_path = tools_path + "/apktool/apktool"
def decompile_apk(apk_path, output_path, verbose):
print("[+] Decompiling the apk\n")
if verbose:
stdout = None
stderr = None
else:
stdout = DEVNULL
stderr = DEVNULL
if not os.path.exists(apk_path):
print("[-] Error: couldn't find the apk!")
return
apk_name = os.path.splitext(os.path.basename(apk_path))[0]
if os.path.exists("temp"):
print("[~] Removing old temp directory")
shutil.rmtree("temp")
print("[+] Creating temp directory")
os.makedirs("temp")
apk_zip = "temp/" + apk_name + ".zip"
shutil.copy2(apk_path, apk_zip)
apk_unziped_dir = "temp/" + apk_name + "_unziped"
os.makedirs(apk_unziped_dir)
zip_ref = zipfile.ZipFile(apk_zip, 'r')
zip_ref.extractall(apk_unziped_dir)
zip_ref.close()
apk_classes = apk_unziped_dir + "/classes.dex"
if not os.path.exists(apk_classes):
print("[-] Error: the apk doesn't have the classes.dex")
return
print("[+] Getting the jar")
apk_jar = "temp/" + apk_name + ".jar"
call(dex2jar_path + " " + apk_classes + " -o " + apk_jar,
stdout=stdout, stderr=stderr, shell=True)
print("[+] Decompiling the jar")
apk_java = "temp/" + apk_name + "_java/src"
call(jd_path + " " + apk_jar + " -od " + apk_java,
stdout=stdout, stderr=stderr, shell=True)
print("[+] Reverse engineering the apk")
apk_re = "temp/" + apk_name + "_re"
call(apktool_path + " d " + apk_path + " -o " + apk_re,
stdout=stdout, stderr=stderr, shell=True)
print("[+] Organizing everything")
output_dir = os.path.join(output_path, apk_name)
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
os.makedirs(output_dir)
print("[+] Moving reverse engineering files")
re_list = os.listdir(apk_re)
for re_files in re_list:
shutil.move(os.path.join(apk_re, re_files), output_dir)
print("[+] Moving java files")
shutil.move(apk_java, output_dir)
if os.path.exists("temp"):
print("[~] Removing temp directory")
shutil.rmtree("temp")
print("\n[+] Done decompiling the apk")
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--apk", help="Apk file",
required=True)
parser.add_argument("-o", "--output-dir", help="Output directory",
required=False, default=".")
parser.add_argument("-v", "--verbose", help="Enables verbose",
required=False, action="store_true", default=False)
return parser.parse_args()
def verify_tools():
if not os.path.exists(dex2jar_path):
print("[-] Error: 'dex2jar' it's missing from the tools directory")
return False
if not os.path.exists(jd_path):
print("[-] Error: 'jd-cli' it's missing from the tools directory")
return False
if not os.path.exists(apktool_path):
print("[-] Error: 'apktool' it's missing from the tools directory")
return False
return True
def main():
args = get_args()
if not verify_tools():
print("Please check if all the tools are in the 'tools' directory")
print("and if all the tools have the permission to be executable.\n")
print("You can give permissions to the tools with this command: ")
print("sudo chmod -R +x tools")
return
decompile_apk(args.apk, args.output_dir, args.verbose)
if __name__ == "__main__":
main()