In more complex cases,these could be separate functions, where in the Create case the driver can (for instance) check to see who the caller is and only let approved callers succeed with opening a device. */
| 285 | and only let approved callers succeed with opening a device. |
| 286 | */ |
| 287 | _Use_decl_annotations_ |
| 288 | NTSTATUS AntiRootkitCreateClose(PDEVICE_OBJECT DeviceObject, PIRP Irp) { |
| 289 | UNREFERENCED_PARAMETER(DeviceObject); |
| 290 | |
| 291 | Log(LogLevel::Verbose, "Create/Close called\n"); |
| 292 | |
| 293 | auto status = STATUS_SUCCESS; |
| 294 | // ��ȡ����ĵ�ǰջ�ռ� |
| 295 | auto stack = IoGetCurrentIrpStackLocation(Irp); |
| 296 | // �ж�����˭���Ƿ���֮ǰ���ɵĿ����豸 |
| 297 | if (DeviceObject != g_DeviceObject) { |
| 298 | CompleteIrp(Irp); |
| 299 | return status; |
| 300 | } |
| 301 | |
| 302 | TraceLoggingWrite(g_Provider, "Create/Close", |
| 303 | TraceLoggingLevel(TRACE_LEVEL_INFORMATION), |
| 304 | TraceLoggingValue(stack->MajorFunction == IRP_MJ_CREATE ? "Create" : "Close", "Operation")); |
| 305 | |
| 306 | if (stack->MajorFunction == IRP_MJ_CREATE) { |
| 307 | // verify it's WinArk client (very simple at the moment) |
| 308 | HANDLE hProcess; |
| 309 | status = ObOpenObjectByPointer(PsGetCurrentProcess(), OBJ_KERNEL_HANDLE, nullptr, 0, *PsProcessType, KernelMode, &hProcess); |
| 310 | if (NT_SUCCESS(status)) { |
| 311 | UCHAR buffer[280] = { 0 }; |
| 312 | status = ZwQueryInformationProcess(hProcess, ProcessImageFileName, buffer, sizeof(buffer) - sizeof(WCHAR), nullptr); |
| 313 | if (NT_SUCCESS(status)) { |
| 314 | auto path = (UNICODE_STRING*)buffer; |
| 315 | auto bs = wcsrchr(path->Buffer, L'\\'); |
| 316 | NT_ASSERT(bs); |
| 317 | if (bs == nullptr || 0 != _wcsicmp(bs, L"\\WinArk.exe")) |
| 318 | status = STATUS_ACCESS_DENIED; |
| 319 | } |
| 320 | ZwClose(hProcess); |
| 321 | } |
| 322 | } |
| 323 | Irp->IoStatus.Status = status; |
| 324 | Irp->IoStatus.Information = 0; // a polymorphic member, meaning different things in different request. |
| 325 | IoCompleteRequest(Irp, IO_NO_INCREMENT); |
| 326 | return status; |
| 327 | } |
| 328 | |
| 329 | // �ڴ�����������ʱ���ں��е��ڴ������ģ�Ҫע���ֹ��������� |
| 330 | // 1.���ƻ������ij��� |
nothing calls this directly
no test coverage detected