[转](15)TSS,TR寄存器,TSS描述符,任务段跳转实验

 

一、TSS,TR寄存器,TSS门描述符的关系

 

首先,通过下图,了解 TSS,TR寄存器,TSS描述符的关系:

 

在这里插入图片描述

 

TSS(Task-state segment)是一块104字节的内存,用于存储大部分寄存器的值;

 

在这里插入图片描述
TSS设计出来的目的是任务切换,或者说是一次性替换一大堆寄存器。

 

TR寄存器存储了TSS的地址,大小,和TSS门描述符选择子;
在这里插入图片描述

 

TSS描述符是GDT表中的一项,操作系统启动时,从门描述符取值初始化TR寄存器。

 

在这里插入图片描述

eq 8003f048    xx00e9xx xxxx0068 

其中 xx xx xxxx 32个字节是TSS内存的地址  也就是自己申请的地址

00e9是 0000 0000 1110 1001 第二个0000 Limit 是(104个字节)起始地址

0068是 segment Limit   =104个字节

发现这里的DPL是3  下面代码0048 或者004B  CPL=0 还是 CPL=3 都可以成功 

测试 e=1110   DPL必须为3时  才可以正常执行  是可以提权读取高2G

说明提权0环是由TTS赋值的CS  SS决定  和DPL CPL无关   

测试将TTS CS  SS  设置成3环的  能不能读取高两G  系统崩溃!

 

二、LTR STR 指令

 

LTR 指令是0环指令,格式如下:

 

mov ax,SelectorTSS
ltr ax

 

执行该指令,从GDT表取TSS描述符填充TR寄存器,但并不会修改其他寄存器。
执行指令后,TSS描述符TYPE域低2位会置1. (1001->1011)当成功将 TSS descriptor 加载到 TR 寄存器后,processor 将 available TSS 置为 busy TSS,在返回时将 busy TSS 置为 available TSS。

 

STR 指令只会读取 TR 的16位选择子部分,该指令没有特权要求。指令格式如下:

 

str ax

 

三、TSS

 

在这里插入图片描述

 

Windows只使用了TSS的SS0和ESP0,用于权限切换。
TSS这个东西是Intel设计出来做任务切换的,windows和linux都没有使用任务,而是自己实现了线程。在windows中,TSS唯一的作用就是权限切换时要用到SS0和ESP0,又或者这样理解,TSS就是用来一次性替换一堆寄存器的。
现在我们也知道了为啥之前的课上老师说过windows没有使用LDT表,因为LDT是任务切换用的,一个任务一个LDT表。

 

四、课后练习

 

1、使用CALL去访问一个任务段,并能够正确返回。
2、使用JMP去访问一个任务段,并能够正确返回。

 

使用CALL FAR 和JMP FAR 都可以访问任务段,有两点区别:
使用 CALL FAR 方式,EFLAGS 的 NT位置1,而JMP FAR 方式 NT位=0;
CPU根据NT位决定返回方式,如果NT=1,CPU使用TSS的 Previous task link 里存储的上一个任务的TSS选择子进行返回;如果NT=0,则使用堆栈中的值返回。

 


 

下面是两个练习题的实验步骤。这个作业卡了好久,有不少坑点,先在此说明:
坑点1:INT 3 会修改FS寄存器,所以使用 INT 3 必须先保存FS的值。
坑点2:TSS可以使用数组,也可以VirtualAlloc,建议后者,因为TSS最好是页对齐的。
坑点3:定义局部数组作为堆栈,传给TSS[14]时,应该传数组尾部的指针,因为压栈ESP减小,如果传数组首地址,那一压栈就越界了。
坑点4:JMP FAR 方式切换任务并不能提权,返回时要用先前保存的TR寄存器的值(原TSS选择子)返回。

 

补充一段Intel白皮书对于JMP 任务切换的描述:

 

