Advertisement
FlyFar

DLL Injector Source Code

Jun 10th, 2023
1,640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.73 KB | Cybersecurity | 0 0
  1. #cs ----------------------------------------------------------------------------
  2.  
  3.     AutoIt Version: 3.3.8.1
  4.     Author: Naker90
  5.  
  6.     Script Function:
  7.     Inyecta una DLL en un proceso en ejecucion
  8.  
  9.     Testeado en W7 x32 y x64
  10.  
  11.     Agradecimientos a M3 & Pink
  12.  
  13.     Ejemplo de uso:
  14.     InjectDLL('C:\Users\Naker90\Desktop\Mensaje.dll', 'calc.exe')
  15.  
  16. #ce ----------------------------------------------------------------------------
  17.  
  18. Func InjectDLL($sDLLPath, $sProcess)
  19.  
  20.     Const $PROCESS_ALL_ACCESS = 0x1F0FFF
  21.     Const $MEM_RESERVE = 0x00001000
  22.     Const $PAGE_READWRITE = 0x40
  23.  
  24.     ;Obtenemos el PID del proceso a injectar
  25.     Local $sProcessID = ProcessExists($sProcess)
  26.  
  27.     ;Si el PID es 0, ejecutamos el proceso y obtenemos el PID de este
  28.     If $sProcessID = 0 Then
  29.         ShellExecute($sProcess)
  30.         ;Damos tiempo para que se abra el proceso, si no se da el tiempo de espera el proceso se cuelga
  31.         Sleep(500)
  32.         $sProcessID = ProcessExists($sProcess)
  33.     EndIf
  34.  
  35.     ;Abrimos el objeto del proceso existente
  36.     Local $sOpen = DllCall('Kernel32.dll', 'handle', 'OpenProcess', 'dword', $PROCESS_ALL_ACCESS, 'bool', False, 'dword', $sProcessID)
  37.     ;depuramos para saber si existe algun error
  38.     If Not ($sOpen[0]) Then
  39.         MsgBox(64, 'ERROR', 'Hubo un error abriendo el proceso ' & $sProcess)
  40.     EndIf
  41.  
  42.     ;Reservamos memoria en el objeto del proceso que abrimos anteriormente
  43.     Local $sStruct = DllStructCreate('byte[' & BinaryLen($sDLLPath) & ']')
  44.     Local $sMemory = DllCall('Kernel32.dll', 'handle', 'VirtualAllocEx', 'handle', $sOpen[0], 'ptr', 0, 'ULONG_PTR', DllStructGetSize($sStruct), 'dword', $MEM_RESERVE, 'dword', $PAGE_READWRITE)
  45.     ;depuramos para saber si existe algun error
  46.     If Not ($sMemory[0]) Then
  47.         MsgBox(64, '', 'Hubo un error reservando memoria')
  48.     EndIf
  49.  
  50.     ;Escribimos en el espacio de memoria obtenido anteriormente la ruta de la DLL
  51.     Local $sWriteMemory = DllCall('Kernel32.dll', 'int', 'WriteProcessMemory', 'handle', $sOpen[0], 'ptr', $sMemory[0], 'str', $sDLLPath, 'ULONG_PTR', DllStructGetSize($sStruct), 'ULONG_PTR', 0)
  52.     ;depuramos para saber si existe algun error
  53.     If Not ($sWriteMemory[0]) Then
  54.         MsgBox(64, '', 'Hubo un error en la escritura en memoria')
  55.     EndIf
  56.  
  57.     ;Obtenemos la direccion de LoadLibrary, para ello utilizamos GetProcAdress.
  58.     Local $sGetModuleHandle = DllCall('Kernel32.dll', 'handle', 'GetModuleHandleA', 'str', 'Kernel32.dll')
  59.     Local $sGetProcAdress = DllCall('Kernel32.dll', 'handle', 'GetProcAddress', 'dword', $sGetModuleHandle[0], 'str', 'LoadLibraryA')
  60.     ;depuramos para saber si existe algun error
  61.     If Not ($sGetProcAdress[0]) Then
  62.         MsgBox(64, '', 'Hubo un error obteniendo la direccion de LoadLibrary')
  63.     EndIf
  64.  
  65.     ;Lanzamos un hilo con CreateRemoteThread, dando como punto de entrada la direccion de LoadLibrary
  66.     Local $sRemoteThread = DllCall('Kernel32.dll', 'Bool', 'CreateRemoteThread', 'int', $sOpen[0], 'ptr', 0, 'ULONG_PTR', 0, 'ptr', $sGetProcAdress[0], 'ptr', $sMemory[0], 'dword', 0, 'dword', 0)
  67.     ;depuramos para saber si existe algun error
  68.     If Not ($sRemoteThread[0]) Then
  69.         MsgBox(64, '', 'Hubo un error lanzando el hilo')
  70.     EndIf
  71.  
  72.     ;Esperamos hasta que el objeto este señalado (0x7FFFFFFF = INFINITE)
  73.     Local $sWait = DllCall('Kernel32.dll', 'ptr', 'WaitForSingleObject', 'handle', $sRemoteThread[0], 'dword', 0x7FFFFFFF)
  74.  
  75.     ;Una vez terminada la inyeccion cerramos los handles abiertos
  76.     Local $sClose1 = DllCall('Kernel32.dll', 'int', 'CloseHandle', 'handle', $sOpen[0])
  77.     Local $sClose2 = DllCall('Kernel32.dll', 'int', 'CloseHandle', 'handle', $sMemory[0])
  78.     ;Volbemos a depurar
  79.     If Not ($sClose1[0]) And ($sClose2[0]) Then
  80.         MsgBox(64, '', 'Error cerrando los handles')
  81.     EndIf
  82.  
  83.     If $sRemoteThread[0] = False Then
  84.         MsgBox(64, '', 'Error Injectando la DLL')
  85.     Else
  86.         MsgBox(64, '', 'DLL Injectada con exito')
  87.     EndIf
  88. EndFunc   ;==>InjectDLL
Tags: dllinjector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement