Hello everyone,
I want to filter buffer returned by IRP_MN_QUERYDIRECTORY to hide some file. But when the buffer contain only one file which I should hide it, whatever I do, It can be shown. code like below:
curEntry = (PFILE_BOTH_DIR_INFORMATION)Irp->UserBuffer;
do
{
nextEntryOffset = curEntry->NextEntryOffset;
nextEntry = (PFILE_BOTH_DIR_INFORMATION)((PCHAR)curEntry + nextEntryOffset);
//
// Get the full path for the file by adding fullPathName
//
RtlCopyUnicodeString( &fullPath, &uniPath );
fileName.Buffer = curEntry->FileName;
fileName.Length = (USHORT)curEntry->FileNameLength;
fileName.MaximumLength = (USHORT)curEntry->FileNameLength;
RtlAppendUnicodeStringToString( &fullPath, &fileName );
if( SNSfpdProtectFileW( fullPath.Buffer, fullPath.Length ) )
{
//
// Delete the entry from buffer
//
if( curEntry != nextEntry ) // not last one
{
memcpy( curEntry, nextEntry, Irp->IoStatus.Information-( (ULONG)nextEntry - (ULONG)Irp->UserBuffer ) );
Irp->IoStatus.Information -= nextEntryOffset;
}
else // last one
{
Irp->IoStatus.Information -= Irp->IoStatus.Information - ((ULONG)curEntry - (ULONG)Irp->UserBuffer);//nextEntryOffset;
if( prevEntry ) prevEntry->NextEntryOffset = 0;
if( curEntry == Irp->UserBuffer )
{
PIRP newIrp = NULL;
PIO_STACK_LOCATION newSp = NULL;
//
// query to lower driver again
//
IrpSp = IoGetCurrentIrpStackLocation( Irp );
IrpSp->Parameters.Others.Argument2 = NULL;
if( Irp->CurrentLocation > 1 )
{
IoCopyCurrentIrpStackLocationToNext( Irp );
IoSetCompletionRoutine( Irp, DirectoryDone, NULL, TRUE, TRUE, TRUE );
status = IoCallDriver( hookExt->FileSystem, Irp );
}
else
{
newIrp = IoAllocateIrp( (CCHAR)( DeviceObject->StackSize + 2 ), FALSE );
if( newIrp )
{
newIrp->MdlAddress = Irp->MdlAddress;
newIrp->Flags = Irp->Flags & (~IRP_ASSOCIATED_IRP);
newIrp->AssociatedIrp.SystemBuffer = Irp->AssociatedIrp.SystemBuffer;
newIrp->RequestorMode = Irp->RequestorMode;
newIrp->UserBuffer = Irp->UserBuffer;
newIrp->IoStatus.Information = 0;
newIrp->IoStatus.Status = STATUS_SUCCESS;
IoSetNextIrpStackLocation( newIrp );
newSp = IoGetCurrentIrpStackLocation( newIrp );
newSp[0] = IrpSp[0];
IoCopyCurrentIrpStackLocationToNext( newIrp );
IoSetCompletionRoutine( newIrp, DirectoryDone, NULL, TRUE, TRUE, TRUE );
status = IoCallDriver( hookExt->FileSystem, newIrp );
}
}
if( newIrp )
{
Irp->IoStatus.Status = newIrp->IoStatus.Status;
IoFreeIrp( newIrp );
}
}
break;
}
//
// No more entries
//
if( curEntry == nextEntry ) break;
}
else
{
//
// No more entries
//
if( curEntry == nextEntry ) break;
prevEntry = curEntry;
curEntry = nextEntry;
}
} while( curEntry );