首页
社区
课程
招聘
[求助]关于驱动中定义全局变量出现nonepaged fault的问题
发表于: 2011-3-19 00:32 6696

[求助]关于驱动中定义全局变量出现nonepaged fault的问题

2011-3-19 00:32
6696
学习ssdt hook,hook了NtCreateFile,但我把要保护的文件名定义为全局变量时,就会BSOD。信息是nonepaged fault。我想了想,未分页内存?改了几个地方,后来把全局变量定义到函数里的时候终于不蓝屏了。想请教一下原因。谢谢!
#include <ntddk.h>

#pragma pack(1)
typedef struct _SSDT_ENTRY
{
	PULONG ServiceTableBase;
	PULONG ServiceCounterTableBase;
	PULONG NumberOfServices;
	PUCHAR ParamTableBase;
}SSDT_ENTRY, PSSDT_ENTRY;
#pragma pack()

typedef NTSTATUS (*NTCREATEFILE)(PHANDLE FileHandle,
							ACCESS_MASK DesiredAccess,
							POBJECT_ATTRIBUTES ObjectAttributes,
							PIO_STATUS_BLOCK IoStatusBlock,
							PLARGE_INTEGER AllocationSize,
							ULONG FileAttributes,
							ULONG ShareAccess,
							ULONG CreateOptions,
							PVOID EaBuffer,
							ULONG EaLength);

__declspec(dllimport) SSDT_ENTRY KeServiceDescriptorTable;

//WCHAR wProtectFileName[] = L"\\??\\C:\\protect.txt";
//UNICODE_STRING usProtectFileName = {0};
NTCREATEFILE OldNtCreateFile = NULL;

NTSTATUS MyNtCreateFile(PHANDLE FileHandle,
					ACCESS_MASK DesiredAccess,
					POBJECT_ATTRIBUTES ObjectAttributes,
					PIO_STATUS_BLOCK IoStatusBlock,
					PLARGE_INTEGER AllocationSize,
					ULONG FileAttributes,
					ULONG ShareAccess,
					ULONG CreateOptions,
					PVOID EaBuffer,
					ULONG EaLength);
VOID DriverUnload(PDRIVER_OBJECT pDriverObject);

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject,PUNICODE_STRING pRegistryPath)
{
	pDriverObject->DriverUnload = DriverUnload;

	//RtlInitUnicodeString(&usProtectFileName,wProtectFileName);

	//OldNtCreateFile = (NTCREATEFILE)KeServiceDescriptorTable.ServiceTableBase[*(ULONG *)((PUCHAR)ZwCreateFile+1)];

	__asm
	{
		cli
		mov eax, cr0
		and eax, ~0x10000
		mov cr0, eax
	}
	
	OldNtCreateFile = (NTCREATEFILE)InterlockedExchange(&KeServiceDescriptorTable.ServiceTableBase[*(ULONG *)((PUCHAR)ZwCreateFile+1)],(ULONG)MyNtCreateFile);
	//KeServiceDescriptorTable.ServiceTableBase[*(ULONG *)((PUCHAR)ZwCreateFile+1)] = (ULONG)MyNtCreateFile;
	
	__asm
	{
		mov eax, cr0
		or eax, 0x10000
		mov cr0, eax
		sti
	}
	
	return STATUS_SUCCESS;
}

VOID DriverUnload(PDRIVER_OBJECT pDriverObject)
{
	__asm
	{
		cli
		mov eax, cr0
		and eax, ~0x10000
		mov cr0, eax
	}
	
	InterlockedExchange(&KeServiceDescriptorTable.ServiceTableBase[*(ULONG *)((PUCHAR)ZwCreateFile+1)],(ULONG)OldNtCreateFile);
	//KeServiceDescriptorTable.ServiceTableBase[*(ULONG *)((PUCHAR)ZwCreateFile+1)] = (ULONG)OldNtCreateFile;

	__asm
	{
		mov eax, cr0
		or eax, 0x10000
		mov cr0, eax
		sti
	}
}

NTSTATUS MyNtCreateFile(PHANDLE FileHandle,
					ACCESS_MASK DesiredAccess,
					POBJECT_ATTRIBUTES ObjectAttributes,
					PIO_STATUS_BLOCK IoStatusBlock,
					PLARGE_INTEGER AllocationSize,
					ULONG FileAttributes,
					ULONG ShareAccess,
					ULONG CreateOptions,
					PVOID EaBuffer,
					ULONG EaLength)
{
	WCHAR wProtectFileName[] = L"\\??\\C:\\protect.txt";
	UNICODE_STRING usProtectFileName = {0};

	RtlInitUnicodeString(&usProtectFileName,wProtectFileName);

	if(!RtlCompareUnicodeString(&usProtectFileName,ObjectAttributes->ObjectName,TRUE))
	{
		FileHandle = NULL;
		return STATUS_INVALID_PARAMETER;
	}
	
	return OldNtCreateFile(FileHandle,
							DesiredAccess,
							ObjectAttributes,
							IoStatusBlock,
							AllocationSize,
							FileAttributes,
							ShareAccess,
							CreateOptions,
							EaBuffer,
							EaLength);
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 693
活跃值: (108)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
2
盼高手回答,但楼主试过将全局变量定义在非分页内存中吗,即前面加一个#pragma data_seg()?
2011-3-28 20:49
0
雪    币: 693
活跃值: (108)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
3
我想了想,有一个不太成熟的想法,请大家多多指正。
分两种情况,第一种,如果该驱动在dispacth_level以下级别执行的话,无论分页还是不分页都没问题。
第二种,如果在dispacth_level上执行,全局变量在分页内存上肯定蓝屏,但作为局部变量,它与hook函数在一起,无论分页与非分页都在一起,一旦函数部分载入内存并执行,该变量都会在内存中,不需再进行内存置换和页面故障处理了,所以不会蓝屏。
一点想法,可能有错误,希望大家多多指正。
2011-3-28 21:18
0
游客
登录 | 注册 方可回帖
返回
//