Files
archived-Detours/tests/corruptor.cpp
Brian Gianforcaro 2de2babb25 Tests: Add initial set of unit tests for Detours (#137)
- Import the Catch2 self-contained C++ test framework.
  It's used by many Microsoft OSS projects:
  - https://github.com/microsoft/cppwinrt/tree/master/test
  - https://github.com/microsoft/wil/tree/master/tests
  As well as many OSS projects in general.

  When the CMake PR is merged, we can remove this as
  a checked in development dependency, and can instead
  download it using CMake.

- Start basic set of unit tests to validate failure modes of

- Hook the execution into the existing NMake build system.

- Hook test execution into CI pipeline
2020-12-01 16:16:13 -08:00

68 lines
1.8 KiB
C++

//////////////////////////////////////////////////////////////////////////////
//
// Unit Test Image Corruptor (corruptor.cpp of unittests.exe)
//
// Microsoft Research Detours Package
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
#include "windows.h"
#include "corruptor.h"
ImageCorruptor::ImageCorruptor(PIMAGE_DOS_HEADER Header)
{
m_TargetDosHeader = Header;
m_OriginalDosHeader = *Header;
m_OriginalDosProtection = 0;
m_TargetNtHeaders = (PIMAGE_NT_HEADERS)((PBYTE)Header + Header->e_lfanew);
m_OriginalNtHeaders = *m_TargetNtHeaders;
m_OriginalNtProtection = 0;
VirtualProtect(
m_TargetDosHeader,
sizeof(*m_TargetDosHeader),
PAGE_READWRITE,
&m_OriginalDosProtection);
VirtualProtect(
m_TargetNtHeaders,
sizeof(*m_TargetNtHeaders),
PAGE_READWRITE,
&m_OriginalNtProtection);
}
ImageCorruptor::~ImageCorruptor()
{
// Restore original header contents.
//
*m_TargetDosHeader = m_OriginalDosHeader;
*m_TargetNtHeaders = m_OriginalNtHeaders;
// Restore original protection of DOS header.
//
DWORD OldProtection {};
VirtualProtect(
m_TargetDosHeader,
sizeof(*m_TargetDosHeader),
m_OriginalDosProtection,
&OldProtection);
// Restore original protection of NT headers.
//
VirtualProtect(
m_TargetNtHeaders,
sizeof(*m_TargetNtHeaders),
m_OriginalNtProtection,
&OldProtection);
}
void ImageCorruptor::ModifyDosMagic(WORD Value)
{
m_TargetDosHeader->e_magic = Value;
}
void ImageCorruptor::ModifyNtSignature(ULONG Value)
{
m_TargetNtHeaders->Signature = Value;
}