1
0
mirror of https://github.com/bitdefender/bddisasm.git synced 2024-11-22 07:28:07 +00:00
bddisasm/bdshemu_fuzz/bdshemu_fuzzer.c

216 lines
4.7 KiB
C
Raw Normal View History

2020-07-21 08:19:18 +00:00
/*
* Copyright (c) 2020 Bitdefender
* SPDX-License-Identifier: Apache-2.0
*/
2023-04-05 08:06:10 +00:00
#ifdef _MSC_VER
2020-07-21 08:19:18 +00:00
#define _CRT_SECURE_NO_WARNINGS
2023-04-05 08:06:10 +00:00
#endif
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
#include <limits.h>
2020-07-21 08:19:18 +00:00
#include <stdarg.h>
2023-04-05 08:06:10 +00:00
#include <stdbool.h>
2020-07-21 08:19:18 +00:00
#include <stdio.h>
2023-04-05 08:06:10 +00:00
#include <stdint.h>
2020-07-21 08:19:18 +00:00
#include <stdlib.h>
#include <string.h>
#include "bddisasm.h"
#include "bdshemu.h"
2023-04-05 08:06:10 +00:00
#pragma clang optimize off
#pragma GCC optimize("O0")
2020-07-21 08:19:18 +00:00
#ifdef ENABLE_LOGGING
#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif // ENABLE_LOGGING
#define DEFAULT_OPTIONS (SHEMU_OPT_TRACE_EMULATION |\
SHEMU_OPT_SUPPORT_AES |\
SHEMU_OPT_SUPPORT_APX)
void ShemuLog(char *data, void *ctx)
2020-07-21 08:19:18 +00:00
{
(void)(ctx);
2023-04-05 08:06:10 +00:00
LOG("%s", data);
2020-07-21 08:19:18 +00:00
}
ND_BOOL
access_shellcode(void *Ctx, ND_UINT64 Gla, ND_SIZET Size, ND_UINT8 *Buffer, ND_BOOL Store)
{
SHEMU_CONTEXT *ctx = Ctx;
ND_UINT32 offset;
offset = (ND_UINT32)(Gla - ctx->ShellcodeBase);
if (Store)
{
memcpy(ctx->Shellcode + offset, Buffer, Size);
}
else
{
memcpy(Buffer, ctx->Shellcode + offset, Size);
}
return true;
}
#if defined(FUZZ_X86) || defined(FUZZ_X64)
2023-04-05 08:06:10 +00:00
#ifdef FUZZ_X86
#define DEF_CODE ND_CODE_32
#define FUZZER_TYPE "x86"
#else
#define DEF_CODE ND_CODE_64
#define FUZZER_TYPE "x64"
#endif // FUZZ_X86
void run_shemu(uint8_t *Data, size_t Size)
2020-07-21 08:19:18 +00:00
{
// More or less copy pasted from disasmtool
SHEMU_CONTEXT ctx = { 0 };
SHEMU_STATUS shs;
#if defined(DIRECT_MAP)
ctx.AccessShellcode = access_shellcode;
ctx.Options |= SHEMU_OPT_DIRECT_MAPPED_SHELL;
#endif
ctx.ArchType = SHEMU_ARCH_TYPE_X86;
2020-07-21 08:19:18 +00:00
ctx.Shellcode = Data;
ctx.Stack = calloc(1, 0x2000);
if (ctx.Stack == NULL)
{
LOG("[-] Failed to allocate Stack!\n");
abort();
}
ctx.Intbuf = calloc(1, Size + 0x2000);
if (ctx.Stack == NULL)
{
LOG("[-] Failed to allocate Intbuf!\n");
abort();
}
ctx.ShellcodeBase = 0x200000;
ctx.ShellcodeSize = (uint32_t)Size;
ctx.StackBase = 0x100000;
ctx.StackSize = 0x2000;
ctx.Arch.X86.Registers.RegRsp = 0x101000;
2020-07-21 08:19:18 +00:00
ctx.IntbufSize = (uint32_t)Size + 0x2000;
ctx.Arch.X86.Registers.RegFlags = NDR_RFLAG_IF | 2;
ctx.Arch.X86.Registers.RegRip = ctx.ShellcodeBase;
ctx.Arch.X86.Segments.Cs.Selector = 0x10;
ctx.Arch.X86.Segments.Ds.Selector = 0x28;
ctx.Arch.X86.Segments.Es.Selector = 0x28;
ctx.Arch.X86.Segments.Ss.Selector = 0x28;
ctx.Arch.X86.Segments.Fs.Selector = 0x30;
ctx.Arch.X86.Segments.Fs.Base = 0x7FFF0000;
ctx.Arch.X86.Segments.Gs.Selector = 0x30;
ctx.Arch.X86.Segments.Gs.Base = 0x7FFF0000;
ctx.Arch.X86.Mode = DEF_CODE;
ctx.Arch.X86.Ring = 3;
ctx.TibBase = ctx.Arch.X86.Mode == ND_CODE_32 ? ctx.Arch.X86.Segments.Fs.Base : ctx.Arch.X86.Segments.Gs.Base;
2020-07-21 08:19:18 +00:00
ctx.MaxInstructionsCount = 4096;
ctx.Log = &ShemuLog;
ctx.Flags = 0;
ctx.Options |= DEFAULT_OPTIONS;
2020-07-21 08:19:18 +00:00
shs = ShemuEmulate(&ctx);
LOG("[+] Shemu returned: 0x%08x\n", shs);
free(ctx.Intbuf);
free(ctx.Stack);
}
#else
#error "Do not know what to fuzz, define one of FUZZ_X86, FUZZ_X64"
#endif
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
#if defined(__AFL_FUZZ_TESTCASE_LEN)
// See https://github.com/AFLplusplus/AFLplusplus/blob/stable/instrumentation/README.persistent_mode.md
__AFL_FUZZ_INIT();
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
int main()
2020-07-21 08:19:18 +00:00
{
2023-04-05 08:06:10 +00:00
__AFL_INIT();
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
uint8_t *buffer = __AFL_FUZZ_TESTCASE_BUF;
while (__AFL_LOOP(UINT_MAX))
2020-07-21 08:19:18 +00:00
{
2023-04-05 08:06:10 +00:00
size_t size = __AFL_FUZZ_TESTCASE_LEN;
run_shemu(buffer, size);
}
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
return 0;
}
#elif defined(__clang__)
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
{
// Shemu will write into the shellcode buffer, so copy it to a new buffer first.
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
uint8_t *buffer = malloc(Size);
if (!buffer) return 1;
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
memcpy(buffer, Data, Size);
run_shemu(buffer, Size);
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
free(buffer);
return 0;
}
#else
uint8_t *read_file(const char *fpath, size_t *size)
2020-07-21 08:19:18 +00:00
{
uint8_t *buffer;
2023-04-05 08:06:10 +00:00
FILE *fd = fopen(fpath, "rb");
if (fd == NULL)
2020-07-21 08:19:18 +00:00
{
2023-04-05 08:06:10 +00:00
LOG("[-] Failed to open \"%s\"\n", fpath);
return NULL;
2020-07-21 08:19:18 +00:00
}
2023-04-05 08:06:10 +00:00
fseek(fd, 0ull, SEEK_END);
*size = ftell(fd);
rewind(fd);
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
buffer = malloc(*size);
if (buffer == NULL)
2020-07-21 08:19:18 +00:00
{
2023-04-05 08:06:10 +00:00
LOG("[-] Failed to allocate %zu bytes\n", *size);
2020-07-21 08:19:18 +00:00
}
else
{
2023-04-05 08:06:10 +00:00
fread(buffer, *size, 1, fd);
2020-07-21 08:19:18 +00:00
}
2023-04-05 08:06:10 +00:00
fclose(fd);
return buffer;
}
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
int main(int argc, char **argv)
{
if (argc < 2)
2020-07-21 08:19:18 +00:00
{
2023-04-05 08:06:10 +00:00
LOG("Missing file argument\n");
abort();
}
2020-07-21 08:19:18 +00:00
2023-04-05 08:06:10 +00:00
size_t size;
uint8_t *buffer = read_file(argv[1], &size);
if (!buffer)
{
LOG("[-] read_file failed for \"%s\"\n", argv[1]);
abort();
2020-07-21 08:19:18 +00:00
}
2023-04-05 08:06:10 +00:00
run_shemu(buffer, size);
free(buffer);
2020-07-21 08:19:18 +00:00
return 0;
}
2023-04-05 08:06:10 +00:00
#endif // defined(__AFL_FUZZ_TESTCASE_LEN)