驱动学习三

#include<ntddk.h>
#include<windef.h>
#include<intrin.h>

PVOID OpenProcessAddr = NULL;
PVOID jmpAddr         = NULL;



KIRQL  WPOFFx64()
{
	KIRQL irql = KeRaiseIrqlToDpcLevel();

	DWORD64 cr0 =__readcr0();

	cr0 &= 0xfffffffffffeffff;

	__writecr0(cr0);

	_disable();

	return irql;

}


void WPONx64(KIRQL irql)
{
	DWORD64 cr0 = __readcr0();

	cr0 |= 0x10000;

	__writecr0(cr0);

	_enable();

	KeLowerIrql(irql);

	return;

}


typedef NTSTATUS(*PNtOpenProcess)
(
	PHANDLE            ProcessHandle,
	ACCESS_MASK        DesiredAccess,
	POBJECT_ATTRIBUTES ObjectAttributes,
	PCLIENT_ID         ClientId
	);






void DrvUnload(PDRIVER_OBJECT driver)
{
	KIRQL tempirql = WPOFFx64();

	RtlCopyMemory(OpenProcessAddr,jmpAddr,13);

	WPONx64(tempirql);

	ExFreePool(jmpAddr);

	jmpAddr = NULL;

	DbgPrint("the Driver has been unload");

	return;
}

NTSTATUS MyNtOpenProcess(
         PHANDLE            ProcessHandle,
         ACCESS_MASK        DesiredAccess,
         POBJECT_ATTRIBUTES ObjectAttributes,
		 PCLIENT_ID         ClientId
)
{
	
	if (ClientId->UniqueProcess == 4116)return STATUS_UNSUCCESSFUL;


	PNtOpenProcess tempaddr = (PNtOpenProcess)jmpAddr;


	return tempaddr(ProcessHandle, DesiredAccess, ObjectAttributes, ClientId);
}









NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING pregpath)
{
	UNREFERENCED_PARAMETER(pregpath);
	
	UNICODE_STRING g_openprocess = { 0 };
	RtlInitUnicodeString(&g_openprocess, L"NtOpenProcess");
	OpenProcessAddr = MmGetSystemRoutineAddress(&g_openprocess);
	if (!OpenProcessAddr)
	{
		return STATUS_NOT_FOUND;
	}

	//DbgPrint("%p",OpenProcessAddr);


	BYTE hookcode[] = { 0x48,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xe0 };

	ULONG tag=NULL;
	//jmpAddr = ExAllocatePool2(POOL_FLAG_NON_PAGED,0x1000,tag);

	jmpAddr = ExAllocatePool(NonPagedPool, 0x1000);

	RtlZeroMemory(jmpAddr, 0x1000);
	RtlCopyMemory(jmpAddr,OpenProcessAddr,13);

	*((PULONG_PTR)(hookcode + 2)) = ((ULONG_PTR)OpenProcessAddr+13);

	RtlCopyMemory((PUCHAR)jmpAddr+13, hookcode, sizeof(hookcode));

	*((PULONG_PTR)(hookcode + 2)) = (PULONG_PTR)&MyNtOpenProcess;

	KIRQL    tempirql = WPOFFx64();

	RtlCopyMemory(OpenProcessAddr, hookcode, sizeof(hookcode));

	WPONx64(tempirql);


	driver->DriverUnload = DrvUnload;




	return STATUS_SUCCESS;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值