home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 October / CMCD1004.ISO / Software / Shareware / Utilitare / pec / pec2setup.exe / pec2hooks / pec2hooks_fastimport / fastimport.asm next >
Encoding:
Assembly Source File  |  2004-06-29  |  7.3 KB  |  388 lines

  1.  
  2. ; -------------------------------------------------------
  3. ; PECompact v2 API Hook Module - Loader plugin-plugin
  4. ; (c)2004 Bitsum Technologies
  5. ; -------------------------------------------------------
  6. ; Compatible with MASM.
  7. ;
  8. ; WARNING: This code is under development and has not 
  9. ;  been fully commented and refined.
  10. ;
  11. ;
  12.  
  13. option casemap:none 
  14. .486p
  15. .model flat,stdcall
  16.     
  17. ; ------------------------------------------------
  18. ; Include files
  19. ; ------------------------------------------------
  20.  
  21. include ..\pec2hooks\miscdef.inc
  22. include ..\pec2hooks\pec2hooks.inc
  23.  
  24. ; ------------------------------------------------
  25. ; public symbols
  26. ; ------------------------------------------------
  27.  
  28. public GetPointerToHookData
  29. public GetHookDataSize    
  30.  
  31. ; ------------------------------------------------
  32. ; Function prototypes
  33. ; ------------------------------------------------
  34. GetPointerToHookData PROTO
  35. GetHookDataSize PROTO
  36. GetHookFlags PROTO
  37.  
  38. Initialize PROTO pHookData:DWORD
  39. LoadLibraryAHook PROTO pszModule:DWORD
  40. GetProcAddressWithHintHook PROTO hModule:DWORD, pszApi:DWORD, nHint:DWORD
  41.  
  42. .code
  43.  
  44. GetPointerToHookData proc
  45.     lea eax,HookData
  46.     ret
  47. GetPointerToHookData endp
  48.  
  49. GetHookDataSize proc
  50.     mov eax,offset HookDataEnd-offset HookData
  51.     ret
  52. GetHookDataSize endp
  53.  
  54. GetHookFlags proc
  55.     mov eax,PEC2_HOOKS_FLAG_MANDATE_LAST_IN_ORDER
  56.     ret
  57. GetHookFlags endp
  58.  
  59. ;-------------------------------------------------
  60. ; HookData 
  61. ;;-------------------------------------------------
  62. HookData HookDataInfo <size HookDataInfo, PEC2_HOOKS_VERSION, \
  63.             offset Initialize-offset HookData, \
  64.             0, \   
  65.             offset LoadLibraryAHook-offset HookData, \
  66.             offset GetProcAddressWithHintHook-offset HookData>
  67.  
  68. g_pLoadLibraryA dd 0
  69. g_pGetProcAddressWithHint dd 0
  70.  
  71. szK32 db 'kernel32',0
  72. szGMHA db 'GetModuleHandleA',0
  73. g_pGetModuleHandleA dd 0
  74.  
  75. ;-------------------------------------------
  76. ;
  77. ;        
  78. Initialize proc uses ebx edi esi pHookDataInfo:DWORD
  79.     GET_DELTA ebx
  80.     
  81.     mov esi,pHookDataInfo
  82.     lea edi,g_pLoadLibraryA[ebx]
  83.     mov ecx,2
  84.     cld
  85.     rep movsd
  86.     
  87.     ; todo: we need to freelibrary on kernel32 -- add deinitialize func?
  88.     lea eax,szK32[ebx]
  89.     push eax
  90.     call LoadLibraryAHook
  91.     
  92.     push 0
  93.     lea ecx,szGMHA[ebx]
  94.     push ecx
  95.     push eax
  96.     call GetProcAddressWithHintHook
  97.     mov g_pGetModuleHandleA[ebx],eax
  98.     
  99.     ret
  100. Initialize endp
  101.  
  102. ;-------------------------------------------
  103. ;
  104. ;
  105. LoadLibraryAHook proc uses ebx edi esi pszModule:DWORD
  106.     GET_DELTA ebx
  107.     push pszModule
  108.     call g_pLoadLibraryA[ebx]
  109.     ret
  110. LoadLibraryAHook endp
  111.  
  112. MYSTRCMP MACRO
  113.     LOCAL endme
  114.     LOCAL loopme
  115.     LOCAL endgood
  116.     LOCAL endgreat
  117.     LOCAL endless
  118.     
  119.   loopme:
  120.     mov al,byte ptr [esi]
  121.     mov ah,byte ptr [edi]
  122.     inc esi
  123.     inc edi
  124.     test ax,ax
  125.     jz endgood
  126.     cmp al,ah
  127.     ja endgreat
  128.     jb endless
  129.     jmp loopme
  130.   endgood:
  131.     xor eax,eax
  132.     jmp endme
  133.     endgreat:
  134.         mov eax,1
  135.         jmp endme
  136.     endless:
  137.         mov eax,-1        
  138.     endme:        
  139. ENDM
  140.  
  141. ;-------------------------------------------
  142. ;
  143. ; GetProcAdress
  144. ;
  145. ;  uses hints
  146. ;  binary search
  147. ;  handles forwarded exports (NTDLL.Name, NTDLL.#123)
  148. ;  
  149. ;
  150. GetProcAddressWithHintHook proc uses ebx edi esi hModule:DWORD, pszApi:DWORD, nHint:DWORD
  151.     LOCAL nStartIndex:DWORD    
  152.     LOCAL nExportSize:DWORD
  153.     LOCAL szTemp1[256]:BYTE
  154.         
  155.     GET_DELTA ebx
  156.     
  157.     push hModule
  158.     call GetPointerToPEHeader
  159.     ; exports is first data directory
  160.     lea edi,([eax]+IMAGE_NT_HEADER.DataDirectories)
  161.     mov esi,[edi]
  162.     mov eax,[edi+4]
  163.     mov nExportSize,eax
  164.     add esi,hModule
  165.     ; esi-> export directory
  166.     
  167.     mov ecx,pszApi
  168.     cmp ecx,0ffffh
  169.     ja ImportByName
  170.     
  171.   ;----------------------------------------------
  172.   ;
  173.   ; ImportByOrdinal 
  174.   ;  ordinal in ecx
  175.   ;
  176.   ImportByOrdinal:    
  177.     sub ecx,([esi]+ExportDirectory.Base)
  178.     mov edi,([esi]+ExportDirectory.AddressOfFunctions)
  179.     add edi,hModule
  180.     shl ecx,2   ; *4
  181.     mov eax,[edi+ecx]
  182.     add eax,hModule
  183.     
  184.     ;
  185.     ; see if this is a forwarded export
  186.     ; check if in export section (COFF spec)
  187.     ;
  188.     cmp eax,esi
  189.     jb NotForwarder
  190.     add esi,nExportSize
  191.     cmp eax,esi
  192.     ja NotForwarder
  193.     
  194.     ;
  195.     ; format:
  196.     ;   NTDLL.FuncName
  197.     ;   NTDLL.#1
  198.     ;
  199.     ; find '.'
  200.     
  201.     lea edi,szTemp1
  202.     ; edi->DLL name
  203.     
  204.     FindDotLoop:
  205.       mov cl,byte ptr [eax]      
  206.       mov byte ptr [edi],cl
  207.       inc eax
  208.       cmp cl,'.'
  209.       jz FoundDot                  
  210.       inc edi          
  211.       jmp FindDotLoop
  212.       
  213.     FoundDot:        
  214.         
  215.       mov byte ptr [edi],0      
  216.       lea edi,szTemp1
  217.           
  218.       ; edi->DLL name
  219.       ; eax->API or ord#
  220.       mov cl,byte ptr [eax]
  221.       cmp cl,'#'
  222.       jnz IsNamedForwarder            
  223.       inc eax
  224.       push eax
  225.       call AsciiDecimalToDword      
  226.     IsNamedForwarder:    
  227.       mov esi,eax      
  228.     ImportIt:            
  229.       push edi
  230.       call g_pGetModuleHandleA[ebx]
  231.       test eax,eax
  232.       jz GotMod    
  233.       push edi
  234.       call LoadLibraryAHook    
  235.     GotMod:
  236.       ; eax=module
  237.       test eax,eax
  238.       jz NotFound
  239.       
  240.       push 0
  241.       push esi
  242.       push eax
  243.       call GetProcAddressWithHintHook                                  
  244.         
  245.     NotForwarder:
  246.     ; eax= api address
  247.         ret
  248.     
  249.     ;--------------------------------------------------------------
  250.     ;
  251.     ; ImportByHint  (index into Name Pointer Table)
  252.     ;
  253.     ;  Hint in eax    
  254.     ;
  255.  ImportByHint:   
  256.     mov edi,([esi]+ExportDirectory.AddressOfNameOrdinals)
  257.     add edi,hModule
  258.     shl eax,1
  259.     xor ecx,ecx
  260.     mov cx,[edi+eax]
  261.     
  262.     mov edx,([esi]+ExportDirectory.Base)
  263.     add ecx, edx    
  264.     
  265.     jmp ImportByOrdinal
  266.     
  267.     ;-------------------------------------------------------------
  268.     ;
  269.     ; Begin Import by name
  270.     ;    
  271.   ImportByName:
  272.  
  273.     ;
  274.     ; do binary search using hint as initial mid
  275.     ;
  276.     mov edi,([esi]+ExportDirectory.AddressOfNames)
  277.     add edi,hModule
  278.     
  279.     ; edi->Names Array
  280.     ; ecx=current index
  281.     ; 
  282.     mov nStartIndex,0
  283.     mov edx,([esi]+ExportDirectory.NumberOfNames)
  284.     dec edx
  285.     ; edx holds end index
  286.     
  287.     xor eax,eax
  288.     add eax,nHint    
  289.     jz BinarySearch 
  290.     cmp edx,eax
  291.     jb BinarySearch  ;; hint out of bounds, ignore it
  292.     
  293.     ;
  294.     ; hint is available so use it as initial mid
  295.     ;                    
  296.     mov ecx,eax
  297.     jmp StartBinarySearchWithHint
  298.     
  299.       BinarySearch:                    
  300.           mov ecx,edx
  301.           mov eax,nStartIndex
  302.           ; ecx=last
  303.           ; eax=first          
  304.           cmp eax,ecx
  305.           ja NotFound
  306.           add ecx,eax                  
  307.           shr ecx,1       ; /2    
  308.           ;ecx=mid ...
  309.      StartBinarySearchWithHint:          
  310.           
  311.           mov eax,[edi+(ecx*4)]
  312.         add eax,hModule
  313.         
  314.         push ecx
  315.         push edx
  316.         push edi
  317.         push esi
  318.         
  319.         mov edi,pszApi
  320.         mov esi,eax
  321.         MYSTRCMP
  322.         
  323.         pop esi
  324.         pop edi
  325.         pop edx
  326.         pop ecx
  327.                 
  328.         ;
  329.         ; if eax > 0 .. then current api > sought api
  330.         ; if eax < 0 ... then current api < sought api
  331.         ; 
  332.         
  333.         cmp eax,0
  334.         jz FoundMatch    
  335.         jg GreaterThan        
  336.         ;
  337.         ; LessThan
  338.         ;  - discard lower half
  339.         inc ecx
  340.         mov nStartIndex,ecx        
  341.         jmp BinarySearch
  342.         
  343.         GreaterThan:
  344.         dec ecx
  345.         mov edx,ecx
  346.         jmp BinarySearch            
  347.         
  348.      FoundMatch:
  349.          ;
  350.         ; ecx=index into names/ordinals array
  351.         ;
  352.         mov eax,ecx
  353.         jmp ImportByHint        
  354.             
  355.     NotFound:    
  356.     xor eax,eax            
  357.     ret
  358. GetProcAddressWithHintHook endp
  359.  
  360. GetPointerToPEHeader proc hImageBase:DWORD    
  361.     mov eax,hImageBase
  362.     add eax,([eax]+IMAGE_DOS_HEADER.e_lfanew)
  363.     ret
  364. GetPointerToPEHeader endp
  365.  
  366. ; DWORD AsciiDecimalToDword(char *string);
  367. AsciiDecimalToDword proc uses ebx edi esi src:DWORD    
  368.     mov    esi,src
  369.     xor    eax,eax    
  370.     xor    ebx,ebx    
  371. asz_loop:
  372.     mov    bl,byte ptr [esi]
  373.     inc    esi    
  374.     or    bl,bl
  375.     jz    end_as
  376.     and ebx,0fh    
  377.     imul eax,10
  378.     add    eax,ebx
  379. next_as_iter:
  380.     jmp    asz_loop
  381. end_as:    
  382.     ret        
  383. AsciiDecimalToDword endp
  384.  
  385. HookDataEnd:
  386. END
  387.