-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_capstone_linear.cc
More file actions
67 lines (54 loc) · 1.38 KB
/
basic_capstone_linear.cc
File metadata and controls
67 lines (54 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <string>
#include <capstone/capstone.h>
#include "loader.h"
int disasm(Binary *bin);
int main(int argc, char *argv[])
{
Binary bin;
std::string fname;
if (argc < 2) {
printf("Usage %s <binary>\n", argv[0]);
return 1;
}
fname.assign(argv[1]);
if (load_binary(fname, &bin, Binary::BIN_TYPE_AUTO) < 0) {
return 1;
}
if (disasm(&bin) < 0) {
return 1;
}
return 0;
}
int disasm(Binary *bin)
{
csh dis;
cs_insn *insns;
Section *text;
size_t n;
text = bin->get_text_sections();
if (!text) {
fprintf(stderr, "Nothing to disassemble\n");
return 0;
}
if (cs_open(CS_ARCH_X86, CS_MODE_64, &dis) != CS_ERR_OK) {
fprintf(stderr, "Failed to open Capstone\n");
return -1;
}
n = cs_disasm(dis, text->bytes.get(), text->size, text->vma, 0, &insns);
if (n < 0) {
fprintf(stderr, "Disassembly error: %s\n", cs_strerror(cs_errno(dis)));
return -1;
}
for(size_t i = 0; i < n; i++) {
printf("0x%016jx: ", insns[i].address);
for (size_t j = 0; j < 16; j++) {
if (j < insns[i].size) printf("%02x ", insns[i].bytes[j]);
else printf(" ");
}
printf("%-12s %s\n", insns[i].mnemonic, insns[i].op_str);
}
cs_free(insns, n);
cs_close(&dis);
return 0;
}