WIN32程序挂钩SetLastError,输出错误描述到控制台

WIN32程序挂钩SetLastError,输出错误描述到控制台

作者:徐灵甫

一、窗口模式应用程序(GUI)启用控制台的方法为

步骤 方法
1 启动/关闭控制台 AllocConsole()
FreeConsole()
2 重定向输入/输出 freopen("CONIN$","r",stdin)
freopen("CONOUT$","w",stdout)
freopen("CONOUT$","w",stderr)
3 控制台输入/输出 #include <conio.h>
#include <stdio.h>
printf(...)
scanf(...)
system("pause")



二、挂钩API函数的简单方法为:

1. DEBUG模式下,函数名值为指令“JMP函数体”的地址。指令格式为“E9 □□□□”,附带的参数为四字节表示的转移偏移量。因此“函数名值 + *(DWORD*)((DWORD)函数名值 + 1)”为函数体入口地址。“使用转到反汇编”的功能计算出函数体入口栈指令长度,得出实际入口地址为“函数名值 + *(DWORD*)((DWORD)函数名值 + 1) + 入口栈指令长度”;

2. RELEASE模式下,函数名值直接为函数体的入口地址。使用“转到反汇编”的功能计算出函数体除退出指令外的指令长度,得出函数出口地址为“函数名 + 指令长度”,API函数正是这种模式;

3. 使用“::WriteProcessMemory(::GetCurrentProcess(), API函数出口地址...)”的方法在API函数上挂钩以下调用:

序号 说明 指令 参数值
1 调用挂钩函数 E8 □□□□ 挂钩函数体实际入口地址
2 退出 C2 □□ 函数参数总长度,用于恢复栈的状态



三、挂钩API函数SetLastError,并输出错误描述到控制台的范例

#include <stdio.h>
#include <windows.h>
void hook_SetLastError()//为简化调用挂钩函数时的栈操作,挂钩函数无参数和返回值。
{
 if (::GetLastError())
 {
  LPVOID lpMsgBuf = 0;
  if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | 
       FORMAT_MESSAGE_IGNORE_INSERTS, 
       0, ::GetLastError(), LANG_USER_DEFAULT, (LPTSTR) &lpMsgBuf, 0, 0))
  {
   ::printf("ERROR: %d %s", ::GetLastError(), (LPCSTR)lpMsgBuf);
   ::LocalFree(lpMsgBuf);
  }
 }
}

int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
  unsigned char setup_SetLastError[8] = {0xE8, 0, 0, 0, 0, 0xC2, 4, 0};
#ifdef _DEBUG
  *(unsigned int*)(setup_SetLastError + 1) = (unsigned int)hook_SetLastError + 
	*(unsigned int*)((unsigned char*)hook_SetLastError + 1) - (unsigned int)SetLastError - 18;
#else
  *(unsigned int*)(setup_SetLastError + 1) = (unsigned int)hook_SetLastError - 
     (unsigned int)SetLastError - 23;
#endif
  ::WriteProcessMemory(::GetCurrentProcess(), (LPVOID)((unsigned int)::SetLastError + 18), 
      setup_SetLastError, 8, new SIZE_T);
  ::AllocConsole();
  ::freopen("CONIN$", "r", stdin);
  ::freopen("CONOUT$", "w", stdout);

  //此处添加自己的代码
  ::WriteProcessMemory(::GetCurrentProcess(), (LPVOID)((unsigned int)::SetLastError + 18), 
       setup_SetLastError + 5, 3, new SIZE_T);
  ::system("pause");
  return 0;
}

(全文完)

http://blog.csdn.net/jiangxinyu/article/details/5386000

原文地址:https://www.cnblogs.com/findumars/p/5811507.html