This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathapk2java.py
executable file
·169 lines (135 loc) · 5.74 KB
/
apk2java.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
import sys, os, string
import urllib.request
import zipfile
import subprocess
from optparse import OptionParser
class APK2Java:
external = "https://github.com/TheZ3ro/apk2java-linux/releases/download/tool2/tool.zip"
def __init__(self):
self.home = ''
self.tool = ''
self.outdir = ''
self.apk_file = ''
self.project_name = ''
self.smali = False
self.nosc = False
def _call(self, cmd, **kwargs):
print('Running: {0}'.format(' '.join(cmd)))
return subprocess.call(cmd, **kwargs)
def _report(self, blocknr, blocksize, size):
current = blocknr * blocksize
sys.stdout.write("\rProgress: {0:.2f}%".format(100.0 * current / size) + " - {0:.1f} MB".format(
current / 1024 / 1024) + "/{0:.1f} MB".format(size / 1024 / 1024))
def _check_home(self, path):
return os.path.isdir(path + "/tool")
def _getunzipped(self, theurl, thedir, report):
if not os.path.exists(thedir):
os.mkdir(thedir)
print("Downloading external tool... -> " + thedir + "/tool/")
name = os.path.join(thedir, 'temp.zip')
try:
name, hdrs = urllib.request.urlretrieve(theurl, name, report)
except IOError as e:
print("Can't retrieve %r to %r: %s" % (theurl, thedir, e))
return
try:
z = zipfile.ZipFile(name)
except zipfile.error as e:
print("Bad zipfile (from %r): %s" % (theurl, e))
return
for n in z.namelist():
(dirname, filename) = os.path.split(n)
perm = ((z.getinfo(n).external_attr >> 16) & 0x0777)
if filename == '':
# directory
newdir = thedir + '/' + dirname
if not os.path.exists(newdir):
os.mkdir(newdir)
else:
# file
fd = os.open(thedir + "/" + n, os.O_CREAT | os.O_WRONLY, perm)
os.write(fd, z.read(n))
os.close(fd)
z.close()
os.unlink(name)
print("")
def check_tools(self, home):
if self._check_home(home) == False:
self._getunzipped(APK2Java.external, home, self._report)
self.home = home
self.tool = os.path.join(self.home, 'tool/')
def _print_header(self, text):
block = "*********************************************"
print(block)
print('**' + text.center(len(block) - 4) + '**')
print(block)
def _apktool(self):
self._print_header('Extract, fix resource files')
if self.apk_file != '':
cmd = ['java', '-jar', os.path.join(self.tool, 'apktool_2.3.0.jar'), 'd', self.apk_file, '-o', self.out, '-f']
if not self.smali:
cmd[-1] = '-sf'
self._call(cmd)
if not self.smali:
self._call(['mv', os.path.join(self.out, 'classes.dex'), os.path.join(self.out, 'original/')])
print('Done')
def _dex2jar(self):
self._print_header("Convert 'apk' to 'jar'")
if self.apk_file != '':
self._call([os.path.join(self.tool, 'dex2jar-2.0/d2j-dex2jar.sh'), '-f', '-o', self.out + '.jar', self.apk_file])
print('Done')
def _cfr(self):
self._print_header('Decompiling class files')
if self.apk_file != '':
self._call(['java', '-jar', os.path.join(self.tool, 'cfr_0_123.jar'), self.out + '.jar',
'--outputdir', os.path.join(self.out, 'src/')])
print('Done')
def decompile(self, file):
if os.path.isfile(file) and os.path.splitext(file)[-1].lower() == '.apk':
self.apk_file = file
self.project_name = os.path.splitext(os.path.basename(file))[0].lower()
self.out = os.path.join(self.outdir, self.project_name)
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
self._call(["cp", self.apk_file, os.path.join(self.outdir, self.project_name + "-new.apk")])
self._apktool()
if self.smali == False and self.nosc == False:
self._dex2jar()
self._cfr()
print("Output source is at {}".format(self.out))
else:
raise Exception("You must select a valid APK file!")
def main():
# current working directory
cwd = os.getcwd()
# apk2java installation path
home = os.path.dirname(os.path.realpath(sys.argv[0]))
usage = "usage: %prog file [options]"
parser = OptionParser(usage=usage)
parser.add_option("--java", action="store_true", dest="java", default=True,
help="select java source format [DEFAULT]")
parser.add_option("--smali", action="store_true", dest="smali", default=False, help="select smali source format")
parser.add_option("--no-source", action="store_true", dest="nosc", default=False, help="no source code generation")
parser.add_option("-o", dest="outdir", default=cwd, help="specify the output directory "
+ "(if not specified the decomipled version will be store in a folder in the script directory)")
(options, args) = parser.parse_args()
app = APK2Java()
# check if tools are instaled in the script dir
app.check_tools(home)
# by default is cwd
app.outdir = options.outdir
if (options.smali + options.nosc) > 1:
parser.error("You can only select 1 source format --[smali/java/no-source]")
exit(1)
app.smali = options.smali
app.nosc = options.nosc
if len(args) == 1:
app.decompile(args[0])
else:
parser.print_help()
# Script start Here
if __name__ == "__main__":
main()