(The JMP instruction cannot be used to perform inter-privilege-level
far jumps.) Executing a task switch with the JMP instruction is
somewhat similar to executing a jump through a call gate. Here the
target operand specifies the segment selector of the task gate for the
task being switched to (and the offset part of the target operand is
ignored). The task gate in turn points to the TSS for the task, which
contains the segment selectors for the task’s code and stack segments.
The TSS also contains the EIP value for the next instruc- tion that
was to be executed before the task was suspended. This instruction
pointer value is loaded into the EIP register so that the task begins
executing again at this next instruction. The JMP instruction can also
specify the segment selector of the TSS directly, which eliminates the
indirection of the task gate. See Chapter 7 in Intel® 64 and IA-32
Architectures Software Developer’s Manual, Volume 3A, for detailed
information on the mechanics of a task switch.

tr 寄存器表示当前的任务段,我们可以使用windbg 的 dg指令解析该段

可以看到当前的TR寄存器选择子是 0028

 3. TSS的作用(在Windows操作系统上)
    线程切换时,一次保存多个寄存器。
    注意:是线程切换,不是进程切换,进程切换还要替换整个环境远不止寄存器那么多!!

4. 如何查看TSS段内的内容
    TSS的地址就是TSS段描述符描述的基地址,因此我们通过 dg tr 查看其Base为 80042000。
    对于查看TSS段,有一个单独的指令。
    dt _KTSS base_address
    效果如下图(此时没用到,之后线程切换会有新的效果)

  5. 构造tss门并进行提权
  1)实验代码

