home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Stone / windows / stnapih.arj / STNAPIH.ASM < prev    next >
Assembly Source File  |  1998-02-20  |  6KB  |  265 lines

  1. comment % A Windows NT pseudo resident Patcher - sourcecodes by Stone / UCF '98
  2.       Thanks to Patriarch the almight.
  3.       Jammer/UCF, Acp/UCF and Net Walker/Brazil :)
  4.  
  5.       A lot of the concepts and some of the code here is adopted from
  6.       APISPY by Matt Pietrek 1995.
  7.  
  8.  
  9.                 email: stone@one.se
  10.                 http://www.one.se/~stone
  11.                 
  12.                     2nd&mi!
  13.  
  14.     %
  15.  
  16. CHUNKVA EQU 40305ch
  17. AbusePage EQU 402000h
  18.  
  19. reg_Eip EQU 0b8h
  20.  
  21. .386P
  22. Locals
  23. jumps
  24.  
  25. .Model Flat ,StdCall
  26. Extrn    ReadProcessMemory    : PROC
  27. Extrn     WriteProcessMemory     : PROC
  28. Extrn     LoadLibrary         : PROC
  29. Extrn    WaitForDebugEvent    : PROC
  30. Extrn    LoadLibraryA        : PROC
  31. Extrn   ContinueDebugEvent    : PROC
  32. Extrn    GetProcAddress        : PROC
  33. Extrn   GetThreadContext    : PROC
  34. Extrn    SetThreadContext    : PROC
  35.  
  36. UNICODE=0
  37. include w32.inc
  38.  
  39. ;──────────────────────────────────────────────────────────────────────────────
  40. .Data  
  41. FileName     db "target.exe",0
  42. LibName        db "flaf.dll",0
  43. Kernel        db "kernel32.dll",0
  44. GetProcA    db "GetProcAddress",0
  45. LoadLib        db "LoadLibraryA",0
  46.  
  47. hProcess     dd 0            ; Process Info Structure
  48. hThread      dd 0 
  49. dwProcessID     dd 0
  50. dwThreadID      dd 0
  51.  
  52. continueflags   dd 010002h 
  53.  
  54. dwDebugEventCode dd 0            ; DebugEvent Structure
  55. _dwProcessID     dd 0
  56. _dwThreadID     dd 0
  57. U db 63h dup (0)            ; space for structures
  58.  
  59. ; The context structure *MUST* be at an address A so that (A and 3) = 0 
  60. ; Why? - don't ask me why.. WIndows NT if fucked up wierd on that account!
  61. ; took me a couple of hours debugging to find that out!
  62. Context        dd 00010001h        ; Context flags.
  63.         dd 33 dup (0)        ; Space for Thread registers
  64.                     
  65. StartUpInfo      dd 44h             ; size of startup info-structure
  66.         db 44h dup (0)
  67.  
  68. NumberOfBytes    dd 0             ; this many bytes written
  69. PatchThisOff    dd AbusePage        ; patch at this offset
  70.  
  71.  
  72.  
  73. MoooH        db "FLAF",0        ; name of function in DLL
  74. SaveEip     dd 0
  75. ReadBuffer     db 1000h dup (0)    ; one full page.
  76. FirstEvent     db TRUE
  77.  
  78. .Code                                  
  79. ;──────────────────────────────────────────────────────────────────────────────
  80. Main:
  81.     
  82.     call CreateProcessA, 0, offset FileName,  0,  0, FALSE, DEBUG_ONLY_THIS_PROCESS, 0, 0, offset StartUpInfo, offset hProcess
  83.     test eax,eax
  84.     jz   abort
  85.  
  86. RunProgram:
  87.     call WaitForDebugEvent, offset dwDebugEventCode, -1
  88.     
  89.     cmp dword ptr [dwDebugEventCode],3
  90.     jnz RunOn1
  91.     lea edi, U                ; delete U
  92.     mov ecx, 40
  93.     mov al,0
  94.     rep stosb
  95.     jmp RunOn
  96. RunOn1:
  97.     cmp dword ptr [dwDebugEventCode],1
  98.     jnz RunOn
  99.     cmp dword ptr [U], STATUS_BREAKPOINT
  100.     jnz RunOn
  101.     call BreakPointEncountered
  102.  
  103. RunOn:
  104.     cmp dword ptr [dwDebugEventCode], 5 ; Exit_Process (Program terminated)
  105.     jz ProgramTerminated
  106.  
  107.  
  108.     push dword ptr [continueflags]
  109.     push dword ptr [_dwThreadID]
  110.     push dword ptr [_dwProcessID]
  111.     call ContinueDebugEvent           ; let the Program run on
  112.     
  113.  
  114.     jmp RunProgram
  115.  
  116. ProgramTerminated:
  117.  
  118.  
  119. abort:
  120.     Push LARGE-1
  121.     Call ExitProcess              ; can't you guess? :)
  122.  
  123. BreakPointEncountered PROC
  124.  
  125.     cmp byte ptr [FirstEvent], TRUE    ; first time we inject the DLL
  126.     jz FirstTime
  127.  
  128.     Call RestoreProcess        ; second time we extract our code
  129.     ret
  130.  
  131. FirstTime:
  132.     mov [FirstEvent], FALSE
  133.     call CalculateCode        ; calculate the code that loads the DLL
  134.                     ; and "hooks the API"
  135.     call InjectDLL            ; inject the DLL
  136.  
  137.     ret
  138. BreakPointEncountered EndP
  139.  
  140. RestoreProcess PROC
  141.     push offset NumberOfBytes    ; Bytes written
  142.     push 1000h
  143.     push offset ReadBuffer
  144.     push [PatchThisOff]        ; write to here
  145.     push [hProcess]         ; using this handle
  146.     call WriteProcessMemory
  147.  
  148.     mov eax, [SaveEip]        ; restore EIP
  149.     mov [Context+reg_Eip],eax
  150.  
  151.     push offset Context        ; reSet Registers of child
  152.     push [hThread]
  153.     call SetThreadContext
  154.  
  155.     ret
  156. RestoreProcess ENDP
  157.  
  158. InjectDLL PROC
  159.     push offset NumberOfBytes    ; Bytes written
  160.     push 1000h            ; [HowManyToPatch]
  161.     push offset ReadBuffer
  162.     push [PatchThisOff]        ; Read From here
  163.     push [hProcess]         ; using this handle
  164.     call ReadProcessMemory
  165.  
  166.     push offset NumberOfBytes    ; Bytes written
  167.     push AppEnd-AppStart
  168.     push offset AppStart
  169.     push [PatchThisOff]        ; write to here
  170.     push [hProcess]         ; using this handle
  171.     call WriteProcessMemory
  172.  
  173.  
  174.     push offset Context        ; Get Registers of child
  175.     push [hThread]
  176.     call GetThreadContext
  177.     
  178.     mov eax, [Context+reg_Eip]    ; save EIP
  179.     mov [SaveEip], eax
  180.  
  181.     mov eax, [PatchThisOff]        ; Get Entrypoint - and set it as ThreadID
  182.     mov [Context+reg_Eip],eax
  183.  
  184.     push offset Context        ; Get Registers of child
  185.     push [hThread]
  186.     call SetThreadContext
  187.  
  188.     ret
  189. InjectDLL ENDP
  190.  
  191. CalculateCode PROC
  192. ;----- Fetch addresses for LoadLib og GetProcAddress 
  193.     push offset Kernel        ; find the Handle of Kernel32.dll
  194.     call GetModuleHandleA
  195.  
  196.     push eax
  197.  
  198.     push offset LoadLib        ; find the LoadLibraryA entrypoint
  199.     push eax
  200.     call GetProcAddress
  201.     mov Dword ptr [AppLoadLib], eax ; save it
  202.  
  203.     pop eax
  204.     push offset GetProcA        ; get the GetProcessAddress ep
  205.     push eax
  206.     call GetProcAddress
  207.     mov dword ptr [AppGetProc], eax ; save it
  208.  
  209.     ret
  210. CalculateCode ENDP
  211.  
  212.  
  213. .data
  214. WriteThis PROC
  215. AppStart:
  216.     call Next            ; Calculate "Delta offset"
  217. Next:    pop ebp                ; Not really need - because we know
  218.     sub ebp, offset Next        ; the VA of this code - but it's
  219.                     ; a convinient way of dealing with
  220.                     ; delta-problems
  221.     mov eax, offset AppKernel
  222.     add eax, ebp            ; Adjust for Delta
  223.     push eax
  224.  
  225.     mov edx, offset AppLoadLib
  226.     add edx, ebp
  227.  
  228.  
  229.     call dword ptr [EDX]        ; Call LoadLibraryA
  230.                         
  231.     mov ebx, offset AppFlaf            
  232.     add ebx, ebp
  233.  
  234.     push ebx
  235.     push eax
  236.     mov edx, offset AppGetProc
  237.     add edx, ebp
  238.     call dword ptr [EDX]        ; call GetProcessAddress
  239.  
  240.     mov edx, [eax+2]    
  241.  
  242.     mov ebx, offset ChunkVA
  243.     add ebx, ebp
  244.     mov ebx, [ebx]            ; EBX = ChunkVA
  245.     mov ecx, [ebx]
  246.     
  247.     mov [EDX],ecx            ; IAT entry to "Own IAT" in DLL
  248.     mov [ebx], eax            ; DLL Function to IAT
  249.  
  250.     int 3                ; return to debugger
  251.  
  252. AppKernel    db "flaf.dll",0
  253. AppFlaf        db "FLAF",0
  254.  
  255. AppGetProc    dd 0
  256. AppLoadLib    dd 0
  257. ChunkVA     dd CHUNKVA        ; Chunk VA of Function to intercept
  258.  
  259. AppEnd:
  260.  
  261. WriteThis EndP
  262.  
  263. ;──────────────────────────────────────────────────────────────────────────────
  264. End Main
  265.