1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013 */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9 
10 static csh handle;
11 
12 struct platform {
13 	cs_arch arch;
14 	cs_mode mode;
15 	unsigned char *code;
16 	size_t size;
17 	const char *comment;
18 	cs_opt_type opt_type;
19 	cs_opt_value opt_value;
20 };
21 
print_string_hex(const char * comment,unsigned char * str,size_t len)22 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
23 {
24 	unsigned char *c;
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 
get_eflag_name(uint64_t flag)34 static const char *get_eflag_name(uint64_t flag)
35 {
36 	switch(flag) {
37 		default:
38 			return NULL;
39 		case X86_EFLAGS_UNDEFINED_OF:
40 			return "UNDEF_OF";
41 		case X86_EFLAGS_UNDEFINED_SF:
42 			return "UNDEF_SF";
43 		case X86_EFLAGS_UNDEFINED_ZF:
44 			return "UNDEF_ZF";
45 		case X86_EFLAGS_MODIFY_AF:
46 			return "MOD_AF";
47 		case X86_EFLAGS_UNDEFINED_PF:
48 			return "UNDEF_PF";
49 		case X86_EFLAGS_MODIFY_CF:
50 			return "MOD_CF";
51 		case X86_EFLAGS_MODIFY_SF:
52 			return "MOD_SF";
53 		case X86_EFLAGS_MODIFY_ZF:
54 			return "MOD_ZF";
55 		case X86_EFLAGS_UNDEFINED_AF:
56 			return "UNDEF_AF";
57 		case X86_EFLAGS_MODIFY_PF:
58 			return "MOD_PF";
59 		case X86_EFLAGS_UNDEFINED_CF:
60 			return "UNDEF_CF";
61 		case X86_EFLAGS_MODIFY_OF:
62 			return "MOD_OF";
63 		case X86_EFLAGS_RESET_OF:
64 			return "RESET_OF";
65 		case X86_EFLAGS_RESET_CF:
66 			return "RESET_CF";
67 		case X86_EFLAGS_RESET_DF:
68 			return "RESET_DF";
69 		case X86_EFLAGS_RESET_IF:
70 			return "RESET_IF";
71 		case X86_EFLAGS_TEST_OF:
72 			return "TEST_OF";
73 		case X86_EFLAGS_TEST_SF:
74 			return "TEST_SF";
75 		case X86_EFLAGS_TEST_ZF:
76 			return "TEST_ZF";
77 		case X86_EFLAGS_TEST_PF:
78 			return "TEST_PF";
79 		case X86_EFLAGS_TEST_CF:
80 			return "TEST_CF";
81 		case X86_EFLAGS_RESET_SF:
82 			return "RESET_SF";
83 		case X86_EFLAGS_RESET_AF:
84 			return "RESET_AF";
85 		case X86_EFLAGS_RESET_TF:
86 			return "RESET_TF";
87 		case X86_EFLAGS_RESET_NT:
88 			return "RESET_NT";
89 		case X86_EFLAGS_PRIOR_OF:
90 			return "PRIOR_OF";
91 		case X86_EFLAGS_PRIOR_SF:
92 			return "PRIOR_SF";
93 		case X86_EFLAGS_PRIOR_ZF:
94 			return "PRIOR_ZF";
95 		case X86_EFLAGS_PRIOR_AF:
96 			return "PRIOR_AF";
97 		case X86_EFLAGS_PRIOR_PF:
98 			return "PRIOR_PF";
99 		case X86_EFLAGS_PRIOR_CF:
100 			return "PRIOR_CF";
101 		case X86_EFLAGS_PRIOR_TF:
102 			return "PRIOR_TF";
103 		case X86_EFLAGS_PRIOR_IF:
104 			return "PRIOR_IF";
105 		case X86_EFLAGS_PRIOR_DF:
106 			return "PRIOR_DF";
107 		case X86_EFLAGS_TEST_NT:
108 			return "TEST_NT";
109 		case X86_EFLAGS_TEST_DF:
110 			return "TEST_DF";
111 		case X86_EFLAGS_RESET_PF:
112 			return "RESET_PF";
113 		case X86_EFLAGS_PRIOR_NT:
114 			return "PRIOR_NT";
115 		case X86_EFLAGS_MODIFY_TF:
116 			return "MOD_TF";
117 		case X86_EFLAGS_MODIFY_IF:
118 			return "MOD_IF";
119 		case X86_EFLAGS_MODIFY_DF:
120 			return "MOD_DF";
121 		case X86_EFLAGS_MODIFY_NT:
122 			return "MOD_NT";
123 		case X86_EFLAGS_MODIFY_RF:
124 			return "MOD_RF";
125 		case X86_EFLAGS_SET_CF:
126 			return "SET_CF";
127 		case X86_EFLAGS_SET_DF:
128 			return "SET_DF";
129 		case X86_EFLAGS_SET_IF:
130 			return "SET_IF";
131 	}
132 }
133 
print_insn_detail(csh ud,cs_mode mode,cs_insn * ins)134 static void print_insn_detail(csh ud, cs_mode mode, cs_insn *ins)
135 {
136 	int count, i;
137 	cs_x86 *x86;
138 	cs_regs regs_read, regs_write;
139 	uint8_t regs_read_count, regs_write_count;
140 
141 	// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
142 	if (ins->detail == NULL)
143 		return;
144 
145 	x86 = &(ins->detail->x86);
146 
147 	print_string_hex("\tPrefix:", x86->prefix, 4);
148 
149 	print_string_hex("\tOpcode:", x86->opcode, 4);
150 
151 	printf("\trex: 0x%x\n", x86->rex);
152 
153 	printf("\taddr_size: %u\n", x86->addr_size);
154 	printf("\tmodrm: 0x%x\n", x86->modrm);
155 	if (x86->encoding.modrm_offset != 0) {
156 		printf("\tmodrm_offset: 0x%x\n", x86->encoding.modrm_offset);
157 	}
158 
159 	printf("\tdisp: 0x%" PRIx64 "\n", x86->disp);
160 	if (x86->encoding.disp_offset != 0) {
161 		printf("\tdisp_offset: 0x%x\n", x86->encoding.disp_offset);
162 	}
163 
164 	if (x86->encoding.disp_size != 0) {
165 		printf("\tdisp_size: 0x%x\n", x86->encoding.disp_size);
166 	}
167 
168 	// SIB is not available in 16-bit mode
169 	if ((mode & CS_MODE_16) == 0) {
170 		printf("\tsib: 0x%x\n", x86->sib);
171 		if (x86->sib_base != X86_REG_INVALID)
172 			printf("\t\tsib_base: %s\n", cs_reg_name(handle, x86->sib_base));
173 		if (x86->sib_index != X86_REG_INVALID)
174 			printf("\t\tsib_index: %s\n", cs_reg_name(handle, x86->sib_index));
175 		if (x86->sib_scale != 0)
176 			printf("\t\tsib_scale: %d\n", x86->sib_scale);
177 	}
178 
179 	// XOP code condition
180 	if (x86->xop_cc != X86_XOP_CC_INVALID) {
181 		printf("\txop_cc: %u\n", x86->xop_cc);
182 	}
183 
184 	// SSE code condition
185 	if (x86->sse_cc != X86_SSE_CC_INVALID) {
186 		printf("\tsse_cc: %u\n", x86->sse_cc);
187 	}
188 
189 	// AVX code condition
190 	if (x86->avx_cc != X86_AVX_CC_INVALID) {
191 		printf("\tavx_cc: %u\n", x86->avx_cc);
192 	}
193 
194 	// AVX Suppress All Exception
195 	if (x86->avx_sae) {
196 		printf("\tavx_sae: %u\n", x86->avx_sae);
197 	}
198 
199 	// AVX Rounding Mode
200 	if (x86->avx_rm != X86_AVX_RM_INVALID) {
201 		printf("\tavx_rm: %u\n", x86->avx_rm);
202 	}
203 
204 	// Print out all immediate operands
205 	count = cs_op_count(ud, ins, X86_OP_IMM);
206 	if (count) {
207 		printf("\timm_count: %u\n", count);
208 		for (i = 1; i < count + 1; i++) {
209 			int index = cs_op_index(ud, ins, X86_OP_IMM, i);
210 			printf("\t\timms[%u]: 0x%" PRIx64 "\n", i, x86->operands[index].imm);
211 			if (x86->encoding.imm_offset != 0) {
212 				printf("\timm_offset: 0x%x\n", x86->encoding.imm_offset);
213 			}
214 
215 			if (x86->encoding.imm_size != 0) {
216 				printf("\timm_size: 0x%x\n", x86->encoding.imm_size);
217 			}
218 		}
219 	}
220 
221 	if (x86->op_count)
222 		printf("\top_count: %u\n", x86->op_count);
223 
224 	// Print out all operands
225 	for (i = 0; i < x86->op_count; i++) {
226 		cs_x86_op *op = &(x86->operands[i]);
227 
228 		switch((int)op->type) {
229 			case X86_OP_REG:
230 				printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
231 				break;
232 			case X86_OP_IMM:
233 				printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
234 				break;
235 			case X86_OP_MEM:
236 				printf("\t\toperands[%u].type: MEM\n", i);
237 				if (op->mem.segment != X86_REG_INVALID)
238 					printf("\t\t\toperands[%u].mem.segment: REG = %s\n", i, cs_reg_name(handle, op->mem.segment));
239 				if (op->mem.base != X86_REG_INVALID)
240 					printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
241 				if (op->mem.index != X86_REG_INVALID)
242 					printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
243 				if (op->mem.scale != 1)
244 					printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
245 				if (op->mem.disp != 0)
246 					printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
247 				break;
248 			default:
249 				break;
250 		}
251 
252 		// AVX broadcast type
253 		if (op->avx_bcast != X86_AVX_BCAST_INVALID)
254 			printf("\t\toperands[%u].avx_bcast: %u\n", i, op->avx_bcast);
255 
256 		// AVX zero opmask {z}
257 		if (op->avx_zero_opmask != false)
258 			printf("\t\toperands[%u].avx_zero_opmask: TRUE\n", i);
259 
260 		printf("\t\toperands[%u].size: %u\n", i, op->size);
261 
262 		switch(op->access) {
263 			default:
264 				break;
265 			case CS_AC_READ:
266 				printf("\t\toperands[%u].access: READ\n", i);
267 				break;
268 			case CS_AC_WRITE:
269 				printf("\t\toperands[%u].access: WRITE\n", i);
270 				break;
271 			case CS_AC_READ | CS_AC_WRITE:
272 				printf("\t\toperands[%u].access: READ | WRITE\n", i);
273 				break;
274 		}
275 	}
276 
277 	// Print out all registers accessed by this instruction (either implicit or explicit)
278 	if (!cs_regs_access(ud, ins,
279 				regs_read, &regs_read_count,
280 				regs_write, &regs_write_count)) {
281 		if (regs_read_count) {
282 			printf("\tRegisters read:");
283 			for(i = 0; i < regs_read_count; i++) {
284 				printf(" %s", cs_reg_name(handle, regs_read[i]));
285 			}
286 			printf("\n");
287 		}
288 
289 		if (regs_write_count) {
290 			printf("\tRegisters modified:");
291 			for(i = 0; i < regs_write_count; i++) {
292 				printf(" %s", cs_reg_name(handle, regs_write[i]));
293 			}
294 			printf("\n");
295 		}
296 	}
297 
298 	if (x86->eflags) {
299 		printf("\tEFLAGS:");
300 		for(i = 0; i <= 45; i++)
301 			if (x86->eflags & ((uint64_t)1 << i)) {
302 				printf(" %s", get_eflag_name((uint64_t)1 << i));
303 			}
304 		printf("\n");
305 	}
306 
307 	printf("\n");
308 }
309 
test()310 static void test()
311 {
312 //#define X86_CODE32 "\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x78\x56\x00\x00"
313 //#define X86_CODE32 "\x05\x78\x56\x00\x00"
314 //#define X86_CODE32 "\x01\xd8"
315 //#define X86_CODE32 "\x05\x23\x01\x00\x00"
316 //#define X86_CODE32 "\x8d\x87\x89\x67\x00\x00"
317 //#define X86_CODE32 "\xa1\x13\x48\x6d\x3a\x8b\x81\x23\x01\x00\x00\x8b\x84\x39\x23\x01\x00\x00"
318 //#define X86_CODE32 "\xb4\xc6"	// mov	ah, 0x6c
319 //#define X86_CODE32 "\x77\x04"	// ja +6
320 #define X86_CODE64 "\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"
321 //#define X86_CODE64 "\xe9\x79\xff\xff\xff"	// jmp 0xf7e
322 
323 #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\x66\xe9\xb8\x00\x00\x00\x67\xff\xa0\x23\x01\x00\x00\x66\xe8\xcb\x00\x00\x00\x74\xfc"
324 #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\xe9\xea\xbe\xad\xde\xff\xa0\x23\x01\x00\x00\xe8\xdf\xbe\xad\xde\x74\xff"
325 //#define X86_CODE32 "\x05\x23\x01\x00\x00\x0f\x01\xda"
326 //#define X86_CODE32 "\x0f\xa7\xc0"	// xstorerng
327 //#define X86_CODE32 "\x64\xa1\x18\x00\x00\x00"	// mov eax, dword ptr fs:[18]
328 //#define X86_CODE32 "\x64\xa3\x00\x00\x00\x00"	// mov [fs:0x0], eax
329 //#define X86_CODE32 "\xd1\xe1"	// shl ecx, 1
330 //#define X86_CODE32 "\xd1\xc8"	// ror eax, 1
331 //#define X86_CODE32 "\x83\xC0\x80"	// add	eax, -x80
332 //#define X86_CODE32 "\xe8\x26\xfe\xff\xff"		// call	0xe2b
333 //#define X86_CODE32 "\xcd\x80"		// int 0x80
334 //#define X86_CODE32 "\x24\xb8"		// and    $0xb8,%al
335 //#define X86_CODE32 "\xf0\x01\xd8"   // lock add eax,ebx
336 //#define X86_CODE32 "\xf3\xaa"		// rep stosb
337 //#define X86_CODE32 "\x81\xc6\x23\x01\x00\x00"
338 
339 	struct platform platforms[] = {
340 		{
341 			CS_ARCH_X86,
342 			CS_MODE_16,
343 			(unsigned char *)X86_CODE16,
344 			sizeof(X86_CODE16) - 1,
345 			"X86 16bit (Intel syntax)"
346 		},
347 		{
348 			CS_ARCH_X86,
349 			CS_MODE_32,
350 			(unsigned char *)X86_CODE32,
351 			sizeof(X86_CODE32) - 1,
352 			"X86 32 (AT&T syntax)",
353 			CS_OPT_SYNTAX,
354 			CS_OPT_SYNTAX_ATT,
355 		},
356 		{
357 			CS_ARCH_X86,
358 			CS_MODE_32,
359 			(unsigned char *)X86_CODE32,
360 			sizeof(X86_CODE32) - 1,
361 			"X86 32 (Intel syntax)"
362 		},
363 		{
364 			CS_ARCH_X86,
365 			CS_MODE_64,
366 			(unsigned char *)X86_CODE64,
367 			sizeof(X86_CODE64) - 1,
368 			"X86 64 (Intel syntax)"
369 		},
370 	};
371 
372 	uint64_t address = 0x1000;
373 	cs_insn *insn;
374 	int i;
375 	size_t count;
376 
377 	for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
378 		cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
379 		if (err) {
380 			printf("Failed on cs_open() with error returned: %u\n", err);
381 			abort();
382 		}
383 
384 		if (platforms[i].opt_type)
385 			cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
386 
387 		cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
388 
389 		count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
390 		if (count) {
391 			size_t j;
392 
393 			printf("****************\n");
394 			printf("Platform: %s\n", platforms[i].comment);
395 			print_string_hex("Code:", platforms[i].code, platforms[i].size);
396 			printf("Disasm:\n");
397 
398 			for (j = 0; j < count; j++) {
399 				printf("0x%" PRIx64 ":\t%s\t%s\n\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
400 				print_insn_detail(handle, platforms[i].mode, &insn[j]);
401 			}
402 			printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
403 
404 			// free memory allocated by cs_disasm()
405 			cs_free(insn, count);
406 		} else {
407 			printf("****************\n");
408 			printf("Platform: %s\n", platforms[i].comment);
409 			print_string_hex("Code:", platforms[i].code, platforms[i].size);
410 			printf("ERROR: Failed to disasm given code!\n");
411 			abort();
412 		}
413 
414 		printf("\n");
415 
416 		cs_close(&handle);
417 	}
418 }
419 
main()420 int main()
421 {
422 	test();
423 
424 	return 0;
425 }
426