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
|
""" @author: Zeuxis Lo @structure: - dex - apk - apktool - dex2jar @usage: @setup apk directory - python run.py -b
@other - python run.py -h """ import os, optparse, subprocess, time
CURRENT_ROOT = os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/') APK_ROOT = CURRENT_ROOT + "/apk" APP_ROOT = APK_ROOT + "/app" UNZIP_ROOT = APK_ROOT + "/unzip" JAR_ROOT = APK_ROOT + "/jar" PACKAGE_ROOT = APK_ROOT + "/package"
DEX_SCRIPT = CURRENT_ROOT + "/dex2jar/dex2jar.sh" APKTOOL_SCRIPT = CURRENT_ROOT + "/apktool/apktool"
def makeCommand(command): print(outputColor("Command: %s" % command, True, True))
result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True).communicate()[0] if len(result) > 0: for line in result.split("\n"): print(" %s" % line)
def outputColor(string, status, bold): attr = [] if status: attr.append('32') else: attr.append('31')
if bold: attr.append('1')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
class FileController(object): def __init__(self): pass
def get_apks(self): apks = {};
for root, dirs, files in os.walk(APP_ROOT): for file in files: apks[len(apks) + 1] = file
return apks;
def list(self): for id, apk in self.get_apks().iteritems(): print("%s. %s" % (id, apk))
def rename(self): for id, apk in self.get_apks().iteritems(): old_apk_name = apk new_apk_name = '_'.join(apk.split(".")[:-1]) + "." + apk.split(".")[-1:][0];
old_apk_path = APP_ROOT + '/' + old_apk_name new_apk_path = APP_ROOT + '/' + new_apk_name
print("%20s => %20s" % (old_apk_name, new_apk_name)) os.rename(old_apk_path, new_apk_path)
def remove(self, apk): file_root = APP_ROOT + "/" + apk
if os.path.exists(file_root) and os.path.isfile(file_root): file_prefix = apk.split(".")[:-1][0];
makeCommand("rm -rf %s*" % (UNZIP_ROOT + "/" + file_prefix)) makeCommand("rm -rf %s*" % (JAR_ROOT + "/" + file_prefix)) makeCommand("rm -rf %s*" % (PACKAGE_ROOT + "/" + file_prefix)) else: print(outputColor("Can not found App file", False, False));
def clear(self): for directory in [UNZIP_ROOT, JAR_ROOT, PACKAGE_ROOT]: makeCommand("rm -rf %s" % directory)
def build(self): for directory in [APP_ROOT, UNZIP_ROOT, JAR_ROOT, PACKAGE_ROOT]: if os.path.exists(directory) and os.path.isdir(directory): print(outputColor("Exists: %s" % directory, False, False)) else: makeCommand("mkdir %s" % directory)
class DecompileController(object): def __init__(self): pass
def file(self, apk): file_root = APP_ROOT + "/" + apk unzip_file_root = UNZIP_ROOT + "/" + apk de_apktool_file_root = os.path.splitext(CURRENT_ROOT + "/" + apk)[0] dex_file = unzip_file_root + "/classes.dex" jar_file = JAR_ROOT + "/" + apk + ".jar";
if os.path.exists(file_root) and os.path.isfile(file_root):
if os.path.exists(unzip_file_root) and os.path.isdir(unzip_file_root): makeCommand("rm -Rf %s" % unzip_file_root);
makeCommand("mkdir %s" % unzip_file_root) makeCommand("tar zxvf %s -C %s" % (file_root, unzip_file_root)) makeCommand("%s %s" % (DEX_SCRIPT, dex_file)) makeCommand("mv %s/*.jar %s" % (unzip_file_root, jar_file))
makeCommand("%s d -f %s" % (APKTOOL_SCRIPT, file_root)) makeCommand("cp -rf %s %s" % (de_apktool_file_root, PACKAGE_ROOT)) makeCommand("rm -Rf %s" % de_apktool_file_root) else: print(outputColor("Can not found App file", False, False));
def directory(self, directory): if directory is not True: print("%12s %s" % ("Direcotry:", directory))
if os.path.exists(directory) and os.path.isdir(directory): print("%12s Success" % "Is Exists:") print("%12s %s" % ("Switch to:", directory))
new_app_root = directory else: print(outputColor("%12s Faild" % "Is Exists:", False, False)) print("%12s %s" % ("Switch to:", APP_ROOT))
new_app_root = APP_ROOT
time.sleep(3);
for id, apk in FileController().get_apks().iteritems(): print(""); print("%12s %s" % ("Processing:", apk)) self.file(apk) else: print("%12s %s" % ("Lookup:", directory)) self.directory(directory);
if __name__ == "__main__": parser = optparse.OptionParser(usage="Usage: %prog [options]") parser.add_option("-r", "--rename", action="store_true", dest="rename", help="rename all apks") parser.add_option("-l", "--list", action="store_true", dest="list", help="list all apks") parser.add_option("-f", "--file", action="store", dest="file", help="decompile file") parser.add_option("-u", "--use_directory", action="store", dest="directory", help="decompile directory by input") parser.add_option("-d", "--default_direcotry", action="store_true", dest="default_directory", help="decompile default directory")
group = optparse.OptionGroup(parser, "Other Helpers") group.add_option("-x", "--remove", action="store", dest="remove", help="remove unzip, jar and package by FILE") group.add_option("-c", "--clear", action="store_true", dest="clear", help="clear unzip, jar and package") group.add_option("-b", "--build", action="store_true", dest="build", help="build decompile directory structure") parser.add_option_group(group)
(options, args) = parser.parse_args()
if options.rename: FileController().rename() elif options.list: FileController().list() elif options.file: DecompileController().file(options.file) elif options.directory: DecompileController().directory(options.directory) elif options.default_directory: DecompileController().directory(APP_ROOT) elif options.remove: FileController().remove(options.remove) elif options.clear: FileController().clear() elif options.build: FileController().build() else: parser.print_help()
|