-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdeoat.py
executable file
·274 lines (198 loc) · 8.56 KB
/
deoat.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
#!/usr/bin/python
# Copyright 2015 Coron
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Convert the OAT format on ART to DEX format on DALVIKVM.
Usage: deoat.py [OPTIONS] <otapackage.zip> [<otapackage.deoat.zip>]
OPTIONS:
--app, -a: only de-oat the apk in system.
--framework, -f: only de-oat the jar in system.
"""
__author__ = '[email protected]'
import os
import commands
import re
import shutil
from common import Utils, Log
# Global
TAG="reverse-deoat"
OPTIONS = None
class OatZip:
""" Model of OAT ZIP file
"""
OAT2DEX = os.path.join(os.path.dirname(__file__), "de-oat", "oat2dex.sh")
def __init__(self, unzipRoot):
self.mRoot = unzipRoot
self.mFrwDir = os.path.join(self.mRoot, "system/framework")
self.mAppDir = os.path.join(self.mRoot, "system/app")
self.mPrivAppDir = os.path.join(self.mRoot, "system/priv-app")
# boot.oat
self.mBootOAT = self.findBootOAT()
if self.mBootOAT != None:
self.mBootOATDir = os.path.dirname(self.mBootOAT)
self.mBootClassFolder = os.path.join(self.mBootOATDir, "dex")
def findBootOAT(self):
""" Find the absolute path of boot.oat
In Android 5.0+, all the jars of BOOTCLASSPATH are packaged into boot.oat
"""
bootOATPath = os.path.join(self.mFrwDir, "arm/boot.oat")
if os.path.exists(bootOATPath):
return bootOATPath
bootOATPath = os.path.join(self.mFrwDir, "x86/boot.oat")
if os.path.exists(bootOATPath):
return bootOATPath
bootOATPath = None
cmd = "find %s -name boot.oat" % (commands.mkarg(self.mFrwDir))
(sts, text) = commands.getstatusoutput(cmd)
try:
if sts == 0:
text = text.split("\n")[0]
if len(text) > 0:
return text
except:
bootOATPath = None
return bootOATPath
def deoat(self):
""" De-oat the OTA package.
"""
if self.mBootOAT == None:
Log.i(TAG, "deoat(): boot.oat not found in %s, nothing need deoat" % self.mRoot)
return self
if os.path.exists(self.mBootClassFolder):
Log.d(TAG, "Delete the already exists %s" %self.mBootClassFolder)
shutil.rmtree(self.mBootClassFolder)
# Phase 1: de-oat boot.oat
OatZip.deoatBootOAT(self.mBootOAT)
# Phase 2: de-oat all the other oat files, of which suffix is odex.
# [Android 5.0]: All the oat jars are located in the same folder with boot.oat
OatZip.deoatFrw(self.mBootOATDir)
# Phase 3: de-oat app
OatZip.deoatApp(self.mFrwDir, self.mBootClassFolder)
OatZip.deoatApp(self.mAppDir, self.mBootClassFolder)
OatZip.deoatApp(self.mPrivAppDir, self.mBootClassFolder)
return self
def rebuild(self):
""" Rebuild the deoated zip
"""
if self.mBootOAT == None:
Log.i(TAG, "rebuild(): boot.oat not found, nothing need rebuild")
return
OatZip.repackageFrw(self.mFrwDir, self.mBootClassFolder)
OatZip.repackageApp(self.mFrwDir)
OatZip.repackageApp(self.mAppDir)
OatZip.repackageApp(self.mPrivAppDir)
# Remove the whole OAT directory
if os.path.exists(self.mBootOATDir):
shutil.rmtree(self.mBootOATDir)
@staticmethod
def deoatBootOAT(bootOAT):
""" De-oat boot.oat
"""
Log.i(TAG, "De-oat %s" % bootOAT)
Utils.runWithOutput([OatZip.OAT2DEX, "boot", bootOAT])
@staticmethod
def deoatFrw(oatJarDir):
""" De-oat framework
"""
if not OPTIONS.formatFrw: return
Log.i(TAG, "De-oat files of oat-format in %s" % oatJarDir)
for item in os.listdir(oatJarDir):
if item.endswith(".odex"):
# COMMANDS: oat2dex boot <jar-of-oat-format>
oatJar = os.path.join(oatJarDir, item)
Utils.runWithOutput([OatZip.OAT2DEX, "boot", oatJar])
@staticmethod
def deoatApp(oatApkDir, bootClassFolder):
""" De-oat app
"""
if OPTIONS.formatApp == False: return
Log.i(TAG, "De-oat files of oat-format in %s, with BOOTCLASSFOLDER=%s" %(oatApkDir, bootClassFolder))
for (dirpath, dirnames, filenames) in os.walk(oatApkDir):
dirnames = dirnames # no use, to avoid warning
for filename in filenames:
if filename.endswith(".odex"):
# no need to de-oat if original apk does not exist
apkFile = filename[0:-5] + ".apk"
apkPath = os.path.dirname(dirpath)
if not os.path.exists(os.path.join(apkPath, apkFile)):
continue
oatApk = os.path.join(dirpath, filename)
deoatApk = oatApk[0:-5] + ".dex"
if os.path.exists(deoatApk):
Log.d(TAG, "Delete the already exists %s" % deoatApk)
os.remove(deoatApk)
Utils.runWithOutput([OatZip.OAT2DEX, oatApk, bootClassFolder])
@staticmethod
def repackageFrw(frwDir, bootClassFolder):
""" Repackage the classes.dex into jar of frwDir.
"""
if OPTIONS.formatFrw == False : return
# Keep the old directory, we will change back after some operations.
oldDir = os.path.abspath(os.curdir)
# Some dexFiles are parted, such as framework-classes2.dex
regex = re.compile("(.*)-(classes\d?).dex")
Log.i(TAG, "Repackage JARs of %s" %(frwDir))
os.chdir(frwDir)
for dexFile in os.listdir(bootClassFolder):
if dexFile.endswith(".dex"):
jarFile = dexFile[0:-4] + ".jar"
dexName = "classes.dex"
if not os.path.exists(jarFile):
# Match out the jar file with regex
matcher = regex.match(dexFile)
if matcher != None:
jarFile = matcher.group(1) + ".jar"
dexName = matcher.group(2) + ".dex"
Log.d(TAG, "Repackage %s" %(jarFile))
# Put the dex and framework's jar in the same folder, and jar into the jarFile
shutil.move(os.path.join(bootClassFolder, dexFile), os.path.join(frwDir, dexName))
Utils.runWithOutput(["jar", "uf", jarFile, dexName])
if os.path.exists(dexName):
os.remove(dexName)
os.chdir(oldDir)
@staticmethod
def repackageApp(appDir):
""" Repackage the classes.dex into apk of appDir
"""
if OPTIONS.formatApp == False: return
# Keep the old directory, we will change back after some operations.
oldDir = os.path.abspath(os.curdir)
Log.i(TAG, "Repackage APKs of %s" %(appDir))
for (dirpath, dirnames, filenames) in os.walk(appDir):
dirnames = dirnames # no use, to avoid warning
for dexFile in filenames:
if dexFile.endswith(".dex"):
apkFile = dexFile[0:-4] + ".apk"
apkPath = os.path.dirname(dirpath)
if not os.path.exists(os.path.join(apkPath, apkFile)):
Log.d(TAG, "No apk matched with %s, Ignore" %dexFile)
continue
dexName = "classes.dex"
Log.d(TAG, "Repackage %s" %(apkPath))
# Put the dex and apk in the same folder, and jar into the apk
shutil.move(os.path.join(dirpath, dexFile), os.path.join(apkPath, dexName))
os.chdir(apkPath)
Utils.runWithOutput(["jar", "uf", apkFile, dexName])
if os.path.exists(dexName):
os.remove(dexName)
shutil.rmtree(dirpath)
os.chdir(oldDir)
def debug():
Log.DEBUG = True
root = "root directory the unziped files"
#root = "/w/code/smali-5.0/devices/sony/out/tmp"
OatZip(root).deoat()
if __name__ == "__main__":
debug()