Created
May 8, 2020 16:04
-
-
Save dawid-czarnecki/97c9a6dadd308480f09bfb595c5633a6 to your computer and use it in GitHub Desktop.
Simple tool to automate decompilation of war files using Procyon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import sys | |
import uuid | |
from shutil import rmtree | |
from subprocess import Popen, PIPE | |
def usage(program): | |
print("It's a simple tool to automate decompilation of war files using Procyon.") | |
print(f'Usage:\n python {program} <path/procyon.jar> <path/app.war> <path/output/dir>') | |
print(f'Example:\n python {program} ~/Downloads/procyon-decompiler-0.5.36.jar ~/Downloads/library-x.war ~/Downloads/output') | |
exit() | |
def execute(command): | |
decompiler = Popen(command, shell=False, stdout=PIPE, stderr=PIPE) | |
o, e = decompiler.communicate() | |
e = e.decode('utf8') | |
o = o.decode('utf8') | |
print(o) | |
print(e) | |
def main(): | |
program = sys.argv[0] | |
if len(sys.argv) != 4: | |
usage(program) | |
procyon_path = sys.argv[1] | |
war_path = sys.argv[2] | |
output_dir = sys.argv[3] | |
if not os.path.isfile(procyon_path) or procyon_path[-4:] != '.jar': | |
print(f'[-] Provided procyon path ({procyon_path}) is incorrect') | |
usage(program) | |
if os.path.isdir(war_path): | |
print(f'[-] Provided path to a war file ({war_path}) is a directory') | |
usage(program) | |
elif not os.path.isfile(war_path): | |
print(f'[-] Provided war file ({war_path}) does not exist') | |
usage(program) | |
if os.path.isfile(output_dir): | |
print(f'[-] Provided path to output directory ({output_dir}) is a file') | |
usage(program) | |
elif not os.path.exists(output_dir): | |
print(f'[*] Provided path to output directory ({output_dir}) does not exist. Creating...') | |
os.mkdir(output_dir) | |
classes = [] | |
war_dir = str(uuid.uuid4()) | |
print(war_dir) | |
cmd = ['unzip', '-d', war_dir, war_path] | |
execute(cmd) | |
for (root, dirs, files) in os.walk(war_dir, topdown=True): | |
for file in files: | |
if file[-6:] == '.class': | |
classes.append(f'{root}/{file}') | |
cmd = ['java', '-jar', procyon_path, '-o', output_dir] | |
cmd.extend(classes) | |
execute(cmd) | |
rmtree(war_dir) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment