最近发现了FileDisk这个好东东,于是就想把它改成一个虚拟目录软件.与SUBST不同的是,我想把所有写操作都映射到另一个目录,这样无论对虚拟磁盘做什么操作,都不会影响到原目录的内容.可是现在第一步就不知道如何处理了-----FileDisk需要一个文件,如何才能让它可以直接映射一个目录呢?我认为焦点是在FileDiskThread过程里,但不知道如何改.....系统读取一个文件时(上层API为ReadFile时),对于目录到底是如何处理的呢?有没有什么书详细讲到文件操作与管理呢?附上FileDiskThread过程代码:VOID
FileDiskThread (
IN PVOID Context
)
{
PDEVICE_OBJECT device_object;
PDEVICE_EXTENSION device_extension;
PLIST_ENTRY request;
PIRP irp;
PIO_STACK_LOCATION io_stack;
PUCHAR system_buffer;
PUCHAR buffer; ASSERT(Context != NULL); device_object = (PDEVICE_OBJECT) Context; device_extension = (PDEVICE_EXTENSION) device_object->DeviceExtension; KeSetPriorityThread(KeGetCurrentThread(), LOW_REALTIME_PRIORITY); for (;;)
{
KeWaitForSingleObject(
&device_extension->request_event,
Executive,
KernelMode,
FALSE,
NULL
); if (device_extension->terminate_thread)
{
PsTerminateSystemThread(STATUS_SUCCESS);
} while (request = ExInterlockedRemoveHeadList(
&device_extension->list_head,
&device_extension->list_lock
))
{
irp = CONTAINING_RECORD(request, IRP, Tail.Overlay.ListEntry); io_stack = IoGetCurrentIrpStackLocation(irp); switch (io_stack->MajorFunction)
{
case IRP_MJ_READ:
system_buffer = (PUCHAR) MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority);
if (system_buffer == NULL)
{
irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
irp->IoStatus.Information = 0;
break;
}
buffer = (PUCHAR) ExAllocatePool(PagedPool, io_stack->Parameters.Read.Length);
if (buffer == NULL)
{
irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
irp->IoStatus.Information = 0;
break;
}
ZwReadFile(
device_extension->file_handle,
NULL,
NULL,
NULL,
&irp->IoStatus,
buffer,
io_stack->Parameters.Read.Length,
&io_stack->Parameters.Read.ByteOffset,
NULL
);
RtlCopyMemory(system_buffer, buffer, io_stack->Parameters.Read.Length);
ExFreePool(buffer);
break; case IRP_MJ_WRITE:
if ((io_stack->Parameters.Write.ByteOffset.QuadPart +
io_stack->Parameters.Write.Length) >
device_extension->file_size.QuadPart)
{
irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
irp->IoStatus.Information = 0;
}
ZwWriteFile(
device_extension->file_handle,
NULL,
NULL,
NULL,
&irp->IoStatus,
MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority),
io_stack->Parameters.Write.Length,
&io_stack->Parameters.Write.ByteOffset,
NULL
);
break; case IRP_MJ_DEVICE_CONTROL:
switch (io_stack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_FILE_DISK_OPEN_FILE: //SeImpersonateClient(device_extension->security_client_context, NULL); //irp->IoStatus.Status = FileDiskOpenFile(device_object, irp); //PsRevertToSelf(); break; case IOCTL_FILE_DISK_CLOSE_FILE:
//irp->IoStatus.Status = FileDiskCloseFile(device_object, irp);
break; default:
irp->IoStatus.Status = STATUS_DRIVER_INTERNAL_ERROR;
}
break; default:
irp->IoStatus.Status = STATUS_DRIVER_INTERNAL_ERROR;
} IoCompleteRequest(
irp,
(CCHAR) (NT_SUCCESS(irp->IoStatus.Status) ?
IO_DISK_INCREMENT : IO_NO_INCREMENT)
);
}
}
}
FileDiskThread (
IN PVOID Context
)
{
PDEVICE_OBJECT device_object;
PDEVICE_EXTENSION device_extension;
PLIST_ENTRY request;
PIRP irp;
PIO_STACK_LOCATION io_stack;
PUCHAR system_buffer;
PUCHAR buffer; ASSERT(Context != NULL); device_object = (PDEVICE_OBJECT) Context; device_extension = (PDEVICE_EXTENSION) device_object->DeviceExtension; KeSetPriorityThread(KeGetCurrentThread(), LOW_REALTIME_PRIORITY); for (;;)
{
KeWaitForSingleObject(
&device_extension->request_event,
Executive,
KernelMode,
FALSE,
NULL
); if (device_extension->terminate_thread)
{
PsTerminateSystemThread(STATUS_SUCCESS);
} while (request = ExInterlockedRemoveHeadList(
&device_extension->list_head,
&device_extension->list_lock
))
{
irp = CONTAINING_RECORD(request, IRP, Tail.Overlay.ListEntry); io_stack = IoGetCurrentIrpStackLocation(irp); switch (io_stack->MajorFunction)
{
case IRP_MJ_READ:
system_buffer = (PUCHAR) MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority);
if (system_buffer == NULL)
{
irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
irp->IoStatus.Information = 0;
break;
}
buffer = (PUCHAR) ExAllocatePool(PagedPool, io_stack->Parameters.Read.Length);
if (buffer == NULL)
{
irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
irp->IoStatus.Information = 0;
break;
}
ZwReadFile(
device_extension->file_handle,
NULL,
NULL,
NULL,
&irp->IoStatus,
buffer,
io_stack->Parameters.Read.Length,
&io_stack->Parameters.Read.ByteOffset,
NULL
);
RtlCopyMemory(system_buffer, buffer, io_stack->Parameters.Read.Length);
ExFreePool(buffer);
break; case IRP_MJ_WRITE:
if ((io_stack->Parameters.Write.ByteOffset.QuadPart +
io_stack->Parameters.Write.Length) >
device_extension->file_size.QuadPart)
{
irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
irp->IoStatus.Information = 0;
}
ZwWriteFile(
device_extension->file_handle,
NULL,
NULL,
NULL,
&irp->IoStatus,
MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority),
io_stack->Parameters.Write.Length,
&io_stack->Parameters.Write.ByteOffset,
NULL
);
break; case IRP_MJ_DEVICE_CONTROL:
switch (io_stack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_FILE_DISK_OPEN_FILE: //SeImpersonateClient(device_extension->security_client_context, NULL); //irp->IoStatus.Status = FileDiskOpenFile(device_object, irp); //PsRevertToSelf(); break; case IOCTL_FILE_DISK_CLOSE_FILE:
//irp->IoStatus.Status = FileDiskCloseFile(device_object, irp);
break; default:
irp->IoStatus.Status = STATUS_DRIVER_INTERNAL_ERROR;
}
break; default:
irp->IoStatus.Status = STATUS_DRIVER_INTERNAL_ERROR;
} IoCompleteRequest(
irp,
(CCHAR) (NT_SUCCESS(irp->IoStatus.Status) ?
IO_DISK_INCREMENT : IO_NO_INCREMENT)
);
}
}
}
.text:0100147B ?AddSubst@@YGEPAG0KPAVMESSAGE@@@Z proc near ; CODE XREF: _main+626p
.text:0100147B
.text:0100147B TargetPath = word ptr -234h
.text:0100147B var_1C = dword ptr -1Ch
.text:0100147B var_8 = dword ptr -8
.text:0100147B lpTargetPath = dword ptr -4
.text:0100147B lpDeviceName = dword ptr 8
.text:0100147B arg_4 = dword ptr 0Ch
.text:0100147B arg_C = dword ptr 14h
.text:0100147B
.text:0100147B push ebp
.text:0100147C mov ebp, esp
.text:0100147E sub esp, 234h
.text:01001484 push ebx
.text:01001485 push esi
.text:01001486 push edi
.text:01001487 lea ecx, [ebp+var_1C]
.text:0100148A call ds:__imp_??0FSTRING@@QAE@XZ ; FSTRING::FSTRING(void)
.text:01001490 lea eax, [ebp+lpTargetPath]
.text:01001493 push eax ; int
.text:01001494 push 10Ch ; ucchMax
.text:01001499 lea eax, [ebp+TargetPath]
.text:0100149F push eax ; lpTargetPath
.text:010014A0 mov eax, [ebp+lpDeviceName]
.text:010014A3 movzx eax, word ptr [eax]
.text:010014A6 sub eax, 40h
.text:010014A9 push eax ; int
.text:010014AA call ?QuerySubstedDrive@@YGEKPAGKPAK@Z ; QuerySubstedDrive(ulong,ushort *,ulong,ulong *)
.text:010014AF mov edi, [ebp+arg_4]
.text:010014B2 xor ebx, ebx
.text:010014B4 test al, al
.text:010014B6 jnz loc_100154F
.text:010014BC mov esi, [ebp+lpTargetPath]
.text:010014BF cmp esi, 2
.text:010014C2 jnz loc_1001554
.text:010014C8 push edi ; wchar_t *
.text:010014C9 call ds:__imp__wcslen
.text:010014CF cmp eax, 3
.text:010014D2 pop ecx
.text:010014D3 jnz short loc_1001532
.text:010014D5 cmp word ptr [edi+2], 3Ah
.text:010014DA jnz short loc_1001532
.text:010014DC cmp word ptr [edi+4], 5Ch
.text:010014E1 jnz short loc_1001532
.text:010014E3 cmp [edi+6], bx
.text:010014E7 jnz short loc_1001532
.text:010014E9 push ebx
.text:010014EA push ebx
.text:010014EB lea eax, [ebp+var_8]
.text:010014EE push eax
.text:010014EF push edi
.text:010014F0 call ds:__imp__RtlDosPathNameToNtPathName_U@16 ; RtlDosPathNameToNtPathName_U(x,x,x,x)
.text:010014F6 test al, al
.text:010014F8 jz short loc_1001541
.text:010014FA movzx eax, word ptr [ebp+var_8]
.text:010014FE mov ecx, [ebp+lpTargetPath]
.text:01001501 shr eax, 1
.text:01001503 mov [ecx+eax*2-2], bx
.text:01001508 push [ebp+lpTargetPath] ; lpTargetPath
.text:0100150B push [ebp+lpDeviceName] ; lpDeviceName
.text:0100150E push 1 ; dwFlags
.text:01001510 call ds:__imp__DefineDosDeviceW@12 ; DefineDosDeviceW(x,x,x)
.text:01001516 test eax, eax
.text:01001518 jnz short loc_1001524
.text:0100151A call ds:__imp__GetLastError@0 ; GetLastError()
.text:01001520 mov esi, eax
.text:01001522 jmp short loc_1001526
.text:01001524 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:01001524
.text:01001524 loc_1001524: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+9Dj
.text:01001524 xor esi, esi
.text:01001526
.text:01001526 loc_1001526: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+A7j
.text:01001526 lea eax, [ebp+var_8]
.text:01001529 push eax
.text:0100152A call ds:__imp__RtlFreeUnicodeString@4 ; RtlFreeUnicodeString(x)
.text:01001530 jmp short loc_1001554
.text:01001532 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:01001532
.text:01001532 loc_1001532: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+58j
.text:01001532 ; AddSubst(ushort *,ushort *,ulong,MESSAGE *)+5Fj ...
.text:01001532 push edi ; lpTargetPath
.text:01001533 push [ebp+lpDeviceName] ; lpDeviceName
.text:01001536 push ebx ; dwFlags
.text:01001537 call ds:__imp__DefineDosDeviceW@12 ; DefineDosDeviceW(x,x,x)
.text:0100153D test eax, eax
.text:0100153F jnz short loc_100154B
.text:01001541
.text:01001541 loc_1001541: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+7Dj
.text:01001541 call ds:__imp__GetLastError@0 ; GetLastError()
.text:01001547 mov esi, eax
.text:01001549 jmp short loc_1001554
.text:0100154B ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:0100154B
.text:0100154B loc_100154B: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+C4j
.text:0100154B xor esi, esi
.text:0100154D jmp short loc_1001554
.text:0100154F ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:0100154F loc_100154F: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+3Bj
.text:0100154F mov esi, 87h
.text:01001554
.text:01001554 loc_1001554: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+47j
.text:01001554 ; AddSubst(ushort *,ushort *,ulong,MESSAGE *)+B5j ...
.text:01001554 cmp esi, ebx
.text:01001556 jz loc_10015E7
.text:0100155C cmp esi, 87h
.text:01001562 jnz short loc_1001586
.text:01001564 mov esi, [ebp+arg_C]
.text:01001567 mov eax, [esi]
.text:01001569 push 3
.text:0100156B push ebx
.text:0100156C push 772Ch
.text:01001571 mov ecx, esi
.text:01001573 call dword ptr [eax+0Ch]
.text:01001576 push offset dword_100115C
.text:0100157B push esi
.text:0100157C call ds:__imp_?Display@MESSAGE@@QAAEPBDZZ ; MESSAGE::Display(char const *,...)
.text:01001582 pop ecx
.text:01001583 pop ecx
.text:01001584 jmp short loc_10015E9
.text:01001586 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:01001586
.text:01001586 loc_1001586: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+E7j
.text:01001586 cmp esi, 2
.text:01001589 lea ecx, [ebp+var_1C]
.text:0100158C push 0FFFFFFFFh
.text:0100158E jnz short loc_10015A1
.text:01001590 push edi
.text:01001591 call ds:__imp_?Initialize@FSTRING@@QAEPAVWSTRING@@PAGK@Z ; FSTRING::Initialize(ushort *,ulong)
.text:01001597 push 3
.text:01001599 push ebx
.text:0100159A push 7731h
.text:0100159F jmp short loc_10015C8
.text:010015A1 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:010015A1
.text:010015A1 loc_10015A1: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+113j
.text:010015A1 cmp esi, 5
.text:010015A4 jnz short loc_10015B7
.text:010015A6 push edi
.text:010015A7 call ds:__imp_?Initialize@FSTRING@@QAEPAVWSTRING@@PAGK@Z ; FSTRING::Initialize(ushort *,ulong)
.text:010015AD push 3
.text:010015AF push ebx
.text:010015B0 push 7732h
.text:010015B5 jmp short loc_10015C8
.text:010015B7 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:010015B7
.text:010015B7 loc_10015B7: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+129j
.text:010015B7 push [ebp+lpDeviceName]
.text:010015BA call ds:__imp_?Initialize@FSTRING@@QAEPAVWSTRING@@PAGK@Z ; FSTRING::Initialize(ushort *,ulong)
.text:010015C0 push 3
.text:010015C2 push ebx
.text:010015C3 push 772Fh
.text:010015C8
.text:010015C8 loc_10015C8: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+124j
.text:010015C8 ; AddSubst(ushort *,ushort *,ulong,MESSAGE *)+13Aj
.text:010015C8 mov esi, [ebp+arg_C]
.text:010015CB mov eax, [esi]
.text:010015CD mov ecx, esi
.text:010015CF call dword ptr [eax+0Ch]
.text:010015D2 lea eax, [ebp+var_1C]
.text:010015D5 push eax
.text:010015D6 push offset asc_100117C ; "%"
.text:010015DB push esi
.text:010015DC call ds:__imp_?Display@MESSAGE@@QAAEPBDZZ ; MESSAGE::Display(char const *,...)
.text:010015E2 add esp, 0Ch
.text:010015E5 jmp short loc_10015E9
.text:010015E7 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.text:010015E7
.text:010015E7 loc_10015E7: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+DBj
.text:010015E7 mov bl, 1
.text:010015E9
.text:010015E9 loc_10015E9: ; CODE XREF: AddSubst(ushort *,ushort *,ulong,MESSAGE *)+109j
.text:010015E9 ; AddSubst(ushort *,ushort *,ulong,MESSAGE *)+16Aj
.text:010015E9 lea ecx, [ebp+var_1C]
.text:010015EC call ds:__imp_??1OBJECT@@UAE@XZ ; OBJECT::~OBJECT(void)
.text:010015F2 pop edi
.text:010015F3 pop esi
.text:010015F4 mov al, bl
.text:010015F6 pop ebx
.text:010015F7 leave
.text:010015F8 retn 10h
.text:010015F8 ?AddSubst@@YGEPAG0KPAVMESSAGE@@@Z endp
要实现硬盘保护不是那么容易的,单独的过滤驱动不能解决问题。如果用户用启动盘启动,怎么保护?