分享

API HOOK之注册表简单监控

 guoliyan1 2012-04-04
调用DLL中的函数即可,可以自行扩展hook的函数,这里只hook了 RegCreateKeyEx
一、HOOK DLL的编写:

  1. #include <windows.h>  
  2. #include <Tlhelp32.h>  
  3. #include <stdio.h>  
  4. //==========================================================================================  
  5.   
  6. HINSTANCE glhInstance=NULL; //DLL实例句柄   
  7.   
  8. BYTE g_OldRegCreateKeyExCode[5] = {0}; //存放函数地址  
  9. BYTE g_NewRegCreateKeyExCode[5] = {0}; //存放函数地址  
  10.   
  11. FARPROC FuncAddr = NULL;                //函数地址  
  12. DWORD PID=0;                            //进程PID  
  13. int count=0;  
  14. HANDLE hProcess;  
  15. //==========================================================================================  
  16. typedef struct tagReg_Info    //存放 RegCreateKeyEx() 的信息  
  17. {  
  18.     HKEY hKey;                // handle to an open key  
  19.     LPCTSTR lpSubKey;         // address of subkey name  
  20.     DWORD Reserved;           // reserved  
  21.     LPTSTR lpClass;           // address of class string  
  22.     DWORD dwOptions;          // special options flag  
  23.     REGSAM samDesired;        // desired security access  
  24.     LPSECURITY_ATTRIBUTES lpSecurityAttributes;  
  25.     // address of key security structure  
  26.     PHKEY phkResult;          // address of buffer for opened handle  
  27.     LPDWORD lpdwDisposition;   // address of disposition value buffer     
  28. }Reg_Info;  
  29.   
  30. Reg_Info RegInfo;  
  31.   
  32. //==========================================================================================  
  33. #pragma data_seg("mydata")   
  34. HHOOK hook=NULL;         //安装的鼠标勾子句柄    
  35. #pragma data_seg()   
  36. #pragma comment(linker,"/SECTION:mydata,RWS")   
  37.   
  38. //==========================================================================================  
  39. _declspec (dllexportbool Inject();  
  40. _declspec (dllexportbool SetHook();  
  41. _declspec (dllexportbool UnSetHook();  
  42. bool Init();  
  43.   
  44. LONG MyRegCreateKeyEx(  
  45.                       HKEY hKey,                // handle to an open key  
  46.                       LPCTSTR lpSubKey,         // address of subkey name  
  47.                       DWORD Reserved,           // reserved  
  48.                       LPTSTR lpClass,           // address of class string  
  49.                       DWORD dwOptions,          // special options flag  
  50.                       REGSAM samDesired,        // desired security access  
  51.                       LPSECURITY_ATTRIBUTES lpSecurityAttributes,  
  52.                       // address of key security structure  
  53.                       PHKEY phkResult,          // address of buffer for opened handle  
  54.                       LPDWORD lpdwDisposition   // address of disposition value buffer  
  55.                       );  
  56.   
  57.   
  58. LONG HookOff();  
  59. LRESULT CALLBACK ShellProc(  
  60.                            int nCode,      // hook code  
  61.                            WPARAM wParam,  // event-specific information  
  62.                            LPARAM lParam   // event-specific information  
  63.                            );  
  64. //==========================================================================================  
  65. //==========================================================================================  
  66.   
  67. BOOL WINAPI DllMain(  
  68.                     HINSTANCE hinstDLL,  // handle to DLL module  
  69.                     DWORD fdwReason,     // reason for calling function  
  70.                     LPVOID lpvReserved   // reserved  
  71.                     )  
  72. {  
  73.     glhInstance=hinstDLL;  
  74.     return 1;  
  75. }  
  76. //==========================================================================================  
  77. _declspec (dllexportbool Inject()  
  78. {  
  79.     Init();  
  80.     //hProcess = OpenProcess(PROCESS_ALL_ACCESS,0, PID);   
  81.     if(hProcess == NULL)   
  82.     {  
  83.         return false;   
  84.     }  
  85.     CRITICAL_SECTION cs;  
  86.     InitializeCriticalSection(&cs);  
  87.     EnterCriticalSection(&cs);  
  88.       
  89.     DWORD PROTECT=0;  
  90.     VirtualProtectEx(hProcess, FuncAddr, 5, PAGE_READWRITE, &PROTECT);          //申请CreateWindowExA地址处的写权限,  
  91.     WriteProcessMemory(hProcess, FuncAddr, g_NewRegCreateKeyExCode, 5, NULL);   //然后写入跳转代码,然后恢复权限  
  92.     VirtualProtectEx(hProcess, FuncAddr, 5, PROTECT, &PROTECT);  
  93.          
  94.     LeaveCriticalSection(&cs);  
  95.     DeleteCriticalSection(&cs);  
  96.     CloseHandle(hProcess);     
  97.     return true;  
  98. }  
  99. //==========================================================================================  
  100. _declspec (dllexportbool SetHook()  
  101. {  
  102.       
  103.     hook=SetWindowsHookEx(WH_SHELL,ShellProc,glhInstance,0);  
  104.     if(NULL==hook)  
  105.     {  
  106.         ::MessageBox(NULL,"SetWindowsHookEx!","Error!",MB_ICONERROR);  
  107.         return false;  
  108.     }  
  109.     return true;  
  110. }  
  111.   
  112. //==========================================================================================  
  113. //==========================================================================================  
  114. _declspec (dllexportbool UnSetHook()  
  115. {  
  116.     bool ret=false;  
  117.     if(hook)  
  118.     {  
  119.         ret=UnhookWindowsHookEx(hook);  
  120.         if(!ret)  
  121.         {  
  122.             ::MessageBox(NULL,"UnhookWindowsHookEx!","Error!",MB_ICONERROR);  
  123.             return false;  
  124.         }  
  125.           
  126.         return true;  
  127.     }     
  128.     return false;  
  129. }  
  130.   
  131. LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam,LPARAM lParam)  
  132. {  
  133.     if(nCode==HSHELL_WINDOWCREATED)     //  HOOK的目的只在于映射进DLL,这里后面的处理也可以,  
  134.     {  
  135.         PID=GetCurrentProcessId();      //  但是这里只做API注入,就不用了  
  136.         hProcess = OpenProcess(PROCESS_ALL_ACCESS,0, PID);   
  137.         Init();  
  138.         Inject();  
  139.     }  
  140.     return CallNextHookEx(hook,nCode,wParam,lParam);      
  141. }  
  142.   
  143. //==================================================================================  
  144. //==================================================================================  
  145. LONG MyRegCreateKeyEx(  
  146.                       HKEY hKey,                // handle to an open key  
  147.                       LPCTSTR lpSubKey,         // address of subkey name  
  148.                       DWORD Reserved,           // reserved  
  149.                       LPTSTR lpClass,           // address of class string  
  150.                       DWORD dwOptions,          // special options flag  
  151.                       REGSAM samDesired,        // desired security access  
  152.                       LPSECURITY_ATTRIBUTES lpSecurityAttributes,  
  153.                       // address of key security structure  
  154.                       PHKEY phkResult,          // address of buffer for opened handle  
  155.                       LPDWORD lpdwDisposition   // address of disposition value buffer  
  156.                       )  
  157. {  
  158.     char str[1000]={0};    
  159.     if(HKEY_LOCAL_MACHINE==hKey)  
  160.     {  
  161.         sprintf(str,"注册表位置: HKEY_LOCAL_MACHINE\\%s \nRegedit is being Created !",lpSubKey);   
  162.     }  
  163.     if(HKEY_USERS==hKey)  
  164.     {  
  165.         sprintf(str,"注册表位置: HKEY_USERS\\%s \nRegedit is being Created !",lpSubKey);   
  166.     }  
  167.     if(HKEY_CLASSES_ROOT==hKey)  
  168.     {  
  169.         sprintf(str,"注册表位置: HKEY_CLASSES_ROOT\\%s \nRegedit is being Created !",lpSubKey);   
  170.     }  
  171.     if(HKEY_CURRENT_CONFIG==hKey)  
  172.     {  
  173.         sprintf(str,"注册表位置: HKEY_CURRENT_CONFIG\\%s \nRegedit is being Created !",lpSubKey);          
  174.     }  
  175.     /**/else        //  if(HKEY_CURRENT_USER==hKey)         ((HKEY) (ULONG_PTR)((LONG)0x80000001))  
  176.     {  
  177.         sprintf(str,"注册表位置: HKEY_CURRENT_USER\\%s \nRegedit is being Created !\nPID: %ld",lpSubKey,PID);   
  178.           
  179.     }  
  180.       
  181.     if(count<1)  
  182.     {  
  183.         ::MessageBox(NULL,str,"warning",MB_ICONWARNING);  
  184.     }  
  185.     count++;  
  186.     /*  HANDLE hFile=CreateFile("C:\\RegLog.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
  187.     if(hFile) 
  188.     { 
  189.     ::MessageBox(NULL,"CreateFile Error","warning",MB_ICONWARNING); 
  190.     } 
  191.      
  192.       if(SetFilePointer(hFile,0,NULL,FILE_END)==0xFFFFFFFF) 
  193.       { 
  194.       ::MessageBox(NULL,"SetFilePointer Error","warning",MB_ICONWARNING); 
  195.       } 
  196.       OVERLAPPED olp; 
  197.       olp.hEvent=NULL; 
  198.       olp.OffsetHigh=0; 
  199.        
  200.         if(!WriteFileEx(hFile,str,strlen(str)+1,&olp,NULL)) 
  201.         { 
  202.         ::MessageBox(NULL,"SetFilePointer Error","warning",MB_ICONWARNING); 
  203.         } 
  204.          
  205.     */  
  206.     ZeroMemory(&RegInfo,sizeof(RegInfo));  
  207.     RegInfo.dwOptions=dwOptions;       // 保存传入的信息:  
  208.     RegInfo.hKey=hKey;  
  209.     RegInfo.lpClass=lpClass;  
  210.     RegInfo.lpdwDisposition=lpdwDisposition;  
  211.     RegInfo.lpSecurityAttributes=lpSecurityAttributes;  
  212.     RegInfo.lpSubKey=lpSubKey;  
  213.     RegInfo.phkResult=phkResult;  
  214.     RegInfo.Reserved=Reserved;  
  215.     RegInfo.samDesired=samDesired;  
  216.       
  217.     HookOff();  
  218.     Sleep(1000);  
  219.     LONG  ret=RegCreateKeyEx(RegInfo.hKey,  
  220.         RegInfo.lpSubKey,  
  221.         RegInfo.Reserved,  
  222.         RegInfo.lpClass,  
  223.         RegInfo.dwOptions,  
  224.         RegInfo.samDesired,  
  225.         RegInfo.lpSecurityAttributes,  
  226.         RegInfo.phkResult,  
  227.         RegInfo.lpdwDisposition);  
  228.     Sleep(1000);  
  229.     Inject();   return ret;  
  230.       
  231. }  
  232. //==================================================================================  
  233. LONG HookOff()  
  234. {     
  235.     //hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, PID);   
  236.     if(hProcess == NULL)   
  237.     {  
  238.         return (LONG)1;   
  239.     }  
  240.     //写入原CreateWindowExA的5个字节代码   
  241.     CRITICAL_SECTION cs;  
  242.     InitializeCriticalSection(&cs);  
  243.     EnterCriticalSection(&cs);  
  244.       
  245.        DWORD PROTECT=0;  
  246.        VirtualProtectEx(hProcess, FuncAddr, 5, PAGE_READWRITE, &PROTECT);   
  247.        WriteProcessMemory(hProcess, FuncAddr, g_OldRegCreateKeyExCode, 5, NULL);   
  248.        VirtualProtectEx(hProcess, FuncAddr, 5, PROTECT, &PROTECT);  
  249.          
  250.        LeaveCriticalSection(&cs);  
  251.        DeleteCriticalSection(&cs);  
  252.          
  253.          
  254.        //CloseHandle(hProcess);    
  255.        return (LONG)1;  
  256. }  
  257.   
  258. bool Init()  
  259. {  
  260.        //FuncAddr = GetProcAddress(LoadLibrary("Advapi32.dll"),"RegCreateKeyExW");  
  261.        FuncAddr = GetProcAddress(LoadLibrary("Advapi32.dll"),"RegCreateKeyExA");  
  262.        if(NULL==FuncAddr)  
  263.            return false;  
  264.          
  265.        CRITICAL_SECTION cs;  
  266.        InitializeCriticalSection(&cs);  
  267.        EnterCriticalSection(&cs);  
  268.          
  269.        _asm   
  270.        {   
  271.            lea edi, g_OldRegCreateKeyExCode  
  272.                mov esi, FuncAddr   
  273.                cld   
  274.                movsd //将CreateWindowExA地址起始的4个字节(dword)写入g_OldCreateWindowExACode   
  275.                movsb //将CreateWindowExA+4地址起始处的1个字节(byte)写入g_OldCreateWindowExACode+4   
  276.        }   
  277.        //jmp xxxxxxxx的机器码为e9xxxxxxxx,其中e9后的xxxxxxxx为相对跳转偏移,共5个字节  
  278.        g_NewRegCreateKeyExCode[0] = 0xe9;   
  279.        _asm   
  280.        {   
  281.            lea eax, MyRegCreateKeyEx   //  
  282.                mov ebx, FuncAddr      
  283.                sub eax, ebx   
  284.                sub eax, 5 //获得相对跳转偏移  //偏移地址 = 我们函数的地址 - 原API函数的地址 - 5  
  285.                mov dword ptr [g_NewRegCreateKeyExCode + 1], eax   
  286.        }  
  287.        LeaveCriticalSection(&cs);  
  288.        DeleteCriticalSection(&cs);  
  289.          
  290. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多