Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{9E92BC83-28B7-4878-8BC9-BE670A6335BC}</ProjectGuid> | ||
<TemplateGuid>{1bc93793-694f-48fe-9372-81e2b05556fd}</TemplateGuid> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion> | ||
<Configuration>Debug</Configuration> | ||
<Platform Condition="'$(Platform)' == ''">Win32</Platform> | ||
<RootNamespace>ThreadTracing</RootNamespace> | ||
<WindowsTargetPlatformVersion>$(LatestTargetPlatformVersion)</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<TargetVersion>Windows10</TargetVersion> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset> | ||
<ConfigurationType>Driver</ConfigurationType> | ||
<DriverType>KMDF</DriverType> | ||
<DriverTargetPlatform>Universal</DriverTargetPlatform> | ||
<SpectreMitigation>false</SpectreMitigation> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor> | ||
<OutDir>$(SolutionDir)Output\</OutDir> | ||
<EnableInf2cat>false</EnableInf2cat> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<TreatWarningAsError>false</TreatWarningAsError> | ||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<SDLCheck>false</SDLCheck> | ||
<BufferSecurityCheck>false</BufferSecurityCheck> | ||
<ControlFlowGuard>false</ControlFlowGuard> | ||
</ClCompile> | ||
<Link> | ||
<EntryPointSymbol>EntryPoint</EntryPointSymbol> | ||
<AdditionalDependencies>$(SolutionDir)Output\ByePgLib.lib;%(AdditionalDependencies);$(KernelBufferOverflowLib);$(DDK_LIB_PATH)ntoskrnl.lib;$(DDK_LIB_PATH)hal.lib;$(DDK_LIB_PATH)wmilib.lib;$(KMDF_LIB_PATH)$(KMDF_VER_PATH)\WdfLdr.lib;$(KMDF_LIB_PATH)$(KMDF_VER_PATH)\WdfDriverEntry.lib</AdditionalDependencies> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<FilesToPackage Include="$(TargetPath)" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="main.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="NT\SEH.h" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<ClCompile Include="main.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Filter Include="NT"> | ||
<UniqueIdentifier>{eb4c6698-ed3d-4c22-b6a5-aeaff0f08912}</UniqueIdentifier> | ||
</Filter> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="NT\SEH.h"> | ||
<Filter>NT</Filter> | ||
</ClInclude> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<SignMode>Off</SignMode> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#pragma once | ||
#include <ntimage.h> | ||
#include <ntifs.h> | ||
#include <intrin.h> | ||
|
||
#define UNW_FLAG_EHANDLER 1 | ||
|
||
typedef struct _RUNTIME_FUNCTION | ||
{ | ||
ULONG BeginAddress; | ||
ULONG EndAddress; | ||
ULONG UnwindData; | ||
} RUNTIME_FUNCTION, * PRUNTIME_FUNCTION; | ||
|
||
typedef struct _C_SCOPE_TABLE_ENTRY | ||
{ | ||
ULONG Begin; | ||
ULONG End; | ||
ULONG Handler; | ||
ULONG Target; | ||
} C_SCOPE_TABLE_ENTRY, * PC_SCOPE_TABLE_ENTRY; | ||
|
||
typedef struct _C_SCOPE_TABLE | ||
{ | ||
ULONG NumEntries; | ||
C_SCOPE_TABLE_ENTRY Table[ 1 ]; | ||
} C_SCOPE_TABLE, * PC_SCOPE_TABLE; | ||
|
||
extern "C" NTSYSAPI PEXCEPTION_ROUTINE RtlVirtualUnwind( | ||
LONG HandlerType, | ||
DWORD64 ImageBase, | ||
DWORD64 ControlPc, | ||
PRUNTIME_FUNCTION FunctionEntry, | ||
PCONTEXT ContextRecord, | ||
PVOID * HandlerData, | ||
PDWORD64 EstablisherFrame, | ||
PVOID ContextPointers | ||
); | ||
|
||
extern "C" IMAGE_DOS_HEADER __ImageBase; | ||
|
||
namespace SEH | ||
{ | ||
static RUNTIME_FUNCTION* LookupPrivateFunctionEntry( ULONG64 Rip ) | ||
{ | ||
IMAGE_NT_HEADERS* NtHdrs = ( IMAGE_NT_HEADERS* ) ( PUCHAR( &__ImageBase ) + __ImageBase.e_lfanew ); | ||
|
||
IMAGE_DATA_DIRECTORY& ExceptionDirectory = NtHdrs->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_EXCEPTION ]; | ||
RUNTIME_FUNCTION* FunctionTableIt = ( RUNTIME_FUNCTION* ) ( PUCHAR( &__ImageBase ) + ExceptionDirectory.VirtualAddress ); | ||
RUNTIME_FUNCTION* FunctionTableEnd = ( RUNTIME_FUNCTION* ) ( PUCHAR( &__ImageBase ) + ExceptionDirectory.VirtualAddress + ExceptionDirectory.Size ); | ||
|
||
Rip -= ( ULONG64 ) &__ImageBase; | ||
|
||
while ( FunctionTableIt < FunctionTableEnd ) | ||
{ | ||
if ( FunctionTableIt->BeginAddress <= Rip && Rip < FunctionTableIt->EndAddress ) | ||
return FunctionTableIt; | ||
else | ||
FunctionTableIt++; | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
static LONG HandleException( CONTEXT* ContextRecord, EXCEPTION_RECORD* ExceptionRecord ) | ||
{ | ||
if ( RUNTIME_FUNCTION* RtFn = SEH::LookupPrivateFunctionEntry( ContextRecord->Rip ) ) | ||
{ | ||
ULONG ExceptionRva = ContextRecord->Rip - ( ULONG64 ) &__ImageBase; | ||
CONTEXT ContextRecordVt = *ContextRecord; | ||
|
||
PVOID HandlerData = nullptr; | ||
ULONG64 EstablisherFrame = 0; | ||
EXCEPTION_ROUTINE* Routine = RtlVirtualUnwind( | ||
UNW_FLAG_EHANDLER, | ||
( ULONG64 ) &__ImageBase, | ||
ContextRecord->Rip, | ||
RtFn, | ||
&ContextRecordVt, | ||
&HandlerData, | ||
&EstablisherFrame, | ||
nullptr | ||
); | ||
|
||
// Assuming Routine == jmp to _C_specific_handler | ||
if ( Routine ) | ||
{ | ||
C_SCOPE_TABLE* ScopeTable = ( C_SCOPE_TABLE* ) HandlerData; | ||
|
||
for ( int i = 0; i < ScopeTable->NumEntries; i++ ) | ||
{ | ||
if ( ScopeTable->Table[ i ].Begin <= ExceptionRva && ExceptionRva < ScopeTable->Table[ i ].End ) | ||
{ | ||
if ( ScopeTable->Table[ i ].Handler == 1 ) | ||
{ | ||
ContextRecordVt.Rsp -= 0x8; | ||
*( ULONG64* ) ContextRecordVt.Rsp = ContextRecordVt.Rip; | ||
ContextRecordVt.Rsp -= 0x28; | ||
ContextRecordVt.Rip = ( ULONG64 ) &__ImageBase + ScopeTable->Table[ i ].Target; | ||
ContextRecordVt.Rax = ExceptionRecord->ExceptionCode; | ||
*ContextRecord = ContextRecordVt; | ||
return EXCEPTION_CONTINUE_EXECUTION; | ||
} | ||
else | ||
{ | ||
// No exception filter support! | ||
__debugbreak(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return EXCEPTION_EXECUTE_HANDLER; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <ntifs.h> | ||
#include <ntimage.h> | ||
#include "..\Includes\ByePg.h" | ||
#include "NT/SEH.h" | ||
|
||
#define Log(...) DbgPrintEx( DPFLTR_SYSTEM_ID, DPFLTR_ERROR_LEVEL, "[ByePg] " __VA_ARGS__ ) | ||
|
||
void EntryPoint() | ||
{ | ||
NTSTATUS ByePgStatus = ByePgInitialize( SEH::HandleException, TRUE ); | ||
ASSERT( NT_SUCCESS( ByePgStatus ) ); | ||
|
||
__try | ||
{ | ||
__debugbreak(); | ||
} | ||
__except ( 1 ) | ||
{ | ||
Log( "Exception code: %x!\n", GetExceptionCode() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters