Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch decompile using plugins #20

Open
XinyuShe opened this issue Apr 17, 2024 · 1 comment
Open

Batch decompile using plugins #20

XinyuShe opened this issue Apr 17, 2024 · 1 comment

Comments

@XinyuShe
Copy link

I hope to use the plugin to batch decompile WebAssembly (wasm) files. What is the API, or are there any ready-made Ghidra scripts for this?

@nneonneo
Copy link
Owner

nneonneo commented Apr 17, 2024

This is what I use to decompile a single program (works for any architecture, not just Wasm); you can use the analyzeHeadless command in support in conjunction with a script like this to import + decompile programs automatically.

# Decompile all functions to output.c
# @author nneonneo
# @category PWN
# @keybinding
# @menupath
# @toolbar

from ghidra.app.decompiler import DecompInterface, DecompileOptions

curr = getCurrentProgram()
decompiler = DecompInterface()
opts = DecompileOptions()
opts.grabFromProgram(curr)
decompiler.setOptions(opts)
decompiler.toggleCCode(True)
decompiler.toggleSyntaxTree(True)
decompiler.setSimplificationStyle("decompile")
decompiler.openProgram(curr)

with open("output.c", "w") as outf:
    funcs = curr.functionManager.getFunctions(True)
    for func in funcs:
        res = decompiler.decompileFunction(func, 0, monitor)
        if res.decompiledFunction is None:
            d = "/* Error decompiling %s: %s */" % (func.getName(True), res.errorMessage)
        else:
            d = res.decompiledFunction.c

        print >>outf, d

An example analyzeHeadless call:

analyzeHeadless /tmp/ tmpProjectName -import program.exe -postScript decompile.py -deleteProject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants