Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated version #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
60 changes: 33 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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;
}
```


Expand All @@ -95,3 +92,12 @@ cmake ..
make
./MyProject
```
## Running the example (Windows based OS)
Nitr0-G marked this conversation as resolved.
Show resolved Hide resolved

```shell
mkdir bld
cd bld
cmake ..
Now we can open the .sln file with our project and linked Zydis
```

1 change: 0 additions & 1 deletion deps/zydis
Submodule zydis deleted from 350da1
1 change: 1 addition & 0 deletions src/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
here was source
Nitr0-G marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 10 additions & 7 deletions myproject.c → src/myproject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Nitr0-G marked this conversation as resolved.
Show resolved Hide resolved