【梅哥的Ring0湿润插入教程】第二课:Ring0实现读写SSDT
【梅哥的Ring0湿润插入教程】
Email:mlkui@163.com 转载请注明出处,谢绝喷子记者等,如引起各类不适请自觉滚J8蛋!
第二课:Ring0实现读写SSDT
随着驱动保护技术的逐步成熟,诸如网络游戏公司等越来越多的商业软件公司开始使用Ring0级保护技术保护自己的产品,以起到反用户级调试、反RootKit、反各类钩子、反各类远程注入等作用。目前,使用驱动保护技术的代表产品主要有上海盛大网络发展有限公司开发的GPK(Game Protect Kit)、深圳市腾讯计算机系统有限公司开发的TenSafe及韩国neople棒子公司开发的nProtect等。显然,进入到Ring0级进行某些操作是应对这些驱动保护技术的较好办法之一,而运行于核心态即Ring0级的Windows设备驱动程序则可以说是进入到Ring0的唯一方法。
微软在其WDK,即所谓Windows Driver Kit的官方文档《Getting Started with the Windows Driver Development Environment》一文中开篇指出:“Even for experienced developers, getting started with Windows® device drivers can be difficult. You have a new programming model to learn and a new set of tools for building and debugging”。
正如上文所述,Windows内核编程与Win32编程区别较大,其调试方法也与Win32下的调试方法有极大不同;同时,Ring0级的反汇编与Ring3反汇编环境也有不小的差异;而大量计算机专业从业人员在没有点拨的情况下也难以意识到内核编程化御姐为萝莉、化少年为怪蜀黍的威力(以下省略一万字)。正是出于以上及其他的各种原因,梅哥将在学习Windows内核编程的同时写下本系列的Ring0湿润插入大法,旨在分享Windows内核编程经验、并说明Windows内核编程在绕过/破解商业驱动保护软件中的重要地位和神奇作用!
好了,下面就抛开用户级的桎梏,跟梅哥一起开始深入Ring0的湿润之旅吧~
============================我是湿润的昏割线=============================
【读取SSDT】
简而言之,读取SSDT及其相关内容大致步骤可分为引入KeServiceDescriptorTable表、通过ServiceTableBase+偏移读出当前函数地址两步。
正如前文所述,KeServiceDescriptorTable是访问SSDT的关键,它实际上是一个指向如下结构的指针(目前网上大量资料使用extern ULONG定义是错误的,想深究的可以去看WRK):
typedef struct ServiceDescriptorTable_s
{
PVOID ServiceTableBase; // System Service Dispatch Table的基地址
PVOID ServiceCounterTable(0);
unsigned int NumberOfServices; //由ServiceTableBase描述的服务的数目
PVOID ParamTableBase; //SSPT
}ServiceDescriptorTable,*pServiceDescriptorTable;
extern pServiceDescriptorTable KeServiceDescriptorTable;
因而可以直接使用extern pServiceDescriptorTable KeServiceDescriptorTable;访问,有两种方法:
方法1、内联汇编
ULONG SSDT_NtOpenProcess_Addr;
//[[KeServiceDescriptorTable]+0x7A*4]
__asm
{
push eax
mov eax,KeServiceDescriptorTable
mov eax,[eax] //得到System Service Dispatch Table的基地址
imul eax,eax,0x7A
shl eax,2 //[KeServiceDescriptorTable]+0x7A*4
mov eax,[eax] //[[KeServiceDescriptorTable]+0x7A*4]
mov SSDT_NtOpenProcess_Addr,eax
pop eax
}
KdPrint(("SSDT_NtOpenProcess_Addr=%x\n",SSDT_NtOpenProcess_Addr));
方法2:用指针读取
//读取SSDT相关内容
KdPrint(("Now try to get SSDT information...\n"));
KdPrint(("KeServiceDescriptorTable=0x%X\n",KeServiceDescriptorTable));
KdPrint(("The base address of SSDT=0x%X\n",KeServiceDescriptorTable->ServiceTableBase));
KdPrint(("The current address of NtOpenProcess is 0x%X\n",
*(PLONG)((unsigned long)KeServiceDescriptorTable->ServiceTableBase+4*190)));
注意最后一句,将KeServiceDescriptorTable相应偏移处的内容强制转换为指向32位数据的指针,然后使用*进行解引用。而网上大量流传的一种典型不求甚解的常见误解是:
extern unsigned long KeServiceDescriptroTable;
LONG *SSDT_Adr,SSDT_NtOpenProcess_Cur_Addr,t_addr;
t_addr=(LONG)KeServiceDescriptorTable->ServiceTableBase; //此处默认指针操作,呵呵
KdPrint(("当前ServiceTableBase地址为%x\n",t_addr));
SSDT_Adr=(PLONG)(t_addr+0x7A*4);
KdPrint(("当前t_addr+0x7A*4=%x \n",SSDT_Adr));
SSDT_NtOpenProcess_Cur_Addr=*SSDT_Adr;
KdPrint((“SSDT_NtOpenProcess_Cur_Addr地址为%x \n",SSDT_NtOpenProcess_Cur_Addr));
【获得SSDT原始地址】
MmGetSystemRoutineAddress可以用于获得SSDT中函数的原始地址,即This routine returns a pointer to a function specified by SystemRoutineName,其原型为:
PVOID MmGetSystemRoutineAddress(IN PUNICODE_STRING SystemRoutineName);
If the function name can be resolved, the routine returns a pointer to the function. Otherwise, the routine returns NULL. Drivers can use this routine to determine if a routine is available on a specific version of Windows. It can only be used for routines exported by the kernel or HAL, not for any driver-defined routine.显然,利用该函数返回的原始地址与当前SSDT中地址相比及即可判断某函数是否被SSDT HOOK。
【写入SSDT】
由于SSDT所处的特殊地位,系统一般不允许修改SSDT的内容,所以如果直接去写SSDT时将系统将直接蓝屏。解决的方法主要有:
方法1、强写CR0控制寄存器获得SSDT写权限
Windows对内存使用分页管理,在X86架构中主要涉及CR0控制寄存器。在《Intel Architecture Software Developer Manual》第三卷《Basic ArchitectureSystem Programming Guide》中,有如下图:
其中第16位WP为WriteProtection位,该位Inhibits supervisor-level procedures from writing into user-level read-only pages when set; allows supervisor-level procedures to write into user-level read-only pages when clear.因而,当将该位清零时即可实现对保护页面的写入。其汇编实现为:
__asm
{
cli //关中断
push eax //eax入栈保护
mov eax,cr0
and eax,~(0x1<<16)
mov cr0,eax
pop eax
}
显然,在多任务环境下关闭页面写保护是极为危险的,根据网上的说法该方法在多核环境下有极大的不稳定存在。
方法2、利用MDL(标准做法)
偷懒直接放代码:
//恢复SSDT中的条目
NTSTATUS RestoreSSDT(PSSDT pSSDT,INT Index,ULONG DesireAddress)
{
#ifdef USE_MDL
PMDL pMDL_SSDT;
PULONG pMappedSSDT;
//检查SSDT是否有效
if (!pSSDT || !pSSDT->SSDT_Base)
{
KdPrint(("Invalid SSDT!\n"));
return STATUS_UNSUCCESSFUL;
}
//创建相应的MDL
pMDL_SSDT=IoAllocateMdl(pSSDT->SSDT_Base,pSSDT->ServiceCount*4,FALSE,FALSE,NULL);
if (!pMDL_SSDT)
{
KdPrint(("Allocate SSDT MDL faild!\n"));
IoFreeMdl(pMDL_SSDT);
return STATUS_UNSUCCESSFUL;
}
//Initialize the page number array for a buffer that is allocated from non-paged pool
MmBuildMdlForNonPagedPool(pMDL_SSDT);
//Write SSDT
pMDL_SSDT->MdlFlags=pMDL_SSDT->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
//Maps the physical pages that are described by the specified MDL to a virtual address in system address space
pMappedSSDT=(PULONG)MmMapLockedPagesSpecifyCache(pMDL_SSDT,KernelMode,MmNonCached,NULL,FALSE,NormalPagePriority);
if (!pMappedSSDT)
{
KdPrint(("Lock SSDT MDL faild!\n"));
IoFreeMdl(pMDL_SSDT);
return STATUS_UNSUCCESSFUL;
}
//执行Hook操作
KdPrint(("SSDT[%d].0x%X restored to 0x%X",Index,InterlockedExchange(&(pMappedSSDT[Index]),(LONG)DesireAddress),DesireAddress));
//解除锁定
if (pMappedSSDT)
{
KdPrint(("Unmap locked pages!\n"));
MmUnmapLockedPages(pMappedSSDT,pMDL_SSDT);
}
//释放MDL
IoFreeMdl(pMDL_SSDT);
return STATUS_SUCCESS;
#else
return STATUS_SUCCESS;
#endif
}void HookSSDT(PSSDT pSSDT,PLONG TargetAddress,PLONG HookerAddress)
{
#ifdef USE_MDL
PMDL pMDL_SSDT;
PLONG pMappedSSDT;
//创建相应的MDL
if (!pSSDT || !pSSDT->ServiceAddress)
{
KdPrint(("Invalid SSDT!\n"));
return STATUS_UNSUCCESSFUL;
}
pMDL_SSDT=IoAllocateMdl(pSSDT->ServiceAddress,pSSDT->ServiceCount*4,FALSE,FALSE,NULL);
if (!pMDL_SSDT)
{
KdPrint(("Allocate SSDT MDL faild!\n"));
return STATUS_UNSUCCESSFUL;
}
//Initialize the page number array for a buffer that is allocated from non-paged pool
MmBuildMdlForNonPagedPool(pMDL_SSDT);
//Probes the specified virtual memory pages, makes them resident, and locks them in memory
/*__try
{
MmProbeAndLockPages(pMDL_SSDT,KernelMode,IoWriteAccess);
}
__except(STATUS_ACCESS_VIOLATION)
{
KdPrint(("Probe and lock failed!\n"));
return STATUS_UNSUCCESSFUL;
}*/
//Maps the physical pages that are described by the specified MDL to a virtual address in system address space
pMappedSSDT=(PLONG)MmMapLockedPagesSpecifyCache(pMDL_SSDT,KernelMode,MmNonCached,NULL,FALSE,NormalPagePriority);
if (!pMappedSSDT)
{
KdPrint(("Lock SSDT MDL faild!\n"));
return STATUS_UNSUCCESSFUL;
}
//执行Hook操作
//InterlockedExchange();
//解除锁定
if (pMappedSSDT)
{
MmUnmapLockedPages(pMappedSSDT,pMDL_SSDT);
//MmUnlockPages(pMDL_SSDT);
}
//释放MDL
IoFreeMdl(pMDL_SSDT);
return STATUS_SUCCESS;
#else
__asm
{
cli
push eax
mov eax,cr0
and eax,~0x1000;
mov cr0,eax
pop eax
}
return STATUS_SUCCESS;
#endif
}