Skip to content

Commit b0d2949

Browse files
committed
Ensure no race conditions on handlers
Configurable WfsDelay
1 parent a83ca2b commit b0d2949

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/msf/core/handler/reverse_hop_http.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class << self; attr_accessor :hop_handlers end
3232
attr_accessor :current_url # :nodoc:
3333
attr_accessor :control # :nodoc:
3434
attr_accessor :refs # :nodoc:
35+
attr_accessor :lock # :nodoc:
3536

3637
#
3738
# Keeps track of what hops have active handlers
@@ -59,14 +60,16 @@ def self.general_handler_type
5960
def setup_handler
6061
self.handlers = {}
6162
self.closed_handlers = {}
63+
self.lock = Mutex.new
6264
end
6365

6466
#
6567
# Starts the handler along with a monitoring thread to handle data transfer
6668
#
6769
def start_handler
70+
# Our HTTP client and URL for talking to the hop
6871
uri = URI(full_uri)
69-
# Our HTTP client for talking to the hop
72+
self.control = "#{uri.request_uri}control"
7073
self.mclient = Rex::Proto::Http::Client.new(
7174
uri.host,
7275
uri.port,
@@ -87,9 +90,7 @@ def start_handler
8790
ReverseHopHttp.hop_handlers[full_uri] = self
8891
self.monitor_thread = Rex::ThreadFactory.spawn('ReverseHopHTTP', false, uri,
8992
self) do |uri, hop_http|
90-
control = "#{uri.request_uri}control"
91-
hop_http.control = control
92-
hop_http.send_new_stage(control) # send stage to hop
93+
hop_http.send_new_stage # send stage to hop
9394
delay = 1 # poll delay
9495
# Continue to loop as long as at least one handler or one session is depending on us
9596
until hop_http.refs < 1 && hop_http.handlers.empty?
@@ -112,13 +113,17 @@ def start_handler
112113
urlen = received.slice!(0,4).unpack('V')[0]
113114
urlpath = received.slice!(0,urlen)
114115

116+
# do not want handlers to change while we dispatch this
117+
hop_http.lock.lock
115118
#received is now the binary contents of the message
116119
if hop_http.handlers.include? urlpath
117120
pack = Rex::Proto::Http::Packet.new
118121
pack.body = received
119122
hop_http.current_url = urlpath
120123
hop_http.handlers[urlpath].call(hop_http, pack)
124+
hop_http.lock.unlock
121125
elsif !closed_handlers.include? urlpath
126+
hop_http.lock.unlock
122127
#New session!
123128
conn_id = urlpath.gsub("/","")
124129
# Short-circuit the payload's handle_connection processing for create_session
@@ -132,7 +137,9 @@ def start_handler
132137
:ssl => false,
133138
})
134139
# send new stage to hop so next inbound session will get a unique ID.
135-
hop_http.send_new_stage(control)
140+
hop_http.send_new_stage
141+
else
142+
hop_http.lock.unlock
136143
end
137144
end
138145
hop_http.monitor_thread = nil #make sure we're out
@@ -163,8 +170,10 @@ def add_resource(res, opts={})
163170
# Removes a resource.
164171
#
165172
def remove_resource(res)
173+
lock.lock
166174
handlers.delete(res)
167175
closed_handlers[res] = true
176+
lock.unlock
168177
end
169178

170179
#
@@ -230,7 +239,7 @@ def initialize(info = {})
230239
#
231240
# Generates and sends a stage up to the hop point to be ready for the next client
232241
#
233-
def send_new_stage(control)
242+
def send_new_stage
234243
conn_id = generate_uri_checksum(URI_CHECKSUM_CONN) + "_" + Rex::Text.rand_text_alphanumeric(16)
235244
url = full_uri + conn_id + "/\x00"
236245

modules/payloads/stagers/windows/reverse_hop_http.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def initialize(info = {})
2424
'Arch' => ARCH_X86,
2525
'Handler' => Msf::Handler::ReverseHopHttp,
2626
'Convention' => 'sockedi http',
27+
'DefaultOptions' => { 'WfsDelay' => 30 },
2728
'Stager' =>
2829
{
2930
'Offsets' =>
@@ -284,11 +285,4 @@ def generate
284285
self.module_info['Stager']['Assembly'] = payload_data.to_s
285286
super
286287
end
287-
288-
#
289-
# Always wait at least 20 seconds for this payload (due to staging delays)
290-
#
291-
def wfs_delay
292-
20
293-
end
294288
end

0 commit comments

Comments
 (0)