+from unicorn.x86_const import *
+from capstone.x86_const import *
+BASE_ADDRESS = 0x1000000
+ """init capstone, return instance"""
+ # Initialize emulator in X86-32bit mode
+ mu = Uc(UC_ARCH_X86, UC_MODE_32)
+ # map 2MB memory for this emulation
+ mu.mem_map(BASE_ADDRESS, 8 * 1024 * 1024)
+ mu.mem_write(BASE_ADDRESS, code_bin)
+ # initialize register for stack
+ mu.reg_write(UC_X86_REG_ESP, BASE_ADDRESS + STACK_OFFSET)
+ mu.reg_write(UC_X86_REG_EBP, BASE_ADDRESS + STACK_OFFSET)
+ print("ERROR SETUP:%s" % e)
+#TODO: calculuate the string size
+def get_string(offset, size=0x20):
+ """read string from stack, example: lea ecx, [ebp+var_44], enter 0x44 """
+ return str(emu.mem_read(BASE_ADDRESS + STACK_OFFSET - offset, size)).replace("\x00","")
+ """ read bytes from idb"""
+ return "".join([byte for byte in GetManyBytes( SelStart(), length)])
+ emu.emu_start(BASE_ADDRESS, BASE_ADDRESS + len(data))
+ print("ERROR START: %s" % e)
+ offset = AskLong(0, "Please enter stack offset")
+ print get_string(offset)