Skip to content

Commit f707fac

Browse files
committed
Updates in comments
1 parent 86dc487 commit f707fac

File tree

15 files changed

+128
-106
lines changed

15 files changed

+128
-106
lines changed

RemoteWriteMonitor/RemoteWriteMonitor/Arch/AMD64/amd64.asm

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
; Copyright (c) 2015, tandasat. All rights reserved.
2+
; Use of this source code is governed by a MIT-style license that can be
3+
; found in the LICENSE file.
4+
5+
;
6+
; This module implements stub functions implementing overwritten part of the
7+
; hooked function and providing accesses to the those original functionalities.
18
;
2-
; This module implements the lowest part of hook handlers
9+
; Those functions are taken from ntoskrnl.exe and used only on x64 build.
310
;
411

512
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -24,7 +31,7 @@ jmp_address:
2431
ENDM
2532

2633

27-
34+
; NtMapViewOfSection for Windows 8.1 and 7 (both are identical)
2835
AsmNtMapViewOfSection_Win81_7 PROC
2936
mov qword ptr [rsp+10h], rbx
3037
mov qword ptr [rsp+18h], rsi
@@ -37,7 +44,7 @@ AsmNtMapViewOfSection_Win81_7End PROC
3744
AsmNtMapViewOfSection_Win81_7End ENDP
3845

3946

40-
; For Win 8.1
47+
; NtWriteVirtualMemory for Win 8.1
4148
AsmNtWriteVirtualMemory_Win81 PROC
4249
sub rsp, 38h
4350
mov rax, [rsp+60h]
@@ -50,7 +57,7 @@ AsmNtWriteVirtualMemory_Win81End PROC
5057
AsmNtWriteVirtualMemory_Win81End ENDP
5158

5259

53-
; For Win 7
60+
; NtWriteVirtualMemory for Win 7
5461
AsmNtWriteVirtualMemory_Win7 PROC
5562
mov rax, rsp
5663
mov qword ptr [rax+8h], rbx
@@ -64,5 +71,4 @@ AsmNtWriteVirtualMemory_Win7End PROC
6471
AsmNtWriteVirtualMemory_Win7End ENDP
6572

6673

67-
6874
END

RemoteWriteMonitor/RemoteWriteMonitor/Arch/x86/asm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// found in the LICENSE file.
44

55
//
6-
//
6+
// This module implements empty functions for Asm functions to allow us to build
7+
// the code on x86. Those Asm functions are not used on x86.
78
//
89
#include "stdafx.h"
910
#include "../../asm.h"

RemoteWriteMonitor/RemoteWriteMonitor/RemoteWriteMonitor.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include "ssdt.h"
1515
#include "util.h"
1616

