欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

插APC驱动读写

程序员文章站 2022-06-05 17:22:09
...
//插APC温柔读内存
BOOLEAN APCReadProcessMemory(ULONG PID, PVOID targetaddress, ULONG length, PVOID retdata)
{
	PEPROCESS pepro;
	KAPC_STATE kapc = { 0 };
	pepro = LookupProcess((HANDLE)PID);
	if (pepro == NULL)
		return FALSE;
	ObDereferenceObject(pepro);
	__try
	{
		KeStackAttachProcess(pepro, &kapc);
		ProbeForRead(targetaddress, length, sizeof(CHAR));
		RtlCopyMemory(retdata, targetaddress, length);
		KeUnstackDetachProcess(&kapc);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		KeUnstackDetachProcess(&kapc);
		return FALSE;
	}	
	return TRUE;
}


//插APC温柔写内存
BOOLEAN APCWriteProcessMemory(ULONG PID, PVOID targetaddress, ULONG length, PVOID Indata)
{
	PEPROCESS pepro;
	KAPC_STATE kapc = { 0 };
	pepro = LookupProcess((HANDLE)PID);
	if (pepro == NULL)
		return FALSE;
	ObDereferenceObject(pepro);
	ULONG64 Cr0;
	__try
	{
		KeStackAttachProcess(pepro, &kapc);
		ProbeForWrite(targetaddress, length, sizeof(CHAR));
		_disable();
		Cr0 = __readcr0();
		Cr0 &= 0xfffffffffffeffff;
		__writecr0(Cr0);
		_enable();
		memcpy(targetaddress, Indata, length);
		_disable();
		Cr0 |= 10000;
		__writecr0(Cr0);
		_enable();
		KeUnstackDetachProcess(&kapc);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		_disable();
		Cr0 |= 10000;
		__writecr0(Cr0);
		_enable();
		KeUnstackDetachProcess(&kapc);
		return FALSE;
	}
	return TRUE;
}

 

相关标签: 驱动