C/C++ Capstone 引擎源碼編譯

Capstone 是一個輕量級的多平台、多架構的反彙編框架。Capstone 旨在成為安全社區中二進位分析和反彙編的終極反彙編引擎。Capstone的編譯非常簡單只需要一步即可輕鬆得到對應的Lib庫文件,如下將介紹該引擎如何被編譯,以及簡單的測試編譯。

下載好以後直接打開項目,切換到msvc目錄下,該目錄下就是引擎的編譯入口,我們直接打開capstone.slh文件,設置平台工具集為編譯器的版本。

在右側選擇capstone_static也就是編譯為靜態庫,然後直接編譯文件。

編譯成功後直接將其中的capstone.lib庫文件拿出來,64位也是如此操作。

編譯成功後會得到兩個文件,直接新建lib目錄,將其放進去。

然後再將項目中的include文件一併拷貝到新建的目錄下。至此庫就編譯好了。

當需要在項目中使用該庫時,只需要簡單的引入到項目中。

接著新建一個項目,寫入如下一段測試程式碼,編譯執行即可實現對特定字元串的反彙編操作。

#include <stdio.h>
#include <inttypes.h>
#include <capstone/capstone.h>

#pragma comment(lib,"capstone32.lib")

int main(int argc, char *argv[])
{
	char *buffer = "\x55\x8b\xec\x81\xec\x24\x03\x00\x00\x6a\x17\x90\x90\x90";

	csh handle;
	cs_insn *insn;
	size_t count;

	int size = 14;

	printf("By: LyShark \n\n");
	// 打開句柄
	if (cs_open(CS_ARCH_X86, CS_MODE_32, &handle) != CS_ERR_OK)
	{
		return 0;
	}

	// 反彙編程式碼,地址從0x1000開始,返回總條數
	// //www.cnblogs.com/lyshark
	count = cs_disasm(handle, (unsigned char *)buffer, 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);

	getchar();
	return 0;
}

預覽效果如下: