windows驱动保活的思路/apc demo

本文介绍了如何通过注册关机回调函数,使驱动在系统关机时检查自身服务和文件状态。同时展示了在用户模式和内核模式下使用Apc(异步过程调用)的示例代码,确保驱动在下次启动时能够正常运行。尽管这种方法可以在某些情况下确保驱动的启动,但在安全模式下仍可能被检测到。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:某驱动云下发器

   通过注册关机回调,使得驱动能够接收系统的关机时间,在这个时候检测自己的服务及文件是否正常。如下图:

                                              *注册关机回调

                                         *系统关机消息处理函数

注意:

当然还可以寻找更晚的时机来确保驱动能在下次开机能够启动。 不过,这种还是会在比如,安全模式下被发现的。

*kernel shellcode

//apc demo

//user-mode 

//
//ethread is the target execution environment,
//addr is the user-mode shellcode address
//
VOID UsermodeApc(PETHREAD ethread, PVOID address)
{
	PRKAPC apc = NULL;

	//check the apcs condition
	if (KeAreAllApcsDisabled() == FALSE&&
		address)
	{
		//we can use apcs
		apc = (PRKAPC)ExAllocatePool(NonPagedPool, sizeof(KAPC));
		if (apc)
		{
			//initialize apc structure
			//
			// for user mode apc
			// set RundownRoutine to NULL
			// ProcessorMode to UserMode
			// NormalRoutine to user-mdoe routine address
			//
			KeInitializeApc(
				apc,   //apc
				ethread,               //thread set the target thread obejct ,PsLookupThreadByThradId
				OriginalApcEnvironment,               //Enviroment
				KernelRoutine,               //KernelRoutine
				NULL,//RundownRoutine,               //RundownRoutine
				(PKNORMAL_ROUTINE)address,               //NormalRoutine
				UserMode,         //ProcessMode,specify under which mode the NormalRoutine will run
				NULL);              //NormalContext

			//insert apc into apc list
			DbgPrint("[%s]inserting user-mode apc!\n", __FUNCTION__);
			KeInsertQueueApc(
				apc,               //apc
			    NULL,                        //SystemArguemnt1,which can be used in KernelRoutine and NormalRoutine
				NULL,                           //SystemArgument2,which can be used in KernelRoutine and NormalRoutine
				IO_NO_INCREMENT                 //Increment
			);
		}
	}
}

//kernel mode 

//using KernelMode apc

void NTAPI KernelRoutine(PKAPC apc,
	PKNORMAL_ROUTINE* NormalRoutine,
	PVOID* NormalContext,
	PVOID* SystemArgument1, 
	PVOID* SystemArgument2)
{
	UNREFERENCED_PARAMETER(NormalRoutine);
	UNREFERENCED_PARAMETER(NormalContext);
	UNREFERENCED_PARAMETER(SystemArgument1);
	UNREFERENCED_PARAMETER(SystemArgument2);

	ExFreePool(apc);
}

void NTAPI RundownRoutine(PRKAPC apc)
{
	ExFreePool(apc);
}

void NTAPI NormalRoutine(PVOID NormalContext,
	PVOID SystemArgument1,
	PVOID SystemArgument2)
{
	UNREFERENCED_PARAMETER(NormalContext);
	UNREFERENCED_PARAMETER(SystemArgument1);
	UNREFERENCED_PARAMETER(SystemArgument2);

	//to do something
	DbgPrint("%s", (char*)SystemArgument1);
}

