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
| NTSTATUS KernelCopyFile(PIRP pIrp) {
ParamKernelCopyFile Param = { 0 }; WCHAR dstFile[0x1000] = { 0 }; WCHAR srcFile[0x1000] = { 0 }; RtlZeroMemory(dstFile, 0x1000); RtlZeroMemory(srcFile, 0x1000);
RtlCopyMemory((PVOID)&Param, pIrp->AssociatedIrp.SystemBuffer, sizeof(ParamKernelCopyFile)); RtlCopyMemory((PVOID)dstFile, Param.dstFile, Param.dstFileLen); RtlCopyMemory((PVOID)srcFile, Param.srcFile, Param.srcFileLen);
NTSTATUS status = STATUS_SUCCESS; UNICODE_STRING DstFilePath = { 0 }; UNICODE_STRING SrcFilePath = { 0 }; RtlInitUnicodeString(&DstFilePath, dstFile); RtlInitUnicodeString(&SrcFilePath, srcFile); IO_STATUS_BLOCK IoBlock = { 0 }; HANDLE hSrcFile = NULL; HANDLE hDstFile = NULL;
OBJECT_ATTRIBUTES DstFileAttribute = { 0 }; OBJECT_ATTRIBUTES SrcFileAttribute = { 0 }; InitializeObjectAttributes(&DstFileAttribute, &DstFilePath, OBJ_CASE_INSENSITIVE, NULL, NULL); InitializeObjectAttributes(&SrcFileAttribute, &SrcFilePath, OBJ_CASE_INSENSITIVE, NULL, NULL);
status = ZwOpenFile(&hSrcFile, GENERIC_ALL, &SrcFileAttribute, &IoBlock, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SYNCHRONOUS_IO_NONALERT); if (!NT_SUCCESS(status)) { DbgPrint("Open File %wZ Failed, status: %x\n", SrcFilePath, status); return status; }
FILE_STANDARD_INFORMATION StdFileInfo = { 0 }; status = ZwQueryInformationFile(hSrcFile, &IoBlock, &StdFileInfo, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation); if (!NT_SUCCESS(status)) { DbgPrint("Query File %wZ Failed, status: %x\n", SrcFilePath, status); ZwClose(hSrcFile); return status; }
PVOID Transfer = ExAllocatePool2(POOL_FLAG_NON_PAGED, StdFileInfo.EndOfFile.QuadPart, 'ymym'); if (Transfer == NULL) { DbgPrint("ExAllocatePool Transfer Buffer Failed, status: %x\n", status); ZwClose(hSrcFile); return status; } RtlZeroMemory(Transfer, StdFileInfo.EndOfFile.QuadPart);
LARGE_INTEGER TempReadCount = { 0 }; status = ZwReadFile(hSrcFile, NULL, NULL, NULL, &IoBlock, Transfer, (ULONG)StdFileInfo.EndOfFile.QuadPart, &TempReadCount, NULL); if (!NT_SUCCESS(status)) { DbgPrint("Read to Transfer Buffer Failed, status: %x\n", status); ExFreePool(Transfer); return status; } ZwClose(hSrcFile);
status = ZwCreateFile(&hDstFile, GENERIC_ALL, &DstFileAttribute, &IoBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_SUPERSEDE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (!NT_SUCCESS(status)) { DbgPrint("Create File Failed, status: %x\n", status); ExFreePool(Transfer); return status; }
status = ZwWriteFile(hDstFile, NULL, NULL, NULL, &IoBlock, Transfer, (ULONG)StdFileInfo.EndOfFile.QuadPart, &TempReadCount, NULL); if (!NT_SUCCESS(status)) { DbgPrint("Write File Failed, status: %x\n", status); ExFreePool(Transfer); ZwClose(hDstFile); return status; } ZwClose(hDstFile); DbgPrint("Created New File %wZ\n", DstFilePath); return status; }
|