[2021]PatchGuard的 542875F90F9B47F497B64BA219CACF69 回调研究(一) huoji PG,pathgurad 2021-06-28 1447 次浏览 26 次点赞 在研究/看别人研究PG的时候,我注意到了在KiFilterFiberContext函数里面的一个 ![](https://key08.com/usr/uploads/2021/06/652292203.png) 这个操作 在 [windows10 patchguard 分析研究(版本:1803)](https://key08.com/index.php/2021/01/09/880.html "windows10 patchguard 分析研究(版本:1803)") 中,他们说这个是注册了一个 "TV" 回调 实际上是错误的,这个 "TV"字符串只是用于填充 ObjectAttributes.ObjectName这个字段的unicode_string,真实名字应该叫做"542875F90F9B47F497B64BA219CACF69",仅此而已 如果你想跟进sub_140386010 你就错了,**这个函数是一个巨大的、不可名状的、超过1W+行代码**的PG检查函数(10990行代码),至于是什么我们会在下文说,先关心一下这个注册了什么回调: 根据 https://github.com/huoji120/ExecutiveCallbackObjects/tree/master/542875F90F9B47F497B64BA219CACF69 的说法,这个注册的对象是mssecflt.sys的SecKernelIntegrityCallback ![](https://key08.com/usr/uploads/2021/06/204554973.png) 按照之前在ntoskrnl的信息,标注上符号 ![](https://key08.com/usr/uploads/2021/06/4245838023.png) 查看引用,发现确实存在之前的"TV"也就是542875F90F9B47F497B64BA219CACF69的注册信息 ![](https://key08.com/usr/uploads/2021/06/879608837.png) 本文本部分会粗略介绍这个回调的几个启动方式. ### 线程启动 查看参数1的引用 ![](https://key08.com/usr/uploads/2021/06/430710226.png) 让我们从SecDeferredIntegrityCheck开始: ![](https://key08.com/usr/uploads/2021/06/650708944.png) 而这个SecDeferredIntegrityCheck是被SecKernelIntegrityImmediateCheck所引用的 ![](https://key08.com/usr/uploads/2021/06/4047501455.png) 而SecKernelIntegrityImmediateCheck只是PG众多函数的一员,他是在SecCreateThreadNotifyRoutine中引用的,而这个SecCreateThreadNotifyRoutine是系统注册的一个createthread回调**(也就是说当有线程创建的时候,这个PG Context就会attach到线程里面,究极套娃)** 此外你也看到,他有三种不同的方式 ![](https://key08.com/usr/uploads/2021/06/745915253.png) 回到SecKernelIntegrityImmediateCheck,他首先会执行疑似解密PG的context操作 ![](https://key08.com/usr/uploads/2021/06/1841155672.png) 解密完成后,给当前线程插入一个APC通过 KeInitializeApc参数 ![](https://key08.com/usr/uploads/2021/06/4167421315.png) 回头看SecDeferredIntegrityCheck: ![](https://key08.com/usr/uploads/2021/06/654258217.png) ## FltCreateCommunicationPort 在mssecflt.sys创建的时候,会调用这个FltCreateCommunicationPort创建一个minifilter端口 ![](https://key08.com/usr/uploads/2021/06/1675273092.png) 当通讯id为一个指定的数值的时候(具体是啥我懒得算了) ![](https://key08.com/usr/uploads/2021/06/995854157.png) 就会调用这个SecDeferredIntegrityCheck **这个minifilter port可以被用于R3/R0手动触发PG测试** 也许是windows defender或者其他的系统组件会手动触发测试这个pg的呢 ## 定时器 另外一个定时器就比较套娃了,通过ExAllocateTimer来做定时器: 简单介绍一下 DriverEntry -> SecDeviceOpen(SecCreateDeviceObject)/SecRegistrationWatchdogInitializeSec(PsCreateSystemThread) -> SecIntilization > SecDetInitialize -> SecDetInitializeTimers-> SecDetTimerCallback(ExAllocateTimer) -> SecDetTimerPerformDeferredAssertions(IoQueueWorkItem) -> SecDetTimerPerformDeferredAssertionsImpl -> SecKernelIntegrityPeriodicCheck->SecKernelIntegrityCheck->PG回调 此外这个SecRegistrationWatchdogInitializeSec,是一个系统线程来着 ![](https://key08.com/usr/uploads/2021/06/728169484.png) 然后初始化的时候又注册一个(因此他不仅仅只有一个PG context,是有多个) ## 句柄回调(obregcallback) 在SecObAddCallback(这个被driver entry调用)函数中,他会注册线程、进程两个句柄回调: ![](https://key08.com/usr/uploads/2021/06/4000034757.png) 无论如何,最终走向SecDetPerformProcessAssertionsWithContextImpl,到最后也是pg的回调. ![](https://key08.com/usr/uploads/2021/06/1724536639.png) 下一文章将会讲述这个回调到底在干什么, 未完待续... 本文由 huoji 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。 点赞 26
还不快抢沙发