VOID TestKernelApc()
{
	PRKAPC apc =NULL;
	CHAR RawData[0x100] = "in apc NormalRoutine";

	//check the apcs condition
	if (KeAreAllApcsDisabled()==FALSE)
	{
		//we can use apcs
		apc = (PRKAPC)ExAllocatePool(NonPagedPool, sizeof(KAPC));
		if (apc)
		{
			//initialize apc structure
			//
			// for user mode apc
			// set RundownRoutine to NULL
			// ProcessorMode to UserMode
			// NormalRoutine to user-mdoe routine address
			//
			KeInitializeApc(apc,   //apc
				PsGetCurrentThread(),               //thread set the target thread obejct ,PsLookupThreadByThradId
				OriginalApcEnvironment,               //Enviroment
				KernelRoutine,               //KernelRoutine
				RundownRoutine,               //RundownRoutine
				NormalRoutine,               //NormalRoutine
				KernelMode,         //ProcessMode,specify under which mode the NormalRoutine will run
				NULL);              //NormalContext

			//insert apc into apc list
			DbgPrint("[%s]inserting apc!\n",__FUNCTION__);
			KeInsertQueueApc(apc,               //apc
				RawData,                        //SystemArguemnt1,which can be used in KernelRoutine and NormalRoutine
				NULL,                           //SystemArgument2,which can be used in KernelRoutine and NormalRoutine
				IO_NO_INCREMENT                 //Increment
			);
		}
	}
	
}

### MPO APC 光纤连接器技术概述 MPO(Multi-fiber Push-On)光纤连接器是一种多芯光纤连接器,广泛应用于高密度光纤网络中。APC(Angled Physical Contact)则指光纤端面以一定角度抛光,以减少反射和提高信号质量。以下是关于 MPO APC 光纤连接器及其差异的详细说明: #### 1. **MPO APC 的定义与特点** MPO APC 是一种结合了 MPO 连接器结构和 APC 抛光工艺的光纤连接器。其主要特点是通过将光纤端面以 8° 的角度抛光[^2],从而显著降低后向反射率,适用于对反射敏感的应用场景,例如射频传输或长距离光纤通信。 - **低反射率**:由于端面倾斜设计,反射光不会直接返回光源,从而减少了干扰。 - **高性能应用**:特别适合于需要低反射、高稳定性的系统,如相干光通信或测试设备。 #### 2. **MPO APC 与 MPO UPC 的区别** MPO UPC(Ultra Physical Contact)是另一种常见的 MPO 连接器类型,其端面为平面抛光。以下是两者的主要区别: | 参数 | MPO APC | MPO UPC | |-----------------|---------------------------------------|--------------------------------------| | **端面抛光** | 8° 角度抛光 | 平面抛光 | | **反射率** | 极低(< -60 dB) | 较低(-40 dB 到 -55 dB) | | **应用场景** | 高性能、低反射需求 | 普通数据传输 | | **颜色标识** | 通常为绿色 | 通常为蓝色 | MPO APC 的反射率更低,因此在对反射敏感的环境中表现更优。而 MPO UPC 更适合一般的数据中心或局域网应用。 #### 3. **MPO APC 的优势** - **降低信号衰减**:通过减少反射,MPO APC 可以提供更稳定的信号传输。 - **支持高速率传输**:适用于 40G、100G 甚至更高带宽的网络环境。 - **兼容性**:虽然 MPO APC 和 MPO UPC 不可互配,但它们都遵循相同的 MPO 标准框架,便于设计和维护。 #### 4. **技术挑战与注意事项** - **安装复杂性**:由于 APC 的角度抛光设计,安装时需要更高的精度,以避免对准误差。 - **成本较高**:相比 MPO UPC,MPO APC 的制造工艺更复杂,导致成本增加。 - **不可混用**:MPO APC 和 MPO UPC 不可以直接连接,否则可能导致损坏或性能下降。 ```python # 示例代码:检查 MPO APC 和 MPO UPC 的兼容性 def check_compatibility(connector_type_1, connector_type_2): if connector_type_1 == "MPO APC" and connector_type_2 == "MPO UPC": return "Incompatible: MPO APC and MPO UPC cannot be directly connected." elif connector_type_1 == connector_type_2: return "Compatible: Connectors are of the same type." else: return "Unknown compatibility." print(check_compatibility("MPO APC", "MPO UPC")) ``` ### 结论 MPO APC 是一种高性能的光纤连接器,具有低反射率和高稳定性,适用于对信号质量要求极高的场景。然而,其较高的成本和安装复杂性需要在实际应用中加以权衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值