Skip to content

Instantly share code, notes, and snippets.

@0xc010d
Created December 19, 2012 08:13
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 0xc010d/4335229 to your computer and use it in GitHub Desktop.
Save 0xc010d/4335229 to your computer and use it in GitHub Desktop.
Hopper's 'Insert ASM instruction' script
#requires yasm to be at /usr/local/bin
from subprocess import call
asmFile = "/tmp/tmp.s"
binFile = "/tmp/tmp.o"
doc = Document.getCurrentDocument()
seg = doc.getCurrentSegment()
addr = doc.getCurrentAddress()
asm = Document.ask("Enter instruction:")
if asm != None:
f = open(asmFile, "w")
f.write("[BITS 64]\n")
f.write(asm)
f.close()
res = call(["/usr/local/bin/yasm", "-o", binFile, asmFile])
if res == 0:
bytes = bytearray(open(binFile, "rb").read())
for i in range(len(bytes)):
byte = bytes[i]
seg.writeByte(addr, byte)
addr = addr + 1
seg.markAsCode(doc.getCurrentAddress())
@johndpope
Copy link

Hi,

Thanks so much sharing this code snippet. I added it to the hopper sample scripts library on github.

When I try to insert this code
add rax, qword [ss:rbp+0x563ee8a7]
it appears correctly in hopper but when I build new binary and open file - I see this it being mis-interpreted as var.

screen shot 2015-12-11 at 12 24 16 am

I hardcoded script to a /tmp/tmp.s file but couldn't succeed.

[BITS 64]
add rax, qword [ss:rbp+0x563ee8a7]

[BITS 64]
add rax, qword [ss:rbp+1446963367]

[BITS 64]
add rax, qword [ss:rbp+'1446963367']

any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment