Txzcy2004
驱动牛犊
驱动牛犊
  • 注册日期2004-05-25
  • 最后登录2005-06-23
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1006回复:2

..............

楼主#
更多 发布于:2004-06-28 13:09
发现自己比较sb......

[编辑 -  7/15/04 by  Txzcy2004]

最新喜欢:

cyliucyliu
cnmmd
驱动牛犊
驱动牛犊
  • 注册日期2004-03-15
  • 最后登录2018-05-26
  • 粉丝0
  • 关注0
  • 积分62分
  • 威望48点
  • 贡献值0点
  • 好评度4点
  • 原创分0分
  • 专家分0分
  • 社区居民
沙发#
发布于:2004-07-06 12:59
我和你的发现一样的。经过苦苦搜索,发现,
必须自己创建IRP,向下面的FSD询问文件名。
以下是我参照FileMon做的。
但实际情况是加载驱动后蓝苹。

用GetFullPathName可以得到文件名。

#define DBGPUS1( key,msg ) \
KdPrint( ("%10s:%5d: %s=%wZ\n",__FILE__,__LINE__,(key),(msg) ) )

#define DBGD1( key,value ) \
KdPrint( ("%10s:%5d: %s=%08x\n",__FILE__,__LINE__,(key),(value) ) )

//----------------------------------------------------------------------
//
// PadLockFilterQueryFileComplete
//
// This routine is used to handle I/O completion for our self-generated
// IRP that is used to query a file's name or number.
//
//----------------------------------------------------------------------
NTSTATUS
PadLockFilterQueryFileComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
)
{
PVOID pTmp;
//
// Copy the status information back into the "user" IOSB.
//
*Irp->UserIosb = Irp->IoStatus;
if( !NT_SUCCESS(Irp->IoStatus.Status) )
{
DBGD1("ERROR ON IRP:", Irp->IoStatus.Status);
}

//
// Set the user event - wakes up the mainline code doing this.
//
KeSetEvent(Irp->UserEvent, 0, FALSE);

//
// Free the IRP now that we are done with it.
//
IoFreeIrp(Irp);

pTmp = DeviceObject;
pTmp = Context;

//
// We return STATUS_MORE_PROCESSING_REQUIRED because this "magic" return value
// tells the I/O Manager that additional processing will be done by this driver
// to the IRP - in fact, it might (as it is in this case) already BE done - and
// the IRP cannot be completed.
//

return STATUS_MORE_PROCESSING_REQUIRED;
}

//----------------------------------------------------------------------
//
// PadLockFilterQueryFile
//
// This function retrieves the "standard" information for the
// underlying file system, asking for the filename in particular.
//
//----------------------------------------------------------------------
BOOLEAN
PadLockFilterQueryFile(
    PDEVICE_OBJECT DeviceObject,
    PFILE_OBJECT FileObject,
    FILE_INFORMATION_CLASS FileInformationClass,
    PVOID FileQueryBuffer,
    ULONG FileQueryBufferLength
    )
{
    PIRP irp;
    KEVENT event;
    IO_STATUS_BLOCK IoStatusBlock;
    PIO_STACK_LOCATION ioStackLocation;

    DBGD1("Getting file name for FileObject", FileObject);

    //
    // Initialize the event
    //
    KeInitializeEvent(&event, SynchronizationEvent, FALSE);

    //
    // Allocate an irp for this request.  This could also come from a
    // private pool, for instance.
    //
    irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
    if(!irp)
{
        //
        // Failure!
        //
        return FALSE;
    }
  
    //
    // Build the IRP's main body
    //  
    irp->AssociatedIrp.SystemBuffer = FileQueryBuffer;
    irp->UserEvent = &event;
    irp->UserIosb = &IoStatusBlock;
    irp->Tail.Overlay.Thread = PsGetCurrentThread();
    irp->Tail.Overlay.OriginalFileObject = FileObject;
    irp->RequestorMode = KernelMode;
    irp->Flags = 0;

    //
    // Set up the I/O stack location.
    //
    ioStackLocation = IoGetNextIrpStackLocation(irp);
    ioStackLocation->MajorFunction = IRP_MJ_QUERY_INFORMATION;
    ioStackLocation->DeviceObject = DeviceObject;
    ioStackLocation->FileObject = FileObject;
    ioStackLocation->Parameters.QueryFile.Length
= FileQueryBufferLength;
    ioStackLocation->Parameters.QueryFile.FileInformationClass
= FileInformationClass;

    //
    // Set the completion routine.
    //
    IoSetCompletionRoutine(irp, PadLockFilterQueryFileComplete, 0, TRUE, TRUE, TRUE);

    //
    // Send it to the FSD
    //
    (void) IoCallDriver(DeviceObject, irp);

    //
    // Wait for the I/O
    //
    KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, 0);

    //
    // Done! Note that since our completion routine frees the IRP we cannot
    // touch the IRP now.
    //
    return NT_SUCCESS( IoStatusBlock.Status );
}



