官网:The Ultimate Disassembly Framework – Capstone – The Ultimate Disassembler
安装:
apt-get install libcapstone-dev
参考:
1)Capstone反汇编引擎数据类型及API分析与示例(一) - 先知社区
2)Capstone反汇编引擎数据类型及API分析及示例(二) - 先知社区
3) Capstone反汇编引擎数据类型及API分析及示例(三) - 先知社区
4) Capstone反汇编引擎数据类型及API分析及示例(四) - 先知社区
测试代码1:
#include <iostream>
#include <stdio.h>
#include <cinttypes>
#include <capstone/capstone.h>
using namespace std;
#define CODE "\x55\x48\x8b\x05\xb8\x13\x00\x00"
int main(void)
{
csh handle;
cs_insn* insn;
size_t count;
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle)) {
printf("ERROR: Failed to initialize engine!\n");
return -1;
}
count = cs_disasm(handle, (unsigned char*)CODE, sizeof(CODE) - 1, 0x1000, 0, &insn);
if (count) {
size_t j;
for (j = 0; j < count; j++) {
printf("0x%""Ix"":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
}
cs_free(insn, count);
}
else
printf("ERROR: Failed to disassemble given code!\n");
cs_close(&handle);
return 0;
}
执行命令
gcc decode.cpp -o decode -lcapstone -lstdc++
输出:
root@ubuntu:/robin1/testBpf# ./decode
0x1000: push rbp
0x1001: mov rax, qword ptr [rip + 0x13b8]
而具体使用反汇编以及ptrace跟踪函数调用的一个程序:https://github.com/finixbit/print-function-args-debugger
记得目标程序编译时候需要添加 -no-pie选项,
比如:
// Type your code here, or load an example.
#include <stdio.h>
#include <unistd.h>
using namespace std;
void test1(const char * str)
{
static int count = 1;
count++;
printf("%s\n", str);
/*
const char * p1 = str->data();
char * p = (char *)str;
int n = p1 - p;
cout << n << endl;
char * s = p + 8;
cout << s << endl;
*/
}
int main()
{
const char str[] = "hello";
for (int i=0; i<10000; i++)
{
test1(str);
sleep(2);
}
return 1;
}
编译命令为:gcc test1.cpp -o test -g -no-pie
否则在readelf -h ./test中看到的程序类型不对,之后的运行报错;