Skip to content

Commit 9c8c16d

Browse files
committed
Allow multiple handlers to use same hop.
1 parent 16e1280 commit 9c8c16d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lib/msf/core/handler/reverse_hop_http.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class << self; attr_accessor :hop_handlers end
3030
attr_accessor :mclient # :nodoc:
3131
attr_accessor :current_url # :nodoc:
3232
attr_accessor :control # :nodoc:
33+
attr_accessor :refs # :nodoc:
3334

3435
#
3536
# Keeps track of what hops have active handlers
@@ -63,27 +64,33 @@ def setup_handler
6364
#
6465
def start_handler
6566
uri = URI(full_uri)
66-
#Our HTTP client for talking to the hop
67+
# Our HTTP client for talking to the hop
6768
self.mclient = Rex::Proto::Http::Client.new(
6869
uri.host,
6970
uri.port,
7071
{
7172
'Msf' => framework
7273
}
7374
)
74-
#First we need to verify we will not stomp on another handler's hop
75+
@running = true # So we know we can stop it
76+
# If someone is already monitoring this hop, bump the refcount instead of starting a new thread
7577
if ReverseHopHttp.hop_handlers.has_key?(full_uri)
76-
raise RuntimeError, "Already running a handler for hop #{full_uri}."
78+
ReverseHopHttp.hop_handlers[full_uri].refs += 1
79+
return
7780
end
81+
82+
# Sometimes you just have to do everything yourself.
83+
# Declare ownership of this hop and spawn a thread to monitor it.
84+
self.refs = 1
7885
ReverseHopHttp.hop_handlers[full_uri] = self
7986
self.monitor_thread = Rex::ThreadFactory.spawn('ReverseHopHTTP', false, uri,
8087
self) do |uri, hop_http|
8188
control = "#{uri.request_uri}control"
8289
hop_http.control = control
8390
hop_http.send_new_stage(control) # send stage to hop
84-
@finish = false
8591
delay = 1 # poll delay
86-
until @finish && hop_http.handlers.empty?
92+
# Continue to loop as long as at least one handler or one session is depending on us
93+
until hop_http.refs < 1 && hop_http.handlers.empty?
8794
sleep delay
8895
delay = delay + 1 if delay < 10 # slow down if we're not getting anything
8996
crequest = hop_http.mclient.request_raw({'method' => 'GET', 'uri' => control})
@@ -135,7 +142,11 @@ def start_handler
135142
# Stops the handler and monitoring thread
136143
#
137144
def stop_handler
138-
@finish = true
145+
# stop_handler is called like 3 times, don't decrement refcount unless we're still running
146+
if @running
147+
ReverseHopHttp.hop_handlers[full_uri].refs -= 1
148+
@running = false
149+
end
139150
end
140151

141152
#

0 commit comments

Comments
 (0)