初始Microsoft Detours【收集】

  Detours是微软开发的一个函数库(源代码可在http://research.microsoft.com/sn/detours 免费获得), 用于修改运行中的程序在内存中的影像。具体用途是: 拦截Win32 API调用,将其引导到自己的子程序,从而实现Win32 API的定制。


Detours的原理
----1. Win32进程的内存管理
----众所周知,Windows NT实现了虚拟存储器,每一个Win32 进程拥有4GB的虚存空间, 关于Win32进程的虚存结构及其操作的具体细节请参阅 Win32 API手册, 以下仅指出与Detours相关的几点:


进程要执行的指令也放在虚存空间中。
可以使用QueryProtectEx函数把存放指令的页面的权限更改为可读可写可执行,再改写其内容,从而修改正在运行的程序。
可以使用VirtualAllocEx从一个进程为另一正运行的进程分配虚存,再使用 QueryProtectEx函数把页面的权限更改为可读可写可执行,并把要执行的指令以二进制机器码的形式写入,从而为一个正在运行的进程注入任意的代码。
----2. 拦截Win32 API的原理


----Detours定义了三个概念:


Target函数:要拦截的函数,通常为Windows的API。
Trampoline函数:Target函数的复制品。因为Detours将会改写Target函数,所以先把 Target函数复制保存好,一方面仍然保存Target函数的过程调用语义,另一方面便于以后的恢复。
Detours 函数:用来替代Target函数的函数。
---- Detours在Target函数的开头加入JMP Address_of_ Detour_ Function指令(共5个字节)把对Target函数的调用引导到自己的Detours函数, 把 Target函数的开头的5个字节加上JMP Address_of_ Target _ Function+5作为Trampoline 函数。例子如下:

 1 拦截前:Target _ Function:
 2         ;Target函数入口,
 3       以下为假想的常见的子程序入口代码
 4         push        ebp
 5         mov        ebp,    esp
 6         push        eax
 7         push        ebx
 8     Trampoline:
 9         ;以下是Target函数的继续部分
10         ......
11 
12 
13     拦截后:Target _ Function:
14         jmp        Detour_Function
15      Trampoline:
16         ;以下是Target函数的继续部分
17         ......
18 
19 
20      Trampoline_Function:
21         ; Trampoline函数入口, 开头的5个字节与
22           Target函数相同
23         push        ebp
24         mov        ebp,    esp
25         push        eax
26         push        ebx
27         ;跳回去继续执行Target函数
28         jmp     Target_Function+5







使用Detours需要有detours.lib和detours.h,这个可以从网上下载


具体使用例子:用TP_PathFileExists来拦截系统消息PathFileExists

 1 DETOUR_TRAMPOLINE(BOOL  WINAPI Detour_PathFileExists  (LPCTSTR pszPath), PathFileExists);
 2 
 3 
 4 extern "C" BOOL  WINAPI TP_PathFileExists  (LPCTSTR pszPath)
 5     {
 6         if( Detour_PathFileExists( pszPath))
 7             return TRUE;
 8         if(lstrlen(pszPath) <1return FALSE;
 9         if(pszPath[0== '\\')
10         {
11             HANDLE  hFile;  
12             hFile = CreateFile(pszPath
13                 ,GENERIC_READ
14                 ,FILE_SHARE_READ|FILE_SHARE_WRITE
15                 ,NULL
16                 ,OPEN_EXISTING
17                 ,FILE_ATTRIBUTE_NORMAL
18                 ,NULL);
19 
20 
21             if(INVALID_HANDLE_VALUE == hFile)
22                 return FALSE;
23             else
24             {
25                 CloseHandle(hFile);
26                 return TRUE;
27             }
28         }
29         else
30         {
31             WIN32_FIND_DATA lpFindFileData;
32             HANDLE hFind = FindFirstFile( pszPath,&lpFindFileData);
33             if( INVALID_HANDLE_VALUE == hFind) return FALSE;
34             else {FindClose(hFind);return TRUE;}
35         }
36     }
37 
38 
39 
40 
41 BOOL TP_InitSysHook()
42 {
43     DWORD dwVersion = GetVersion();
44     if(dwVersion < 0x80000000)
45     {        
46         DetourFunctionWithTrampoline((PBYTE)Detour_PathFileExists,    (PBYTE)TP_PathFileExists);    
47         return TRUE;
48     }
49     return TRUE;
50 }
51 
52 
53 BOOL TP_ReleaseSysHook()
54 {
55     DWORD dwVersion = GetVersion();
56     if(dwVersion < 0x80000000)
57     {        
58         DetourRemove((PBYTE)Detour_PathFileExists,     (PBYTE)TP_PathFileExists);        
59         return TRUE;
60     }
61     return TRUE;
62 }




 

原文地址:https://www.cnblogs.com/weisteve/p/1972058.html