Win10和Win7驱动_security_init_cookie蓝屏兼容问题
2021-07-01
1593
1
今天使用VS2019+WDK10写了一个简单的64驱动,在Win7系统下安装时出现了蓝屏问题。
使用windbg进行栈回溯,发现出现蓝屏的方法是发生在调用驱动入口函数DriverEntry之前。
RetAddr : Call Site
fffff880`030ba015 : bytekitsdrv!__security_init_cookie+0x2d
fffff800`042ae467 : bytekitsdrv!GsDriverEntry+0x15 [minkernel\tools\gs_support\kmodefastfail\gs_driverentry.c @ 46]
fffff800`042ae865 : nt!EtwWriteStartScenario+0x2267
fffff800`03ed3a21 : nt!EtwWriteStartScenario+0x2665
fffff800`04166cce : nt!KeAcquireInStackQueuedSpinLockAtDpcLevel+0x2f1
fffff800`03ebafe6 : nt!PsCreateSystemThread+0x1da
00000000`00000000 : nt!KeInitializeSemaphore+0x24a
可以看到,出现蓝屏的方法是GsDriverEntry函数内部调用security_init_cookie时,即初始化cookie时。
使用windbg对security_init_cookie函数进行反汇编。
kd> u bytekitsdrv!__security_init_cookie l15
bytekitsdrv!__security_init_cookie [minkernel\tools\gs_support\kmodefastfail\gs_support.c @ 37]:
fffff880`030ba02c 488b05cddfffff mov rax,qword ptr [bytekitsdrv!__security_cookie (fffff880`030b8000)]
fffff880`030ba033 4885c0 test rax,rax
fffff880`030ba036 741a je bytekitsdrv!__security_init_cookie+0x26 (fffff880`030ba052)
fffff880`030ba038 48b932a2df2d992b0000 mov rcx,2B992DDFA232h
fffff880`030ba042 483bc1 cmp rax,rcx
fffff880`030ba045 740b je bytekitsdrv!__security_init_cookie+0x26 (fffff880`030ba052)
fffff880`030ba047 48f7d0 not rax
fffff880`030ba04a 488905b7dfffff mov qword ptr [bytekitsdrv!__security_cookie_complement (fffff880`030b8008)],rax
fffff880`030ba051 c3 ret
fffff880`030ba052 b906000000 mov ecx,6
fffff880`030ba057 cd29 int 29h
fffff880`030ba059 cc int 3
fffff880`030ba05a cc int 3
fffff880`030ba05b cc int 3
查看蓝屏的地址:
kd> u bytekitsdrv!__security_init_cookie+0x2d l1
bytekitsdrv!__security_init_cookie+0x2d:
fffff880`030ba059 cc int 3
kd> u bytekitsdrv!__security_init_cookie+0x26 l1
bytekitsdrv!__security_init_cookie+0x26 [minkernel\tools\gs_support\kmodefastfail\gs_support.c @ 46]:
fffff880`030ba052 b906000000 mov ecx,6
对照 bytekitsdrv!_security_init_cookie函数,发现程序执行了
fffff880`030ba052 b906000000 mov ecx,6
fffff880`030ba057 cd29 int 29h
fffff880`030ba059 cc int 3
fffff880`030ba05a cc int 3
fffff880`030ba05b cc int 3
蓝屏,而引起这段代码的运行是因为
fffff880`030ba02c 488b05cddfffff mov rax,qword ptr [bytekitsdrv!__security_cookie (fffff880`030b8000)]
fffff880`030ba033 4885c0 test rax,rax
fffff880`030ba036 741a je bytekitsdrv!__security_init_cookie+0x26 (fffff880`030ba052)
fffff880`030ba038 48b932a2df2d992b0000 mov rcx,2B992DDFA232h
fffff880`030ba042 483bc1 cmp rax,rcx
fffff880`030ba045 740b je bytekitsdrv!__security_init_cookie+0x26
引起,这段代码发现是进行了2个值的判断,如果有任意一个判断成功,均跳到 bytekitsdrv!_security_init_cookie+0x26处进行蓝屏。
这里的代码判断的是qword ptr [bytekitsdrv!_security_cookie (fffff880 030b8000)]处的值是否为0,或者为2B992DDFA232h,通过windbg发现满足值等于2B992DDFA232h,所以产生蓝屏。
kd> dq fffff880`030b8000
fffff880`030b8000 00002b99`2ddfa232 ffffd466`d2205dcd
原因分析
通过函数名称可以知道,这个蓝屏的原因是cookie判断引起的,所以基本上确定是由于平台兼容性引起的,即使用wdk10编译出的驱动在win7平台下是存在兼容问题的。
解决方法
使用 Visual Studio:
使用记事本或其他文本编辑器打开驱动程序项目文件 (*.vcxproj)。 在项目文件中,找到驱动程序支持的配置的
<KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
如我们这里对x64和debug和release进行配置
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<TargetVersion>Windows10</TargetVersion>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset>
<ConfigurationType>Driver</ConfigurationType>
<DriverType>WDM</DriverType>
<SpectreMitigation>false</SpectreMitigation>
<KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<TargetVersion>Windows10</TargetVersion>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset>
<ConfigurationType>Driver</ConfigurationType>
<DriverType>WDM</DriverType>
<SpectreMitigation>false</SpectreMitigation>
<KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
</PropertyGroup>