使用libcapstone反汇编

官网: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中看到的程序类型不对,之后的运行报错;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值