def smaliTrans2dex(smaliDir, targetFile, smalijarfile=env.TOOL_DEF_SMALI_JARFILE): ''' @brief smali文件编译打包成dex文件 @param[in] smaliDir smali文件存放目录 @param[in] targetFile 生成的dex文件路径 @return >0:失败; 0:成功 ''' smaliDir = my_utils.getFullPath(smaliDir) targetFile = my_utils.getFullPath(targetFile) smaliFile = my_utils.getToolPath(smalijarfile) if not os.path.exists(targetFile) or not os.path.exists(smaliFile): my_utils.printf("smaliTrans2dex: %s or %s file not exist." % (targetFile, smaliFile)) return 1 cmd = '%s -jar -Xms512m -Xmx512m "%s" "%s" -o "%s"' % ( my_utils.getJavaPath('java'), smaliFile, smaliDir, targetFile) ret = my_utils.execFormatCmd(cmd) if ret: #my_utils.printf("smaliTrans2dex: execFormatCmd(%s) failure ret(%d)." % (cmd,ret)) return ret else: return 0
def decompileApk(apkFile, targetDir, tmpPath, apkTool=env.TOOL_DEF_APKTOOL_JARFILE): ''' @brief 反编译apk文件 @param[in] apkFile apk文件路径 @param[in] targetDir 反编译文件存放目录 @param[in] tmpPath 反编译文件apktool工具 @param[in] apkTool 反编译工具jar包 @return 1:失败; 0:成功 ''' apkFile = my_utils.getFullPath(apkFile) targetDir = my_utils.getFullPath(targetDir) apkTool = my_utils.getToolPath(apkTool) if not os.path.exists(apkFile) or not os.path.exists(apkTool): my_utils.printf("decompileApk: %s or %s file not exist." % (apkFile, apkTool)) return 1 if os.path.exists(targetDir): my_utils.delete_file_folder(targetDir) os.makedirs(targetDir) # tmpPath 没有用 # cmd = '"{0}" -jar "{1}" -q d --frame-path "{2}" -b -f -o "{3}" "{4}"'.format( cmd = '"{0}" -jar "{1}" d --frame-path "{2}" -f -o "{3}" "{4}"'.format( my_utils.getJavaPath('java'), apkTool, tmpPath, targetDir, apkFile) log_utils.getLogger().debug(cmd) ret = my_utils.execFormatCmd(cmd) if ret: #my_utils.printf("decompileApk: execFormatCmd failure ret = %d." % ret) return ret else: return 0
def dexTrans2Smali(dexFile, targetDir, baksmali=env.TOOL_DEF_BAKSMALI_JARFILE): ''' @brief 把dex文件反编译成smali格式文件组成的内容 @param[in] dexFile dex文件路径 @param[in] targetDir smali文件存放 @param[in] baksmali反编译需要的辅助jar包 @return >0:失败; 0:成功 ''' dexFile = my_utils.getFullPath(dexFile) baksmaliFile = my_utils.getToolPath(baksmali) targetDir = my_utils.getFullPath(targetDir) if not os.path.exists(targetDir): os.mkdir(targetDir) if not os.path.exists(dexFile) or not os.path.exists(baksmaliFile): my_utils.printf("dexTrans2Smali: file %s or %s not exist." % (baksmali, dexFile)) return 1 cmd = '%s -jar "%s" -o "%s" "%s"' % (my_utils.getJavaPath('java'), baksmaliFile, targetDir, dexFile) print('--------xxx----' + cmd) ret = my_utils.execFormatCmd(cmd) if ret: #my_utils.printf("dexTrans2Smali: execFormatCmd(%s) failure ret(%d)." % (cmd,ret)) return ret else: return 0
def signApk(apkFile, keyStore, storepassword, keyalias, aliaspassword): ''' @brief 签名apk文件 @param[in] apkFile apk文件路径 @param[in] keyStore keystore文件路径 @param[in] storepassword keystore文件路径 @param[in] keyalias keystore文件路径 @param[in] aliaspassword keystore文件路径 @return >0:失败; 0:成功 ''' apkFile = my_utils.getFullPath(apkFile) aapt = my_utils.getToolPath('aapt') keyStore = my_utils.getFullPath(keyStore) if not os.path.exists(apkFile) or not os.path.exists(keyStore): my_utils.printf("signApk: %s or %s file not exist." % (apkFile, keyStore)) return 2 listcmd = '%s list %s' % (aapt, apkFile) listcmd = listcmd.decode('utf-8') output = os.popen(str(listcmd)).read() for filename in output.split('\n'): if filename.find('META-INF') == 0: rmcmd = '"%s" remove "%s" "%s"' % (aapt, apkFile, filename) print(' --------xxx----' + rmcmd) bReturn = my_utils.execFormatCmd(rmcmd) jarsingnCmd = '"%s" -keystore "%s" -storepass "%s" -keypass "%s" "%s" "%s" -sigalg SHA1withRSA -digestalg SHA1' % \ (my_utils.getJavaPath('jarsigner'), #java的执行文件路径 keyStore, storepassword, aliaspassword, apkFile, keyalias) print('--------xxx----' + jarsingnCmd) ret = my_utils.execFormatCmd(str(jarsingnCmd)) if ret: #my_utils.printf("signApk: execFormatCmd(%s) failure ret(%d)." % (jarsingnCmd,ret)) return ret else: return 0
def recompileApk(srcFolder, apkFile, tmpPath, apkTool=env.TOOL_DEF_APKTOOL_JARFILE): '''重新编译生成Apk文件''' srcFolder = my_utils.getFullPath(srcFolder) apkTool = my_utils.getToolPath(apkTool) if not os.path.exists(apkTool) or not os.path.exists(srcFolder): my_utils.printf("recompileApk: %s or %s file not exist." % (srcFolder, apkTool)) return 1 apkFile = my_utils.getFullPath(apkFile) # cmd = '"{0}" -jar "{1}" -q b --frame-path "{2}" -f -o "{3}" "{4}"'.format( # 调用apktool.jar 把反编译后的目录,编译成apkFile cmd = '"{0}" -jar "{1}" b --frame-path "{2}" -f -o "{3}" "{4}"'.format( my_utils.getJavaPath('java'), apkTool, tmpPath, apkFile, srcFolder) print('--------xxx----' + cmd.decode('utf-8')) ret = my_utils.execFormatCmd(cmd) if ret: #my_utils.printf("recompileApk: execFormatCmd failure ret = %d." % ret) return 2 else: return 0