Advertisement
Guest User

Inject.au3

a guest
Oct 26th, 2013
2,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 9.70 KB | None | 0 0
  1. #include-once
  2.  
  3. ; #INDEX# =======================================================================================================================
  4. ; Title .........: Inject UDF library for AutoIt v3
  5. ; AutoIt Version : 3.3.4, Inject.au3 v 1.1 (1/2/2010)
  6. ; Language ......: English
  7. ; Description ...: Functions for getting process information and for in/ejecting a .dll file into/off a process
  8. ; Requirements ..: NomadMemory.au3, Memory.au3, WinAPI.au3, Kernel32.dll  ;Script has to be compiled in x86 Mode  ;#RequireAdmin
  9. ; Author(s) .....: Deathly Assassin (http://www.autoitbot.de)
  10. ; ===============================================================================================================================
  11.  
  12. ; #REQUIRED INCLUDES# ===========================================================================================================
  13. #include <Memory.au3>
  14. #include <WinAPI.au3>
  15. #include <NomadMemory.au3>
  16. ; ===============================================================================================================================
  17.  
  18. ; #CURRENT# =====================================================================================================================
  19. ;_InjectAttachDll
  20. ;_InjectDetachDllEx
  21. ;_InjectDetachDll
  22. ;_InjectModulInfo
  23. ; ===============================================================================================================================
  24.  
  25. ; #FUNCTION# ====================================================================================================================
  26. ; Name...........: _InjectAttachDll
  27. ; Description ...: Injects a .dll file into a process
  28. ; Syntax.........: _InjectAttachDll($sPath, $PID)
  29. ; Parameters ....: $sPath - Path and filename of the .dll file to be injected
  30. ;                  $PID - A process identifier
  31. ; Return values .: Success      - Returns hModule of the injected dll
  32. ;                  Failure      - Returns @Error of the failed function and sets @Error:
  33. ;                  |@error = 1 - _MemoryOpen failed -> $PID might be wrong
  34. ;                  |@error = 2 - _MemoryWrite failed -> "SeDebugPrivilege" might not have been set. #RequireAdmin might solve this problem / $sPath might not have been found
  35. ;                  |@error = 3 - DllOpen failed -> kernel32.dll might not have been found
  36. ;                  |@error = 4 - GetExitCodeThread failed
  37. ; Author ........: Deathly Assassin (http://www.autoitbot.de)
  38. ; Modified.......:
  39. ; Remarks .......:
  40. ; Related .......:
  41. ; Link ..........:
  42. ; Example .......: Yes
  43. ; ===============================================================================================================================
  44.  
  45.  
  46. Func _InjectAttachDll($sPath, $PID)
  47.     Local $hRemote, $iLen = StringLen($sPath), $hProcess, $pAllocAdresse, $vError, $hOpen, $pLoadLibraryA, $vStruct
  48.     SetPrivilege("SeDebugPrivilege", 1)
  49.     SetError(0)
  50.  
  51.     $hProcess = _MemoryOpen($PID)
  52.  
  53.     $vError = @error
  54.     If $vError Then
  55.         SetError(1)
  56.         Return $vError
  57.     EndIf
  58.  
  59.     $pAllocAdresse = _MemVirtualAllocEx($hProcess[1], 0, $iLen + 1, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)
  60.  
  61.     _MemoryWrite($pAllocAdresse, $hProcess, $sPath, 'char[' & $iLen & ']')
  62.  
  63.     $vError = @error
  64.     If $vError Then
  65.         SetError(2)
  66.         Return $vError
  67.     EndIf
  68.  
  69.     $hOpen = DllOpen("Kernel32.dll")
  70.  
  71.     $vError = @error
  72.     If $vError Then
  73.         SetError(3)
  74.         Return $vError
  75.     EndIf
  76.  
  77.     $pLoadLibraryA = DllCall($hOpen, "HANDLE", "GetProcAddress", "HANDLE", _WinAPI_GetModuleHandle("kernel32.dll"), "str", "LoadLibraryA")
  78.     $hRemote = DllCall($hOpen, "HANDLE", "CreateRemoteThread", "HANDLE", $hProcess[1], "ptr", 0, "ptr", 0, "ptr", $pLoadLibraryA[0], "ptr", $pAllocAdresse, "DWORD", 0, "ptr", 0)
  79.  
  80.     _WinAPI_WaitForSingleObject($hRemote[0])
  81.     $vStruct = DllStructCreate("HANDLE;")
  82.     DllCall($hOpen, "BOOL", "GetExitCodeThread", "HANDLE", $hRemote[0], "ptr", DllStructGetPtr($vStruct, 1))
  83.     $vError = DllStructGetData($vStruct, 1)
  84.  
  85.     DllClose($hOpen)
  86.     _MemVirtualFreeEx($hProcess, $pAllocAdresse, $iLen, $MEM_DECOMMIT)
  87.     _MemoryClose($hProcess)
  88.  
  89.     If $vError = False Then
  90.         SetError(4)
  91.         Return $vError
  92.     EndIf
  93.  
  94.     Return $vError
  95. EndFunc   ;==>_InjectAttachDll
  96.  
  97. ; #FUNCTION# ====================================================================================================================
  98. ; Name...........: _InjectDetachDllEx
  99. ; Description ...: Ejects a .dll file off a process
  100. ; Syntax.........: _InjectDetachDllEx($hModule, $PID)
  101. ; Parameters ....: $hModule - hModule of the dll to be ejected
  102. ;                  $PID - A process identifier
  103. ; Return values .: Success      - Returns True
  104. ;                  Failure      - Returns @Error of the failed function and sets @Error:
  105. ;                  |@error = 1 - _MemoryOpen failed -> $PID might be wrong
  106. ;                  |@error = 2 - DllOpen failed -> kernel32.dll might not have been found
  107. ;                  |@error = 3 - GetExitCodeThread failed -> "SeDebugPrivilege" might not have been set. #RequireAdmin might solve this problem
  108. ; Author ........: Deathly Assassin (http://www.autoitbot.de)
  109. ; Modified.......:
  110. ; Remarks .......:
  111. ; Related .......:
  112. ; Link ..........:
  113. ; Example .......: Yes
  114. ; ===============================================================================================================================
  115.  
  116.  
  117. Func _InjectDetachDllEx($hModule, $PID)
  118.     Local $hRemote, $hProcess, $vError, $hOpen, $pFreeLibrary, $vStruct
  119.     SetPrivilege("SeDebugPrivilege", 1)
  120.     SetError(0)
  121.  
  122.     $hProcess = _MemoryOpen($PID)
  123.  
  124.     $vError = @error
  125.     If $vError Then
  126.         SetError(1)
  127.         Return $vError
  128.     EndIf
  129.  
  130.  
  131.     $hOpen = DllOpen("Kernel32.dll")
  132.  
  133.     $vError = @error
  134.     If $vError Then
  135.         SetError(2)
  136.         Return $vError
  137.     EndIf
  138.  
  139.     $pFreeLibrary = DllCall($hOpen, "HANDLE", "GetProcAddress", "HANDLE", _WinAPI_GetModuleHandle("kernel32.dll"), "str", "FreeLibrary")
  140.     $hRemote = DllCall($hOpen, "HANDLE", "CreateRemoteThread", "HANDLE", $hProcess[1], "int", 0, "int", 0, "DWORD", $pFreeLibrary[0], "ptr", $hModule, "DWORD", 0, "DWORD*", 0)
  141.     _WinAPI_WaitForSingleObject($hRemote[0])
  142.     $vStruct = DllStructCreate("BOOL;")
  143.     $vError = DllCall($hOpen, "BOOL", "GetExitCodeThread", "HANDLE", $hRemote[0], "ptr", DllStructGetPtr($vStruct, 1))
  144.     $vError = DllStructGetData($vStruct, 1)
  145.  
  146.     DllClose($hOpen)
  147.     _MemoryClose($hProcess)
  148.  
  149.     If $vError = False Then
  150.         SetError(3)
  151.         Return $vError
  152.     EndIf
  153.  
  154.     Return $vError
  155. EndFunc   ;==>_InjectDetachDllEx
  156.  
  157. ; #FUNCTION# ====================================================================================================================
  158. ; Name...........: _InjectDetachDll
  159. ; Description ...: Ejects a .dll file off a process
  160. ; Syntax.........: _InjectDetachDll($sPath, $PID)
  161. ; Parameters ....: $sPath - Path of the dll to be ejected
  162. ;                  $PID - A process identifier
  163. ; Return values .: Success      - Returns True
  164. ;                  Failure      - Returns -1 and sets @Error or -1 / Returns the return of _InjectDetachDllEx and sets _InjectDetachDllEx's @Error
  165. ;                  |@error = -1 - _InjectModulInfo failed -> Return=1:Wrong PID;     Return=2:DllOpen failed -> kernel32.dll might not have been found
  166. ;                  |@error = -2 - Modul wasn't found
  167. ; Author ........: Deathly Assassin (http://www.autoitbot.de)
  168. ; Modified.......:
  169. ; Remarks .......:
  170. ; Related .......:
  171. ; Link ..........:
  172. ; Example .......: Yes
  173. ; ===============================================================================================================================
  174.  
  175.  
  176. Func _InjectDetachDll($sPath, $PID)
  177.     Local $aArray, $i, $vError
  178.     $aArray = _InjectModulInfo($PID)
  179.     $vError = @error
  180.     If $vError Then
  181.         SetError(-1)
  182.         Return $vError
  183.     EndIf
  184.  
  185.     For $i = 0 To UBound($aArray) - 1
  186.         If $aArray[$i][9] = $sPath Then
  187.             Return _InjectDetachDllEx($aArray[$i][7], $PID)
  188.         EndIf
  189.     Next
  190.  
  191.     SetError(-2)
  192.     Return -1
  193. EndFunc   ;==>_InjectDetachDll
  194.  
  195. ; #FUNCTION# ====================================================================================================================
  196. ; Name...........: _InjectModulInfo
  197. ; Description ...: Returns information about every modul in the specified process
  198. ; Syntax.........: _InjectModulInfo($PID)
  199. ; Parameters ....: $PID - A process identifier
  200. ; Return values .: Success      - Returns an 2d array with the modules and there information
  201. ;                  Failure      - Returns -1 and sets @Error:
  202. ;                  |@error = 1 - Wrong PID
  203. ;                  |@error = 2 - DllOpen failed -> kernel32.dll might not have been found
  204. ; Author ........: Deathly Assassin (http://www.autoitbot.de)
  205. ; Modified.......:
  206. ; Remarks .......:
  207. ; Related .......:
  208. ; Link ..........:
  209. ; Example .......: Yes
  210. ; ===============================================================================================================================
  211.  
  212.  
  213. Func _InjectModulInfo($PID)
  214.     Local $hModule, $hSnapshot, $hOpen, $iCount = 0, $aArray[1][10], $i, $vMODULEENTRY32, $vNext
  215.  
  216.     If Not ProcessExists($PID) Then
  217.         SetError(1)
  218.         Return -1
  219.     EndIf
  220.  
  221.     SetPrivilege("SeDebugPrivilege", 1)
  222.     $hOpen = DllOpen("Kernel32.dll")
  223.  
  224.     If @error Then
  225.         SetError(2)
  226.         Return -1
  227.     EndIf
  228.  
  229.     $vMODULEENTRY32 = DllStructCreate("DWORD dwSize; DWORD th32ModuleID; DWORD th32ProcessID; DWORD GlblcntUsage; DWORD ProccntUsage; ptr modBaseAddr; DWORD modBaseSize; HANDLE hModule; CHAR szModule[256]; CHAR szExePath[260];")
  230.     DllStructSetData($vMODULEENTRY32, 1, DllStructGetSize($vMODULEENTRY32))
  231.     $hSnapshot = DllCall($hOpen, "HANDLE", "CreateToolhelp32Snapshot", "DWORD", 8, "DWORD", $PID)
  232.     DllCall($hOpen, "BOOL", "Module32First", "HANDLE", $hSnapshot[0], "ptr", DllStructGetPtr($vMODULEENTRY32))
  233.  
  234.     Do
  235.         ReDim $aArray[$iCount + 1][10]
  236.         For $i = 1 To 10
  237.             $aArray[$iCount][$i - 1] = DllStructGetData($vMODULEENTRY32, $i)
  238.         Next
  239.         $iCount += 1
  240.         $vNext = DllCall($hOpen, "BOOL", "Module32Next", "HANDLE", $hSnapshot[0], "ptr", DllStructGetPtr($vMODULEENTRY32))
  241.     Until Not $vNext[0]
  242.  
  243.     DllClose($hOpen)
  244.  
  245.     Return $aArray
  246. EndFunc   ;==>_InjectModulInfo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement