# 批量反编译
# *_* coding:UTF-8 *_*
import os
import subprocess

count = [0, 0]  # 记录样本中恶意代码和良性代码数量

def decompiling(from_path, to_path, index):

    files = os.listdir(from_path)
    total = len(files)

    for cnt, fileName in enumerate(files):
        fromPath = os.path.join(from_path, fileName)
        toPath = os.path.join(to_path, fileName)
        command = "apktool d " +  fromPath + " -o " + toPath  # 加壳会无法反编译，得到空文件夹
        subprocess.call(command, shell=True)
        count[index] += 1
        print("%s已完成" % fileName)
        print("进度：", (cnt + 1)/total * 100, "%")


if __name__ == "__main__":
    apk_type = ['kind', 'virus']
    base_path = os.path.dirname(os.getcwd()) + r'\apk_sample'
    smaliPath = r"smali"
    for ind, tp in enumerate(apk_type):
        decompiling(base_path+'\\'+tp, smaliPath+'\\'+tp, ind)
