Created
May 31, 2016 22:11
-
-
Save mbikovitsky/fadb934118d9860954bd028a3b5da4af to your computer and use it in GitHub Desktop.
Keystone test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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