Skip to content

Commit

Permalink
Add ability to log if the current thread's stack was made RWX via Vir…
Browse files Browse the repository at this point in the history
…tualProtectEx/NtProtectVirtualMemory

Also make the Debug releases of cuckoomon define CUCKOODBG (for reading from a predefined config file and outputting logs to the filesystem) and name them *_dbg.dll to make it clear which is which.
  • Loading branch information
spender-sandbox committed Jan 8, 2016
1 parent c63b10c commit 397f0d5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
8 changes: 5 additions & 3 deletions cuckoomon.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{15C69A24-71D1-4A1A-B39B-0466181ACD7E}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -67,10 +68,11 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<TargetName>$(ProjectName)_dbg</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<TargetName>$(ProjectName)_x64</TargetName>
<TargetName>$(ProjectName)_x64_dbg</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>true</LinkIncremental>
Expand All @@ -85,7 +87,7 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS_DEBUG;_WINDOWS;_USRDLL;MONGO_HAVE_STDINT;MONGO_STATIC_BUILD;CUCKOOMON_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>CUCKOODBG;WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS_DEBUG;_WINDOWS;_USRDLL;MONGO_HAVE_STDINT;MONGO_STATIC_BUILD;CUCKOOMON_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
Expand All @@ -109,7 +111,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS_DEBUG;_WINDOWS;_USRDLL;MONGO_HAVE_STDINT;MONGO_STATIC_BUILD;CUCKOOMON_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>CUCKOODBG;WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS_DEBUG;_WINDOWS;_USRDLL;MONGO_HAVE_STDINT;MONGO_STATIC_BUILD;CUCKOOMON_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
Expand Down
2 changes: 1 addition & 1 deletion hook_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ HOOKDEF(NTSTATUS, WINAPI, NtSetInformationProcess,
__in ULONG ProcessInformationLength
) {
NTSTATUS ret = Old_NtSetInformationProcess(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength);
if (NT_SUCCESS(ret) && (ProcessInformationClass == ProcessDEPPolicy || ProcessInformationClass == ProcessBreakOnTermination) && ProcessInformationLength == 4)
if (NT_SUCCESS(ret) && (ProcessInformationClass == ProcessInfoDEPPolicy || ProcessInformationClass == ProcessBreakOnTermination) && ProcessInformationLength == 4)
LOQ_ntstatus("misc", "ii", "ProcessInformationClass", ProcessInformationClass, "Value", *(int *)ProcessInformation);
return ret;
}
Expand Down
32 changes: 25 additions & 7 deletions hook_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,22 @@ HOOKDEF(NTSTATUS, WINAPI, NtProtectVirtualMemory,
VirtualQueryEx(ProcessHandle, *BaseAddress, &meminfo, sizeof(meminfo));
set_lasterrors(&lasterrors);
}
LOQ_ntstatus("process", "pPPhhHs", "ProcessHandle", ProcessHandle, "BaseAddress", BaseAddress,
"NumberOfBytesProtected", NumberOfBytesToProtect,
"MemoryType", meminfo.Type,
"NewAccessProtection", NewAccessProtection,
"OldAccessProtection", OldAccessProtection, "StackPivoted", is_stack_pivoted() ? "yes" : "no");

if (NewAccessProtection == PAGE_EXECUTE_READWRITE && GetCurrentProcessId() == our_getprocessid(ProcessHandle) &&
(ULONG_PTR)meminfo.AllocationBase >= get_stack_bottom() && (((ULONG_PTR)meminfo.AllocationBase + meminfo.RegionSize) <= get_stack_top())) {
LOQ_ntstatus("process", "pPPhhHss", "ProcessHandle", ProcessHandle, "BaseAddress", BaseAddress,
"NumberOfBytesProtected", NumberOfBytesToProtect,
"MemoryType", meminfo.Type,
"NewAccessProtection", NewAccessProtection,
"OldAccessProtection", OldAccessProtection, "StackPivoted", is_stack_pivoted() ? "yes" : "no", "IsStack", "yes");
}
else {
LOQ_ntstatus("process", "pPPhhHs", "ProcessHandle", ProcessHandle, "BaseAddress", BaseAddress,
"NumberOfBytesProtected", NumberOfBytesToProtect,
"MemoryType", meminfo.Type,
"NewAccessProtection", NewAccessProtection,
"OldAccessProtection", OldAccessProtection, "StackPivoted", is_stack_pivoted() ? "yes" : "no");
}
return ret;
}

Expand Down Expand Up @@ -569,8 +580,15 @@ HOOKDEF(BOOL, WINAPI, VirtualProtectEx,
set_lasterrors(&lasterrors);
}

LOQ_bool("process", "ppphhHs", "ProcessHandle", hProcess, "Address", lpAddress,
"Size", dwSize, "MemType", meminfo.Type, "Protection", flNewProtect, "OldProtection", lpflOldProtect, "StackPivoted", is_stack_pivoted() ? "yes" : "no");
if (flNewProtect == PAGE_EXECUTE_READWRITE && GetCurrentProcessId() == our_getprocessid(hProcess) &&
(ULONG_PTR)meminfo.AllocationBase >= get_stack_bottom() && (((ULONG_PTR)meminfo.AllocationBase + meminfo.RegionSize) <= get_stack_top())) {
LOQ_bool("process", "ppphhHss", "ProcessHandle", hProcess, "Address", lpAddress,
"Size", dwSize, "MemType", meminfo.Type, "Protection", flNewProtect, "OldProtection", lpflOldProtect, "StackPivoted", is_stack_pivoted() ? "yes" : "no", "IsStack", "yes");
}
else {
LOQ_bool("process", "ppphhHs", "ProcessHandle", hProcess, "Address", lpAddress,
"Size", dwSize, "MemType", meminfo.Type, "Protection", flNewProtect, "OldProtection", lpflOldProtect, "StackPivoted", is_stack_pivoted() ? "yes" : "no");
}
return ret;
}

Expand Down
2 changes: 2 additions & 0 deletions hooking.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ static __inline PVOID get_peb(void)
#endif
}

// Higher virtual address, not 'top' of stack
static __inline ULONG_PTR get_stack_top(void)
{
#ifndef _WIN64
Expand All @@ -188,6 +189,7 @@ static __inline ULONG_PTR get_stack_top(void)
#endif
}

// Lower virtual address, not base of stack
static __inline ULONG_PTR get_stack_bottom(void)
{
#ifndef _WIN64
Expand Down
2 changes: 1 addition & 1 deletion ntapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ typedef enum {

typedef enum {
ProcessBreakOnTermination = 29,
ProcessDEPPolicy = 34
ProcessInfoDEPPolicy = 34
} PROCESSINFOCLASS;

typedef struct _FILE_FS_VOLUME_INFORMATION {
Expand Down

0 comments on commit 397f0d5

Please sign in to comment.