diff --git a/CMakeLists.txt b/CMakeLists.txt index 738b31c..f7f9299 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,24 @@ cmake_minimum_required(VERSION "3.15") + +include(FetchContent) + project("MyProject") # Register Zydis dependency. +FetchContent_Declare( + Zydis + GIT_REPOSITORY https://github.com/zyantific/zydis.git + GIT_TAG master +) # Disable build of tools and examples. -option(ZYDIS_BUILD_TOOLS "" OFF) -option(ZYDIS_BUILD_EXAMPLES "" OFF) -add_subdirectory("deps/zydis") +set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "" FORCE) +set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +# Make available +FetchContent_MakeAvailable(Zydis) # Add our project executable -add_executable("MyProject" "myproject.c") +add_executable("MyProject" "src/myproject.c") -# Have CMake link our project executable against Zydis. -target_link_libraries("MyProject" PRIVATE "Zydis") +# Have CMake link our project executable against Zydis. ${PROJECT_NAME} it's our name on the fifth line +target_link_libraries(${PROJECT_NAME} PRIVATE "Zydis") +target_include_directories(${PROJECT_NAME} PRIVATE "Zydis") diff --git a/README.md b/README.md index cf39a82..c668b05 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,33 @@ ## This project was created using these steps -#### Create a new local git repository - -```shell -git init myproject -cd myproject -``` - -#### Add Zydis as a submodule - -```shell -mkdir deps -git submodule add 'https://github.com/zyantific/zydis.git' deps/zydis -git submodule update --init --recursive -``` - #### Create CMakeLists.txt and myproject.c CMakeLists.txt ```cmake cmake_minimum_required(VERSION "3.15") + +include(FetchContent) + project("MyProject") # Register Zydis dependency. +FetchContent_Declare( + Zydis + GIT_REPOSITORY https://github.com/zyantific/zydis.git + GIT_TAG master +) # Disable build of tools and examples. -option(ZYDIS_BUILD_TOOLS "" OFF) -option(ZYDIS_BUILD_EXAMPLES "" OFF) -add_subdirectory("deps/zydis") +set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "" FORCE) +set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +# Make available +FetchContent_MakeAvailable(Zydis) # Add our project executable add_executable("MyProject" "myproject.c") -# Have CMake link our project executable against Zydis. -target_link_libraries("MyProject" PRIVATE "Zydis") +# Have CMake link our project executable against Zydis. ${PROJECT_NAME} it's our name on the fifth line +target_link_libraries(${PROJECT_NAME} PRIVATE "Zydis") +target_include_directories(${PROJECT_NAME} PRIVATE "Zydis") ``` myproject.c @@ -52,7 +47,7 @@ int main() // Initialize decoder context ZydisDecoder decoder; - ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64); + ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64); // Initialize formatter. Only required when you actually plan to do instruction // formatting ("disassembling"), like we do here @@ -66,23 +61,25 @@ int main() ZyanUSize offset = 0; const ZyanUSize length = sizeof(data); ZydisDecodedInstruction instruction; - while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(&decoder, data + offset, length - offset, - &instruction))) + ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; + while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, data + offset, length - offset, + &instruction, operands))) { // Print current instruction pointer. printf("%016" PRIX64 " ", runtime_address); - // Format & print the binary instruction structure to human readable format + // Format & print the binary instruction structure to human-readable format char buffer[256]; - ZydisFormatterFormatInstruction(&formatter, &instruction, buffer, sizeof(buffer), - runtime_address); + ZydisFormatterFormatInstruction(&formatter, &instruction, operands, + instruction.operand_count_visible, buffer, sizeof(buffer), runtime_address, ZYAN_NULL); puts(buffer); offset += instruction.length; runtime_address += instruction.length; } -} + return 0; +} ``` @@ -95,3 +92,12 @@ cmake .. make ./MyProject ``` +## Running the example (Windows based OS) + +```shell +mkdir bld +cd bld +cmake .. +Now we can open the .sln file with our project and linked Zydis +``` + diff --git a/deps/zydis b/deps/zydis deleted file mode 160000 index 350da19..0000000 --- a/deps/zydis +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 350da19790a12390dc53d7271caf7352f5cd1f0d diff --git a/src/README.txt b/src/README.txt new file mode 100644 index 0000000..6ff5123 --- /dev/null +++ b/src/README.txt @@ -0,0 +1 @@ +here was source diff --git a/myproject.c b/src/myproject.c similarity index 79% rename from myproject.c rename to src/myproject.c index 1e41730..f557375 100644 --- a/myproject.c +++ b/src/myproject.c @@ -13,7 +13,7 @@ int main() // Initialize decoder context ZydisDecoder decoder; - ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64); + ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64); // Initialize formatter. Only required when you actually plan to do instruction // formatting ("disassembling"), like we do here @@ -27,19 +27,22 @@ int main() ZyanUSize offset = 0; const ZyanUSize length = sizeof(data); ZydisDecodedInstruction instruction; - while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(&decoder, data + offset, length - offset, - &instruction))) + ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; + while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, data + offset, length - offset, + &instruction, operands))) { // Print current instruction pointer. printf("%016" PRIX64 " ", runtime_address); - // Format & print the binary instruction structure to human readable format + // Format & print the binary instruction structure to human-readable format char buffer[256]; - ZydisFormatterFormatInstruction(&formatter, &instruction, buffer, sizeof(buffer), - runtime_address); + ZydisFormatterFormatInstruction(&formatter, &instruction, operands, + instruction.operand_count_visible, buffer, sizeof(buffer), runtime_address, ZYAN_NULL); puts(buffer); offset += instruction.length; runtime_address += instruction.length; } -} + + return 0; +} \ No newline at end of file