본문 바로가기
HACKING/mobile_Android

Android setting for Pentesting#1 Android Setting Code

by asdf12345 2020. 6. 3.

 

[ Android 프로젝트를 위한 폴더, 디컴파일 소스코드 등 Setting ]

 

 

1. Function

APK 다운로드 폴더 입력 시,

폴더하위 내 4개의 폴더([1]Smali, [2]Java, [3]Sign, [4]Document) 생성

[1]Smali 하위 폴더 내 APK Smali Code 저장

[3]Sign 하위 폴더 내 repacakaging.cmd(리패키징 자동화 파일) 생성

 

 

2. How to use

#0 apktool & signapk Download 한 후, set_apk.py 소스코드 내 apktool_path, sign_dir 변수 값 지정 

#1 폴더 생성 후, 하위에 APK 파일 삽입

#2 apk_set.py 실행 후, APK 존재 경로 입력 

 

 

3. Files

APKTool Download URL

https://ibotpeaches.github.io/Apktool/install/bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.4.1.jar

 

Signapk File Download 

Signapk.7z
0.01MB

 

Python sourcecode

import os

apktool_path = ""
sign_dir=""

def search(dirname,key):
    filenames = os.listdir(dirname)
    for filename in filenames:
        full_filename = os.path.join(dirname, filename)
        if full_filename.count(key)>0:
            return full_filename

def make_dir(dirname,dir_name2):
    try:
        if not(os.path.isdir(os.path.join(dirname,dir_name2))):
            os.makedirs(os.path.join(dirname,dir_name2))
            if dir_name2=="[1]Smali":
                smali_path=os.path.join(dirname,"[1]Smali")
                os.system("copy {} {}".format(apktool_path,smali_path)) #copy apktool in Smali Folder
                os.system("copy {} {}".format(dirname+".apk",smali_path)) #copy apk file in Smali Folder to decompile
    except OSError as e:
        if e.errno != errno.EEXIST:
            print("Failed to create directory")
            raise            
        
def decompile(smali_path):
    os.system("cd {}".format(smali_path))
    os.system("java -jar {} d {} -o {}".format(
        search(smali_path,"apktool"),
        search(smali_path,".apk"),
        search(smali_path,".apk")[:-4]))

def copy_signfile(full_filenm):
    os.system("copy {} {}".format(sign_dir,os.path.join(full_filenm[:-4],"[3]Sign\\")))

def apk_compile(smali_path):
    os.system("cd {}".format(smali_path))
    os.system("java -jar {} b {}".format(search(smali_path,"apktool"), search(smali_path,".apk")[:-4]))

def resign(smali_path,sign_path):
    os.system("copy {} {}".format(search(
        os.path.join(search(smali_path,".apk")[:-4],"dist"),".apk"),
        os.path.join(sign_path+"\\")
        )
    )
    os.system("cd {}".format(sign_path))
    apk_path=search(sign_path,".apk")
    os.system("java -jar signapk.jar certificate.pem key.pk8 {} {}".format(apk_path,apk_path[:-4]+"_repack.apk"))


def make_cmd(file_path):
    sign_path=os.path.join(file_path,"[3]Sign")
    smali_path=os.path.join(file_path,"[1]Smali")
    apk_path=search(sign_path,".apk")
    with open(os.path.join(file_path,"[3]Sign","repackaging.cmd"), "w+") as f:
        f.write("cd {}\n".format(smali_path))
        f.write("java -jar {} b {}\n".format(search(smali_path,"apktool"), search(smali_path,".apk")[:-4]))
        f.write("copy {} {}\n".format(
            search(
                os.path.join(search(smali_path,".apk")[:-4],"dist"),".apk"),
                os.path.join(sign_path+"\\")
            )
        )
        f.write("cd {}\n".format(sign_path))
        f.write("java -jar signapk.jar certificate.pem key.pk8 {} {}\n".format(apk_path,apk_path[:-4]+"_repack.apk"))


dirname=input("What is the directory path? : ")
full_filenm=search(dirname,".apk")
dir_name2=["[1]Smali","[2]Java","[3]Sign","[4]Document"]

print("[+] Decompile {}".format(full_filenm[full_filenm.rfind("\\")+1:]))
for i in dir_name2:
    make_dir(full_filenm[:-4],i)
print("[+] Copy SignApk files to under [3]Sign\\")
copy_signfile(full_filenm)
decompile(os.path.join(full_filenm[:-4],"[1]Smali"))
apk_compile(os.path.join(full_filenm[:-4],"[1]Smali"))
resign(os.path.join(full_filenm[:-4],"[1]Smali"),os.path.join(full_filenm[:-4],"[3]Sign"))
make_cmd(full_filenm[:-4])