Skip to content

Commit 27e0054

Browse files
committed
Fixed Some Stuff
1 parent a6c1b63 commit 27e0054

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

KDMapper Dumper/Callbacks.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#include "NT.h"
55
#include "Utils.h"
66

7-
typedef NTSTATUS(*PFN_ORIGINAL_IO_CONTROL)(
7+
typedef NTSTATUS(*_IO_CONTROL)(
88
_In_ PDEVICE_OBJECT DeviceObject,
99
_In_ PIRP Irp
1010
);
1111

12-
PFN_ORIGINAL_IO_CONTROL OriginalIOControl = NULL;
12+
_IO_CONTROL OriginalIOControl = NULL;
1313

1414
NTSTATUS hk_ControlIO(
1515
_In_ PDEVICE_OBJECT DeviceObject,
@@ -38,7 +38,7 @@ NTSTATUS hk_ControlIO(
3838

3939
DBG("Dumping Memory! Source: 0x%p Destination: 0x%p Size: %d", Buffer->Source, Buffer->Destination, Buffer->Length);
4040

41-
NTSTATUS Status = DumpMemoryToDisk(L"KDMapperDumper", (PVOID)((UINT64)Buffer->Source - 0x400), Buffer->Length + 0x400);
41+
NTSTATUS Status = DumpMemoryToDisk(L"KDMapperDumper", Buffer->Source, Buffer->Length);
4242
if (NT_SUCCESS(Status) == false)
4343
{
4444
DBG("Failed to dump memory - 0x%X", Status);
@@ -59,6 +59,10 @@ NTSTATUS hk_ControlIO(
5959
//
6060
// Attempt to dump the original driver with the PE header by attaching to the source process
6161
// and then going back 0x1000 bytes from the mapped driver.
62+
//
63+
// This abuses the fact that when KDMapper sends the IOCTL request, it just adjusts
64+
// the base address of the data it sends to the driver to skip the PE header.
65+
// This means we can just go back 0x1000 bytes from the base address to get the PE header.
6266
//
6367
PVOID Pool = ExAllocatePool2(POOL_FLAG_NON_PAGED, Buffer->Length + 0x1000, POOL_TAG2);
6468
if (Pool == NULL)
@@ -184,7 +188,8 @@ VOID ImageLoadCallback(
184188
return;
185189

186190
//
187-
// Copy the first 0x1000 bytes of the image to an allocated pool buffer.
191+
// Copy the first 0x1000 bytes of the image (the PE header) to a pool buffer.
192+
// If the image size is less than 0x1000 bytes, we will just copy the entire image.
188193
//
189194
PVOID ImageBase = ImageInfo->ImageBase;
190195
SIZE_T PoolSize = min(ImageInfo->ImageSize, 0x1000);
@@ -199,7 +204,8 @@ VOID ImageLoadCallback(
199204
RtlCopyMemory(ImageBuffer, ImageBase, PoolSize);
200205

201206
//
202-
// Check the file header timestamp to see if it matches the timestamp of the KDMapper driver.
207+
// Check the file header timestamp to see if it matches the timestamp of the
208+
// vulnerable Intel LAN driver that KDMapper uses.
203209
//
204210
PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)ImageBuffer;
205211
if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
@@ -217,11 +223,15 @@ VOID ImageLoadCallback(
217223

218224
DBG("Found Intel LAN driver at: 0x%p", ImageBase);
219225

226+
//
227+
// Hook IoCreateDevice() so we can redirect all IOCTL requests to
228+
// our own handler that will dump the memory.
229+
//
220230
NTSTATUS Status = HookIATEntry(ImageBase, "ntoskrnl.exe", "IoCreateDevice", hk_IoCreateDevice);
221231
if (NT_SUCCESS(Status) == false)
222232
{
223233
DBG("Failed to hook IoCreateDevice() in Intel LAN Driver! - 0x%X", Status);
224234
}
225235

226-
return;
236+
ExFreePoolWithTag(ImageBuffer, POOL_TAG);
227237
}

KDMapper Dumper/Entry.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ extern "C" NTSTATUS DriverEntry(
2525
NTSTATUS Status;
2626
DriverObject->DriverUnload = Unload;
2727

28+
//
29+
// This driver is not intended to be manually mapped.
30+
// If you do use something like KDMapper to load this driver
31+
// you will get a bugcheck on setting DriverObject->DriverUnload
32+
// and on recieving a callback on ImageLoadCallback.
33+
//
2834
Status = PsSetLoadImageNotifyRoutine(ImageLoadCallback);
2935
if (NT_SUCCESS(Status) == false)
3036
{

KDMapper Dumper/Globals.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef union _virt_addr_t
3535
} virt_addr_t, * pvirt_addr_t;
3636

3737
#define POOL_TAG 'KDM '
38-
#define POOL_TAG2 'KDM2'
38+
#define POOL_TAG2 'KDM1'
39+
#define POOL_TAG3 'KDM2'
3940

4041
#define DBG(Message, ...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[KDMapper Dumper] " __FUNCTION__ "() - " Message "\n", __VA_ARGS__)

KDMapper Dumper/KDMapper Dumper.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@
7272
<PropertyGroup />
7373
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
7474
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
75+
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
7576
</PropertyGroup>
7677
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
7778
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
79+
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
7880
</PropertyGroup>
7981
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
8082
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>

KDMapper Dumper/Utils.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#pragma warning(disable: 4244)
77

8+
// credit: https://github.com/uefibootkit/kdmapper-dumper
89
NTSTATUS DumpMemoryToDisk(
910
_In_ const WCHAR* FileNamePrefix,
1011
_In_ PVOID BaseAddress,
@@ -16,38 +17,35 @@ NTSTATUS DumpMemoryToDisk(
1617
return STATUS_INVALID_PARAMETER;
1718
}
1819

19-
HANDLE h_file;
20-
UNICODE_STRING name;
21-
OBJECT_ATTRIBUTES attr;
22-
IO_STATUS_BLOCK status_block;
23-
LARGE_INTEGER offset{ NULL };
20+
HANDLE File;
21+
UNICODE_STRING FileNameUnicode;
22+
OBJECT_ATTRIBUTES Attributes;
23+
IO_STATUS_BLOCK StatusBlock;
24+
LARGE_INTEGER Offset{ NULL };
2425

2526
//
26-
// Get the current kernel time and use that as the filen name.
27+
// Get the current system time and use that in the file name.
2728
//
2829
LARGE_INTEGER CurrentTime;
2930
KeQuerySystemTime(&CurrentTime);
3031

31-
//
32-
// Create the file name using the current time.
33-
//
3432
WCHAR FileName[260];
3533
swprintf(FileName, L"\\??\\C:\\%s_%lld.bin", FileNamePrefix, CurrentTime.QuadPart);
3634

3735
//
3836
// Initialize the unicode string.
3937
//
40-
RtlInitUnicodeString(&name, FileName);
41-
InitializeObjectAttributes(&attr, &name,
38+
RtlInitUnicodeString(&FileNameUnicode, FileName);
39+
InitializeObjectAttributes(&Attributes, &FileNameUnicode,
4240
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
4341
NULL, NULL
4442
);
4543

4644
NTSTATUS Status = ZwCreateFile(
47-
&h_file,
45+
&File,
4846
GENERIC_WRITE,
49-
&attr,
50-
&status_block,
47+
&Attributes,
48+
&StatusBlock,
5149
NULL,
5250
FILE_ATTRIBUTE_NORMAL,
5351
NULL,
@@ -63,21 +61,21 @@ NTSTATUS DumpMemoryToDisk(
6361
}
6462

6563
Status = ZwWriteFile(
66-
h_file,
64+
File,
6765
NULL,
6866
NULL,
6967
NULL,
70-
&status_block,
68+
&StatusBlock,
7169
BaseAddress,
7270
Size,
73-
&offset,
71+
&Offset,
7472
NULL
7573
);
7674
if (NT_SUCCESS(Status) == false)
7775
{
7876
DBG("Failed to write to file - 0x%X", Status);
7977
}
8078

81-
ZwClose(h_file);
79+
ZwClose(File);
8280
return Status;
8381
}

0 commit comments

Comments
 (0)