|
from hexdump2 import hexdump |
|
import gradio as gr |
|
import subprocess |
|
import tempfile |
|
|
|
description = """This is a space testing a method for evaluating the quality of decompilation. |
|
|
|
Currently unhandled features: |
|
* Global references |
|
* Function calls |
|
* Compiler and flag selection |
|
""" |
|
|
|
|
|
def compile(source): |
|
|
|
with tempfile.NamedTemporaryFile(suffix=".c", delete=False) as temp_c_file: |
|
temp_c_file.write(source.encode()) |
|
temp_c_file_name = temp_c_file.name |
|
|
|
|
|
with tempfile.NamedTemporaryFile(suffix=".o", delete=False) as temp_o_file: |
|
temp_o_file_name = temp_o_file.name |
|
|
|
|
|
subprocess.run(["cc", "-c", temp_c_file_name, "-o", temp_o_file_name], check=True) |
|
|
|
|
|
with tempfile.NamedTemporaryFile(suffix=".raw", delete=True) as raw_bytes_file: |
|
subprocess.run( |
|
[ |
|
"objcopy", |
|
"--only-section", |
|
".text", |
|
"-O", |
|
"binary", |
|
temp_o_file_name, |
|
raw_bytes_file.name, |
|
] |
|
) |
|
compiled_bytes = raw_bytes_file.read() |
|
|
|
return compiled_bytes |
|
|
|
|
|
def predict(target, source): |
|
bytes = compile(source) |
|
return hexdump(bytes), 1.0 |
|
|
|
|
|
def run(): |
|
demo = gr.Interface( |
|
fn=predict, |
|
description=description, |
|
inputs=[ |
|
gr.Textbox(lines=10, label="Python bytestring of binary code"), |
|
gr.Textbox(lines=10, label="Decompiled C Source Code"), |
|
gr.Textbox(label="Compiler", default="gcc"), |
|
gr.Textbox(label="Compiler Flags", default="-O2"), |
|
], |
|
outputs=[gr.Textbox(), gr.Number()], |
|
) |
|
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|
|
|
|
run() |
|
|