SEH机制大家都很熟悉,但TRY-EXCEPT的CODE与SEH的组合可能不是很了解,都是未公开的,说得再细也不如,运行调试一下代码便知。全球没有第二家!!! 

希望能抛砖引玉。

源码如下:

#include <windows.h>
#include <stdio.h>

//该结构是try except 块的向量表(一数组),是未公开的
typedef struct __vect_handler
{
  DWORD vFlag;
  PVOID TryHandler;/*try-except{code}*/
  PVOID NextContiueCode;/*异常处理后的下一条指令地址*/

}vect_handler;

typedef struct __EXCEPTION_LIST
{
  __EXCEPTION_LIST* next;//next一个SEH链
  PVOID             handler;//异常处理程序
  vect_handler*     vecthandler;
  DWORD             IndexVechandler;/*当前处理的try-except块号,抛出异常(在try-except里) 都会修改它*/
  
}EXCEPTION_LIST;

char* ff = "try";

DWORD WINAPI handler()
{
  /*突破except{}里不能定义局部变量,增加局部变量*/
  /*调试器监测*/
  /*加解密TRY 块代码*/
      __asm pushad
    
    __asm push 0
    __asm push 0
    __asm push ff
    __asm push 0
    __asm mov eax,MessageBoxA
    __asm call eax
    __asm popad
  //  __asm jmp OldExceptCode实现该行才算完美hook
    return EXCEPTION_EXECUTE_HANDLER/*jmp Contiue函数*/;
};

void   Contiue()
{
        MessageBoxA( 0,"contiue",0,0);
    ExitProcess( 0 );
};

int main( int argn, char** argv )
{
  char k;
  char* v = 0;//&k;
  DWORD prepageprotect;

  DWORD ntry = 0;
  EXCEPTION_LIST* pe;
  EXCEPTION_LIST* currentpe;


  __asm mov eax, fs:[0]
    __asm mov pe, eax
    
  currentpe = pe;

  BOOL ok = VirtualProtect( &currentpe->vecthandler[0].TryHandler, 8, PAGE_EXECUTE_READWRITE, &prepageprotect );
  currentpe->vecthandler[0].TryHandler = handler;//hook try-except 【0 】block
  currentpe->vecthandler[0].NextContiueCode = Contiue;/*hook try-except 【0】 block 可以调到任何IP,可自己扩展功能*/
//以下do-while为显示SEH-try链没啥作用
    do
  {
    printf( "Next:%08x,handler:%08x\n", pe->next, pe->handler );
    if( pe->vecthandler )
    {
      vect_handler* v = pe->vecthandler;

      while( v->vFlag == 0xffffffff )
      {
        printf( "------try:%d,handler:%08x,NextCode:%08x\n", ntry, v->TryHandler, v->NextContiueCode );
        v++;
        ntry++;
      };
    };
    pe = pe->next;

  }while( pe!=(EXCEPTION_LIST*)0xffffffff );
  
  __try{
    /*ASM 这里会有mov [ebp-4],0*///try-except 【0】对应vecthandler[0]
    *v = 0;//将抛出异常,它已hooked 
    
  }__except(EXCEPTION_EXECUTE_HANDLER)
  {
                          // 该异常处理将变化为调用handler函数
           //异常处理完好后将调用Contiue函数;
    
  };
  
  __try{
    /*ASM 这里会有mov [ebp-4],1*///try-except 【1】对应vecthandler[1]
    *v = 0;
    
  }__except(EXCEPTION_CONTINUE_EXECUTION)
  {
    
  };
  getchar();
  return 0;
}