Skip to content

Instantly share code, notes, and snippets.

@roblabla
Last active March 9, 2019 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblabla/c5c7d6866e8b32210f399c246bd37e38 to your computer and use it in GitHub Desktop.
Save roblabla/c5c7d6866e8b32210f399c246bd37e38 to your computer and use it in GitHub Desktop.
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//Decompile an entire program
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.Option;
import ghidra.app.util.exporter.CppExporter;
public class DecompilerExplorer extends GhidraScript {
@Override
public void run() throws Exception {
File outputFile = this.askFile("decompiler output", "yes");
CppExporter cppExporter = new CppExporter();
List<Option> options = new ArrayList<Option>();
options.add(new Option(CppExporter.CREATE_HEADER_FILE, true));
cppExporter.setOptions(options);
cppExporter.setExporterServiceProvider(state.getTool());
cppExporter.export(outputFile, currentProgram, null, monitor);
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Decompiler Explorer</title>
<style type="text/css" media="screen">
html, body {
margin: 0;
height: 100%;
}
.container {
display: flex;
height: 100%;
}
#editor {
/*position: absolute;*/
flex-grow: 1;
}
#output {
/*position: absolute;*/
flex-grow: 1;
}
</style>
</head>
<body>
<div class="container">
<div id="editor"></div>
<div id="output"></div>
</div>
<script src="https://gitcdn.xyz/repo/ajaxorg/ace-builds/master/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/assembly_x86");
editor.session.on('change', _.debounce(async function() {
output.session.getDocument().setValue("decompiling...");
let response = await fetch("/decompile", {
method: 'POST',
body: editor.session.getDocument().getValue(),
});
if (response.ok) {
let c = await response.text();
output.session.getDocument().setValue(c);
} else {
output.session.getDocument().setValue("Decompilation failed");
}
}, 1000));
var output = ace.edit("output");
output.setTheme("ace/theme/monokai");
output.session.setMode("ace/mode/c_cpp");
output.setReadOnly(true);
</script>
</body>
</html>
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
use rocket_contrib::templates::Template;
use std::process::Command;
use std::error::Error;
use std::io::{Write, Read};
use tempfile::{NamedTempFile, TempDir, Builder};
use std::path::PathBuf;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
#[get("/")]
fn index() -> Template {
Template::render("index", ())
}
#[post("/decompile", data = "<input>")]
fn decompile(input: String) -> Result<String, Box<dyn Error>> {
let mut source_asm_file = Builder::new().prefix("decexp_src_asm_").suffix(".s").tempfile()?;
let mut binary_file = Builder::new().prefix("decexp_bin_").tempfile()?;
let mut out_asm_file = Builder::new().prefix("decexp_out_asm_").tempfile()?;
let mut ghidra_project = TempDir::new()?;
source_asm_file.write(input.as_bytes())?;
source_asm_file.flush()?;
let status = Command::new("clang")
.arg(source_asm_file.path())
.arg("-o")
.arg(binary_file.path())
.status()?;
if !status.success() {
return Ok("Failed to compile".to_string());
}
let status = Command::new("/Applications/ghidra_9.0/support/analyzeHeadless")
.arg(ghidra_project.path())
.arg("temp")
.arg("-import")
.arg(binary_file.path())
.arg("-postScript")
.arg("DecompilerExplorer.java")
.arg(out_asm_file.path())
.status()?;
if !status.success() {
return Ok("Failed to compile".to_string());
}
let mut out = String::new();
out_asm_file.read_to_string(&mut out)?;
Ok(out)
}
fn main() {
rocket::ignite()
.attach(Template::fairing())
.mount("/", routes![index, decompile]).launch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment