Skip to content

Commit afded4b

Browse files
committed
Improved heap search for bigger NT heaps
1 parent 1646fa2 commit afded4b

File tree

9 files changed

+727
-54
lines changed

9 files changed

+727
-54
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ endif()
2828
## Ease for includes
2929
add_library(common_includes INTERFACE)
3030
add_library(interrogate_include INTERFACE)
31+
target_include_directories(common_includes INTERFACE ${PROJECT_SOURCE_DIR}/include/ntdll)
3132
target_include_directories(common_includes INTERFACE ${PROJECT_SOURCE_DIR}/include/keyreaper)
3233
target_include_directories(interrogate_include INTERFACE ${PROJECT_SOURCE_DIR}/include/interrogate)
3334

include/keyreaper/key_scanner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ScannerFacade {
3838

3939
// Query
4040
bool IsProcessAlive() const;
41-
std::unordered_set<std::shared_ptr<Key>, Key::KeyHashFunction, Key::KeyHashFunction> DoScan();
41+
std::unordered_set<std::shared_ptr<Key>, Key::KeyHashFunction, Key::KeyHashFunction> DoScan(bool extended_search_enabled);
4242
std::unordered_set<std::shared_ptr<Key>, Key::KeyHashFunction, Key::KeyHashFunction> GetKeys();
4343
error_handling::ProgramResult ExportKeysToJSON(std::string output_json);
4444
error_handling::ProgramResult ExportKeysToBinary();

include/keyreaper/process_capturer.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <vector>
66
#include <tlhelp32.h>
77

8+
#include "ntdll.h"
9+
#include "winntheap.h"
810
#include "injection/custom_ipc.h"
911
#include "program_result.h"
1012

@@ -14,9 +16,15 @@ typedef _Return_type_success_(return >= 0) LONG NTSTATUS;
1416

1517
typedef NTSTATUS(NTAPI *pNtSuspendProcess)(
1618
HANDLE ProcessHandle
17-
); // Undocumented NTDLL function
19+
);
1820
}
1921

22+
typedef NTSTATUS (NTAPI* pNtQueryVirtualMemory)(
23+
HANDLE, PVOID, int, PVOID, SIZE_T, PSIZE_T);
24+
25+
typedef NTSTATUS (NTAPI* pNtQueryInformationProcess)(
26+
HANDLE, int, PVOID, ULONG, PULONG);
27+
2028
namespace process_manipulation {
2129

2230
// from x64dbg
@@ -150,7 +158,7 @@ class ProcessCapturer {
150158
error_handling::ProgramResult GetMemoryChunk(LPCVOID start, SIZE_T size, BYTE* buffer, SIZE_T* bytes_read);
151159
void WriteBufferToFile(unsigned char* buffer, SIZE_T size, std::string file_name);
152160

153-
error_handling::ProgramResult EnumerateHeaps(std::vector<HeapInformation>* heaps);
161+
error_handling::ProgramResult EnumerateHeaps(std::vector<HeapInformation>& heaps, bool extended_search = false);
154162

155163
/**
156164
* From the information about the heap, copies the whole heap to a buffer.
@@ -226,6 +234,13 @@ class ProcessCapturer {
226234
*/
227235
error_handling::ProgramResult GetKeyBlobFromRemote(HCRYPTKEY key_handle, DWORD blob_type, std::vector<BYTE>& key_blob);
228236

237+
/**
238+
* Returns a copy of the Process Environment Block of the target process
239+
* @param peb A pointer to a PEB structure
240+
*/
241+
error_handling::ProgramResult CopyPEB(void* peb);
242+
void* GetPEBLocation();
243+
229244
private:
230245
/**
231246
* Function for initializing dynamically imported functions, such as `NtSuspendProcess`.
@@ -240,6 +255,17 @@ class ProcessCapturer {
240255
error_handling::ProgramResult StartControllerServerOnProcess();
241256
error_handling::ProgramResult StopControllerServerOnProcess(bool terminate = false);
242257

258+
/**
259+
* Looks for heaps by enumerating the memory regions of the process and matching
260+
* for the HEAP_ENTRY structure
261+
* @param heaps (OUT) vector where the heap region base and size will be placed
262+
*/
263+
error_handling::ProgramResult ExtendedHeapSearch(std::vector<HeapInformation>& heaps);
264+
error_handling::ProgramResult SimpleHeapSearch(std::vector<HeapInformation>& heaps);
265+
266+
HANDLE proc_handle_;
267+
pNtQueryInformationProcess fnNtQueryInformationProcess_;
268+
pNtQueryVirtualMemory fnNtQueryVirtualMemory_;
243269
static nt_suspend::pNtSuspendProcess fNtPauseProcess;
244270
custom_ipc::CustomClient injection_client_;
245271
DWORD pid_;

0 commit comments

Comments
 (0)