ejschwartz commited on
Commit
ff724df
·
1 Parent(s): d65ea20
Files changed (1) hide show
  1. main.py +31 -3
main.py CHANGED
@@ -51,15 +51,37 @@ def compile(compiler, flags, source):
51
  )
52
  compiled_bytes = raw_bytes_file.read()
53
 
 
 
 
 
 
 
 
54
  if result.returncode == 0:
55
- return compiled_bytes, compile_output
56
  else:
57
- return None, compile_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
 
60
  def predict(target_bytes, source, compiler, flags):
61
  target_bytes = bytes.fromhex(target_bytes)
62
- compiled_bytes, compile_output = compile(compiler, flags, source)
 
63
 
64
  if compiled_bytes is not None:
65
  return (
@@ -67,6 +89,8 @@ def predict(target_bytes, source, compiler, flags):
67
  hexdump(target_bytes, result="return"),
68
  editdistance.eval(compiled_bytes, target_bytes),
69
  compile_output,
 
 
70
  )
71
  else:
72
  return (
@@ -74,6 +98,8 @@ def predict(target_bytes, source, compiler, flags):
74
  hexdump(target_bytes, result="return"),
75
  -1,
76
  compile_output,
 
 
77
  )
78
 
79
 
@@ -100,6 +126,8 @@ def run():
100
  gr.Textbox(label="Target bytes"),
101
  gr.Number(label="Edit distance (lower is better)"),
102
  gr.Textbox(label="Compiler Output"),
 
 
103
  ],
104
  )
105
 
 
51
  )
52
  compiled_bytes = raw_bytes_file.read()
53
 
54
+ # Disassemble the object file
55
+ disassembly = subprocess.run(
56
+ ["objdump", "-d", temp_o_file_name],
57
+ capture_output=True,
58
+ text=True
59
+ ).stdout
60
+
61
  if result.returncode == 0:
62
+ return compiled_bytes, compile_output, disassembly
63
  else:
64
+ return None, compile_output, disassembly
65
+
66
+
67
+ def disassemble_bytes(byte_data):
68
+ with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as temp_bin_file:
69
+ temp_bin_file.write(byte_data)
70
+ temp_bin_file_name = temp_bin_file.name
71
+
72
+ disassembly = subprocess.run(
73
+ ["objdump", "-D", "-b", "binary", "-m", "i386", temp_bin_file_name],
74
+ capture_output=True,
75
+ text=True
76
+ ).stdout
77
+
78
+ return disassembly
79
 
80
 
81
  def predict(target_bytes, source, compiler, flags):
82
  target_bytes = bytes.fromhex(target_bytes)
83
+ compiled_bytes, compile_output, compiled_disassembly = compile(compiler, flags, source)
84
+ target_disassembly = disassemble_bytes(target_bytes)
85
 
86
  if compiled_bytes is not None:
87
  return (
 
89
  hexdump(target_bytes, result="return"),
90
  editdistance.eval(compiled_bytes, target_bytes),
91
  compile_output,
92
+ compiled_disassembly,
93
+ target_disassembly
94
  )
95
  else:
96
  return (
 
98
  hexdump(target_bytes, result="return"),
99
  -1,
100
  compile_output,
101
+ compiled_disassembly,
102
+ target_disassembly
103
  )
104
 
105
 
 
126
  gr.Textbox(label="Target bytes"),
127
  gr.Number(label="Edit distance (lower is better)"),
128
  gr.Textbox(label="Compiler Output"),
129
+ gr.Textbox(label="Compiled Disassembly"),
130
+ gr.Textbox(label="Target Disassembly"),
131
  ],
132
  )
133