1
0
mirror of https://github.com/bitdefender/bddisasm.git synced 2024-11-22 15:38:07 +00:00
bddisasm/bdshemu/bdshemu.c

550 lines
13 KiB
C
Raw Normal View History

2020-07-21 08:19:18 +00:00
/*
* Copyright (c) 2020 Bitdefender
* SPDX-License-Identifier: Apache-2.0
*/
2020-11-17 14:03:16 +00:00
#include "bdshemu.h"
#include "include/bdshemu_common.h"
#include "../bddisasm/include/bddisasm_crt.h"
2020-07-21 08:19:18 +00:00
#ifndef UNREFERENCED_PARAMETER
#define UNREFERENCED_PARAMETER(P) ((void)(P))
#endif
2020-07-21 08:19:18 +00:00
//
2024-02-26 04:13:24 +00:00
// shemu_printf - simple version
2020-07-21 08:19:18 +00:00
//
#ifndef BDDISASM_NO_FORMAT
void
2020-07-21 08:19:18 +00:00
shemu_printf(
SHEMU_CONTEXT *Context,
char *formatstring,
...
)
{
char buff[768];
2020-07-21 08:19:18 +00:00
va_list args;
if (ND_NULL == Context->Log)
2020-07-21 08:19:18 +00:00
{
return;
}
nd_memzero(buff, sizeof(buff));
va_start(args, formatstring);
nd_vsnprintf_s(buff, sizeof(buff), sizeof(buff) - 1, formatstring, args);
va_end(args);
Context->Log(buff, Context->AuxData);
2020-07-21 08:19:18 +00:00
}
#else
void
shemu_printf(
SHEMU_CONTEXT *Context,
char *formatstring,
...
)
{
UNREFERENCED_PARAMETER(Context);
UNREFERENCED_PARAMETER(formatstring);
}
#endif // !BDDISASM_NO_FORMAT
2020-07-21 08:19:18 +00:00
//
// shemu_memcpy
//
void *
2020-07-21 08:19:18 +00:00
shemu_memcpy(
void *Dest,
const void *Source,
ND_SIZET Size
2020-07-21 08:19:18 +00:00
)
{
void *start = Dest;
ND_UINT32 index = 0;
2020-07-21 08:19:18 +00:00
if (ND_NULL == Dest)
2020-07-21 08:19:18 +00:00
{
return ND_NULL;
2020-07-21 08:19:18 +00:00
}
if (ND_NULL == Source)
2020-07-21 08:19:18 +00:00
{
return ND_NULL;
2020-07-21 08:19:18 +00:00
}
while (Size--)
{
*(char *)Dest = *((char *)Source + index);
Dest = (char *)Dest + 1;
index++;
}
return start;
}
//
// ShemuHexlify
2020-07-21 08:19:18 +00:00
//
void
ShemuHexlify(
ND_UINT8 *Value,
ND_UINT64 ValueSize,
char *Hex,
ND_UINT64 HexSize
2020-07-21 08:19:18 +00:00
)
{
const char hexDigits[16] =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
2020-07-21 08:19:18 +00:00
nd_memzero(Hex, (ND_SIZET)HexSize);
2020-07-21 08:19:18 +00:00
if (HexSize <= ValueSize * 2)
{
return;
}
2020-07-21 08:19:18 +00:00
// Convert instruction bytes to string.
for (ND_UINT32 i = 0; i < ValueSize; i++)
{
Hex[(i * 2) + 0] = hexDigits[(Value[i] >> 4) & 0xF];
Hex[(i * 2) + 1] = hexDigits[(Value[i] >> 0) & 0xF];
}
2020-07-21 08:19:18 +00:00
// Since we make sure HexSize is greater than twice the size of the Value, and since we always nd_memzero Hex
// at the beginning, we are guaranteed that Hex will be properly NULL terminated.
2020-07-21 08:19:18 +00:00
}
//
// ShemuBmpStateUpdate
2020-07-21 08:19:18 +00:00
//
void
ShemuBmpStateUpdate(
ND_UINT8 *Bitmap,
ND_UINT64 Size,
ND_UINT64 Start,
ND_UINT64 Count,
ND_UINT8 Flags,
ND_BOOL Clear
2020-07-21 08:19:18 +00:00
)
//
// Sets the indicated state inside the Bitmap.
2020-07-21 08:19:18 +00:00
//
{
ND_UINT64 i;
2020-07-21 08:19:18 +00:00
if (ND_NULL == Bitmap || Start >= Size || Count > Size || Start + Count > Size)
{
return;
}
for (i = 0; i < Count; i++)
2020-07-21 08:19:18 +00:00
{
if (!Clear)
2020-07-21 08:19:18 +00:00
{
Bitmap[Start + i] |= Flags;
2020-07-21 08:19:18 +00:00
}
else
{
Bitmap[Start + i] &= ~Flags;
2020-07-21 08:19:18 +00:00
}
}
}
//
// ShemuBmpStateCheck
2020-07-21 08:19:18 +00:00
//
ND_BOOL
ShemuBmpStateCheck(
ND_UINT8 *Bitmap,
ND_UINT64 Size,
ND_UINT64 Start,
ND_UINT64 Count,
ND_UINT8 Flags
2020-07-21 08:19:18 +00:00
)
//
// Check if any of the state bytes in the Bitmap has any of the Flags set.
//
// @returns ND_TRUE if any state byte contains the indicated Flags.
2020-07-21 08:19:18 +00:00
//
{
ND_UINT64 i;
if (ND_NULL == Bitmap || Start >= Size || Count > Size || Start + Count > Size)
{
return ND_FALSE;
}
2020-07-21 08:19:18 +00:00
for (i = 0; i < Count; i++)
2020-07-21 08:19:18 +00:00
{
if (Bitmap[Start + i] & Flags)
2020-07-21 08:19:18 +00:00
{
return ND_TRUE;
2020-07-21 08:19:18 +00:00
}
}
return ND_FALSE;
2020-07-21 08:19:18 +00:00
}
//
// ShemuIsShellcodePtr
2020-07-21 08:19:18 +00:00
//
ND_BOOL
ShemuIsShellcodePtr(
2020-07-21 08:19:18 +00:00
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size
2020-07-21 08:19:18 +00:00
)
{
return (Gla >= Context->ShellcodeBase && Gla < Context->ShellcodeBase + Context->ShellcodeSize &&
Gla + Size > Context->ShellcodeBase && Gla + Size <= Context->ShellcodeBase + Context->ShellcodeSize);
}
2020-07-21 08:19:18 +00:00
//
// ShemuIsStackPtr
//
ND_BOOL
ShemuIsStackPtr(
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size
)
{
return (Gla >= Context->StackBase && Gla < Context->StackBase + Context->StackSize &&
Gla + Size > Context->StackBase && Gla + Size <= Context->StackBase + Context->StackSize);
}
2020-07-21 08:19:18 +00:00
//
// ShemuIsIcachePtr
//
ND_BOOL
ShemuIsIcachePtr(
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size
)
{
return (Gla >= Context->Icache.Address && Gla < Context->Icache.Address + Context->Icache.Size &&
Gla + Size > Context->Icache.Address && Gla + Size <= Context->Icache.Address + Context->Icache.Size);
}
2020-07-21 08:19:18 +00:00
//
2024-02-26 04:13:24 +00:00
// ShemuCopyMem
//
void
ShemuCopyMem(
ND_UINT8 *Destination,
ND_UINT8 *Source,
ND_UINT64 Size
)
{
2024-06-04 16:13:52 +00:00
shemu_memcpy(Destination, Source, (ND_SIZET)Size);
}
//
// ShemuMemLoad
//
SHEMU_STATUS
ShemuMemLoad(
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size,
ND_UINT8 *Value
)
{
if (ShemuIsShellcodePtr(Context, Gla, Size))
2020-07-21 08:19:18 +00:00
{
// Shellcode access.
if (0 == (Context->Options & SHEMU_OPT_DIRECT_MAPPED_SHELL))
2020-07-21 08:19:18 +00:00
{
// Shellocode is local copy, we can directly access it.
ShemuCopyMem(Value, Context->Shellcode + (Gla - Context->ShellcodeBase), Size);
2020-07-21 08:19:18 +00:00
}
else
{
// Shellcode is directly mapped, we must go through the shellcode access API.
if ((ND_NULL == Context->AccessShellcode) ||
!Context->AccessShellcode(Context, Gla, (ND_SIZET)Size, Value, ND_FALSE))
2020-07-21 08:19:18 +00:00
{
return SHEMU_ABORT_GLA_OUTSIDE;
2020-07-21 08:19:18 +00:00
}
}
}
else if (ShemuIsStackPtr(Context, Gla, Size))
2020-07-21 08:19:18 +00:00
{
ShemuCopyMem(Value, Context->Stack + (Gla - Context->StackBase), Size);
2020-07-21 08:19:18 +00:00
}
else
2020-07-21 08:19:18 +00:00
{
ND_UINT32 selfOffset = 0;
ND_BOOL res = ND_FALSE;
// We allow a maximum number of external memory accesses, due to performance reasons.
if (++Context->ExtMemAccess > Context->MemThreshold)
2020-07-21 08:19:18 +00:00
{
goto _check_special_access;
2020-07-21 08:19:18 +00:00
}
// NOTE: The accessed GLA may partially access an internal address (shellcode or stack) and an external address.
// Since the AccessMemory callback can be provided with the full SHEMU_CONTEXT, the integrator can choose how
// to handle those accesses; some options are:
// - Don't handle them at all, and return error (ND_FALSE);
// - Handle them by reading the actual memory value; this has the disadvantage that if the shellcode/stack
// portion has been modified due to emulation, the AccessMemory function would return the original memory
// value;
// - Handle them properly, by returning the emulated values for the internal addresses, and the external
// values for the external addresses.
// bdshemu does not care directly about this, and lets the integrator choose his own strategy.
if (ND_NULL != Context->AccessMemory)
2020-07-21 08:19:18 +00:00
{
res = Context->AccessMemory(Context, Gla, (ND_SIZET)Size, Value, ND_FALSE);
2020-07-21 08:19:18 +00:00
}
if (res)
2020-07-21 08:19:18 +00:00
{
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
_check_special_access:
if (Context->ArchType == SHEMU_ARCH_TYPE_X86)
2020-07-21 08:19:18 +00:00
{
selfOffset = Context->Arch.X86.Mode == ND_CODE_32 ? 0x18 : 0x30;
2020-07-21 08:19:18 +00:00
}
else
{
return SHEMU_ABORT_INVALID_PARAMETER;
2020-07-21 08:19:18 +00:00
}
// If we got here, the external memory access has not been handled.
if (Gla == Context->TibBase + selfOffset)
2020-07-21 08:19:18 +00:00
{
// TEB.Self access.
if (Size == 4)
2020-07-21 08:19:18 +00:00
{
*(ND_UINT32*)Value = (ND_UINT32)Context->TibBase;
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
else if (Size == 8)
2020-07-21 08:19:18 +00:00
{
*(ND_UINT64*)Value = (ND_UINT64)Context->TibBase;
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
}
return SHEMU_ABORT_GLA_OUTSIDE;
2020-07-21 08:19:18 +00:00
}
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
//
// ShemuMemStore
2020-07-21 08:19:18 +00:00
//
SHEMU_STATUS
ShemuMemStore(
2020-07-21 08:19:18 +00:00
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size,
ND_UINT8 *Value
2020-07-21 08:19:18 +00:00
)
{
if (ShemuIsShellcodePtr(Context, Gla, Size))
2020-07-21 08:19:18 +00:00
{
// Bypass self-writes, if needed to. No need to invalidate the icache in this case.
if (!!(Context->Options & SHEMU_OPT_BYPASS_SELF_WRITES))
2020-07-21 08:19:18 +00:00
{
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
// Flush the instruction cache, if any stored address in it. We can do this AFTER checking
// for SHEMU_OPT_BYPASS_SELF_WRITES - with that flag set, the shellcode will never be modified.
if (ShemuIsIcachePtr(Context, Gla, 1) || ShemuIsIcachePtr(Context, Gla + Size - 1, 1))
2020-07-21 08:19:18 +00:00
{
ShemuFlushIcache(Context);
2020-07-21 08:19:18 +00:00
}
// Shellcode access.
if (0 == (Context->Options & SHEMU_OPT_DIRECT_MAPPED_SHELL))
2020-07-21 08:19:18 +00:00
{
// Shellocode is local copy, we can directly access it.
ShemuCopyMem(Context->Shellcode + (Gla - Context->ShellcodeBase), Value, Size);
2020-07-21 08:19:18 +00:00
}
else
2020-07-21 08:19:18 +00:00
{
// Shellcode is directly mapped, we must go through the memory access API. Note that the integrator
// is free to do whatever it wants with this call - generally, it is a good idea to just discard
// modifications. For safety, currently we force the SHEMU_OPT_BYPASS_SELF_WRITES option whenever
// the SHEMU_OPT_DIRECT_MAPPED_SHELL option is used, to avoid problems.
if ((ND_NULL == Context->AccessShellcode) ||
!Context->AccessShellcode(Context, Gla, (ND_SIZET)Size, Value, ND_TRUE))
{
return SHEMU_ABORT_GLA_OUTSIDE;
}
2020-07-21 08:19:18 +00:00
}
}
else if (ShemuIsStackPtr(Context, Gla, Size))
{
ShemuCopyMem(Context->Stack + (Gla - Context->StackBase), Value, Size);
}
else
{
ND_BOOL res = ND_FALSE;
// We allow a maximum number of external memory accesses, due to performance reasons.
if (++Context->ExtMemAccess > Context->MemThreshold)
2020-07-21 08:19:18 +00:00
{
return SHEMU_ABORT_GLA_OUTSIDE;
2020-07-21 08:19:18 +00:00
}
// Handling external stores made by the shellcode can be done in a variety of ways by the integrator. Some
// of the solutions are:
// - Abort on external stores; this will cause the emulation to immediately stop;
// - Discard external stores; this is very simple, and it assumes that modified memory addresses will
// not be read later on by the shellcode;
// - Create a store-buffer like structure, where every external store is cached; when a load is issued on
// a previously written address, the value from the store-buffer can be returned;
// For obvious reasons, actually storing the value at the indicated address is a very, very bad idea.
if (ND_NULL != Context->AccessMemory)
2020-07-21 08:19:18 +00:00
{
res = Context->AccessMemory(Context, Gla, (ND_SIZET)Size, Value, ND_TRUE);
2020-07-21 08:19:18 +00:00
}
if (res)
2020-07-21 08:19:18 +00:00
{
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
return SHEMU_ABORT_GLA_OUTSIDE;
}
2020-07-21 08:19:18 +00:00
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
//
// ShemuMemFetch
2020-07-21 08:19:18 +00:00
//
SHEMU_STATUS
ShemuMemFetch(
2020-07-21 08:19:18 +00:00
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size,
ND_UINT8 *Bytes
2020-07-21 08:19:18 +00:00
)
{
ND_UINT8 *addr;
ND_UINT64 offset;
if (Size > SHEMU_ICACHE_SIZE || Size == 0)
2020-07-21 08:19:18 +00:00
{
return SHEMU_ABORT_INVALID_PARAMETER;
2020-07-21 08:19:18 +00:00
}
if (!ShemuIsShellcodePtr(Context, Gla, Size))
{
return SHEMU_ABORT_RIP_OUTSIDE;
}
addr = Context->Shellcode;
offset = Gla - Context->ShellcodeBase;
if (0 != (Context->Options & SHEMU_OPT_DIRECT_MAPPED_SHELL))
{
// If the entire address is NOT inside the cache, re-fill the entire line, starting with Gla.
if (!ShemuIsIcachePtr(Context, Gla, 1) || !ShemuIsIcachePtr(Context, Gla + Size - 1, 1))
{
// Re-fill the line.
Context->Icache.Address = Gla;
Context->Icache.Size = MIN(SHEMU_ICACHE_SIZE, Context->ShellcodeSize - offset);
if (Context->Icache.Size < Size)
{
return SHEMU_ABORT_RIP_OUTSIDE;
}
2020-07-21 08:19:18 +00:00
if ((ND_NULL == Context->AccessShellcode) ||
!Context->AccessShellcode(Context, Gla, (ND_SIZET)Context->Icache.Size, Context->Icache.Icache, ND_FALSE))
{
return SHEMU_ABORT_FETCH_ERROR;
}
}
2020-07-21 08:19:18 +00:00
addr = Context->Icache.Icache;
offset = Gla - Context->Icache.Address;
2020-07-21 08:19:18 +00:00
}
shemu_memcpy(Bytes, addr + offset, (ND_SIZET)Size);
2020-07-21 08:19:18 +00:00
return SHEMU_SUCCESS;
2020-07-21 08:19:18 +00:00
}
void
ShemuFlushIcache(
SHEMU_CONTEXT *Context
2020-07-21 08:19:18 +00:00
)
{
Context->Icache.Address = 0;
Context->Icache.Size = 0;
2020-07-21 08:19:18 +00:00
}
//
// ShemuDisplayMemValue
2020-07-21 08:19:18 +00:00
//
void
ShemuDisplayMemValue(
2020-07-21 08:19:18 +00:00
SHEMU_CONTEXT *Context,
ND_UINT64 Gla,
ND_UINT64 Size,
ND_UINT8 *Value,
ND_BOOL Load
2020-07-21 08:19:18 +00:00
)
{
char svalue[ND_MAX_REGISTER_SIZE * 2 + 2] = { 0 };
2020-07-21 08:19:18 +00:00
if (Size > ND_MAX_REGISTER_SIZE)
{
// Truncate the displayed value to 64 bytes.
Size = ND_MAX_REGISTER_SIZE;
}
ShemuHexlify(Value, Size, svalue, sizeof(svalue));
shemu_printf(Context, " Memory %s, address 0x%016llx, size %lld, value %s\n",
Load ? "LOAD" : "STOR", Gla, Size, svalue);
2020-07-21 08:19:18 +00:00
}
//
// ShemuEmulate
2020-07-21 08:19:18 +00:00
//
SHEMU_STATUS
ShemuEmulate(
SHEMU_CONTEXT *Context
2020-07-21 08:19:18 +00:00
)
{
if (Context->ArchType == SHEMU_ARCH_TYPE_X86)
2020-07-21 08:19:18 +00:00
{
return ShemuX86Emulate(Context);
2020-07-21 08:19:18 +00:00
}
else
{
return SHEMU_ABORT_INVALID_PARAMETER;
2020-07-21 08:19:18 +00:00
}
2024-02-26 04:13:24 +00:00
}