1 Undocumented Windows NT中通过callgate实现的无驱动进如ring0的代码中的两个宏汇编代码
直接仿制于NT系统代码(_KiSystemService),随NT版本而变。
Ring0Prolog macro
PUSHAD
PUSHFD
PUSH FS
;FS:0即指向FFDFF000h这个重要结构,用户态与核心态的FS值不同,下面是例行公事而已
MOV EBX,00000030h
MOV FS,BX
SUB ESP, 50h
MOV EBP,ESP
;Setup the exception frame to NULL
MOV EBX,DWORD PTR CS:[0FFDFF000h]
MOV DWORD PTR DS:[0FFDFF000h], 0FFFFFFFFh
MOV DWORD PTR [EBP],EBX
;CS:[FFDFF124h]存有大家再熟悉不过的线程核心块KTHREAD,其中偏移128h处为TrapFrame
;Save away the existing KSS EBP
MOV ESI, DWORD PTR CS:[0FFDFF124h]
MOV EBX,DWORD PTR [ESI+00000128h]
MOV DWORD PTR [EBP+4h],EBX
MOV DWORD PTR [ESI+00000128h],EBP
;块偏移137h处为PreviousMode,简单说供核心函数区分是用户态还是核心态请求,直接决定了某些函数调
;用的成功与否。137与上面的128都是nt上的偏移,2000/XP下是不同的,所以这段代码平台相关
;Save away the kernel time and the thread mode (kernel/user)
MOV EDI,DWORD PTR [ESI+00000137h]
MOV DWORD PTR [EBP+8h],EDI
;Set the thread mode (kernel/user) based on the code selector
MOV EBX,DWORD PTR [EBP+7Ch]
AND EBX,01
MOV BYTE PTR [ESI+00000137h],BL
STI
endm
Ring0Epilog macro
;Restore the KSS EBP
MOV ESI,DWORD PTR CS:[0FFDFF124h]
MOV EBX,DWORD PTR [EBP+4]
MOV DWORD PTR [ESI+00000128h],EBX
;Restore the exception frame
MOV EBX,DWORD PTR [EBP]
MOV DWORD PTR FS:[00000000],EBX
;Restore the thread mode
MOV EBX,DWORD PTR [EBP+8h]
MOV ESI,DWORD PTR FS:[00000124h]
MOV BYTE PTR [ESI+00000137h],BL
ADD ESP, 50h
POP FS
POPFD
POPAD
endm
EPROCESS 的开头部分就是KPROCESS,名字上的所区别可以表明二者的主要定义与使用者的不同:K***意为微内核使用(kernel),在调度等代码使用,所需结构简单,只要一部分;E***意为执行体使用,需要额外的部分,比如活动进程链供任务枚举。KTHREAD、ETHREAD类似。至于 ETHREAD、EPROCESS的联系有结构定义很容易看到,还有TEB/PEB,ETHREAD/EPROCESS等。
2 关于nt内存管理
//PDE-PTE(和硬件页表项的格式相同),页目录的自映射问题告诉我们PDE与PTE的格式相同!
ULONG LinearAddressToPhysicalAddress(ULONG lAddress)
{
unsigned int *pAddr;
unsigned int *PageDirectoryEntry=(unsigned int *)0xC0300000;
unsigned int *PageTableEntry=(unsigned int *)0xC0000000;
if((!(PageDirectoryEntry[lAddress>>22]&0xFFFFF000))
&&(!(PageDirectoryEntry[lAddress>>22]&0x00000001)))
return 0;
pAddr=(unsigned int *)((int)PageTableEntry+((lAddress&0xFFFFF000)>>10));
if((*pAddr)&1)
return ((*pAddr) &0xFFFFF000) |(lAddress&0x00000FFF);
return 0;
}
3 /Device/PhysicalMemory对象
/Device/PhysicalMemory对象有下面的权限:
user SYSTEM: Delete, Change Permissions, Change Owner, Query Data,
Query State, Modify State
user Administrator: Query Data, Query State
详见:http://www.xfocus.net/articles/200208/430.html