Merge branch 'develop' of github.com:zyantific/zyan-disassembler-engine into develop

This commit is contained in:
flobernd 2017-01-11 17:29:36 +01:00
commit 794a769800
3 changed files with 68 additions and 46 deletions

View File

@ -94,6 +94,7 @@ if (ZYDIS_BUILD_EXAMPLES)
"examples/FormatHelper.h") "examples/FormatHelper.h")
target_link_libraries("FormatterHooks" "Zydis") target_link_libraries("FormatterHooks" "Zydis")
set_target_properties ("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter") set_target_properties ("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif () endif ()
# Tools # Tools
@ -101,8 +102,10 @@ if (ZYDIS_BUILD_TOOLS)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c") add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis") target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools") set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
add_executable("ZydisFuzzIn" "tools/ZydisFuzzIn.c") add_executable("ZydisFuzzIn" "tools/ZydisFuzzIn.c")
target_link_libraries("ZydisFuzzIn" "Zydis") target_link_libraries("ZydisFuzzIn" "Zydis")
set_target_properties("ZydisFuzzIn" PROPERTIES FOLDER "Tools") set_target_properties("ZydisFuzzIn" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif () endif ()

View File

@ -38,26 +38,19 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc != 2) if (argc < 1 || argc > 2)
{ {
fprintf(stderr, "Usage: %s <input file>\n", (argc > 0 ? argv[0] : "ZydisDisasm")); fprintf(stderr, "Usage: %s [input file]\n", (argc > 0 ? argv[0] : "ZydisDisasm"));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
FILE* file = fopen(argv[1], "rb"); FILE* file = argc >= 2 ? fopen(argv[1], "rb") : stdin;
if (!file) if (!file)
{ {
fprintf(stderr, "Can not open file: %s\n", strerror(errno)); fprintf(stderr, "Can not open file: %s\n", strerror(errno));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ZydisFileInput input;
if (!ZYDIS_SUCCESS(ZydisInputInitFileInput(&input, file)))
{
fputs("Failed to initialize file-input\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionFormatter formatter; ZydisInstructionFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter, if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter,
ZYDIS_FORMATTER_STYLE_INTEL, ZYDIS_FMTFLAG_FORCE_SEGMENTS | ZYDIS_FMTFLAG_FORCE_OPERANDSIZE, ZYDIS_FORMATTER_STYLE_INTEL, ZYDIS_FMTFLAG_FORCE_SEGMENTS | ZYDIS_FMTFLAG_FORCE_OPERANDSIZE,
@ -68,26 +61,46 @@ int main(int argc, char** argv)
} }
ZydisInstructionDecoder decoder; ZydisInstructionDecoder decoder;
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoderEx(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT, if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoder(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT)))
(ZydisCustomInput*)&input, ZYDIS_DECODER_FLAG_SKIP_DATA)))
{ {
fputs("Failed to initialize instruction-decoder\n", stderr); fputs("Failed to initialize instruction-decoder\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
char buffer[256]; uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH];
ZydisInstructionInfo info; size_t numBytesRead;
while (ZYDIS_SUCCESS(ZydisDecoderDecodeNextInstruction(&decoder, &info))) do
{ {
if (info.instrFlags & ZYDIS_INSTRFLAG_ERROR_MASK) numBytesRead = fread(readBuf, 1, sizeof(readBuf), file);
ZydisInstructionInfo info;
ZydisStatus status;
size_t readOffs = 0;
while ((status = ZydisDecoderDecodeInstruction(
&decoder, readBuf + readOffs, numBytesRead - readOffs, &info
)) != ZYDIS_STATUS_NO_MORE_DATA)
{ {
printf("db %02X\n", info.data[0]); if (!ZYDIS_SUCCESS(status))
continue; {
} ++decoder.instructionPointer;
++readOffs;
printf("db %02X\n", info.data[0]);
continue;
}
ZydisFormatterFormatInstruction(&formatter, &info, buffer, sizeof(buffer)); char printBuffer[256];
puts(buffer); ZydisFormatterFormatInstruction(&formatter, &info, printBuffer, sizeof(printBuffer));
} puts(printBuffer);
readOffs += info.length;
}
if (readOffs < sizeof(readBuf))
{
memmove(readBuf, readBuf + readOffs, sizeof(readBuf) - readOffs);
}
} while (numBytesRead == sizeof(readBuf));
return 0;
} }
/* ============================================================================================== */ /* ============================================================================================== */

