-
Notifications
You must be signed in to change notification settings - Fork 0
/
deodexApks.py
executable file
·67 lines (52 loc) · 2.83 KB
/
deodexApks.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
#!/usr/bin/env python2.7
# Dependence: 'zipalign' in PATH
import os, sys, shutil, argparse, tempfile;
from zipfile import ZipFile, ZIP_STORED;
from WillirPy2_7Utils import ArgParseOdexList, ArgsReadableDir;
from WillirPyUtils import checkCall;
from certDelFromApks import removeCertFromApk;
def deodexApk(dexPath, apkPath, frameworksDir, verbose=False, removeOdexFile=True):
tmpSmaliDir = tempfile.mkdtemp();
tmpDexFile = tempfile.NamedTemporaryFile();
try:
if verbose: sys.stderr.write("Baksmaling... ");
checkCall("baksmali -x -d '%s' -o '%s' '%s'" % (frameworksDir, tmpSmaliDir, dexPath));
if verbose: sys.stderr.write("Smaling... ");
checkCall("smali -o '%s' '%s'" % (tmpDexFile.name, tmpSmaliDir));
if verbose: sys.stderr.write("Adding dex to zip... ");
with ZipFile(apkPath, mode='a', compression=ZIP_STORED) as zipApk:
zipApk.write(tmpDexFile.name, arcname='classes.dex');
removeCertFromApk(apkPath);
if removeOdexFile:
os.remove(dexPath);
finally:
shutil.rmtree(tmpSmaliDir);
tmpDexFile.close();
def deodexListOfApk(odexList, frameworksDir, verbose=False):
frameworksDir = os.path.abspath(frameworksDir);
for (dexPath, apkPath) in odexList:
if verbose: sys.stderr.write("Deodexing '%s': " % dexPath[:-5]);
deodexApk(dexPath, apkPath, frameworksDir, removeOdexFile=False, verbose=verbose);
if verbose: sys.stderr.write("Done!\n");
if verbose: sys.stderr.write("\nRemoving odex files...!");
for (dexPath, _) in odexList:
os.remove(dexPath);
if verbose: sys.stderr.write("Done!\n");
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Deodex apk files.\n" +
"Sign will be removed from apk.\n" +
"Dependences: 'baksmali', 'smali', 'zip' utils in PATH.",
formatter_class=argparse.RawTextHelpFormatter);
parser.add_argument("-d", "--bootclasspath-dir", dest="frameworksDir", action=ArgsReadableDir,
required=True,
help="the base folder to look for the bootclasspath files in.");
parser.add_argument("-s", "--silence", dest="silence", action="store_true",
help="No verbose output");
parser.add_argument("-r", "--recursive", dest="recursive", action="store_true",
help="make recursive search for all odexed apk files in dirs");
parser.add_argument("odexList", nargs="+", action=ArgParseOdexList,
help="List of odexed apk files." +
"It can be odex file, or apk file, or name without extension.");
args = parser.parse_args();
deodexListOfApk(args.odexList, args.frameworksDir, verbose=(not args.silence));