// test.cpp : Defines the entry point for the console application.
        //
        
        #include "stdafx.h"
        #include "windows.h"
        
        DWORD iTSS[26];    //104个字节
        DWORD ESP0[0x1000];
        DWORD ESP3[0x1000];
        
        DWORD dwESP;
        DWORD dwCS;
        DWORD dwCR3;
        
        _declspec(naked) void Call(){
            _asm{
            
                mov dwESP,ESP;    //读取ESP
                mov ax,cs;
                mov dwCS,eax;    //读取cs
        
                iretd;
            }
        }
        int main(int argc, char* argv[])
        {
            memset(iTSS,0,sizeof(iTSS));
            memset(ESP0,0,sizeof(ESP0));
            memset(ESP3,0,sizeof(ESP3));
        
            dwESP = 0;
            dwCS = 0;
            dwCR3 = 0;
        
            iTSS[1] = (DWORD)(ESP0+0x900);    // ESP
            iTSS[2] = 0x10;                    // SS0
            iTSS[8] = (DWORD)Call;            // EIP
            iTSS[14] = (DWORD)(ESP3+0x900); // ESP3
            iTSS[18] = 0x23;    // ES  
            iTSS[19] = 0x08;    // CS  CPL=0
            iTSS[20] = 0x10;    // SS  
            iTSS[21] = 0x23;    // DS
            iTSS[22] = 0x30;    // FS
        
            printf("iTSS:%x ESP3:%x ESP0:%x\n",iTSS,(ESP3+0x900),(ESP0+0x900));
            printf("input cr3:");
            scanf("%x",&dwCR3);
            iTSS[7] = dwCR3;        // cr3
        
            char buf[6] = {0,0,0,0,0x48,0};
            _asm{
                call fword ptr buf;
            }
            printf("ESP:%x CS:%x\n",dwESP,dwCS);
            return 0;
            return 0;
}

  2)查看TSS的内存地址,然后来修改任务门的偏移量(注意之前为函数地址,但任务门是一次性替换全部寄存器,因此函数地址在tss的eip中)。

  0x48偏移处构造,windbg指令 eq 8003f048 0000e943`2c680068

  3)使用 !process 0 0 找到对应的CR3(物理页处),然后输入进去。

4)可以看到其直接使用了构造的esp3(虽然我们权限是零环,但它并没有使用esp0)。

 

 int 3 断下时,如果不恢复eflags寄存器,其就会卡死。
    因为当进行TSS跳转时,其会将老的TSS保存在新的TSS头部(上面我们看到),当我们使用iretd返回时,其不是像中断那样根据返回地址,而是根据TSS段选择子找旧的TSS段内存,然后将里面的寄存器全部加载进去。
    而INT 3 会清空 VM、NT、IF、TF四个位,其中NT表示嵌套任务段(nested task),如果清空,其就认为不存在任务段嵌套,直接像常规那样,根据返回地址返回,此时就会出错。

 

下面是代码和执行结果,我会在代码里输出需要执行的指令。
1、使用CALL去访问一个任务段,并能够正确返回。

 

// TSS.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

DWORD dwOk;
DWORD dwESP;
DWORD dwCS;


// 任务切换后的EIP
void __declspec(naked) R0Func()
{
	__asm
	{
		pushad
		pushfd

		push fs
		int 3 // int 3 会修改FS
		pop fs

		mov eax,1
		mov dword ptr ds:[dwOk],eax
		mov eax,esp
		mov dword ptr ds:[dwESP],eax
		mov ax,cs
		mov word ptr ds:[dwCS],ax

		popfd
		popad
		iretd
	}
}

int _tmain(int argc, _TCHAR* argv[])
{	
	DWORD dwCr3; // windbg获取
	char esp[0x1000]; // 任务切换后的栈,数组名就是ESP
	
	// 此数组的地址就是TSS描述符中的Base
	DWORD *TSS = (DWORD*)VirtualAlloc(NULL,104,MEM_COMMIT,PAGE_READWRITE);
	if (TSS == NULL)
	{
		printf("VirtualAlloc 失败,%d\n", GetLastError());
		getchar();
		return -1;
	}
	printf("请在windbg执行: eq 8003f048 %02x00e9%02x`%04x0068\n", ((DWORD)TSS>>24) & 0x000000FF,((DWORD)TSS>>16) & 0x000000FF, (WORD)TSS);
	printf("请在windbg中执行!process 0 0,复制TSS.exe进程DirBase的值,并输入.\nCR3: "); // 在windbg中执行 !process 0 0 获取,DirBase: 13600420  这个数要启动程序后现查
	scanf("%x", &dwCr3); // 注意是%x
	
	TSS[0] = 0x00000000; // Previous Task Link CPU填充,表示上一个任务的选择子
	TSS[1] = 0x00000000; // ESP0
	TSS[2] = 0x00000000; // SS0
	TSS[3] = 0x00000000; // ESP1
	TSS[4] = 0x00000000; // SS1
	TSS[5] = 0x00000000; // ESP2
	TSS[6] = 0x00000000; // SS2
	TSS[7] = dwCr3; // CR3 学到页就知道是啥了
	TSS[8] = (DWORD)R0Func; // EIP
	TSS[9] = 0x00000000; // EFLAGS
	TSS[10] = 0x00000000; // EAX
	TSS[11] = 0x00000000; // ECX
	TSS[12] = 0x00000000; // EDX
	TSS[13] = 0x00000000; // EBX
	TSS[14] = (DWORD)esp+0x900; // ESP,解释:esp是一个0x1000的字节数组,作为裸函数的栈,这里传进去的应该是高地址,压栈才不会越界
	TSS[15] = 0x00000000; // EBP
	TSS[16] = 0x00000000; // ESI
	TSS[17] = 0x00000000; // EDI
	TSS[18] = 0x00000023; // ES
	TSS[19] = 0x00000008; // CS 0x0000001B
	TSS[20] = 0x00000010; // SS 0x00000023
	TSS[21] = 0x00000023; // DS
	TSS[22] = 0x00000030; // FS 0x0000003B
	TSS[23] = 0x00000000; // GS
	TSS[24] = 0x00000000; // LDT Segment Selector
	TSS[25] = 0x20ac0000; // I/O Map Base Address

	char buff[6] = {0,0,0,0,0x48,0};	
	__asm
	{
		call fword ptr[buff]
	}
	printf("ok: %d\nESP: %x\nCS: %x\n", dwOk, dwESP, dwCS);

	return 0;
}

 

执行结果如图:

 

在这里插入图片描述

 