VOID
GetFullPathName(PDEVICE_OBJECT devObj,PFILE_OBJECT fileObject)
{
PFILE_NAME_INFORMATION fileNameInfo;
UNICODE_STRING      fullUniName;


fileNameInfo = (PFILE_NAME_INFORMATION)
ExAllocatePool( NonPagedPool,MAXPATHLEN*sizeof(WCHAR) );

if( fileNameInfo
&& PadLockFilterQueryFile( devObj ,
fileObject,
FileNameInformation,
fileNameInfo,
(MAXPATHLEN )*sizeof(WCHAR) )
)
{

fullUniName.Length = (SHORT) fileNameInfo->FileNameLength;
fullUniName.Buffer = fileNameInfo->FileName;

//if( NT_SUCCESS( RtlUnicodeStringToAnsiString( &fileName, &fullUniName, TRUE ))) {

// fullPathName[ fileName.Length + prefixLen ] = 0;

// if( hookExt->Type == NPFS ) {

// strcpy( fullPathName, NAMED_PIPE_PREFIX );

// } else if( hookExt->Type == MSFS ) {

// strcpy( fullPathName, MAIL_SLOT_PREFIX );

// } else if( fileObject->DeviceObject->DeviceType != FILE_DEVICE_NETWORK_FILE_SYSTEM ) {

// sprintf( fullPathName, "%C:", hookExt->LogicalDrive );

// } else {

// //
// // No prefix for network devices
// //
// }

// memcpy( &fullPathName[prefixLen], fileName.Buffer, fileName.Length );
// gotPath = TRUE;
// RtlFreeAnsiString( &fileName );
// fileName.Buffer = NULL;
//}
}
DBGPUS1( "FileName",&fullUniName);

if( fileNameInfo ) ExFreePool( fileNameInfo );
}
punk
驱动小牛
驱动小牛
  • 注册日期2001-04-07
  • 最后登录2018-06-01
  • 粉丝0
  • 关注0
  • 积分621分
  • 威望164点
  • 贡献值0点
  • 好评度60点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-07-09 13:53
我和你的发现一样的。经过苦苦搜索,发现,
必须自己创建IRP,向下面的FSD询问文件名。
以下是我参照FileMon做的。
但实际情况是加载驱动后蓝苹。

用GetFullPathName可以得到文件名。

#define DBGPUS1( key,msg )
KdPrint( ("%10s:%5d: %s=%wZn",__FILE__,__LINE__,(key),(msg) ) )

#define DBGD1( key,value )
KdPrint( ("%10s:%5d: %s=%08xn",__FILE__,__LINE__,(key),(value) ) )

//----------------------------------------------------------------------
//
// PadLockFilterQueryFileComplete
//
// This routine is used to handle I/O completion for our self-generated
// IRP that is used to query a file's name or number.
//
//----------------------------------------------------------------------
NTSTATUS
PadLockFilterQueryFileComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
)
{
PVOID pTmp;
//
// Copy the status information back into the "user" IOSB.
//
*Irp->UserIosb = Irp->IoStatus;
if( !NT_SUCCESS(Irp->IoStatus.Status) )
{
DBGD1("ERROR ON IRP:", Irp->IoStatus.Status);
}

//
// Set the user event - wakes up the mainline code doing this.
//
KeSetEvent(Irp->UserEvent, 0, FALSE);

//
// Free the IRP now that we are done with it.
//
IoFreeIrp(Irp);

pTmp = DeviceObject;
pTmp = Context;

//
// We return STATUS_MORE_PROCESSING_REQUIRED because this "magic" return value
// tells the I/O Manager that additional processing will be done by this driver
// to the IRP - in fact, it might (as it is in this case) already BE done - and
// the IRP cannot be completed.
//

return STATUS_MORE_PROCESSING_REQUIRED;
}

//----------------------------------------------------------------------
//
// PadLockFilterQueryFile
//
// This function retrieves the "standard" information for the
// underlying file system, asking for the filename in particular.
//
//----------------------------------------------------------------------
BOOLEAN
PadLockFilterQueryFile(
    PDEVICE_OBJECT DeviceObject,
    PFILE_OBJECT FileObject,
    FILE_INFORMATION_CLASS FileInformationClass,
    PVOID FileQueryBuffer,
    ULONG FileQueryBufferLength
    )
{
    PIRP irp;
    KEVENT event;
    IO_STATUS_BLOCK IoStatusBlock;
    PIO_STACK_LOCATION ioStackLocation;

    DBGD1("Getting file name for FileObject", FileObject);

    //
    // Initialize the event
    //
    KeInitializeEvent(&event, SynchronizationEvent, FALSE);

    //
    // Allocate an irp for this request.  This could also come from a
    // private pool, for instance.
    //
    irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
    if(!irp)
{
        //
        // Failure!
        //
        return FALSE;
    }
  
    //
    // Build the IRP's main body
    //  
    irp->AssociatedIrp.SystemBuffer = FileQueryBuffer;
    irp->UserEvent = &event;
    irp->UserIosb = &IoStatusBlock;
    irp->Tail.Overlay.Thread = PsGetCurrentThread();
    irp->Tail.Overlay.OriginalFileObject = FileObject;
    irp->RequestorMode = KernelMode;
    irp->Flags = 0;

    //
    // Set up the I/O stack location.
    //
    ioStackLocation = IoGetNextIrpStackLocation(irp);
    ioStackLocation->MajorFunction = IRP_MJ_QUERY_INFORMATION;
    ioStackLocation->DeviceObject = DeviceObject;
    ioStackLocation->FileObject = FileObject;
    ioStackLocation->Parameters.QueryFile.Length
= FileQueryBufferLength;
    ioStackLocation->Parameters.QueryFile.FileInformationClass
= FileInformationClass;

    //
    // Set the completion routine.
    //
    IoSetCompletionRoutine(irp, PadLockFilterQueryFileComplete, 0, TRUE, TRUE, TRUE);

    //
    // Send it to the FSD
    //
    (void) IoCallDriver(DeviceObject, irp);

    //
    // Wait for the I/O
    //
    KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, 0);

    //
    // Done! Note that since our completion routine frees the IRP we cannot
    // touch the IRP now.
    //
    return NT_SUCCESS( IoStatusBlock.Status );
}



VOID
GetFullPathName(PDEVICE_OBJECT devObj,PFILE_OBJECT fileObject)
{
PFILE_NAME_INFORMATION fileNameInfo;
UNICODE_STRING      fullUniName;


fileNameInfo = (PFILE_NAME_INFORMATION)
ExAllocatePool( NonPagedPool,MAXPATHLEN*sizeof(WCHAR) );

if( fileNameInfo
&& PadLockFilterQueryFile( devObj ,
fileObject,
FileNameInformation,
fileNameInfo,
(MAXPATHLEN )*sizeof(WCHAR) )
)
{

fullUniName.Length = (SHORT) fileNameInfo->FileNameLength;
fullUniName.Buffer = fileNameInfo->FileName;

//if( NT_SUCCESS( RtlUnicodeStringToAnsiString( &fileName, &fullUniName, TRUE ))) {

// fullPathName[ fileName.Length + prefixLen ] = 0;

// if( hookExt->Type == NPFS ) {

// strcpy( fullPathName, NAMED_PIPE_PREFIX );

// } else if( hookExt->Type == MSFS ) {

// strcpy( fullPathName, MAIL_SLOT_PREFIX );

// } else if( fileObject->DeviceObject->DeviceType != FILE_DEVICE_NETWORK_FILE_SYSTEM ) {

// sprintf( fullPathName, "%C:", hookExt->LogicalDrive );

// } else {

// //
// // No prefix for network devices
// //
// }

// memcpy( &fullPathName[prefixLen], fileName.Buffer, fileName.Length );
// gotPath = TRUE;
// RtlFreeAnsiString( &fileName );
// fileName.Buffer = NULL;
//}
}
DBGPUS1( "FileName",&fullUniName);

if( fileNameInfo ) ExFreePool( fileNameInfo );
}


1.SetCompleteRoute没有当前IRP栈?
2.StackSize是否够用?

不停学习
游客

返回顶部