| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import print_function |
| 5 |
from capstone import * |
| 6 |
from capstone.x86 import * |
| 7 |
|
| 8 |
|
| 9 |
X86_CODE64 = b"\x55\x48\x8b\x05\xb8\x13\x00\x00\xe9\xea\xbe\xad\xde\xff\x25\x23\x01\x00\x00\xe8\xdf\xbe\xad\xde\x74\xff" |
| 10 |
X86_CODE16 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6\x66\xe9\xb8\x00\x00\x00\x67\xff\xa0\x23\x01\x00\x00\x66\xe8\xcb\x00\x00\x00\x74\xfc" |
| 11 |
X86_CODE32 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6\xe9\xea\xbe\xad\xde\xff\xa0\x23\x01\x00\x00\xe8\xdf\xbe\xad\xde\x74\xff" |
| 12 |
|
| 13 |
mask32 = lambda v: v & 0xFFFFFFFF |
| 14 |
mask64 = lambda v: v & 0xFFFFFFFFFFFFFFFF |
| 15 |
|
| 16 |
all_tests = ( |
| 17 |
(CS_ARCH_X86, CS_MODE_16, X86_CODE16, "X86 16bit (Intel syntax)", None), |
| 18 |
(CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (AT&T syntax)", CS_OPT_SYNTAX_ATT), |
| 19 |
(CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", None), |
| 20 |
(CS_ARCH_X86, CS_MODE_64, X86_CODE64, "X86 64 (Intel syntax)", None), |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def get_eflag_name(eflag): |
| 25 |
if eflag == X86_EFLAGS_UNDEFINED_OF: |
| 26 |
return "UNDEF_OF" |
| 27 |
elif eflag == X86_EFLAGS_UNDEFINED_SF: |
| 28 |
return "UNDEF_SF" |
| 29 |
elif eflag == X86_EFLAGS_UNDEFINED_ZF: |
| 30 |
return "UNDEF_ZF" |
| 31 |
elif eflag == X86_EFLAGS_MODIFY_AF: |
| 32 |
return "MOD_AF" |
| 33 |
elif eflag == X86_EFLAGS_UNDEFINED_PF: |
| 34 |
return "UNDEF_PF" |
| 35 |
elif eflag == X86_EFLAGS_MODIFY_CF: |
| 36 |
return "MOD_CF" |
| 37 |
elif eflag == X86_EFLAGS_MODIFY_SF: |
| 38 |
return "MOD_SF" |
| 39 |
elif eflag == X86_EFLAGS_MODIFY_ZF: |
| 40 |
return "MOD_ZF" |
| 41 |
elif eflag == X86_EFLAGS_UNDEFINED_AF: |
| 42 |
return "UNDEF_AF" |
| 43 |
elif eflag == X86_EFLAGS_MODIFY_PF: |
| 44 |
return "MOD_PF" |
| 45 |
elif eflag == X86_EFLAGS_UNDEFINED_CF: |
| 46 |
return "UNDEF_CF" |
| 47 |
elif eflag == X86_EFLAGS_MODIFY_OF: |
| 48 |
return "MOD_OF" |
| 49 |
elif eflag == X86_EFLAGS_RESET_OF: |
| 50 |
return "RESET_OF" |
| 51 |
elif eflag == X86_EFLAGS_RESET_CF: |
| 52 |
return "RESET_CF" |
| 53 |
elif eflag == X86_EFLAGS_RESET_DF: |
| 54 |
return "RESET_DF" |
| 55 |
elif eflag == X86_EFLAGS_RESET_IF: |
| 56 |
return "RESET_IF" |
| 57 |
elif eflag == X86_EFLAGS_TEST_OF: |
| 58 |
return "TEST_OF" |
| 59 |
elif eflag == X86_EFLAGS_TEST_SF: |
| 60 |
return "TEST_SF" |
| 61 |
elif eflag == X86_EFLAGS_TEST_ZF: |
| 62 |
return "TEST_ZF" |
| 63 |
elif eflag == X86_EFLAGS_TEST_PF: |
| 64 |
return "TEST_PF" |
| 65 |
elif eflag == X86_EFLAGS_TEST_CF: |
| 66 |
return "TEST_CF" |
| 67 |
elif eflag == X86_EFLAGS_RESET_SF: |
| 68 |
return "RESET_SF" |
| 69 |
elif eflag == X86_EFLAGS_RESET_AF: |
| 70 |
return "RESET_AF" |
| 71 |
elif eflag == X86_EFLAGS_RESET_TF: |
| 72 |
return "RESET_TF" |
| 73 |
elif eflag == X86_EFLAGS_RESET_NT: |
| 74 |
return "RESET_NT" |
| 75 |
elif eflag == X86_EFLAGS_PRIOR_OF: |
| 76 |
return "PRIOR_OF" |
| 77 |
elif eflag == X86_EFLAGS_PRIOR_SF: |
| 78 |
return "PRIOR_SF" |
| 79 |
elif eflag == X86_EFLAGS_PRIOR_ZF: |
| 80 |
return "PRIOR_ZF" |
| 81 |
elif eflag == X86_EFLAGS_PRIOR_AF: |
| 82 |
return "PRIOR_AF" |
| 83 |
elif eflag == X86_EFLAGS_PRIOR_PF: |
| 84 |
return "PRIOR_PF" |
| 85 |
elif eflag == X86_EFLAGS_PRIOR_CF: |
| 86 |
return "PRIOR_CF" |
| 87 |
elif eflag == X86_EFLAGS_PRIOR_TF: |
| 88 |
return "PRIOR_TF" |
| 89 |
elif eflag == X86_EFLAGS_PRIOR_IF: |
| 90 |
return "PRIOR_IF" |
| 91 |
elif eflag == X86_EFLAGS_PRIOR_DF: |
| 92 |
return "PRIOR_DF" |
| 93 |
elif eflag == X86_EFLAGS_TEST_NT: |
| 94 |
return "TEST_NT" |
| 95 |
elif eflag == X86_EFLAGS_TEST_DF: |
| 96 |
return "TEST_DF" |
| 97 |
elif eflag == X86_EFLAGS_RESET_PF: |
| 98 |
return "RESET_PF" |
| 99 |
elif eflag == X86_EFLAGS_PRIOR_NT: |
| 100 |
return "PRIOR_NT" |
| 101 |
elif eflag == X86_EFLAGS_MODIFY_TF: |
| 102 |
return "MOD_TF" |
| 103 |
elif eflag == X86_EFLAGS_MODIFY_IF: |
| 104 |
return "MOD_IF" |
| 105 |
elif eflag == X86_EFLAGS_MODIFY_DF: |
| 106 |
return "MOD_DF" |
| 107 |
elif eflag == X86_EFLAGS_MODIFY_NT: |
| 108 |
return "MOD_NT" |
| 109 |
elif eflag == X86_EFLAGS_MODIFY_RF: |
| 110 |
return "MOD_RF" |
| 111 |
elif eflag == X86_EFLAGS_SET_CF: |
| 112 |
return "SET_CF" |
| 113 |
elif eflag == X86_EFLAGS_SET_DF: |
| 114 |
return "SET_DF" |
| 115 |
elif eflag == X86_EFLAGS_SET_IF: |
| 116 |
return "SET_IF" |
| 117 |
else: |
| 118 |
return None |
| 119 |
|
| 120 |
|
| 121 |
def print_insn_detail(mode, insn): |
| 122 |
def print_string_hex(comment, str): |
| 123 |
print(comment, end=' '), |
| 124 |
for c in str: |
| 125 |
print("0x%02x " % c, end=''), |
| 126 |
print() |
| 127 |
|
| 128 |
|
| 129 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 130 |
|
| 131 |
|
| 132 |
if insn.id == 0: |
| 133 |
return |
| 134 |
|
| 135 |
|
| 136 |
print_string_hex("\tPrefix:", insn.prefix) |
| 137 |
|
| 138 |
|
| 139 |
print_string_hex("\tOpcode:", insn.opcode) |
| 140 |
|
| 141 |
|
| 142 |
print("\trex: 0x%x" % (insn.rex)) |
| 143 |
|
| 144 |
|
| 145 |
print("\taddr_size: %u" % (insn.addr_size)) |
| 146 |
|
| 147 |
|
| 148 |
print("\tmodrm: 0x%x" % (insn.modrm)) |
| 149 |
|
| 150 |
|
| 151 |
if insn.encoding.modrm_offset != 0: |
| 152 |
print("\tmodrm_offset: 0x%x" % (insn.encoding.modrm_offset)) |
| 153 |
|
| 154 |
|
| 155 |
print("\tdisp: 0x%x" % mask32(insn.disp)) |
| 156 |
|
| 157 |
|
| 158 |
if insn.encoding.disp_offset != 0: |
| 159 |
print("\tdisp_offset: 0x%x" % (insn.encoding.disp_offset)) |
| 160 |
|
| 161 |
|
| 162 |
if insn.encoding.disp_size != 0: |
| 163 |
print("\tdisp_size: 0x%x" % (insn.encoding.disp_size)) |
| 164 |
|
| 165 |
|
| 166 |
if (mode & CS_MODE_16 == 0): |
| 167 |
|
| 168 |
print("\tsib: 0x%x" % (insn.sib)) |
| 169 |
if (insn.sib): |
| 170 |
if insn.sib_base != 0: |
| 171 |
print("\t\tsib_base: %s" % (insn.reg_name(insn.sib_base))) |
| 172 |
if insn.sib_index != 0: |
| 173 |
print("\t\tsib_index: %s" % (insn.reg_name(insn.sib_index))) |
| 174 |
if insn.sib_scale != 0: |
| 175 |
print("\t\tsib_scale: %d" % (insn.sib_scale)) |
| 176 |
|
| 177 |
|
| 178 |
if insn.xop_cc != X86_XOP_CC_INVALID: |
| 179 |
print("\txop_cc: %u" % (insn.xop_cc)) |
| 180 |
|
| 181 |
|
| 182 |
if insn.sse_cc != X86_SSE_CC_INVALID: |
| 183 |
print("\tsse_cc: %u" % (insn.sse_cc)) |
| 184 |
|
| 185 |
|
| 186 |
if insn.avx_cc != X86_AVX_CC_INVALID: |
| 187 |
print("\tavx_cc: %u" % (insn.avx_cc)) |
| 188 |
|
| 189 |
|
| 190 |
if insn.avx_sae: |
| 191 |
print("\tavx_sae: TRUE") |
| 192 |
|
| 193 |
|
| 194 |
if insn.avx_rm != X86_AVX_RM_INVALID: |
| 195 |
print("\tavx_rm: %u" % (insn.avx_rm)) |
| 196 |
|
| 197 |
count = insn.op_count(X86_OP_IMM) |
| 198 |
if count > 0: |
| 199 |
print("\timm_count: %u" % count) |
| 200 |
for i in range(count): |
| 201 |
op = insn.op_find(X86_OP_IMM, i + 1) |
| 202 |
print("\t\timms[%u]: 0x%x" % (i + 1, mask64(op.imm))) |
| 203 |
if insn.encoding.imm_offset != 0: |
| 204 |
print("\timm_offset: 0x%x" % (insn.encoding.imm_offset)) |
| 205 |
if insn.encoding.imm_size != 0: |
| 206 |
print("\timm_size: 0x%x" % (insn.encoding.imm_size)) |
| 207 |
|
| 208 |
if len(insn.operands) > 0: |
| 209 |
print("\top_count: %u" % len(insn.operands)) |
| 210 |
c = -1 |
| 211 |
for i in insn.operands: |
| 212 |
c += 1 |
| 213 |
if i.type == X86_OP_REG: |
| 214 |
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg))) |
| 215 |
if i.type == X86_OP_IMM: |
| 216 |
print("\t\toperands[%u].type: IMM = 0x%x" % (c, mask64(i.imm))) |
| 217 |
if i.type == X86_OP_MEM: |
| 218 |
print("\t\toperands[%u].type: MEM" % c) |
| 219 |
if i.mem.segment != 0: |
| 220 |
print("\t\t\toperands[%u].mem.segment: REG = %s" % (c, insn.reg_name(i.mem.segment))) |
| 221 |
if i.mem.base != 0: |
| 222 |
print("\t\t\toperands[%u].mem.base: REG = %s" % (c, insn.reg_name(i.mem.base))) |
| 223 |
if i.mem.index != 0: |
| 224 |
print("\t\t\toperands[%u].mem.index: REG = %s" % (c, insn.reg_name(i.mem.index))) |
| 225 |
if i.mem.scale != 1: |
| 226 |
print("\t\t\toperands[%u].mem.scale: %u" % (c, i.mem.scale)) |
| 227 |
if i.mem.disp != 0: |
| 228 |
print("\t\t\toperands[%u].mem.disp: 0x%x" % (c, mask64(i.mem.disp))) |
| 229 |
|
| 230 |
|
| 231 |
if i.avx_bcast != X86_AVX_BCAST_INVALID: |
| 232 |
print("\t\toperands[%u].avx_bcast: %u" % (c, i.avx_bcast)) |
| 233 |
|
| 234 |
|
| 235 |
if i.avx_zero_opmask: |
| 236 |
print("\t\toperands[%u].avx_zero_opmask: TRUE" % (c)) |
| 237 |
|
| 238 |
print("\t\toperands[%u].size: %u" % (c, i.size)) |
| 239 |
|
| 240 |
if i.access == CS_AC_READ: |
| 241 |
print("\t\toperands[%u].access: READ\n" % (c)) |
| 242 |
elif i.access == CS_AC_WRITE: |
| 243 |
print("\t\toperands[%u].access: WRITE\n" % (c)) |
| 244 |
elif i.access == CS_AC_READ | CS_AC_WRITE: |
| 245 |
print("\t\toperands[%u].access: READ | WRITE\n" % (c)) |
| 246 |
|
| 247 |
(regs_read, regs_write) = insn.regs_access() |
| 248 |
|
| 249 |
if len(regs_read) > 0: |
| 250 |
print("\tRegisters read:", end="") |
| 251 |
for r in regs_read: |
| 252 |
print(" %s" %(insn.reg_name(r)), end="") |
| 253 |
print("") |
| 254 |
|
| 255 |
if len(regs_write) > 0: |
| 256 |
print("\tRegisters modified:", end="") |
| 257 |
for r in regs_write: |
| 258 |
print(" %s" %(insn.reg_name(r)), end="") |
| 259 |
print("") |
| 260 |
|
| 261 |
if insn.eflags: |
| 262 |
updated_flags = [] |
| 263 |
for i in range(0,46): |
| 264 |
if insn.eflags & (1 << i): |
| 265 |
updated_flags.append(get_eflag_name(1 << i)) |
| 266 |
print("\tEFLAGS: %s" % (','.join(p for p in updated_flags))) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
def test_class(): |
| 271 |
|
| 272 |
for (arch, mode, code, comment, syntax) in all_tests: |
| 273 |
print("*" * 16) |
| 274 |
print("Platform: %s" % comment) |
| 275 |
print("Code: %s" % code.hex(' ')) |
| 276 |
print("Disasm:") |
| 277 |
|
| 278 |
try: |
| 279 |
md = Cs(arch, mode) |
| 280 |
md.detail = True |
| 281 |
|
| 282 |
if syntax is not None: |
| 283 |
md.syntax = syntax |
| 284 |
|
| 285 |
for insn in md.disasm(code, 0x1000): |
| 286 |
print_insn_detail(mode, insn) |
| 287 |
print () |
| 288 |
print ("0x%x:\n" % (insn.address + insn.size)) |
| 289 |
except CsError as e: |
| 290 |
print("ERROR: %s" % e) |
| 291 |
|
| 292 |
|
| 293 |
if __name__ == '__main__': |
| 294 |
test_class() |