2016-08-29 05:12:40 +08:00
|
|
|
|
/***************************************************************************************************
|
|
|
|
|
|
|
|
|
|
Zyan Disassembler Engine (Zydis)
|
|
|
|
|
|
|
|
|
|
Original Author : Joel Höner
|
|
|
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
|
|
|
|
|
***************************************************************************************************/
|
|
|
|
|
|
2016-11-21 21:55:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
2016-09-02 01:14:08 +08:00
|
|
|
|
* This file implements a tool that is supposed to be fed as input for fuzzers like AFL,
|
|
|
|
|
* reading a control block from stdin, allowing the fuzzer to reach every possible
|
|
|
|
|
* code-path, testing any possible combination of disassembler configurations.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-08-29 05:12:40 +08:00
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <Zydis/Zydis.h>
|
|
|
|
|
|
2017-11-02 06:39:10 +08:00
|
|
|
|
typedef struct ZydisFuzzControlBlock_
|
|
|
|
|
{
|
2017-07-03 09:58:25 +08:00
|
|
|
|
ZydisMachineMode machineMode;
|
|
|
|
|
ZydisAddressWidth addressWidth;
|
2017-11-02 06:39:10 +08:00
|
|
|
|
ZydisBool decoderMode[ZYDIS_DECODER_MODE_MAX_VALUE + 1];
|
2016-09-13 11:35:25 +08:00
|
|
|
|
ZydisFormatterStyle formatterStyle;
|
2016-11-29 02:12:28 +08:00
|
|
|
|
ZydisFormatterFlags formatterFlags;
|
2016-11-29 01:56:39 +08:00
|
|
|
|
ZydisFormatterAddressFormat formatterAddrFormat;
|
|
|
|
|
ZydisFormatterDisplacementFormat formatterDispFormat;
|
|
|
|
|
ZydisFormatterImmediateFormat formatterImmFormat;
|
2016-08-29 05:12:40 +08:00
|
|
|
|
} ZydisFuzzControlBlock;
|
|
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|
|
|
|
|
/* Entry point */
|
|
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2017-09-20 21:46:51 +08:00
|
|
|
|
if (ZydisGetVersion() != ZYDIS_VERSION)
|
|
|
|
|
{
|
|
|
|
|
fputs("Invalid zydis version\n", stderr);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 05:12:40 +08:00
|
|
|
|
ZydisFuzzControlBlock controlBlock;
|
|
|
|
|
if (fread(&controlBlock, 1, sizeof(controlBlock), stdin) != sizeof(controlBlock))
|
|
|
|
|
{
|
|
|
|
|
fputs("not enough bytes to fuzz\n", stderr);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 23:36:03 +08:00
|
|
|
|
ZydisDecoder decoder;
|
2017-11-02 06:39:10 +08:00
|
|
|
|
if (!ZYDIS_SUCCESS(
|
|
|
|
|
ZydisDecoderInit(&decoder, controlBlock.machineMode, controlBlock.addressWidth)))
|
2017-06-26 05:28:15 +08:00
|
|
|
|
{
|
|
|
|
|
fputs("Failed to initialize decoder\n", stderr);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2017-11-02 06:39:10 +08:00
|
|
|
|
for (ZydisDecoderMode mode = 1; mode <= ZYDIS_DECODER_MODE_MAX_VALUE; ++mode)
|
|
|
|
|
{
|
|
|
|
|
if (!ZYDIS_SUCCESS(
|
|
|
|
|
ZydisDecoderEnableMode(&decoder, mode, controlBlock.decoderMode[mode] ? 1 : 0)))
|
|
|
|
|
{
|
|
|
|
|
fputs("Failed to adjust decoder-mode\n", stderr);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-26 05:28:15 +08:00
|
|
|
|
|
2017-07-03 23:36:03 +08:00
|
|
|
|
ZydisFormatter formatter;
|
|
|
|
|
if (!ZYDIS_SUCCESS(ZydisFormatterInitEx(&formatter, controlBlock.formatterStyle,
|
|
|
|
|
controlBlock.formatterFlags, controlBlock.formatterAddrFormat,
|
2016-11-29 01:56:39 +08:00
|
|
|
|
controlBlock.formatterDispFormat, controlBlock.formatterImmFormat)))
|
2016-08-29 05:12:40 +08:00
|
|
|
|
{
|
2017-01-11 19:18:26 +08:00
|
|
|
|
fputs("failed to initialize instruction-formatter\n", stderr);
|
2016-08-29 05:12:40 +08:00
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 17:29:42 +08:00
|
|
|
|
uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH * 1024];
|
2017-01-11 19:18:26 +08:00
|
|
|
|
size_t numBytesRead;
|
|
|
|
|
do
|
2016-08-29 05:12:40 +08:00
|
|
|
|
{
|
2017-01-11 19:18:26 +08:00
|
|
|
|
numBytesRead = fread(readBuf, 1, sizeof(readBuf), stdin);
|
|
|
|
|
|
2017-07-03 09:58:25 +08:00
|
|
|
|
ZydisDecodedInstruction instruction;
|
2017-01-11 19:18:26 +08:00
|
|
|
|
ZydisStatus status;
|
|
|
|
|
size_t readOffs = 0;
|
2017-07-03 09:58:25 +08:00
|
|
|
|
while ((status = ZydisDecoderDecodeBuffer(&decoder, readBuf + readOffs,
|
|
|
|
|
numBytesRead - readOffs, readOffs, &instruction)) != ZYDIS_STATUS_NO_MORE_DATA)
|
2016-08-29 05:12:40 +08:00
|
|
|
|
{
|
2017-01-11 19:18:26 +08:00
|
|
|
|
if (!ZYDIS_SUCCESS(status))
|
|
|
|
|
{
|
|
|
|
|
++readOffs;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char printBuffer[256];
|
2017-07-03 09:58:25 +08:00
|
|
|
|
ZydisFormatterFormatInstruction(
|
|
|
|
|
&formatter, &instruction, printBuffer, sizeof(printBuffer));
|
|
|
|
|
readOffs += instruction.length;
|
2016-08-29 05:12:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 19:18:26 +08:00
|
|
|
|
if (readOffs < sizeof(readBuf))
|
|
|
|
|
{
|
|
|
|
|
memmove(readBuf, readBuf + readOffs, sizeof(readBuf) - readOffs);
|
|
|
|
|
}
|
|
|
|
|
} while (numBytesRead == sizeof(readBuf));
|
2016-09-02 01:14:08 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
2016-08-29 05:12:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|