2、使用JMP去访问一个任务段,并能够正确返回。
和CALL FAR对比,NT位不会置1,TSS previous task link 也不会填充旧的TR,因此想要返回,可以先保存旧的TR,然后JMP FAR回去。

 

// TSS.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

DWORD dwOk;
DWORD dwESP;
DWORD dwCS;

BYTE PrevTr[6]; // 旧TR,供裸函数返回

// 任务切换后的EIP
void __declspec(naked) R3Func()
{
	__asm
	{
		pushad
		pushfd

		push fs
		int 3 // int 3 会修改FS
		pop fs

		mov eax,1
		mov dword ptr ds:[dwOk],eax
		mov eax,esp
		mov dword ptr ds:[dwESP],eax
		mov ax,cs
		mov word ptr ds:[dwCS],ax

		popfd
		popad
		
		jmp fword ptr ds:[PrevTr]
	}
}

int _tmain(int argc, _TCHAR* argv[])
{	
	DWORD dwCr3; // windbg获取
	char esp[0x1000]; // 任务切换后的栈,数组名就是ESP
	
	// 此数组的地址就是TSS描述符中的Base
	DWORD *TSS = (DWORD*)VirtualAlloc(NULL,104,MEM_COMMIT,PAGE_READWRITE);
	if (TSS == NULL)
	{
		printf("VirtualAlloc 失败,%d\n", GetLastError());
		getchar();
		return -1;
	}
	printf("请在windbg执行: eq 8003f048 %02x00e9%02x`%04x0068\n", ((DWORD)TSS>>24) & 0x000000FF,((DWORD)TSS>>16) & 0x000000FF, (WORD)TSS);
	printf("请在windbg中执行!process 0 0,复制TSS.exe进程DirBase的值,并输入.\nCR3: "); // 在windbg中执行 !process 0 0 获取,DirBase: 13600420  这个数要启动程序后现查
	scanf("%x", &dwCr3); // 注意是%x
	
	TSS[0] = 0x00000000; // Previous Task Link CPU填充,表示上一个任务的选择子
	TSS[1] = 0x00000000; // ESP0
	TSS[2] = 0x00000000; // SS0
	TSS[3] = 0x00000000; // ESP1
	TSS[4] = 0x00000000; // SS1
	TSS[5] = 0x00000000; // ESP2
	TSS[6] = 0x00000000; // SS2
	TSS[7] = dwCr3; // CR3 学到页就知道是啥了
	TSS[8] = (DWORD)R3Func; // EIP
	TSS[9] = 0x00000000; // EFLAGS
	TSS[10] = 0x00000000; // EAX
	TSS[11] = 0x00000000; // ECX
	TSS[12] = 0x00000000; // EDX
	TSS[13] = 0x00000000; // EBX
	TSS[14] = (DWORD)esp+0x900; // ESP,解释:esp是一个0x1000的字节数组,作为裸函数的栈,这里传进去的应该是高地址,压栈才不会越界
	TSS[15] = 0x00000000; // EBP
	TSS[16] = 0x00000000; // ESI
	TSS[17] = 0x00000000; // EDI
	TSS[18] = 0x00000023; // ES
	TSS[19] = 0x00000008; // CS 0x0000001B
	TSS[20] = 0x00000010; // SS 0x00000023
	TSS[21] = 0x00000023; // DS
	TSS[22] = 0x00000030; // FS 0x0000003B
	TSS[23] = 0x00000000; // GS
	TSS[24] = 0x00000000; // LDT Segment Selector
	TSS[25] = 0x20ac0000; // I/O Map Base Address

	char buff[6] = {0,0,0,0,0x48,0};	
	__asm
	{
		str ax
		lea edi,[PrevTr+4]
		mov [edi],ax
		
		jmp fword ptr[buff]
	}
	printf("ok: %d\nESP: %x\nCS: %x\n", dwOk, dwESP, dwCS);

	return 0;
}

 

在这里插入图片描述


---------------------
作者:hambaga
来源:CSDN
原文:https://blog.csdn.net/Kwansy/article/details/108890586
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值