View File

@ -40,14 +40,12 @@
#include <Zydis/Zydis.h> #include <Zydis/Zydis.h>
typedef struct ZydisFuzzControlBlock_ { typedef struct ZydisFuzzControlBlock_ {
ZydisDisassemblerMode disasMode; ZydisDisassemblerMode disasMode;
ZydisDecoderFlags decoderFlags;
ZydisFormatterStyle formatterStyle; ZydisFormatterStyle formatterStyle;
ZydisFormatterFlags formatterFlags; ZydisFormatterFlags formatterFlags;
ZydisFormatterAddressFormat formatterAddrFormat; ZydisFormatterAddressFormat formatterAddrFormat;
ZydisFormatterDisplacementFormat formatterDispFormat; ZydisFormatterDisplacementFormat formatterDispFormat;
ZydisFormatterImmediateFormat formatterImmFormat; ZydisFormatterImmediateFormat formatterImmFormat;
uint8_t bufSize;
} ZydisFuzzControlBlock; } ZydisFuzzControlBlock;
/* ============================================================================================== */ /* ============================================================================================== */
@ -63,45 +61,53 @@ int main()
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ZydisFileInput input;
if (!ZYDIS_SUCCESS(ZydisInputInitFileInput(&input, stdin)))
{
fputs("failed to initialize file-input\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionFormatter formatter; ZydisInstructionFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter, if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter,
controlBlock.formatterStyle, controlBlock.formatterFlags, controlBlock.formatterAddrFormat, controlBlock.formatterStyle, controlBlock.formatterFlags, controlBlock.formatterAddrFormat,
controlBlock.formatterDispFormat, controlBlock.formatterImmFormat))) controlBlock.formatterDispFormat, controlBlock.formatterImmFormat)))
{ {
fputs("failed to initialized instruction-formatter\n", stderr); fputs("failed to initialize instruction-formatter\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ZydisInstructionDecoder decoder; ZydisInstructionDecoder decoder;
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoderEx(&decoder, controlBlock.disasMode, if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoder(&decoder, controlBlock.disasMode)))
(ZydisCustomInput*)&input, controlBlock.decoderFlags)))
{ {
fputs("Failed to initialize instruction-decoder\n", stderr); fputs("Failed to initialize instruction-decoder\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ZydisInstructionInfo info; uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH];
char *outBuf = malloc(controlBlock.bufSize); size_t numBytesRead;
while (ZYDIS_SUCCESS(ZydisDecoderDecodeNextInstruction(&decoder, &info))) do
{ {
if (info.instrFlags & ZYDIS_INSTRFLAG_ERROR_MASK) numBytesRead = fread(readBuf, 1, sizeof(readBuf), stdin);
ZydisInstructionInfo info;
ZydisStatus status;
size_t readOffs = 0;
while ((status = ZydisDecoderDecodeInstruction(
&decoder, readBuf + readOffs, numBytesRead - readOffs, &info
)) != ZYDIS_STATUS_NO_MORE_DATA)
{ {
printf("db %02X\n", info.data[0]); if (!ZYDIS_SUCCESS(status))
continue; {
++decoder.instructionPointer;
++readOffs;
continue;
}
char printBuffer[256];
ZydisFormatterFormatInstruction(&formatter, &info, printBuffer, sizeof(printBuffer));
readOffs += info.length;
} }
ZydisFormatterFormatInstruction(&formatter, &info, outBuf, controlBlock.bufSize); if (readOffs < sizeof(readBuf))
puts(outBuf); {
} memmove(readBuf, readBuf + readOffs, sizeof(readBuf) - readOffs);
}
} while (numBytesRead == sizeof(readBuf));
free(outBuf);
return 0; return 0;
} }