【问题标题】:Capstone cs_disasm disassembly only small portion of codeCapstone cs_disasm 仅反汇编一小部分代码
【发布时间】:2018-02-05 04:10:22
【问题描述】:

我正在 MacOS 和 MacOS x86_64 二进制文件上试验 http://www.capstone-engine.org。它或多或少确实有效,但我确实有两个担忧。

我正在加载测试dylib

[self custom_logging:[NSString stringWithFormat:@"Module Path:%@",clientPath]];
NSMutableData *ModuleNSDATA = [NSMutableData dataWithContentsOfFile:clientPath];
[self custom_logging:[NSString stringWithFormat:@"Client Module Size: %lu MB",(ModuleNSDATA.length/1024/1024)]];
[ModuleNSDATA replaceBytesInRange:NSMakeRange(0, 20752) withBytes:NULL length:0];
uint8_t *bytes = (uint8_t*)[ModuleNSDATA bytes];
long size = [ModuleNSDATA length]/sizeof(uint8_t);
[self custom_logging:[NSString stringWithFormat:@"UInt8_t array size: %lu",size]];
ModuleASM = [NSString stringWithCString:disassembly(bytes,size,0x5110).c_str() encoding:[NSString defaultCStringEncoding]];
  1. 就我所做的研究而言,我似乎需要从二进制代码中删除“第一个”字节以删除标头和元数据,直到它遇到真正的指令。但是我不确定 capstone 是否为此提供任何 api,或者我需要按字节模式扫描并定位第一个指令地址。

事实上我已经应用了简单的解决方法,我确实找到了安全地址,它肯定会对我将加载的大多数模块有说明,但是我想应用适当的解决方案。

  1. 我已经使用我描述的解决方法成功地加载和反汇编了部分模块代码。然而,可悲的是,cs_disasm 返回的指令大多不超过 5000-6000 条,这令人困惑,它似乎在不应该中断的常规指令上中断。我不确定我做错了什么。模块的代码超过 15mb,因此要反汇编的指令超过 5k。

以下是我基于 Capstone Docs 示例的功能

string disassembly(uint8_t *bytearray, long size, uint64_t startAddress){
    csh handle;
    cs_insn *insn;
    size_t count;
    string output;
    if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) == CS_ERR_OK){
    count = cs_disasm(handle, bytearray, size, startAddress, 0, &insn);
           printf("\nCOUNT:%lu",count);
        if (count > 0) {
            size_t j;
            for (j = 0; j < count; j++) {
                char buffer[512];
                int i=0;
                i = sprintf(buffer, "0x%" PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,insn[j].op_str);
                output += buffer;
            }
            cs_free(insn, count);
        } else {
            output = "ERROR: Failed to disassemble given code!\n";
        }
    }
    cs_close(&handle);
    return output;
}

我将非常感谢任何帮助。
热情,
大卫

【问题讨论】:

  • 为什么我的问题被 2 次否决了?我认为这个问题问得非常恰当,如果这很明显,为什么你不会回答而是投反对票?

标签: c++ c reverse-engineering disassembly capstone


【解决方案1】:

Anwser 是简单地使用 SKIPDATA 模式。 Capstone 很棒,但他们的文档很糟糕。

下面的工作示例。这个模式还是有很多bug的,所以这个数据扇区的检测最好是自定义代码。对我来说,它只适用于小块代码。但是,它确实会反汇编到文件末尾。

string disassembly(uint8_t *bytearray, long size, uint64_t startAddress){
    csh handle;
    cs_insn *insn;
    size_t count;
    string output;
    cs_opt_skipdata skipdata = {
       .mnemonic = "db",
    };
    if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) == CS_ERR_OK){
        cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
        cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
        cs_option(handle, CS_OPT_SKIPDATA_SETUP, (size_t)&skipdata);
        count = cs_disasm(handle, bytearray, size, startAddress, 0, &insn);
        if (count > 0) {
            size_t j;
            for (j = 0; j < count; j++) {
                char buffer[512];
                int i=0;
                i = sprintf(buffer, "0x%" PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,insn[j].op_str);
                output += buffer;
            }
            cs_free(insn, count);
        } else {
            output = "ERROR: Failed to disassemble given code!\n";
        }
    }
    cs_close(&handle);
    return output;
}

对那些对这个问题投反对票的巨魔感到羞耻。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2015-12-21
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多