Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Second-Best Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <inttypes.h> |
| 7 | |
| 8 | #include <capstone.h> |
| 9 | |
| 10 | static csh handle; |
| 11 | |
| 12 | struct platform { |
| 13 | cs_arch arch; |
| 14 | cs_mode mode; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 15 | unsigned char *code; |
| 16 | size_t size; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | char *comment; |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 18 | cs_opt_type opt_type; |
| 19 | cs_opt_value opt_value; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 20 | }; |
| 21 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 22 | static void print_string_hex(char *comment, unsigned char *str, int len) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 23 | { |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 24 | unsigned char *c; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 25 | |
| 26 | printf("%s", comment); |
| 27 | for (c = str; c < str + len; c++) { |
| 28 | printf("0x%02x ", *c & 0xff); |
| 29 | } |
| 30 | |
| 31 | printf("\n"); |
| 32 | } |
| 33 | |
| 34 | static void print_insn_detail(csh ud, cs_mode mode, cs_insn *ins) |
| 35 | { |
| 36 | int i; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 37 | cs_x86 *x86 = &(ins->detail->x86); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 38 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 39 | print_string_hex("\tPrefix:", x86->prefix, 5); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 40 | |
| 41 | if (x86->segment != X86_REG_INVALID) |
| 42 | printf("\tSegment override: %s\n", cs_reg_name(handle, x86->segment)); |
| 43 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 44 | print_string_hex("\tOpcode:", x86->opcode, 3); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 45 | printf("\top_size: %u, addr_size: %u, disp_size: %u, imm_size: %u\n", x86->op_size, x86->addr_size, x86->disp_size, x86->imm_size); |
| 46 | printf("\tmodrm: 0x%x\n", x86->modrm); |
| 47 | printf("\tdisp: 0x%x\n", x86->disp); |
| 48 | |
| 49 | // SIB is not available in 16-bit mode |
| 50 | if ((mode & CS_MODE_16) == 0) { |
| 51 | printf("\tsib: 0x%x\n", x86->sib); |
| 52 | if (x86->sib_index != X86_REG_INVALID) |
| 53 | printf("\tsib_index: %s, sib_scale: %u, sib_base: %s\n", |
| 54 | cs_reg_name(handle, x86->sib_index), |
| 55 | x86->sib_scale, |
| 56 | cs_reg_name(handle, x86->sib_base)); |
| 57 | } |
| 58 | |
| 59 | int count = cs_op_count(ud, ins, X86_OP_IMM); |
| 60 | if (count) { |
| 61 | printf("\timm_count: %u\n", count); |
| 62 | for (i = 1; i < count + 1; i++) { |
| 63 | int index = cs_op_index(ud, ins, X86_OP_IMM, i); |
| 64 | printf("\t\timms[%u]: 0x%"PRIx64 "\n", i, x86->operands[index].imm); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (x86->op_count) |
| 69 | printf("\top_count: %u\n", x86->op_count); |
| 70 | for (i = 0; i < x86->op_count; i++) { |
| 71 | cs_x86_op *op = &(x86->operands[i]); |
| 72 | |
| 73 | switch((int)op->type) { |
| 74 | case X86_OP_REG: |
| 75 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 76 | break; |
| 77 | case X86_OP_IMM: |
| 78 | printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm); |
| 79 | break; |
| 80 | case X86_OP_FP: |
| 81 | printf("\t\toperands[%u].type: FP = %f\n", i, op->fp); |
| 82 | break; |
| 83 | case X86_OP_MEM: |
| 84 | printf("\t\toperands[%u].type: MEM\n", i); |
| 85 | if (op->mem.base != 0) |
| 86 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base)); |
| 87 | if (op->mem.index != 0) |
| 88 | printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index)); |
| 89 | if (op->mem.scale != 1) |
| 90 | printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale); |
| 91 | if (op->mem.disp != 0) |
| 92 | printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp); |
| 93 | break; |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | printf("\n"); |
| 100 | } |
| 101 | |
| 102 | static void test() |
| 103 | { |
| 104 | //#define X86_CODE32 "\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x78\x56\x00\x00" |
| 105 | //#define X86_CODE32 "\x05\x78\x56\x00\x00" |
| 106 | //#define X86_CODE32 "\x01\xd8" |
| 107 | //#define X86_CODE32 "\x05\x23\x01\x00\x00" |
| 108 | //#define X86_CODE32 "\x8d\x87\x89\x67\x00\x00" |
| 109 | //#define X86_CODE32 "\xa1\x13\x48\x6d\x3a\x8b\x81\x23\x01\x00\x00\x8b\x84\x39\x23\x01\x00\x00" |
| 110 | //#define X86_CODE32 "\xb4\xc6" // mov ah, 0x6c |
| 111 | //#define X86_CODE32 "\x77\x04" // ja +6 |
| 112 | #define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00" |
Nguyen Anh Quynh | 6d552e5 | 2013-12-03 18:23:53 +0800 | [diff] [blame] | 113 | //#define X86_CODE64 "\xe9\x79\xff\xff\xff" // jmp 0xf7e |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 114 | |
| 115 | #define X86_CODE16 "\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" |
| 116 | //#define X86_CODE16 "\x67\x00\x18" |
| 117 | #define X86_CODE32 "\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" |
Nguyen Anh Quynh | b4ce383 | 2013-12-06 08:06:21 +0800 | [diff] [blame] | 118 | //#define X86_CODE32 "\x0f\xa7\xc0" // xstorerng |
Nguyen Anh Quynh | b9b3d29 | 2013-12-02 16:21:55 +0800 | [diff] [blame] | 119 | //#define X86_CODE32 "\x64\xa1\x18\x00\x00\x00" // mov eax, dword ptr fs:[18] |
| 120 | //#define X86_CODE32 "\x64\xa3\x00\x00\x00\x00" // mov [fs:0x0], eax |
Nguyen Anh Quynh | 9c950c1 | 2013-12-02 23:20:07 +0800 | [diff] [blame] | 121 | //#define X86_CODE32 "\xd1\xe1" // shl ecx, 1 |
| 122 | //#define X86_CODE32 "\xd1\xc8" // ror eax, 1 |
Nguyen Anh Quynh | d57fa4f | 2013-12-03 00:45:39 +0800 | [diff] [blame] | 123 | //#define X86_CODE32 "\x83\xC0\x80" // add eax, -x80 |
Nguyen Anh Quynh | 641b783 | 2013-12-04 16:56:40 +0800 | [diff] [blame] | 124 | //#define X86_CODE32 "\xe8\x26\xfe\xff\xff" // call 0xe2b |
| 125 | //#define X86_CODE32 "\xcd\x80" // int 0x80 |
Nguyen Anh Quynh | d57fa4f | 2013-12-03 00:45:39 +0800 | [diff] [blame] | 126 | //#define X86_CODE32 "\x24\xb8" // and $0xb8,%al |
Nguyen Anh Quynh | bdaf3b5 | 2013-12-09 00:55:49 +0800 | [diff] [blame] | 127 | //#define X86_CODE32 "\xf0\x01\xd8" // lock add eax,ebx |
| 128 | //#define X86_CODE32 "\xf3\xaa" // rep stosb |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 129 | |
| 130 | struct platform platforms[] = { |
| 131 | { |
| 132 | .arch = CS_ARCH_X86, |
| 133 | .mode = CS_MODE_16, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 134 | .code = (unsigned char *)X86_CODE16, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 135 | .size = sizeof(X86_CODE16) - 1, |
| 136 | .comment = "X86 16bit (Intel syntax)" |
| 137 | }, |
| 138 | { |
| 139 | .arch = CS_ARCH_X86, |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 140 | .mode = CS_MODE_32, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 141 | .code = (unsigned char *)X86_CODE32, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 142 | .size = sizeof(X86_CODE32) - 1, |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 143 | .comment = "X86 32 (AT&T syntax)", |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 144 | .opt_type = CS_OPT_SYNTAX, |
Nguyen Anh Quynh | c618db4 | 2013-12-04 00:05:04 +0800 | [diff] [blame] | 145 | .opt_value = CS_OPT_SYNTAX_ATT, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 146 | }, |
| 147 | { |
| 148 | .arch = CS_ARCH_X86, |
| 149 | .mode = CS_MODE_32, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 150 | .code = (unsigned char *)X86_CODE32, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 151 | .size = sizeof(X86_CODE32) - 1, |
| 152 | .comment = "X86 32 (Intel syntax)" |
| 153 | }, |
| 154 | { |
| 155 | .arch = CS_ARCH_X86, |
| 156 | .mode = CS_MODE_64, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 157 | .code = (unsigned char *)X86_CODE64, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 158 | .size = sizeof(X86_CODE64) - 1, |
| 159 | .comment = "X86 64 (Intel syntax)" |
| 160 | }, |
| 161 | }; |
| 162 | |
Nguyen Anh Quynh | 5df9e4b | 2013-12-03 15:02:12 +0800 | [diff] [blame] | 163 | uint64_t address = 0x1000; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 164 | cs_insn *insn; |
| 165 | int i; |
| 166 | |
| 167 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 168 | if (cs_open(platforms[i].arch, platforms[i].mode, &handle)) |
| 169 | return; |
| 170 | |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 171 | if (platforms[i].opt_type) |
| 172 | cs_option(handle, platforms[i].opt_type, platforms[i].opt_value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 173 | |
Nguyen Anh Quynh | 04c19be | 2013-12-25 13:26:22 +0800 | [diff] [blame^] | 174 | size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 175 | if (count) { |
| 176 | printf("****************\n"); |
| 177 | printf("Platform: %s\n", platforms[i].comment); |
| 178 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 179 | printf("Disasm:\n"); |
| 180 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 181 | size_t j; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 182 | for (j = 0; j < count; j++) { |
Nguyen Anh Quynh | 7b7b40c | 2013-12-03 12:24:06 +0800 | [diff] [blame] | 183 | printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 184 | print_insn_detail(handle, platforms[i].mode, &insn[j]); |
| 185 | } |
Nguyen Anh Quynh | 7b7b40c | 2013-12-03 12:24:06 +0800 | [diff] [blame] | 186 | printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 187 | |
Nguyen Anh Quynh | 04c19be | 2013-12-25 13:26:22 +0800 | [diff] [blame^] | 188 | // free memory allocated by cs_disasm_ex() |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 189 | cs_free(insn, count); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 190 | } else { |
| 191 | printf("****************\n"); |
| 192 | printf("Platform: %s\n", platforms[i].comment); |
| 193 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 194 | printf("ERROR: Failed to disasm given code!\n"); |
| 195 | } |
| 196 | |
| 197 | printf("\n"); |
| 198 | |
| 199 | cs_close(handle); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int main() |
| 204 | { |
| 205 | test(); |
| 206 | |
| 207 | return 0; |
| 208 | } |