1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| #include "pch.h" #include "SysMon.h" #include "SysMonCommon.h" #include "AutoLock.h" DRIVER_UNLOAD SysMonUnload; DRIVER_DISPATCH SysMonCreateClose, SysMonRead; void OnProcessNotify(_Inout_ PEPROCESS Process, _In_ HANDLE ProcessId, _Inout_opt_ PPS_CREATE_NOTIFY_INFO CreateInfo); void OnThreadNotify(_In_ HANDLE ProcessId, _In_ HANDLE ThreadId, _In_ BOOLEAN Create); void OnImageLoadNotify(_In_opt_ PUNICODE_STRING FullImageName, _In_ HANDLE ProcessId, _In_ PIMAGE_INFO ImageInfo); void PushItem(LIST_ENTRY* entry); Globals g_Globals; extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING) { auto status = STATUS_SUCCESS; InitializeListHead(&g_Globals.ItemsHead); g_Globals.Mutex.Init(); PDEVICE_OBJECT DeviceObject = nullptr; UNICODE_STRING symLink = RTL_CONSTANT_STRING(L"\\??\\sysmon"); bool symLinkCreated = false; bool processCallbacks = false, threadCallbacks = false; do { UNICODE_STRING devName = RTL_CONSTANT_STRING(L"\\Device\\sysmon"); status = IoCreateDevice(DriverObject, 0, &devName, FILE_DEVICE_UNKNOWN, 0, TRUE, &DeviceObject); if (!NT_SUCCESS(status)) { KdPrint((DRIVER_PREFIX "failed to create device (0x%08X)\n", status)); break; } DeviceObject->Flags |= DO_DIRECT_IO; status = IoCreateSymbolicLink(&symLink, &devName); if (!NT_SUCCESS(status)) { KdPrint((DRIVER_PREFIX "failed to create sym link (0x%08X)\n", status)); break; } symLinkCreated = true; status = PsSetCreateProcessNotifyRoutineEx(OnProcessNotify, FALSE); if (!NT_SUCCESS(status)) { KdPrint((DRIVER_PREFIX "failed to register process callback (0x%08X)\n", status)); break; } processCallbacks = true; status = PsSetCreateThreadNotifyRoutine(OnThreadNotify); if (!NT_SUCCESS(status)) { KdPrint((DRIVER_PREFIX "failed to set thread callback (status=%08X)\n", status)); break; } threadCallbacks = true; status = PsSetLoadImageNotifyRoutine(OnImageLoadNotify); if (!NT_SUCCESS(status)) { KdPrint((DRIVER_PREFIX "failed to set image load callback (status=%08X)\n", status)); break; } } while (false); if (!NT_SUCCESS(status)) { if (threadCallbacks) PsRemoveCreateThreadNotifyRoutine(OnThreadNotify); if (processCallbacks) PsSetCreateProcessNotifyRoutineEx(OnProcessNotify, TRUE); if (symLinkCreated) IoDeleteSymbolicLink(&symLink); if (DeviceObject) IoDeleteDevice(DeviceObject); } DriverObject->DriverUnload = SysMonUnload; DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverObject->MajorFunction[IRP_MJ_CLOSE] = SysMonCreateClose; DriverObject->MajorFunction[IRP_MJ_READ] = SysMonRead; return status; } NTSTATUS SysMonCreateClose(PDEVICE_OBJECT, PIRP Irp) { Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, 0); return STATUS_SUCCESS; } NTSTATUS SysMonRead(PDEVICE_OBJECT, PIRP Irp) { auto stack = IoGetCurrentIrpStackLocation(Irp); auto len = stack->Parameters.Read.Length; auto status = STATUS_SUCCESS; auto count = 0; NT_ASSERT(Irp->MdlAddress); auto buffer = (UCHAR*)MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority); if (!buffer) status = STATUS_INSUFFICIENT_RESOURCES; else { AutoLock lock(g_Globals.Mutex); while (true) { if (IsListEmpty(&g_Globals.ItemsHead)) break; auto entry = RemoveHeadList(&g_Globals.ItemsHead); auto info = CONTAINING_RECORD(entry, FullItem<ItemHeader>, Entry); auto size = info->Data.Size; if (len < size) { InsertHeadList(&g_Globals.ItemsHead, entry); break; } g_Globals.ItemCount--; ::memcpy(buffer, &info->Data, size); len -= size; buffer += size; count += size; ExFreePool(info); } } Irp->IoStatus.Status = status; Irp->IoStatus.Information = count; IoCompleteRequest(Irp, 0); return status; } void SysMonUnload(PDRIVER_OBJECT DriverObject) { PsRemoveLoadImageNotifyRoutine(OnImageLoadNotify); PsRemoveCreateThreadNotifyRoutine(OnThreadNotify); PsSetCreateProcessNotifyRoutineEx(OnProcessNotify, TRUE); UNICODE_STRING symLink = RTL_CONSTANT_STRING(L"\\??\\sysmon"); IoDeleteSymbolicLink(&symLink); IoDeleteDevice(DriverObject->DeviceObject); while (!IsListEmpty(&g_Globals.ItemsHead)) { auto entry = RemoveHeadList(&g_Globals.ItemsHead); ExFreePool(CONTAINING_RECORD(entry, FullItem<ItemHeader>, Entry)); } } void OnProcessNotify(PEPROCESS Process, HANDLE ProcessId, PPS_CREATE_NOTIFY_INFO CreateInfo) { UNREFERENCED_PARAMETER(Process); if (CreateInfo) { USHORT allocSize = sizeof(FullItem<ProcessCreateInfo>); USHORT commandLineSize = 0; if (CreateInfo->CommandLine) { commandLineSize = CreateInfo->CommandLine->Length; allocSize += commandLineSize; } auto info = (FullItem<ProcessCreateInfo>*)ExAllocatePoolWithTag(PagedPool, allocSize, DRIVER_TAG); if (info == nullptr) { KdPrint((DRIVER_PREFIX "failed allocation\n")); return; } auto& item = info->Data; KeQuerySystemTimePrecise(&item.Time); item.Type = ItemType::ProcessCreate; item.Size = sizeof(ProcessCreateInfo) + commandLineSize; item.ProcessId = HandleToULong(ProcessId); item.ParentProcessId = HandleToULong(CreateInfo->ParentProcessId); if (commandLineSize > 0) { ::memcpy((UCHAR*)&item + sizeof(item), CreateInfo->CommandLine->Buffer, commandLineSize); item.CommandLineLength = commandLineSize / sizeof(WCHAR); item.CommandLineOffset = sizeof(item); } else item.CommandLineLength = 0; PushItem(&info->Entry); } else { auto info = (FullItem<ProcessExitInfo>*)ExAllocatePoolWithTag(PagedPool, sizeof(FullItem<ProcessExitInfo>), DRIVER_TAG); if (info == nullptr) { KdPrint((DRIVER_PREFIX "failed allocation\n")); return; } auto& item = info->Data; KeQuerySystemTimePrecise(&item.Time); item.Type = ItemType::ProcessExit; item.ProcessId = HandleToULong(ProcessId); item.Size = sizeof(ProcessExitInfo); PushItem(&info->Entry); } } void OnThreadNotify(HANDLE ProcessId, HANDLE ThreadId, BOOLEAN Create) { auto size = sizeof(FullItem<ThreadCreateExitInfo>); auto info = (FullItem<ThreadCreateExitInfo>*)ExAllocatePoolWithTag(PagedPool, size, DRIVER_TAG); if (info == nullptr) { KdPrint((DRIVER_PREFIX "Failed to allocate memory\n")); return; } auto& item = info->Data; KeQuerySystemTimePrecise(&item.Time); item.Size = sizeof(item); item.Type = Create ? ItemType::ThreadCreate : ItemType::ThreadExit; item.ProcessId = HandleToULong(ProcessId); item.ThreadId = HandleToULong(ThreadId); PushItem(&info->Entry); } void OnImageLoadNotify(PUNICODE_STRING FullImageName, HANDLE ProcessId, PIMAGE_INFO ImageInfo) { if (ProcessId == nullptr) { return; } auto size = sizeof(FullItem<ImageLoadInfo>); auto info = (FullItem<ImageLoadInfo>*)ExAllocatePoolWithTag(PagedPool, size, DRIVER_TAG); if (info == nullptr) { KdPrint((DRIVER_PREFIX "Failed to allocate memory\n")); return; } ::memset(info, 0, size); auto& item = info->Data; KeQuerySystemTimePrecise(&item.Time); item.Size = sizeof(item); item.Type = ItemType::ImageLoad; item.ProcessId = HandleToULong(ProcessId); item.ImageSize = ImageInfo->ImageSize; item.LoadAddress = ImageInfo->ImageBase; if (FullImageName) ::memcpy(item.ImageFileName, FullImageName->Buffer, min(FullImageName->Length, MaxImageFileSize * sizeof(WCHAR))); else ::wcscpy_s(item.ImageFileName, L"(unknown)"); PushItem(&info->Entry); } void PushItem(LIST_ENTRY * entry) { AutoLock<FastMutex> lock(g_Globals.Mutex); if (g_Globals.ItemCount > 1024) { auto head = RemoveHeadList(&g_Globals.ItemsHead); g_Globals.ItemCount--; auto item = CONTAINING_RECORD(head, FullItem<ItemHeader>, Entry); ExFreePool(item); } InsertTailList(&g_Globals.ItemsHead, entry); g_Globals.ItemCount++; }
|