[windows内核]陷阱门

陷阱门和中断门差不多,都是存在于IDT表中的
在这里插入图片描述
下面是陷阱门的段描述符构造
在这里插入图片描述
虽然陷阱门和中断门差不多,但是具体还是有区别的,区别如下

6.12.1.2
Flag Usage By Exception- or Interrupt-Handler Procedure
When accessing an exception or interrupt handler through either an interrupt gate or a trap gate, the processor
clears the TF flag in the EFLAGS register after it saves the contents of the EFLAGS register on the stack. (On calls
to exception and interrupt handlers, the processor also clears the VM, RF, and NT flags in the EFLAGS register, after
they are saved on the stack.) Clearing the TF flag prevents instruction tracing from affecting interrupt response. A
subsequent IRET instruction restores the TF (and VM, RF, and NT) flags to the values in the saved contents of the
EFLAGS register on the stack.
The only difference between an interrupt gate and a trap gate is the way the processor handles the IF flag in the
EFLAGS register. When accessing an exception- or interrupt-handling procedure through an interrupt gate, the
processor clears the IF flag to prevent other interrupts from interfering with the current interrupt handler. A subsequent IRET instruction restores the IF flag to its value in the saved contents of the EFLAGS register on the stack.
Accessing a handler procedure through a trap gate does not affect the IF flag.

大概翻译一下就是
中断门和陷阱门的唯一区别在于处理器处理 EFLAGS 寄存器的 IF 位的方式。当通过中断门访问异常或中断处理例程时,处理器清除 IF 位,以阻止另外的中断干扰当前的中断处理例程。后继的 IRET 指令用存储在栈中的 EFLAGS 的内容恢复 IF 的值。而通过陷阱门调用处理
例程时,IF 位不受影响。

IF位全称为 Interrupt Flag
这个标志位对CPU的影响就是 IF位为1称为非屏蔽中断,IF位为0称为可屏蔽中断
什么是可屏蔽中断和非屏蔽中断呢

不可屏蔽中断源一旦提出请求,cpu必须无条件响应,而对于可屏蔽中断源的请求,cpu可以响应,也可以不响应。cup一般设置两根中断请求输入线:可屏蔽中断请求INTR(Interrupt Require)和不可屏蔽中断请求NMI(Nonmaskable Interrupt)。对于可屏蔽中断,除了受本身的屏蔽位的控制外,还都要受一个总的控制,即CPU标志寄存器中的中断允许标志位IF(Interrupt Flag)的控制,IF位为1,可以得到CPU的响应,否则,得不到响应。IF位可以有用户控制,指令STI或Turbo c的Enable()函数,将IF位置1(开中断),指令CLI或Turbo_c 的Disable()函数,将IF位清0(关中断)。
典型的非屏蔽中断源的例子是电源掉电,一旦出现,必须立即无条件地响应,否则进行其他任何工作都是没有意义的。
典型的可屏蔽中断源的例子是打印机中断,CPU对打印机中断请求的响应可以快一些,也可以慢一些,因为让打印机等待儿是完全可以的。

下面我们来构造一个陷阱门,来做个实验
假设门地址为0401000h

Offset in Segment 31:16   P   DPL     D        
000000000100 00001110  1111  0000  0000
Segment Selector        Offset in Segment 15:000000 0000 0000 1000‬     ‭0001 0000 0000 0000

在这里插入图片描述
写入如下代码

#include <iostream>
#include <windows.h>

int g_high2G; // 在中断门中读取高2G内存保存进来。
int g_eflagsBefore; // 保存进入中断门前的 EFLAGS 寄存器。
int g_eflagsAfter; // 保存进入中断门里的 EFLAGS 寄存器。
int g_eax; // 待会在中断门里要用到 eax ,先把旧的保存到这里。



//0401000h
void __declspec(naked)GetRegister()
{
	__asm
	{

		/*
		  此时栈结构(当你执行 int 指令的时候,CPU 会自动在0环栈中压入这些值):
			| eip3 | <- esp0
			| cs3  |
			|eflags|
			| esp3 |
			| ss3  |
		 */
		mov g_eax, eax;
		// 保存当前 eflags
		pushfd;
		pop g_eflagsAfter;
		// 保存原始 eflags
		mov eax, [esp + 0x08];
		mov g_eflagsBefore, eax;
		// 读取 0x80b95400 处的值
		mov eax, ds:[0x80b95400];
		mov g_high2G, eax;
		// 恢复 eax
		mov eax, g_eax;

		// 陷阱门返回
		iretd;

	}

}





int main()
{
	
	__asm {
		// 构造的中断门描述符安装在 IDT[20] 这个位置。
		int 0x20;
	}
	printf("0x80b95400: %08x\n", g_high2G);
	printf("进入陷阱门前的 EFLAGS = %08x\n", g_eflagsBefore);
	printf("进入陷阱门后的 EFLAGS = %08x\n", g_eflagsAfter);
	system("pause");
	GetRegister();
	return 0;
}

在这里插入图片描述
成功提权读取内存

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值