Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
UPDATE: old code
Browse files Browse the repository at this point in the history
  • Loading branch information
thez3ro committed Jul 5, 2016
2 parents b11fa47 + 2c22142 commit b7373dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ This project is a porting for Linux/*nix of https://github.com/rajivvishwa/apk2j

Introduction
--------------------
This script decompiles an apk to its corresponding java sources. Security code review can be done on theses generated applicaion source files so as to identify any potential vulnerabilities present.
This is not made to encourage piracy/plagiarism.
This script decompiles an apk to its corresponding java sources. Security code review can be done on theses generated applicaion source files so as to identify any potential vulnerabilities present.
This is not made to encourage piracy/plagiarism.

*** This script just automates the sequence in which various tools are initiated and does not handle any error events. You will have to go through the cmd verbose to figure out the problem. ***

Usage
--------------------
```
Usage: apk2java.py action file [options]
Usage: apk2java.py action ApkFileName [options]
action can only be 'b' (for build) and 'd' (for decompile)
Options:
-h, --help show this help message and exit
--java select java source format [DEFAULT]
Expand All @@ -40,3 +41,4 @@ apk-tool : http://code.google.com/p/android-apktool/

baksmali : http://code.google.com/p/smali/

Note: These tools and a sample apk are downloaded by the script. There is not need to download them manually
59 changes: 32 additions & 27 deletions apk2java.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
apk_folder=''
project_name=''
sign_file=''
cwd=os.path.dirname(os.path.abspath(__file__))
home=os.path.dirname(os.path.realpath(sys.argv[0]))
tmp='/tmp/apk2java/'
outdir=os.path.dirname(os.path.realpath(sys.argv[1]))
external="https://github.com/TheZ3ro/apk2java-linux/releases/download/tool/tool.zip"

def check_home(path):
Expand Down Expand Up @@ -59,87 +60,90 @@ def apktool(smali):
print ("*********************************************")
if apk_file != '':
if smali == True:
call(home+'/tool/apktool_200rc3.jar d '+apk_file+' -o '+tmp+project_name+' -f',shell=True)
call(home+'/tool/apktool_200rc3.jar d '+apk_file+' -o '+outdir+project_name+' -f',shell=True)
else:
call(home+'/tool/apktool_200rc3.jar d '+apk_file+' -o '+tmp+project_name+' -sf',shell=True)
os.system('mv %s %s' % (tmp+project_name+'/classes.dex', tmp+project_name+'/original/'))
call(home+'/tool/apktool_200rc3.jar d '+apk_file+' -o '+outdir+project_name+' -sf',shell=True)
os.system('mv %s %s' % (outdir+project_name+'/classes.dex', outdir+project_name+'/original/'))
print ('Done')

def dex2jar():
print ("*********************************************")
print ("** Convert 'apk' to 'jar' **")
print ("*********************************************")
if apk_file != '':
call(home+'/tool/dex2jar-0.0.9.15/d2j-dex2jar.sh -f -o '+tmp+project_name+'.jar '+apk_file, shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-asm-verify.sh '+tmp+project_name+'.jar',shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-dex2jar.sh -f -o '+outdir+project_name+'.jar '+apk_file, shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-asm-verify.sh '+outdir+project_name+'.jar',shell=True)
print ('Done')

def procyon():
print ("*********************************************")
print ("** Decompiling class files **")
print ("*********************************************")
if apk_file != '':
call(home+'/tool/procyon-decompiler-0528.jar -jar '+tmp+project_name+'.jar -o '+tmp+project_name+'/src/',shell=True)
call(home+'/tool/procyon-decompiler-0528.jar -jar '+outdir+project_name+'.jar -o '+outdir+project_name+'/src/',shell=True)
print ('Done')

def apktool_build():
print ("*********************************************")
print ("** Building apk from smali **")
print ("*********************************************")
if apk_folder != '':
call(home+'/tool/apktool_200rc3.jar b '+apk_folder+' -o '+tmp+project_name+'-rebuild.apk',shell=True)
global sign_file
sign_file = tmp+project_name+'-rebuild.apk'
call(home+'/tool/apktool_200rc3.jar b '+apk_folder+' -o '+outdir+project_name+'-rebuild.apk',shell=True)
global sign_file
sign_file = outdir+project_name+'-rebuild.apk'
print ('Done')

def jar2jasmin():
print ("*********************************************")
print ("** Convert 'jar' to 'jasmin' **")
print ("*********************************************")
if apk_file != '':
call(home+'/tool/dex2jar-0.0.9.15/d2j-jar2jasmin.sh -f -o '+tmp+project_name+'/jasmin '+tmp+project_name+'.jar',shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-jar2jasmin.sh -f -o '+outdir+project_name+'/jasmin '+outdir+project_name+'.jar',shell=True)
print ('Done')

def jasmin_build():
print ("*********************************************")
print ("** Build apk from jasmin **")
print ("*********************************************")
if apk_folder != '':
call(home+'/tool/dex2jar-0.0.9.15/d2j-jasmin2jar.sh -f -o '+tmp+project_name+'-new.jar '+tmp+project_name+'/jasmin',shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-asm-verify.sh '+tmp+project_name+'-new.jar',shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-jar2dex.sh -f -o '+tmp+project_name+'/classes.dex '+tmp+project_name+'-new.jar',shell=True)
call('zip -r '+tmp+project_name+'-new.apk -j '+tmp+project_name+'/classes.dex',shell=True)
global sign_file
sign_file = tmp+project_name+'-new.apk'
call(home+'/tool/dex2jar-0.0.9.15/d2j-jasmin2jar.sh -f -o '+outdir+project_name+'-new.jar '+outdir+project_name+'/jasmin',shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-asm-verify.sh '+outdir+project_name+'-new.jar',shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-jar2dex.sh -f -o '+outdir+project_name+'/classes.dex '+outdir+project_name+'-new.jar',shell=True)
call('zip -r '+outdir+project_name+'-new.apk -j '+outdir+project_name+'/classes.dex',shell=True)
global sign_file
sign_file = outdir+project_name+'-new.apk'
print ('Done')

def sign():
print ("*********************************************")
print ("** Sign apk **")
print ("*********************************************")
call(home+'/tool/dex2jar-0.0.9.15/d2j-apk-sign.sh -f -o '+tmp+project_name+'-signed.apk '+sign_file,shell=True)
call(home+'/tool/dex2jar-0.0.9.15/d2j-apk-sign.sh -f -o '+outdir+project_name+'-signed.apk '+sign_file,shell=True)
print ('Done')

def main():
global apk_folder,apk_file,project_name,home
global apk_folder,apk_file,project_name,home,outdir
usage = "usage: %prog action file [options]"
parser = OptionParser(usage=usage)
parser.add_option("--java",action="store_true", dest="java", default=True, help="select java source format [DEFAULT]")
parser.add_option("--smali",action="store_true", dest="smali", default=False, help="select smali source format")
parser.add_option("--jasmin",action="store_true", dest="jasmin", default=False, help="select jasmin source format")
parser.add_option("--no-source",action="store_true", dest="nosc", default=False, help="no source code generation")
parser.add_option("-o", dest="outdir", default=cwd+"/", help="specify the output directory "
+"(if not specified the decomipled version will be store in a folder in the script directory)")
(options, args) = parser.parse_args()

if home == "/opt/apk2java":
if home == cwd+"/apk2java":
if check_home(home) == False:
getunzipped(external, home, report)
else:
if check_home(home) == False:
if check_home("/opt/apk2java") == False:
getunzipped(external, "/opt/apk2java", report)
home = "/opt/apk2java"
if check_home(cwd+"/apk2java") == False:
getunzipped(external, cwd+"/apk2java", report)
home = cwd+"/apk2java"
else:
home = "/opt/apk2java"
home = cwd+"/apk2java"
outdir = options.outdir

if (options.smali+options.jasmin+options.nosc) > 1:
print ("[ ERROR ] You can only select 1 source format --[smali/jasmin/java/no-source]")
Expand All @@ -149,7 +153,9 @@ def main():
if os.path.isfile(args[1]) and os.path.splitext(args[1])[-1].lower() == '.apk':
apk_file = args[1]
project_name = os.path.splitext(os.path.basename(args[1]))[0].lower()
#call("cp "+apk_file+" "+tmp+project_name+"-new.apk",shell=True)
if not os.path.exists(outdir):
os.makedirs(outdir)
call("cp "+apk_file+" "+outdir+project_name+"-new.apk",shell=True)
if options.jasmin == True:
dex2jar()
jar2jasmin()
Expand Down Expand Up @@ -181,5 +187,4 @@ def main():

# Script start Here
if __name__=="__main__":
main()

main()

0 comments on commit b7373dc

Please sign in to comment.