看流星社区

 找回密码
 注册账号
查看: 4140|回复: 1

VC hook CreateProcess注入dll源码

[复制链接]

该用户从未签到

发表于 2012-2-17 16:38:08 | 显示全部楼层 |阅读模式
  1. ////////////////////////////////////
  2. //功能:hook CreateProcess注入
  3. ////////////////////////////////////
  4. #include <windows.h>
  5. #include <stdio.h>


  6. #define _CRT_SECURE_NO_DEPRECATE
  7. #define _CRT_SECURE_NO_WARNINGS



  8. DWORD org_ReadProcessMemory;
  9. DWORD org_CreateProcessA;

  10. HANDLE  ghInstance = 0;

  11. char path[1024];

  12. int __declspec(naked) __stdcall OrgReadProcessMemory(
  13.         HANDLE hProcess,             // handle to the process
  14.         LPCVOID lpBaseAddress,       // base of memory area
  15.         LPVOID lpBuffer,             // data buffer
  16.         DWORD nSize,                 // number of bytes to read
  17.         LPDWORD lpNumberOfBytesRead  // number of bytes read
  18.         )




  19. {
  20.         __asm {

  21.                 mov edi,edi
  22.                         push ebp
  23.                         mov ebp,esp
  24.                         jmp [org_ReadProcessMemory]
  25.         }
  26. }

  27. char tmp1[255];

  28. BOOL __stdcall MyReadProcessMemory(
  29.                                                                    HANDLE hProcess,             // handle to the process
  30.                                                                    LPCVOID lpBaseAddress,       // base of memory area
  31.                                                                    LPVOID lpBuffer,             // data buffer
  32.                                                                    DWORD nSize,                 // number of bytes to read
  33.                                                                    LPDWORD lpNumberOfBytesRead  // number of bytes read
  34.                                                                    )



  35. {
  36. //        sprintf(tmp1,"P-[%d]%08X",nSize,lpBaseAddress);OutputDebugString(tmp1);
  37.         //DWORD ret=OrgReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead);
  38.         //return ret;



  39.         return 0;


  40. }








  41. int __declspec(naked) __stdcall OrgCreateProcessA(
  42.         LPCTSTR lpApplicationName,                 // name of executable module
  43.         LPTSTR lpCommandLine,                      // command line string
  44.         LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
  45.         LPSECURITY_ATTRIBUTES lpThreadAttributes,  // SD
  46.         BOOL bInheritHandles,                      // handle inheritance option
  47.         DWORD dwCreationFlags,                     // creation flags
  48.         LPVOID lpEnvironment,                      // new environment block
  49.         LPCTSTR lpCurrentDirectory,                // current directory name
  50.         LPSTARTUPINFO lpStartupInfo,               // startup information
  51.         LPPROCESS_INFORMATION lpProcessInformation // process information


  52.         )




  53. {
  54.         __asm {

  55.                 mov edi,edi
  56.                         push ebp
  57.                         mov ebp,esp
  58.                         jmp [org_CreateProcessA]
  59.         }
  60. }

  61. char tmp2[255];

  62. BOOL __stdcall MyCreateProcessA(
  63.                                                                 LPCTSTR lpApplicationName,                 // name of executable module
  64.                                                                 LPTSTR lpCommandLine,                      // command line string
  65.                                                                 LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
  66.                                                                 LPSECURITY_ATTRIBUTES lpThreadAttributes,  // SD
  67.                                                                 BOOL bInheritHandles,                      // handle inheritance option
  68.                                                                 DWORD dwCreationFlags,                     // creation flags
  69.                                                                 LPVOID lpEnvironment,                      // new environment block
  70.                                                                 LPCTSTR lpCurrentDirectory,                // current directory name
  71.                                                                 LPSTARTUPINFO lpStartupInfo,               // startup information
  72.                                                                 LPPROCESS_INFORMATION lpProcessInformation // process information

  73.                                                                    )



  74. {
  75. //MessageBox(0,lpCommandLine,lpApplicationName,0);
  76.         

  77. BOOL ret=OrgCreateProcessA(
  78.                                                    lpApplicationName,                 // name of executable module
  79.                                                    lpCommandLine,                      // command line string
  80.                                                    lpProcessAttributes, // SD
  81.                                                    lpThreadAttributes,  // SD
  82.                                                    bInheritHandles,                      // handle inheritance option
  83.                                                    CREATE_SUSPENDED,                     // creation flags
  84.                                                    lpEnvironment,                      // new environment block
  85.                                                    lpCurrentDirectory,                // current directory name
  86.                                                    lpStartupInfo,               // startup information
  87.                                                    lpProcessInformation // process information

  88.                                                    );

  89. ;


  90. GetTempPath(255,path);
  91. strcat(path,"unimper.dll");

  92. //MessageBox(0,path,"tip",0);
  93. DWORD size = strlen( path ) + 1;
  94. LPVOID buf = VirtualAllocEx( lpProcessInformation->hProcess, NULL, size, MEM_COMMIT, PAGE_READWRITE );
  95. if ( NULL == buf )
  96. {
  97.         MessageBox(0,"Alloc Memery Failed!\n",0,0);
  98.         CloseHandle( lpProcessInformation->hProcess );

  99. }



  100. DWORD dwWritten;
  101. if ( WriteProcessMemory( lpProcessInformation->hProcess, buf, (PVOID)path, size, &dwWritten ) )
  102. {
  103.         // 要写入字节数与实际写入字节数不相等,仍属失败
  104.         if ( dwWritten != size )
  105.         {
  106.                 MessageBox(0,"Alloc Memery Failed!!!!!!!!!!!!!!!!!!\n",0,0);
  107.                 VirtualFreeEx( lpProcessInformation->hProcess, buf, size, MEM_DECOMMIT );
  108.                 CloseHandle( lpProcessInformation->hProcess );

  109.         }
  110. }
  111. else
  112. {
  113.         MessageBox(0,"Alloc Memery Failed!\n",0,0);
  114.         CloseHandle( lpProcessInformation->hProcess );

  115. }

  116. LPVOID pLoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  117. DWORD dwThreadId;

  118. //
  119. HANDLE hThread = CreateRemoteThread( lpProcessInformation->hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pLoadLibrary, buf, 0 , &dwThreadId );



  120. WaitForSingleObject( hThread, INFINITE );
  121. VirtualFreeEx( lpProcessInformation->hProcess, buf, size, MEM_DECOMMIT );
  122. CloseHandle( hThread );

  123. ResumeThread (lpProcessInformation->hThread);


  124.         
  125.         

  126. return ret;
  127.         
  128.         
  129.         
  130.         
  131.         //return 0;


  132. }



















  133. BOOL WINAPI DllMain(HINSTANCE hinstModule, DWORD dwReason, LPVOID lpvReserved)
  134. {

  135.         if(dwReason == DLL_PROCESS_ATTACH)
  136.         {

  137.                
  138.                 HANDLE hMutex = CreateMutex(NULL, false, "Process");
  139.                 if (GetLastError() == ERROR_ALREADY_EXISTS)
  140.                 {
  141.                         


  142.                         DWORD pReadProcessMemory = (DWORD)GetProcAddress(GetModuleHandle("kernel32.dll"), "ReadProcessMemory");
  143.                         DWORD pCreateProcessA = (DWORD)GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwCreateProcess");



  144.                         DWORD oldflag;
  145.                         VirtualProtect((PVOID)pReadProcessMemory, 5, PAGE_EXECUTE_READWRITE, &oldflag);
  146.                         VirtualProtect((PVOID)pCreateProcessA, 5, PAGE_EXECUTE_READWRITE, &oldflag);


  147.                         *(PCHAR)pReadProcessMemory = '\xE9';
  148.                         *(PCHAR)pCreateProcessA = '\xE9';


  149.                         *(DWORD*)(pReadProcessMemory+1) = (DWORD)MyReadProcessMemory - (pReadProcessMemory+5);
  150.                         *(DWORD*)(pCreateProcessA+1) = (DWORD)MyCreateProcessA - (pCreateProcessA+5);


  151.                         org_ReadProcessMemory = pReadProcessMemory+ 5;
  152.                         org_CreateProcessA = pCreateProcessA+ 5;
  153.                         CloseHandle(hMutex);

  154.                 }

  155.                 else
  156.                 {
  157.                 return 0;
  158.         
  159.         }

  160.         }

  161.         return true;
  162. }
复制代码

该用户从未签到

发表于 2012-3-7 12:43:49 | 显示全部楼层
org打头的是什么函数?系统api吗
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-27 20:40

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表