dll unpack, GetThreadContext error

Тема в разделе "WASM.ASSEMBLER", создана пользователем laomms, 5 июн 2008.

  1. laomms

    laomms New Member

    Публикаций:
    0
    Регистрация:
    10 май 2007
    Сообщения:
    3
    Here is a unpacker for PECompact packed dll, but there are some error occur:
    1、CreateProcess use command line: loaddll.exe *.dll (just like ollydbg)
    2、While CREATE_PROCESS_DEBUG_EVENT,log the DBEvent.u.CreateProcessInfo.hProcess and DBEvent.u.CreateProcessInfo.hThread
    3、While LOAD_DLL_DEBUG_EVENT, when load *.dll, SuspendThread DBEvent.u.CreateProcessInfo.hThread, than set breakpoint in OEP,and ResumeThread DBEvent.u.CreateProcessInfo.hThread, while GetThreadContext, Context.regEip always stop in ntdll.KiFastSystemCallRet, can not get dll's regEip:
    Код (Text):
    1. ProcessDll proc
    2. LOCAL Buffer [64]:byte
    3. LOCAL BP1_data,BP2_data,BP3_data,BP4_data:DWORD
    4. LOCAL BP1,BP2,BP3,BP4:DWORD
    5. LOCAL hProcess:dword
    6. LOCAL tempaddr:dword
    7. LOCAL dlls_imported,PatchAddr:dword
    8. LOCAL szContext[1024]:byte
    9. LOCAL TEB,LDR:dword
    10.  
    11.         mov flag,0
    12.         invoke GetTempPath, sizeof TempPath, addr TempPath
    13.         invoke wsprintf,addr OutBuff,addr Format,addr loaddllPath,addr FilePath  ;loaddll.exe 2.dll
    14.         invoke GetStartupInfo,addr startinfo
    15.         invoke CreateProcess,NULL,addr OutBuff,NULL,NULL,FALSE,DEBUG_PROCESS+DEBUG_ONLY_THIS_PROCESS ,NULL,addr TempPath,addr startinfo,addr ProcessInfo
    16.         .while TRUE        
    17.                 invoke WaitForDebugEvent,addr DBEvent,INFINITE       
    18.                 mov dwDebugOperation,DBG_CONTINUE                
    19.                 .if DBEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT                             
    20.                         invoke OutputInfo,CTEXT("Exit Debug"),0                      
    21.                         .break
    22.                .elseif DBEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT
    23.                     mov edx,DBEvent.u.CreateProcessInfo.hProcess
    24.                     mov myProcess,edx
    25.                     mov ecx,DBEvent.u.CreateProcessInfo.hThread
    26.                     mov myThread,ecx  
    27.                 .elseif DBEvent.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT
    28.                         mov Context.ContextFlags,CONTEXT_FULL
    29.                         invoke GetThreadContext,myThread,addr Context
    30.                         invoke wsprintf,addr OutBuff,CTEXT("regEip %.08X",13,10,0),Context.regEip
    31.                         invoke OutputInfo,addr OutBuff,0
    32.                         invoke GetThreadSelectorEntry,myThread,Context.regFs,addr ldte
    33.                         mov ah,[ldte.HighWord1.Bytes.BaseHi]  
    34.                         shl eax,16
    35.                         mov ax,[ldte.BaseLow]
    36.                         assume fs:nothing
    37.                         mov eax,fs:[18h]
    38.                         mov eax,[eax+30h]
    39.                        
    40.                         mov eax,DBEvent.u.LoadDll.lpBaseOfDll
    41.                         mov dwModBase,eax
    42.                         .if  dwModBase < 60000000h
    43.                                 invoke ReadProcessMemory,myProcess,DBEvent.u.LoadDll.lpImageName,addr OutBuff,4,NULL
    44.                                 mov eax,dword ptr OutBuff
    45.                                 invoke ReadProcessMemory,myProcess,eax,addr pDllName,256,NULL
    46.                                 invoke Unicode2Ansi,addr pDllName,addr outinf
    47.                                 invoke lstrcmp,addr outinf,addr FilePath
    48.                                 .if eax==0
    49.                                         inc flag
    50.                                         invoke wsprintf,addr OutBuff,CTEXT("LoadDll sucess: handle:%08Xh,base:%08Xh,name:%s",13,10,0),DBEvent.u.LoadDll.hFile,DBEvent.u.LoadDll.lpBaseOfDll,addr outinf
    51.                                         invoke OutputInfo,addr OutBuff,0
    52.                                         mov esi,DBEvent.u.LoadDll.lpBaseOfDll
    53.                                         mov DllBase,esi
    54.                                         mov eax,DBEvent.u.LoadDll.hFile
    55.                                         mov hDll,eax
    56.                                 .elseif flag==1                                            
    57.                                         mov edi,DllBase
    58.                                         add edi,EntryPiont
    59.                                         add edi,1
    60.                                         lea esi,Buffer
    61.                                         invoke ReadProcessMemory,myProcess,edi,esi,4,0
    62.                                         mov edi,dword ptr [esi]
    63.                                         invoke wsprintf,addr OutBuff,CTEXT("%s: %.08X",13,10,0),CTEXT("Got the address of  mov eax,xxxxxxxx "),edi
    64.                                         invoke OutputInfo,addr OutBuff,0
    65.                                         invoke Searchcode,myProcess,edi,0ffd7h
    66.                                         .if eax==0
    67.                                             invoke MessageBox,0,CTEXT("Searchcode Error"),0,MB_OK
    68.                                             jmp @@exit
    69.                                         .endif
    70.                                         mov BP1,eax
    71.                                         invoke ReadProcessMemory,myProcess,BP1,addr BP1_data,2,0    
    72.                                         invoke WriteProcessMemory,myProcess,BP1,CTEXT(0EBh,0FEh),2,0
    73.                                         invoke wsprintf,addr OutBuff,CTEXT("%s: %.08X",13,10,0),CTEXT("Set Breakpoint in  'CALL EDI' "),BP1
    74.                                         invoke OutputInfo,addr OutBuff,0                                       
    75.                                         invoke Searchcode,myProcess,BP1,0FFe0h
    76.                                         mov BP2,eax
    77.                                         invoke wsprintf,addr OutBuff,CTEXT("%s: %.08X",13,10,0),CTEXT("Set Breakpoint in 'JMP EAX' "),BP2
    78.                                         invoke OutputInfo,addr OutBuff,0  
    79.                                         invoke ReadProcessMemory,myProcess,BP2,addr BP2_data,2,0
    80.                                         invoke WriteProcessMemory,myProcess,BP2,CTEXT(0CCh,00h),2,0  
    81.                                         _10MB equ 1024*1024*10
    82.                                         invoke VirtualAlloc,0,_10MB,MEM_COMMIT,PAGE_READWRITE
    83.                                         mov pNewImports,eax
    84.                                         invoke OutputInfo,CTEXT("Start Unpacking...",13,10,0),0  
    85.                                         invoke ResumeThread,myThread  
    86.                                         invoke Sleep,100                                        
    87.                                         mov esi,pNewImports
    88.                                         assume esi:ptr IMAGE_IMPORT_DESCRIPTOR
    89.                                         mov dlls_imported,0
    90.                                         mov borland_flag,FALSE
    91.                                         mov redirection_flag,FALSE
    92.                                 @loop:
    93.                                         invoke SuspendThread,myThread
    94.                                         mov Context.ContextFlags,CONTEXT_FULL
    95.                                         invoke GetThreadContext,myThread,addr Context
    96. ;/////////////////////////////////////////////////////////////
    97. ;Error here, Context.regEip=ntdll.KiFastSystemCallRet always
    98.                                         mov eax,Context.regEip
    99. ;/////////////////////////////////////////////////////////////
    100.                                         .if eax==BP1
    101.                                             invoke WriteProcessMemory,myProcess,BP1,addr BP1_data,2,0
    102.                                             mov eax,Context.regEdi
    103.                                             mov tempaddr,eax
    104.                                             invoke Searchcode,myProcess,eax,5751h
    105.                                             add eax,3
    106.                                             mov ebx,eax
    107.                                             mov BP3,ebx
    108.                                             invoke ReadProcessMemory,myProcess,BP3,addr BP3_data,2,0
    109.                                             invoke WriteProcessMemory,myProcess,ebx,CTEXT(0EBh,0FEh),2,0
    110.                                             add ebx,6
    111.                                             mov BP4,ebx
    112.                                             invoke ReadProcessMemory,myProcess,BP4,addr BP4_data,2,0
    113.                                             invoke Searchcode,myProcess,tempaddr,4040h
    114.                                             invoke Searchcode,myProcess,tempaddr,8902h
    115.                                             add eax,1
    116.                                             mov PatchAddr,eax
    117.                                             invoke WriteProcessMemory,myProcess,eax,CTEXT(16h),1,0
    118.                                         .elseif eax==BP3
    119.                                                 invoke WriteProcessMemory,myProcess,BP3,addr BP3_data,2,0
    120.                                                 invoke WriteProcessMemory,myProcess,BP4,CTEXT(0EBh,0FEh),2,0  
    121.                                                 mov eax,Context.regEcx
    122.                                                 sub eax,DllBase
    123.                                                 mov [esi].Name1,eax
    124.                                                 mov eax, Context.regEdi
    125.                                                 .if borland_flag!=TRUE             
    126.                                                 pushad
    127.                                                 invoke  ReadProcessMemory,myProcess,Context.regEdi,addr Buffer,4,0            
    128.                                                 .if Buffer!=0
    129.                                                         mov ebx,PatchAddr                                                                  
    130.                                                         sub ebx,3                                                                          
    131.                                                         invoke WriteProcessMemory,myProcess,ebx,CTEXT(90h,90h,90h,90h),4,0      
    132.                                                     mov borland_flag,TRUE
    133.                                                         invoke OutputInfo,CTEXT("Borland stuff detected - inject code",13,10,0),0
    134.                                                 .endif
    135.                                                 no_borland:
    136.                                                 popad
    137.                                             .endif
    138.                                                 sub eax,ImageBase
    139.                                                 mov [esi].FirstThunk,eax
    140.                                                 add esi,sizeof IMAGE_IMPORT_DESCRIPTOR
    141.                                                 inc dlls_imported
    142.                                         .elseif eax==BP4
    143.                                             invoke WriteProcessMemory,myProcess,BP4,addr BP4_data,2,0
    144.                                             invoke WriteProcessMemory,myProcess,BP3,CTEXT(0EBh,0FEh),2,0
    145.                                         .elseif eax==BP2
    146.                                                 invoke WriteProcessMemory,myProcess,BP2,addr BP2_data,2,0
    147.                                                 invoke wsprintf,addr OutBuff,CTEXT("Set BreakPoint in %.08X  ",13,10,0),BP2
    148.                                                 invoke OutputInfo,addr OutBuff,0  
    149.                                             mov eax,Context.regEax
    150.                                             invoke wsprintf,addr OutBuff,CTEXT("%s: %.08X",13,10,0),CTEXT(13,10,"Find OEP"),eax
    151.                                             invoke OutputInfo,addr OutBuff,0
    152.                                             invoke MessageBox,0,addr OutBuff,CTEXT("Got OEP"),MB_OK
    153.                                             jmp @f
    154.                                         .endif
    155.                                         invoke ResumeThread,myThread
    156.                                         invoke Sleep,8
    157.                                         jmp @loop
    158.                                  @@:        
    159.                                         call FixIAT
    160.                                         call NewPEinfo
    161.                                 .endif
    162. @continue:
    163.                        .endif  
    164.                .elseif DBEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
    165.                        
    166.                .endif
    167.                invoke ContinueDebugEvent,DBEvent.dwProcessId,DBEvent.dwThreadId,dwDebugOperation
    168.        .endw  
    169. @@exit:      
    170.         invoke CloseHandle,myProcess
    171.         invoke CloseHandle,ProcessInfo.hThread        
    172.     ret
    173.  
    174. ProcessDll endp
     
  2. laomms

    laomms New Member

    Публикаций:
    0
    Регистрация:
    10 май 2007
    Сообщения:
    3
    I think I have found the solution.
    Код (Text):
    1. ProcessDll proc
    2. LOCAL Buffer [64]:byte
    3. LOCAL EP,EP_data:dword
    4. LOCAL hProcess:dword
    5. LOCAL tempaddr:dword
    6. LOCAL dlls_imported,PatchAddr:dword
    7. LOCAL szContext[1024]:byte
    8. LOCAL TEB,LDR:dword
    9.  
    10.         invoke GetTempPath, sizeof TempPath, addr TempPath
    11.         invoke wsprintf,addr OutBuff,addr Format,addr loaddllPath,addr FilePath  
    12.         invoke GetStartupInfo,addr startinfo
    13.         invoke CreateProcess,NULL,addr OutBuff,NULL,NULL,FALSE,DEBUG_PROCESS+DEBUG_ONLY_THIS_PROCESS ,NULL,addr TempPath,addr startinfo,addr ProcessInfo
    14.         .while TRUE        
    15.                 invoke WaitForDebugEvent,addr DBEvent,INFINITE       
    16.                 .if DBEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT                             
    17.                         invoke OutputInfo,CTEXT("Exit Debug"),0                      
    18.                         .break
    19.                 .elseif DBEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT
    20.                     mov edx,DBEvent.u.CreateProcessInfo.hProcess
    21.                     mov myProcess,edx
    22.                     mov ecx,DBEvent.u.CreateProcessInfo.hThread
    23.                     mov myThread,ecx  
    24.                 .elseif DBEvent.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT
    25.                         mov eax,DBEvent.u.LoadDll.lpBaseOfDll
    26.                         mov dwModBase,eax
    27.                         .if  dwModBase < 80000000h
    28.                                 invoke ReadProcessMemory,myProcess,DBEvent.u.LoadDll.lpImageName,addr OutBuff,4,NULL
    29.                                 mov eax,dword ptr OutBuff
    30.                                 invoke ReadProcessMemory,myProcess,eax,addr pDllName,256,NULL
    31.                                 invoke Unicode2Ansi,addr pDllName,addr outinf
    32.                                 invoke lstrcmp,addr outinf,addr FilePath
    33.                                 .if eax==0
    34.                                         invoke wsprintf,addr OutBuff,CTEXT("LoadDll succes: handle:%08Xh,base:%08Xh,name:%s",13,10,0),DBEvent.u.LoadDll.hFile,DBEvent.u.LoadDll.lpBaseOfDll,addr outinf
    35.                                         invoke OutputInfo,addr OutBuff,0
    36.                                         mov esi,DBEvent.u.LoadDll.lpBaseOfDll
    37.                                         mov DllBase,esi
    38.                                         add esi,EntryPiont
    39.                                         mov firstbp,esi
    40.                                         mov eax,DBEvent.u.LoadDll.hFile
    41.                                         mov hDll,eax
    42.                                 .endif   
    43.                        .endif              
    44.                 .elseif DBEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
    45.                        .if DBEvent.u.Exception.pExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT
    46.                                mov Context.ContextFlags, CONTEXT_FULL
    47.                                invoke GetThreadContext, ProcessInfo.hThread, addr Context
    48.                                mov eax,Context.regEip
    49.                                mov ecx,firstbp
    50.                                inc ecx
    51.                                .if eax == ecx  
    52.                                        dec Context.regEip
    53.                                        invoke wsprintf,addr OutBuff,CTEXT("breakpoint at %.08X  ",13,10,0),Context.regEip
    54.                                        invoke OutputInfo,addr OutBuff,0                                        
    55.                                        mov edi,Context.regEdi
    56.                                        mov DllBase,edi                        
    57.                                        mov eax,Context.regEax
    58.                                        mov firstbp,eax
    59.                                        mov ecx,Context.regEcx
    60.                                        mov SizeOfImage,ecx
    61.                                        mov edx,Context.regEdx
    62.                                        mov oldbyte,edx
    63.                                        invoke WriteProcessMemory,ProcessInfo.hProcess,Context.regEip,oldbyte,1,0
    64.                                        invoke wsprintf,addr OutBuff,CTEXT("EP: %.08X,Base: %.08X,SizeImage: %.08X ",13,10,0),firstbp,DllBase,SizeOfImage
    65.                                        invoke OutputInfo,addr OutBuff,0  
    66.                                        invoke MessageBox,0,addr OutBuff,CTEXT("dll info"),MB_OK
    67.                                        invoke TerminateProcess,myProcess,0
    68.                                .endif
    69.                                invoke ContinueDebugEvent,DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_CONTINUE
    70.                                .continue                                       
    71.                        .endif
    72.                 .endif
    73.                 invoke ContinueDebugEvent,DBEvent.dwProcessId,DBEvent.dwThreadId,DBG_CONTINUE
    74.         .endw  
    75. @@exit:      
    76.         invoke CloseHandle,myProcess
    77.         invoke CloseHandle,ProcessInfo.hThread        
    78.     ret
    79.  
    80. ProcessDll endp