• 10.4 认识Capstone反汇编引擎


    Capstone 是一款开源的反汇编框架,目前该引擎支持的CPU架构包括x86、x64、ARM、MIPS、POWERPC、SPARC等,Capstone 的特点是快速、轻量级、易于使用,它可以良好地处理各种类型的指令,支持将指令转换成AT&T汇编语法或Intel汇编语法等多种格式。Capstone的库可以集成到许多不同的应用程序和工具中,因此被广泛应用于反汇编、逆向工程、漏洞分析和入侵检测等领域,著名的比如IDA Pro、Ghidra、Hopper Disassembler等调试器都在使用该引擎。

    • 官方网站:http://www.capstone-engine.org/

    读者可自行下载符合条件的版本,这里笔者选择的是capstone-4.0.2-win32版本,下载并解压这个版本,当读者解压后以后即可在项目中引用该引擎,Capstone引擎的配置非常容易,仅仅需要配置引用目录及库目录即可,配置完成如下图所示;

    实现反汇编的第一步则是打开一个可执行文件,通常在引擎内可调用cs_open()函数实现打开,当打开成功时则该函数会返回一个句柄(handle)用来进行后续的反汇编操作,函数的原型通常如下:

    cs_err cs_open
    (
        cs_arch arch, 
        cs_mode mode, 
        csh *handle
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其中,各参数的含义如下:

    • arch:指定要打开的CPU架构,例如CS_ARCH_X86表示x86架构,CS_ARCH_ARM表示ARM架构等。
    • mode:指定CPU的模式,例如CS_MODE_32表示32位模式,CS_MODE_64表示64位模式等。
    • handle:一个指针,用于返回打开成功后的句柄handle。

    如上所示,函数返回值为cs_err类型,表示函数执行的状态或错误码,它是一个枚举类型,当函数执行成功时返回的数值为CS_ERR_OK,其次函数的第一个参数是指定CPU架构为x86,第二个参数是指定模式为32位模式,最后一个参数用来返回(handle)句柄。

    当一个进程被打开后,则下一步可以通过调用cs_disasm()函数来实现对打开文件的反汇编,cs_disasm函数是Capstone反汇编框架中的一个函数,用于对指定的二进制数据进行反汇编,返回解码后的指令信息。函数原型通常如下:

    size_t cs_disasm
    (
        csh handle,
        const uint8_t *code,
        size_t code_size,
        uint64_t address,
        size_t count,
        cs_insn *insn
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    其中,各参数的含义如下:

    • handle:反汇编器句柄,表示使用哪一个Capstone实例来执行反汇编操作。
    • code:待反汇编的二进制数据的指针,可以是一个地址。
    • code_size:待反汇编的数据的长度,以字节为单位。
    • address:指定待反汇编数据的地址,通常为起始地址。
    • count:指定要反汇编的指令数,如果为0,则会一直反汇编至遇到code_size终止。
    • insn:指向用于保存反汇编结果的cs_insn结构体对象指针,在函数调用结束后存储反汇编结果。

    函数返回值为size_t类型,代表解码的指令数量。在cs_disasm()函数中,我们通过将待反汇编的数据以及其它必要的参数传递给该函数,然后使用cs_insn结构体对象来存储反汇编结果。通过该函数,我们可以获取指令的指令助记符、指令操作数、寻址模式、使用的寄存器等信息。

    当读者理解了这两个API接口后,那么反汇编实现将变得很容易实现,我们来看一下官方针对反汇编实现的一种方式,我们自行封装一个DisassembleCode()函数,该函数传入机器码字符串以及该字符串的长度则会输出该字符串的反汇编代码片段,这段代码的实现如下所示;

    #include 
    #include 
    #include 
    
    #pragma comment(lib,"capstone32.lib")
    
    // 反汇编字符串
    void DisassembleCode(char *start_offset, int size)
    {
        csh handle;
        cs_insn *insn;
        size_t count;
    
        char *buffer = "\x55\x8b\xec\x81\xec\x24\x03\x00\x00\x6a\x17\x90\x90\x90";
    
        // 打开句柄
        if (cs_open(CS_ARCH_X86, CS_MODE_32, &handle) != CS_ERR_OK)
        {
            return;
        }
    
        // 反汇编代码,地址从0x1000开始,返回总条数
        count = cs_disasm(handle, (unsigned char *)start_offset, size, 0x1000, 0, &insn);
    
        if (count > 0)
        {
            size_t index;
            for (index = 0; index < count; index++)
            {
                for (int x = 0; x < insn[index].size; x++)
                {
                    printf("机器码: %d -> %02X \n", x, insn[index].bytes[x]);
                }
    
                printf("地址: 0x%"PRIx64" | 长度: %d 反汇编: %s %s \n", insn[index].address, insn[index].size, insn[index].mnemonic, insn[index].op_str);
            }
    
            cs_free(insn, count);
        }
        else
        {
            printf("反汇编返回长度为空 \n");
        }
    
        cs_close(&handle);
    }
    
    int main(int argc, char *argv[])
    {
        char *buffer = "\x55\x8b\xec\x81\xec\x24\x03\x00\x00\x6a\x17\x90\x90\x90";
        DisassembleCode(buffer, 14);
    
        system("pause");
        return 0;
    }
    
    • 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

    运行上述代码片段,则可看到如下图所示的输出效果;

    上述代码虽然实现了反汇编但并无法保存结果,对于一个通用程序来说,我们当然是希望这写反汇编代码能够存储到一个特殊的容器内,当需要使用是可以随时调出来,此处我们通过定义一个MyStruct并将所需反汇编指令通过ptr.push_back(location)放入到一个全局容器内进行存储,当读者调用DisassembleCode(buffer, 14)函数是则会返回std::vector ptr,并在主函数内通过循环输出这个容器,改进后的代码将会更加易于使用;

    #include 
    #include 
    #include 
    #include 
    
    #pragma comment(lib,"capstone32.lib")
    
    using namespace std;
    
    typedef struct
    {
        int OpCodeSize;
        int OpStringSize;
        unsigned long long Address;
        unsigned char OpCode[16];
        char OpString[256];
    }MyStruct;
    
    static void print_string_hex(unsigned char *str, size_t len)
    {
        unsigned char *c;
        for (c = str; c < str + len; c++)
        {
            printf("0x%02x ", *c & 0xff);
        }
        printf("\n");
    }
    
    // 反汇编字符串
    std::vector<MyStruct> DisassembleCode(char *start_offset, int size)
    {
        std::vector<MyStruct> ptr = {};
    
        csh handle;
        cs_insn *insn;
        size_t count;
    
        // 打开句柄
        if (cs_open(CS_ARCH_X86, CS_MODE_32, &handle) != CS_ERR_OK)
        {
            return{};
        }
    
        // 反汇编代码,地址从0x1000开始,返回总条数
        count = cs_disasm(handle, (unsigned char *)start_offset, size, 0x0, 0, &insn);
    
        if (count > 0)
        {
            size_t index;
    
            // 循环反汇编代码
            for (index = 0; index < count; index++)
            {
                // 清空
                MyStruct location;
                memset(&location, 0, sizeof(MyStruct));
    
                // 循环拷贝机器码
                for (int x = 0; x < insn[index].size; x++)
                {
                    location.OpCode[x] = insn[index].bytes[x];
                }
    
                // 拷贝地址长度
                location.Address = insn[index].address;
                location.OpCodeSize = insn[index].size;
    
                // 拷贝反汇编指令
                strcpy_s(location.OpString, insn[index].mnemonic);
                strcat_s(location.OpString, " ");
                strcat_s(location.OpString, insn[index].op_str);
    
                // 得到反汇编长度
                location.OpStringSize = strlen(location.OpString);
    
                ptr.push_back(location);
            }
            cs_free(insn, count);
        }
        else
        {
            return{};
        }
        cs_close(&handle);
        return ptr;
    }
    
    int main(int argc, char *argv[])
    {
        char *buffer = "\x55\x8b\xec\x81\xec\x24\x03\x00\x00\x6a\x17\x90\x90\x90";
    
        // 反汇编并返回容器
        std::vector<MyStruct> ptr = DisassembleCode(buffer, 14);
    
        // 循环整个容器
        for (int x = 0; x < ptr.size(); x++)
        {
            // 输出地址
            printf("%08X | ", ptr[x].Address);
            printf("%03d | ", ptr[x].OpStringSize);
    
            // 输出反汇编
            for (int z = 0; z < ptr[x].OpStringSize; z++)
            {
                printf("%c", ptr[x].OpString[z]);
            }
            printf("\n");
    
            // 输出机器码
            for (int y = 0; y < ptr[x].OpCodeSize; y++)
            {
                printf("%02X ", ptr[x].OpCode[y]);
            }
    
            printf("\n");
            // print_string_hex(ptr[x].OpCode, ptr[x].OpCodeSize);
        }
    
        system("pause");
        return 0;
    }
    
    • 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
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    运行后输出效果图如下图所示;

  • 相关阅读:
    图像处理技术的综合应用——提取篮球
    slam数学补充
    华为机试 HJ36 字符串加密【Java实现】
    TairSearch:加速多列索引查询
    计算机组成和体系结构[备考]
    SpringBootWeb项目启动时报错端口号被占,又找不到哪个项目启动后没关,解决办法如下:
    没有五十瓶红牛我是不会告诉你——面试中应该如何正确谈薪
    Docker系列第06部分:备份与迁移+dockerfile
    LeetCode 88. 合并两个有序数组(JavaScript 简单)
    手动开发-简单的Spring基于注解配置的程序--源码解析
  • 原文地址:https://blog.csdn.net/lyshark_csdn/article/details/133605750