| 127 | } |
| 128 | |
| 129 | void OnImageLoadNotify(_In_opt_ PUNICODE_STRING FullImageName, _In_ HANDLE ProcessId, _In_ PIMAGE_INFO ImageInfo) { |
| 130 | if (ProcessId == nullptr) { // kernel image |
| 131 | HANDLE pid = PsGetCurrentProcessId(); |
| 132 | LogInfo("pid = %d\n", pid); |
| 133 | //system image , ignore |
| 134 | PEParser parser(ImageInfo->ImageBase); |
| 135 | auto entryPoint = parser.GetAddressEntryPoint(); |
| 136 | if (FullImageName) { |
| 137 | KdPrint(("[Library] Driver Load %wZ AddressEntryPoint 0x%p\n", FullImageName, entryPoint)); |
| 138 | BackupFile(FullImageName); |
| 139 | } |
| 140 | else |
| 141 | KdPrint(("[Library] Unknown Driver Load AddressEntryPoint: 0x%p\n",entryPoint)); |
| 142 | |
| 143 | // do something... |
| 144 | |
| 145 | if (ImageInfo->ExtendedInfoPresent) { |
| 146 | auto exinfo = CONTAINING_RECORD(ImageInfo, IMAGE_INFO_EX, ImageInfo); |
| 147 | // access FileObject |
| 148 | PFLT_FILE_NAME_INFORMATION nameInfo; |
| 149 | if (NT_SUCCESS(FltGetFileNameInformationUnsafe(exinfo->FileObject, nullptr, |
| 150 | FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT, &nameInfo))) { |
| 151 | LogInfo("FileNameInfo %wZ\n", &nameInfo->Name); |
| 152 | FltReleaseFileNameInformation(nameInfo); |
| 153 | } |
| 154 | } |
| 155 | return; |
| 156 | } |
| 157 | ASSERT(ImageInfo); |
| 158 | |
| 159 | // exe or dll image file |
| 160 | auto size = sizeof(FullItem<ImageLoadInfo>); |
| 161 | auto info = (FullItem<ImageLoadInfo>*)ExAllocatePoolWithTag(PagedPool, size, SYSMON_TAG); |
| 162 | if (info == nullptr) { |
| 163 | KdPrint((SYSMON_PREFIX "Failed to allocate memory\n")); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | ::memset(info, 0, size); |
| 168 | |
| 169 | auto& item = info->Data; |
| 170 | //KeQuerySystemTimePrecise(&item.Time); |
| 171 | item.Size = sizeof(item); |
| 172 | item.Type = ItemType::ImageLoad; |
| 173 | item.ProcessId = HandleToULong(ProcessId); |
| 174 | item.ImageSize = ImageInfo->ImageSize; |
| 175 | item.LoadAddress = ImageInfo->ImageBase; |
| 176 | |
| 177 | if (FullImageName) { |
| 178 | ::memcpy(item.ImageFileName, FullImageName->Buffer, min(FullImageName->Length, |
| 179 | MaxImageFileSize * sizeof(WCHAR))); |
| 180 | } |
| 181 | else { |
| 182 | ::wcscpy_s(item.ImageFileName, L"(unknown)"); |
| 183 | } |
| 184 | |
| 185 | PushItem(&info->Entry); |
| 186 |
nothing calls this directly
no test coverage detected