Skip to content

Instantly share code, notes, and snippets.

@mbikovitsky
Created May 31, 2016 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbikovitsky/fadb934118d9860954bd028a3b5da4af to your computer and use it in GitHub Desktop.
Save mbikovitsky/fadb934118d9860954bd028a3b5da4af to your computer and use it in GitHub Desktop.
Keystone test
#!/usr/bin/env python3
import struct
from keystone import *
from capstone import *
CODE = """
begin:
call get_eip
get_eip:
pop eax
sub eax, get_eip - begin
ret
"""
def main():
print("Assembling:")
for line in CODE.split("\n"):
print("\t%s" % (line,))
assembler = Ks(KS_ARCH_X86, KS_MODE_32)
encoding, count = assembler.asm(CODE.encode("UTF-8"))
binary = struct.pack("%dB" % (len(encoding),), *encoding)
print("Result: %s" % (binary,))
print("---")
print("Disassembling: %s" % (binary,))
disassembler = Cs(CS_ARCH_X86, CS_MODE_32)
for instruction in disassembler.disasm(binary, 0x1337):
print("\t0x%x:\t%s\t%s" % (instruction.address,
instruction.mnemonic,
instruction.op_str))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment