NTSTATUS NewNtReadFile(
IN HANDLE FileHandle,
IN HANDLE Event OPTIONAL,
IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
IN PVOID ApcContext OPTIONAL,
OUT PIO_STATUS_BLOCK IoStatusBlock,
OUT PVOID Buffer,
IN ULONG Length,
IN PLARGE_INTEGER ByteOffset OPTIONAL,
IN PULONG Key OPTIONAL)
{
NTSTATUS status = 0;
CHAR aProcessName[PROCNAMELEN];
ANSI_STRING ansiName;
UNICODE_STRING un;
IO_STATUS_BLOCK file_status;
FILE_STANDARD_INFORMATION fpi;
FILE_POSITION_INFORMATION psi;
FILE_POSITION_INFORMATION tmp;
PIO_STATUS_BLOCK pstat = NULL;
PLARGE_INTEGER byteset = NULL;
CHAR buf[10];
ULONG len = 10;
GetProcessName( aProcessName );
if(0 != strncmp(aProcessName,"wordpad.exe",strlen("wordpad.exe")))
{
return OldNtReadFile(FileHandle,Event,
ApcRoutine,ApcContext,IoStatusBlock,Buffer,Length,ByteOffset,Key);
}
status = ZwQueryInformationFile(FileHandle,&file_status,&tmp,
sizeof(FILE_POSITION_INFORMATION),FilePositionInformation);
ZwQueryInformationFile(FileHandle, &file_status, &fpi,
sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
if(fpi.EndOfFile.LowPart == 37)
{
if(KeGetCurrentIrql() != PASSIVE_LEVEL)
{
DbgPrint("Not PSSIVE_LEVEL\n");
}
byteset = (PLARGE_INTEGER)ExAllocatePool(NonPagedPool,sizeof(LARGE_INTEGER));
if(NULL == byteset)
{
DbgPrint("byteset err\n");
}
psi = tmp;
psi.CurrentByteOffset.LowPart = 20;
status = ZwSetInformationFile(FileHandle,&file_status,&psi,
sizeof(FILE_POSITION_INFORMATION),FilePositionInformation);
ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
pstat = IoStatusBlock;
// DbgPrint("%d,%d\n",Buffer,buf);
status = OldNtReadFile(FileHandle,NULL,
NULL,NULL,pstat,buf,len,NULL,NULL);
if(NT_SUCCESS(status))
{
// memcpy(buf,Buffer,6);
buf[9] = '\0';
DbgPrint(" %s \n",buf);
}
else
{
DbgPrint(" ReadFile failed. \n");
}
ZwSetInformationFile(FileHandle,&file_status,&tmp,
sizeof(FILE_POSITION_INFORMATION),FilePositionInformation);
if(NULL != byteset)
{
ExFreePool(byteset);
}
}
return OldNtReadFile(FileHandle,Event,
ApcRoutine,ApcContext,IoStatusBlock,Buffer,Length,ByteOffset,Key);
}
根据网上某大牛的Hook ZwCreteProcess文章,我改成Hook ZwReadFile。发现能Hook到,但是在进入我的NewReadFile里面后,用OldZwReadFile(系统的)读取文件某一部分内容,比如读取文件第20个字节后的10个字节。这时发现,用我定义的pstat,buf两个参数传进OldZwReadFile后,总是ZwReadFile失败。即这行语句:status = OldNtReadFile(FileHandle,NULL,
NULL,NULL,pstat,buf,len,NULL,NULL);
if(NT_SUCCESS(status))
{
// memcpy(buf,Buffer,6);
buf[9] = '\0';
DbgPrint(" %s \n",buf);
}
else
{
DbgPrint(" ReadFile failed. \n");
}
而换成系统传进的参数却可以成功:
status = OldNtReadFile(FileHandle,NULL,
NULL,NULL,IoStatusBlock,Buffer,len,NULL,NULL);
if(NT_SUCCESS(status))
{
// memcpy(buf,Buffer,6);
buf[9] = '\0';
DbgPrint(" %s \n",buf);
}
else
{
DbgPrint(" ReadFile failed. \n");
}
本人也是才学驱动编程,搞了两天也没弄出所以然,不知哪位知道原因,不胜感谢。