17-
namespace stdexp = std::experimental;
18-
1917
////////////////////////////////////////////////////////////////////////////////
2018
//
2119
// macro utilities
@@ -98,8 +96,8 @@ EXTERN_C static NTSTATUS NTAPI RWMonpNtMapViewOfSection_Hook(
9896
// variables
9997
//
10098

101-
static HookInfo g_RWMonpNtMapViewOfSectionInfo = {};
102-
static HookInfo g_RWMonpNtWriteVirtualMemoryInfo = {};
99+
static InlineHookInfo g_RWMonpNtMapViewOfSectionInfo = {};
100+
static InlineHookInfo g_RWMonpNtWriteVirtualMemoryInfo = {};
103101

104102
static NtMapViewOfSectionType g_RWMonpNtMapViewOfSectionOriginal = nullptr;
105103
static NtWriteVirtualMemoryType g_RWMonpNtWriteVirtualMemoryOriginal = nullptr;
@@ -120,8 +118,8 @@ EXTERN_C NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject,
120118
_In_ PUNICODE_STRING RegistryPath) {
121119
PAGED_CODE();
122120
UNREFERENCED_PARAMETER(RegistryPath);
123-
auto status = STATUS_UNSUCCESSFUL;
124121

122+
auto status = STATUS_UNSUCCESSFUL;
125123
DriverObject->DriverUnload = RWMonpDriverUnload;
126124
DBG_BREAK();
127125

@@ -164,6 +162,7 @@ EXTERN_C NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject,
164162
auto scopedCheckTermination =
165163
stdexp::make_scope_exit([] { CheckTermination(); });
166164

165+
// Install hooks
167166
status = RWMonpInstallHooks();
168167
if (!NT_SUCCESS(status)) {
169168
return status;
@@ -176,6 +175,7 @@ EXTERN_C NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject,
176175
return status;
177176
}
178177

178+
// Perform version check and fill out global variable based on the version
179179
ALLOC_TEXT(INIT, RWMonpInitVersionDependentValues)
180180
EXTERN_C static NTSTATUS RWMonpInitVersionDependentValues() {
181181
PAGED_CODE();
@@ -335,6 +335,7 @@ EXTERN_C static NTSTATUS RWMonpSleep(_In_ LONG Millisecond) {
335335
return KeDelayExecutionThread(KernelMode, FALSE, &interval);
336336
}
337337

338+
// Install hooks
338339
ALLOC_TEXT(INIT, RWMonpInstallHooks)
339340
EXTERN_C static NTSTATUS RWMonpInstallHooks() {
340341
PAGED_CODE();
@@ -361,6 +362,7 @@ EXTERN_C static NTSTATUS RWMonpInstallHooks() {
361362
return status;
362363
}
363364

365+
// Uninstall hooks
364366
ALLOC_TEXT(PAGED, RWMonpUninstallHooks)
365367
EXTERN_C static NTSTATUS RWMonpUninstallHooks() {
366368
PAGED_CODE();
@@ -420,4 +422,4 @@ RWMonpNtWriteVirtualMemory_Hook(_In_ HANDLE ProcessHandle,
420422
CheckData(ProcessHandle, BaseAddress, Buffer, BytesToWrite);
421423
}
422424
return result;
423-
}
425+
}

RemoteWriteMonitor/RemoteWriteMonitor/RemoteWriteMonitor.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
</ItemGroup>
7575
<ItemGroup>
7676
<MASM Include="Arch\AMD64\amd64.asm">
77-
<Filter>Source Files</Filter>
77+
<Filter>Source Files\Arch\AMD64</Filter>
7878
</MASM>
7979
</ItemGroup>
8080
</Project>

RemoteWriteMonitor/RemoteWriteMonitor/check.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
// found in the LICENSE file.
44

55
//
6-
// This module implements an entry point of the driver and initializes other
7-
// components in this module.
6+
// This module implements functions for checking if data is written
7+
// by a remote process and saving it if so.
88
//
99
#include "stdafx.h"
1010
#include "check.h"
1111
#include "log.h"
1212

13-
namespace stdexp = std::experimental;
14-
1513
////////////////////////////////////////////////////////////////////////////////
1614
//
1715
// macro utilities
@@ -28,6 +26,7 @@ static const auto CHECKP_WHITELIST_ARRAY_SIZE = 1000;
2826
//
2927
// types
3028
//
29+
3130
struct SYSTEM_PROCESS_INFORMATION {
3231
ULONG NextEntryOffset;
3332
ULONG NumberOfThreads;
@@ -46,6 +45,7 @@ struct SYSTEM_PROCESS_INFORMATION {
4645
enum SYSTEM_INFORMATION_CLASS {
4746
SystemProcessInformation = 5,
4847
};
48+
4949
////////////////////////////////////////////////////////////////////////////////
5050
//
5151
// prototypes
@@ -95,7 +95,7 @@ EXTERN_C static NTSTATUS CheckpWriteFile(_In_ const wchar_t *OutPathW,
9595
// variables
9696
//
9797

98-
static wchar_t g_CheckpLogDirecotry[MAX_PATH];
98+
static wchar_t g_CheckpLogDirecotry[MAX_PATH] = {};
9999
static HANDLE g_CheckpWhiteListedProcessIDs[CHECKP_WHITELIST_ARRAY_SIZE] = {};
100100
static BCRYPT_ALG_HANDLE g_CheckpSha1AlgorithmHandle = nullptr;
101101

@@ -104,6 +104,7 @@ static BCRYPT_ALG_HANDLE g_CheckpSha1AlgorithmHandle = nullptr;
104104
// implementations
105105
//
106106

107+
// Initialize the Check subsystem
107108
ALLOC_TEXT(INIT, CheckInitialization)
108109
EXTERN_C NTSTATUS CheckInitialization(_In_ const wchar_t *LogDirectry) {
109110
PAGED_CODE();
@@ -133,9 +134,11 @@ EXTERN_C NTSTATUS CheckInitialization(_In_ const wchar_t *LogDirectry) {
133134
return status;
134135
}
135136

137+
// Terminates the check subsystem
136138
ALLOC_TEXT(PAGED, CheckTermination)
137139
EXTERN_C void CheckTermination() {
138140
PAGED_CODE();
141+
139142
BCryptCloseAlgorithmProvider(g_CheckpSha1AlgorithmHandle, 0);
140143
}
141144

@@ -225,20 +228,19 @@ EXTERN_C bool CheckData(_In_ HANDLE ProcessHandle, _In_ void *RemoteAddress,
225228
[targetProcess] { ObDereferenceObject(targetProcess); });
226229

227230
// Allocate a memory to copy written data
228-
auto data = stdexp::make_unique_resource(
229-
ExAllocatePoolWithTag(PagedPool, DataSize, RWMON_POOL_TAG_NAME),
230-
[](void *p) { ExFreePoolWithTag(p, RWMON_POOL_TAG_NAME); });
231+
auto data = ExAllocatePoolWithTag(PagedPool, DataSize, RWMON_POOL_TAG_NAME);
231232
if (!data) {
232233
return false;
233234
}
235+
auto scopedData = stdexp::make_scope_exit(
236+
[data]() { ExFreePoolWithTag(data, RWMON_POOL_TAG_NAME); });
234237

235238
// Copy the written data
236239
auto status = STATUS_SUCCESS;
237240
if (Contents) {
238-
status =
239-
CheckpCopyDataFromUserSpace(data.get(), Contents, DataSize, nullptr);
241+
status = CheckpCopyDataFromUserSpace(data, Contents, DataSize, nullptr);
240242
} else {
241-
status = CheckpCopyDataFromUserSpace(data.get(), RemoteAddress, DataSize,
243+
status = CheckpCopyDataFromUserSpace(data, RemoteAddress, DataSize,
242244
targetProcess);
243245
}
244246
if (!NT_SUCCESS(status)) {
@@ -248,7 +250,7 @@ EXTERN_C bool CheckData(_In_ HANDLE ProcessHandle, _In_ void *RemoteAddress,
248250

249251
// Calculate SHA1 of the written data
250252
UCHAR sha1Hash[20] = {};
251-
if (!CheckpGetSha1(sha1Hash, data.get(), DataSize)) {
253+
if (!CheckpGetSha1(sha1Hash, data, DataSize)) {
252254
return false;
253255
}
254256
wchar_t sha1HashW[41] = {};
@@ -265,8 +267,8 @@ EXTERN_C bool CheckData(_In_ HANDLE ProcessHandle, _In_ void *RemoteAddress,
265267
LOG_ERROR_SAFE("RtlStringCchPrintfW failed (%08x)", status);
266268
return false;
267269
}
268-
status = CheckpWriteFile(outPathW, data.get(), DataSize, GENERIC_WRITE,
269-
FILE_CREATE);
270+
status =
271+
CheckpWriteFile(outPathW, data, DataSize, GENERIC_WRITE, FILE_CREATE);
270272
if (!NT_SUCCESS(status) && status != STATUS_OBJECT_NAME_COLLISION) {
271273
LOG_ERROR_SAFE("WriteFile failed (%08x)", status);
272274
return false;

RemoteWriteMonitor/RemoteWriteMonitor/check.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// found in the LICENSE file.
44

55
//
6-
//
6+
// This module declares interfaces to functions for checking if data is written
7+
// by a remote process and saving it if so.
78
//
89
#pragma once
910

RemoteWriteMonitor/RemoteWriteMonitor/inline.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
// found in the LICENSE file.
44

55
//
6-
// This module implements an entry point of the driver and initializes other
7-
// components in this module.
6+
// This module implements inline hook related functions.
87
//
98
#include "stdafx.h"
109
#include "inline.h"
1110
#include "log.h"
1211
#include "util.h"
1312

14-
namespace stdexp = std::experimental;
15-
1613
////////////////////////////////////////////////////////////////////////////////
1714
//
1815
// macro utilities
@@ -34,9 +31,6 @@ struct TrampolineCode {
3431
UCHAR jmp[6];
3532
FARPROC FunctionAddress;
3633
};
37-
static const auto DISPGP_MININUM_EPILOGUE_LENGTH = sizeof(TrampolineCode);
38-
static_assert(sizeof(TrampolineCode) == DISPGP_MININUM_EPILOGUE_LENGTH,
39-
"Size check");
4034
#include <poppack.h>
4135

4236
////////////////////////////////////////////////////////////////////////////////
@@ -62,15 +56,13 @@ EXTERN_C static TrampolineCode InlinepMakeTrampolineCode(
6256
// implementations
6357
//
6458

65-
// Fill out HookInfo in order to hook the begging of the function. This is not
66-
// designed to execute original code like what DispgpSetEpilogueHookInfo() does.
59+
// Fill out a InlineHookInfo struct with given parameters
6760
ALLOC_TEXT(INIT, InlineInitHookInfo)
6861
EXTERN_C NTSTATUS
6962
InlineInitHookInfo(_In_ UCHAR *HookAddress, _In_ FARPROC HookHandler,
7063
_In_ FARPROC AsmHandler, _In_ FARPROC AsmHandlerEnd,
71-
_Out_ HookInfo *Info) {
64+
_Out_ InlineHookInfo *Info) {
7265
PAGED_CODE();
73-
7466
NT_ASSERT(HookHandler);
7567
NT_ASSERT(AsmHandler);
7668
NT_ASSERT(AsmHandlerEnd);
@@ -82,7 +74,7 @@ InlineInitHookInfo(_In_ UCHAR *HookAddress, _In_ FARPROC HookHandler,
8274

8375
Info->HookHandler = HookHandler;
8476
Info->HookAddress = HookAddress;
85-
Info->OriginalCodeSize = DISPGP_MININUM_EPILOGUE_LENGTH;
77+
Info->OriginalCodeSize = sizeof(TrampolineCode);
8678
memcpy(Info->OriginalCode, Info->HookAddress, Info->OriginalCodeSize);
8779

8880
auto status = InlinepFixupAsmCode(HookAddress, AsmHandler, AsmHandlerEnd);
@@ -101,10 +93,11 @@ ALLOC_TEXT(PAGED, InlinepMakeTrampolineCode)
10193
EXTERN_C static TrampolineCode InlinepMakeTrampolineCode(
10294
_In_ UCHAR *HookAddress, _In_ FARPROC HookHandler) {
10395
PAGED_CODE();
96+
UNREFERENCED_PARAMETER(HookAddress);
97+
10498
// jmp qword ptr [nextline]
10599
// nextline:
106100
// dq HookHandler
107-
UNREFERENCED_PARAMETER(HookAddress);
108101
return {
109102
{
110103
0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
@@ -114,18 +107,18 @@ EXTERN_C static TrampolineCode InlinepMakeTrampolineCode(
114107
}
115108

116109
// Replaces placeholder (0xffffffffffffffff) in AsmHandler with a given
117-
// ReturnAddress. AsmHandler does not has to be writable. Race condition between
118-
// multiple processors should be taken care of by a programmer it exists; this
119-
// function does not care about it.
110+
// OriginalRoutine. AsmHandler does not has to be writable. Race condition
111+
// between multiple processors should be taken care of by a programmer.
120112
ALLOC_TEXT(PAGED, InlinepFixupAsmCode)
121113
EXTERN_C
122114
NTSTATUS static InlinepFixupAsmCode(_In_ UCHAR *OriginalRoutine,
123115
_In_ FARPROC AsmHandler,
124116
_In_ FARPROC AsmHandlerEnd) {
125117
PAGED_CODE();
126118
ASSERT(AsmHandlerEnd > AsmHandler);
127-
SIZE_T asmHandlerSize = reinterpret_cast<ULONG_PTR>(AsmHandlerEnd) -
128-
reinterpret_cast<ULONG_PTR>(AsmHandler);
119+
120+
const auto asmHandlerSize = reinterpret_cast<ULONG_PTR>(AsmHandlerEnd) -
121+
reinterpret_cast<ULONG_PTR>(AsmHandler);
129122

130123
ULONG64 pattern = 0xffffffffffffffff;
131124
auto addressOfMarker = UtilMemMem(reinterpret_cast<void *>(AsmHandler),
@@ -137,20 +130,29 @@ NTSTATUS static InlinepFixupAsmCode(_In_ UCHAR *OriginalRoutine,
137130
sizeof(destinationAddress));
138131
}
139132

140-
// Install a inline hook (modify code) using HookInfo.
141-
ALLOC_TEXT(PAGED, InlineInstallHook)
142-
EXTERN_C NTSTATUS InlineInstallHook(_In_ const HookInfo &Info) {
143-
PAGED_CODE();
133+
// Install a inline hook (modify code) based on InlineHookInfo. It is not
134+
// multi-processor safe.
135+
EXTERN_C NTSTATUS InlineInstallHook(_In_ const InlineHookInfo &Info) {
144136
LOG_DEBUG("%p => %p", Info.HookAddress, Info.HookHandler);
145137
auto newCode = InlinepMakeTrampolineCode(Info.HookAddress, Info.HookHandler);
138+
139+
KIRQL oldIrql = 0;
140+
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
141+
const auto scopedIrql =
142+
stdexp::make_scope_exit([oldIrql]() { KeLowerIrql(oldIrql); });
143+
146144
auto status = UtilForceMemCpy(Info.HookAddress, newCode.jmp, sizeof(newCode));
147145
UtilInvalidateInstructionCache(Info.HookAddress, sizeof(newCode));
148146
return status;
149147
}
150148

151-
ALLOC_TEXT(PAGED, InlineUninstallHook)
152-
EXTERN_C NTSTATUS InlineUninstallHook(_In_ const HookInfo &Info) {
153-
PAGED_CODE();
149+
// Uninstall a inline hook (modify code) based on InlineHookInfo.
150+
EXTERN_C NTSTATUS InlineUninstallHook(_In_ const InlineHookInfo &Info) {
151+
KIRQL oldIrql = 0;
152+
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
153+
const auto scopedIrql =
154+
stdexp::make_scope_exit([oldIrql]() { KeLowerIrql(oldIrql); });
155+
154156
auto status = UtilForceMemCpy(Info.HookAddress, Info.OriginalCode,
155157
Info.OriginalCodeSize);
156158
UtilInvalidateInstructionCache(Info.HookAddress, Info.OriginalCodeSize);

0 commit comments

Comments
 (0)