自己Swap自己,Ring0下做测试器时用的代码

作者:vxk

#include "vx_main.h"
#include "mem.h"
#include <undoc.h>
#include <stdio.h>
void MySwapContextHook();
void MySwapContextEndHook();
static DWORD SwapContextPtr=0;
static DWORD SwapContextCnt=0;
extern HANDLE CurrentProcessId;      // used in VXGetCurrentProcessId
NTSTATUS InitSwapContextHook()
{
  // The code "mov byte ptr es:[esi+2Dh], 2" is near the start of
  // SwitchContext function in NTOSKRNL.EXE. It is the only place we see
  // these bytes in NT4 & 2K and the first place in XP
    PULONG pOldAttr;
    DisableWriteProtect(pOldAttr);
     unsigned char SwapContextStart[]={0x26,(char)0xc6,0x46,0x2d,0x02};      
     SwapContextPtr=Search(VXGetModuleBase("ntoskrnl.exe"), SwapContextStart, sizeof(SwapContextStart));
     if(SwapContextPtr<(DWORD)VXSystemRangeStart)
     {
           DbgPrint("VX: Unable to find SwapContext in NTOSKRNL.EXE!");
           return STATUS_PROCEDURE_NOT_FOUND;
     } else
           DbgPrint("VX: SwapContext found at %08X/n",SwapContextPtr);

  SwapContextCnt=SwapContextPtr+5;

  __asm cli;      // Disable task switching

  DWORD Addr=(DWORD)MySwapContextHook-(SwapContextPtr+5);
  *(char*)SwapContextPtr=(char)0xe9;
  *(DWORD*)(SwapContextPtr+1)=Addr;

  __asm sti;
  EnableWriteProtect(pOldAttr);
     return STATUS_SUCCESS;
}


void RemoveSwapContextHook()
{
    PULONG pOldAttr;
    DisableWriteProtect(pOldAttr);
     if(SwapContextPtr)
     {
        __asm cli;      // Disable task switching
     
        memcpy((void*)SwapContextPtr,"/x26/xc6/x46/x2d/x02",5);

        __asm sti;
           SwapContextPtr=0;
     }
     EnableWriteProtect(pOldAttr);
}


void __stdcall
HandleContextSwap(ETHREAD *New, ETHREAD *Old)
{
     PULONG pOldAttr;
    DisableWriteProtect(pOldAttr);
     CurrentProcessId=Old->Cid.UniqueProcess;

     VXDeferAllUserModeBreakpoints();      // I don't disable kernel breaks to increase speed

     CurrentProcessId=New->Cid.UniqueProcess;

     // Now install hook in the new thread's context so I can do what I need...

  DWORD* Stack=(DWORD*)New->Tcb.KernelStack;

  // Stack of SwapContext in NT4-XP looks like:
  // esp+8 -> return address │ Stack[2]
  // esp+4 -> EFLAGS       │ Stack[1]
  // esp   -> ECX         │ Stack[0]
  // I'll insert address of my hook function between EFLAGS and return address

     New->Tcb.KernelStack=((char*)New->Tcb.KernelStack)-4;
     memmove(New->Tcb.KernelStack,Stack,8);
     Stack[1]=(DWORD)MySwapContextEndHook;
     EnableWriteProtect(pOldAttr);
}


void __stdcall HandleContextSwapEnd()
{      
     VXActivateAllUserModeBreakpoints();
}


void __declspec(naked) MySwapContextHook()
{
     __asm {
        mov byte ptr es:[esi+2Dh], 2
        pushfd
        pushad
        push edi
        push esi
        call HandleContextSwap
        popad
        popfd
        jmp [SwapContextCnt]
     }
}


void __declspec(naked) MySwapContextEndHook()
{
     __asm {
           pushfd
           pushad
           call HandleContextSwapEnd
           popad
           popfd
           retn
     }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值