@@ -4,7 +4,7 @@ namespace Testing
4
4
{
5
5
struct HookClass
6
6
{
7
- void HookFn ( RemoteContext& context )
7
+ void HookNtOpenProcess ( RemoteContext& context )
8
8
{
9
9
//
10
10
// Get process ID
@@ -26,6 +26,18 @@ namespace Testing
26
26
}
27
27
}
28
28
29
+ void HookNtAllocateVirtualMemory ( RemoteContext& context )
30
+ {
31
+ AssertEx::AreEqual ( reinterpret_cast <HANDLE>(context.getArg ( 0 )), GetCurrentProcess () );
32
+ AssertEx::AreNotEqual ( context.getArg ( 1 ), DWORD64 ( 0 ) );
33
+ AssertEx::AreEqual ( context.getArg ( 2 ), DWORD64 ( 0 ) );
34
+ AssertEx::AreNotEqual ( context.getArg ( 3 ), DWORD64 ( 0 ) );
35
+ AssertEx::AreEqual ( context.getArg ( 4 ), DWORD64 ( MEM_RESERVE | MEM_COMMIT ) );
36
+ AssertEx::AreEqual ( context.getArg ( 5 ), DWORD64 ( PAGE_EXECUTE_READWRITE ) );
37
+
38
+ calls++;
39
+ }
40
+
29
41
Process process;
30
42
int calls = 0 ;
31
43
};
@@ -54,7 +66,7 @@ namespace Testing
54
66
AssertEx::IsTrue ( pHookFn.success () );
55
67
56
68
// Hook and try to terminate from remote process
57
- AssertEx::NtSuccess ( hooker.process .hooks ().Apply ( RemoteHook::hwbp, pHookFn->procAddress , &HookClass::HookFn , hooker ) );
69
+ AssertEx::NtSuccess ( hooker.process .hooks ().Apply ( RemoteHook::hwbp, pHookFn->procAddress , &HookClass::HookNtOpenProcess , hooker ) );
58
70
59
71
auto terminate = MakeRemoteFunction<long ( *)(DWORD)>( hooker.process , terminatePtr->procAddress );
60
72
auto result = terminate ( GetCurrentProcessId () );
@@ -65,5 +77,33 @@ namespace Testing
65
77
AssertEx::AreEqual ( ERROR_ACCESS_DENIED, result.result () );
66
78
AssertEx::AreEqual ( 1 , hooker.calls );
67
79
}
80
+
81
+ TEST_METHOD ( NtAllocateVirtualMemory )
82
+ {
83
+ HookClass hooker;
84
+
85
+ auto path = GetTestHelperHost ();
86
+ AssertEx::IsFalse ( path.empty () );
87
+
88
+ // Give process some time to initialize
89
+ AssertEx::NtSuccess ( hooker.process .CreateAndAttach ( path ) );
90
+ Sleep ( 100 );
91
+
92
+ // Get function
93
+ auto pHookFn = hooker.process .modules ().GetNtdllExport ( " NtAllocateVirtualMemory" );
94
+ AssertEx::IsTrue ( pHookFn.success () );
95
+
96
+ PVOID base = nullptr ;
97
+ SIZE_T size = 0xDEAD ;
98
+ auto NtAllocateVirtualMemory = MakeRemoteFunction<NTSTATUS ( __stdcall * )(HANDLE, PVOID*, ULONG_PTR, PSIZE_T, ULONG, ULONG)>( hooker.process , pHookFn->procAddress );
99
+
100
+ // Hook and try to call
101
+ AssertEx::NtSuccess ( hooker.process .hooks ().Apply ( RemoteHook::hwbp, pHookFn->procAddress , &HookClass::HookNtAllocateVirtualMemory, hooker ) );
102
+ auto result = NtAllocateVirtualMemory.Call ( { GetCurrentProcess (), &base, 0 , &size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE } );
103
+
104
+ hooker.process .Terminate ();
105
+
106
+ AssertEx::AreEqual ( 1 , hooker.calls );
107
+ }
68
108
};
69
109
}
0 commit comments