@@ -32,6 +32,7 @@ class << self; attr_accessor :hop_handlers end
32
32
attr_accessor :current_url # :nodoc:
33
33
attr_accessor :control # :nodoc:
34
34
attr_accessor :refs # :nodoc:
35
+ attr_accessor :lock # :nodoc:
35
36
36
37
#
37
38
# Keeps track of what hops have active handlers
@@ -59,14 +60,16 @@ def self.general_handler_type
59
60
def setup_handler
60
61
self . handlers = { }
61
62
self . closed_handlers = { }
63
+ self . lock = Mutex . new
62
64
end
63
65
64
66
#
65
67
# Starts the handler along with a monitoring thread to handle data transfer
66
68
#
67
69
def start_handler
70
+ # Our HTTP client and URL for talking to the hop
68
71
uri = URI ( full_uri )
69
- # Our HTTP client for talking to the hop
72
+ self . control = " #{ uri . request_uri } control"
70
73
self . mclient = Rex ::Proto ::Http ::Client . new (
71
74
uri . host ,
72
75
uri . port ,
@@ -87,9 +90,7 @@ def start_handler
87
90
ReverseHopHttp . hop_handlers [ full_uri ] = self
88
91
self . monitor_thread = Rex ::ThreadFactory . spawn ( 'ReverseHopHTTP' , false , uri ,
89
92
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
93
94
delay = 1 # poll delay
94
95
# Continue to loop as long as at least one handler or one session is depending on us
95
96
until hop_http . refs < 1 && hop_http . handlers . empty?
@@ -112,13 +113,17 @@ def start_handler
112
113
urlen = received . slice! ( 0 , 4 ) . unpack ( 'V' ) [ 0 ]
113
114
urlpath = received . slice! ( 0 , urlen )
114
115
116
+ # do not want handlers to change while we dispatch this
117
+ hop_http . lock . lock
115
118
#received is now the binary contents of the message
116
119
if hop_http . handlers . include? urlpath
117
120
pack = Rex ::Proto ::Http ::Packet . new
118
121
pack . body = received
119
122
hop_http . current_url = urlpath
120
123
hop_http . handlers [ urlpath ] . call ( hop_http , pack )
124
+ hop_http . lock . unlock
121
125
elsif !closed_handlers . include? urlpath
126
+ hop_http . lock . unlock
122
127
#New session!
123
128
conn_id = urlpath . gsub ( "/" , "" )
124
129
# Short-circuit the payload's handle_connection processing for create_session
@@ -132,7 +137,9 @@ def start_handler
132
137
:ssl => false ,
133
138
} )
134
139
# 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
136
143
end
137
144
end
138
145
hop_http . monitor_thread = nil #make sure we're out
@@ -163,8 +170,10 @@ def add_resource(res, opts={})
163
170
# Removes a resource.
164
171
#
165
172
def remove_resource ( res )
173
+ lock . lock
166
174
handlers . delete ( res )
167
175
closed_handlers [ res ] = true
176
+ lock . unlock
168
177
end
169
178
170
179
#
@@ -230,7 +239,7 @@ def initialize(info = {})
230
239
#
231
240
# Generates and sends a stage up to the hop point to be ready for the next client
232
241
#
233
- def send_new_stage ( control )
242
+ def send_new_stage
234
243
conn_id = generate_uri_checksum ( URI_CHECKSUM_CONN ) + "_" + Rex ::Text . rand_text_alphanumeric ( 16 )
235
244
url = full_uri + conn_id + "/\x00 "
236
245
0 commit comments