File size: 2,047 Bytes
70ebd4a
66f8fc1
20b2b87
b4c7402
 
 
e98177c
 
 
 
 
 
 
 
20b2b87
b4c7402
 
 
 
e98177c
b4c7402
 
 
e98177c
b4c7402
20b2b87
e98177c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36c1bbc
e98177c
b4c7402
 
e98177c
20b2b87
 
ba2de1a
66f8fc1
 
329f9c0
 
 
e98177c
41e9ae6
330481b
41e9ae6
20f12de
 
41e9ae6
2fb9d5e
329f9c0
66f8fc1
329f9c0
720784d
e98177c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from hexdump2 import hexdump
import gradio as gr
import shlex
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
"""


def compile(compiler, flags, source):
    # Create a temporary file for the C source code
    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

    # Create a temporary file for the object file
    with tempfile.NamedTemporaryFile(suffix=".o", delete=False) as temp_o_file:
        temp_o_file_name = temp_o_file.name

    # Compile the C file to an object file
    subprocess.run([compiler, "-c", temp_c_file_name] + shlex.split(flags) + ["-o", temp_o_file_name], check=True)

    # Create a temporary file for the raw bytes
    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()
        print(compiled_bytes)

    return compiled_bytes


def predict(target, source, compiler, flags):
    bytes = compile(compiler, flags, source)
    return hexdump(bytes, result="return"), 1.0


def run():
    demo = gr.Interface(
        fn=predict,
        description=description,
        inputs=[
            gr.Textbox(lines=10, label="Hex-bytes of target", value="61 62 63"),
            gr.Textbox(lines=10, label="Decompiled C Source Code"),
            gr.Textbox(label="Compiler", value="gcc"),
            gr.Textbox(label="Compiler Flags", value="-O2"),
        ],
        outputs=[gr.Textbox(label="Compiled bytes"), gr.Number(label="Accuracy (TBD)")],
    )

    demo.launch(server_name="0.0.0.0", server_port=7860)


run()