From 5ac1ee1d734b43db4813260b5695a1bd1223ebf3 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 26 Mar 2015 18:26:56 -0500 Subject: [PATCH] fix http/s handler reference counting for pymet add a persistent session counter to avoid stopping listening when pymet stages over http/s --- lib/msf/core/handler.rb | 7 +++++++ lib/msf/core/handler/reverse_http.rb | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/msf/core/handler.rb b/lib/msf/core/handler.rb index 552a00702a..844a26b432 100644 --- a/lib/msf/core/handler.rb +++ b/lib/msf/core/handler.rb @@ -77,6 +77,9 @@ module Handler # Initialize the pending_connections counter to 0 self.pending_connections = 0 + # Initialize the sessions counter to 0 + self.sessions = 0 + # Create the waiter event with auto_reset set to false so that # if a session is ever created, waiting on it returns immediately. self.session_waiter_event = Rex::Sync::Event.new(false, false) @@ -234,10 +237,14 @@ protected # Decrement the pending connections counter now that we've processed # one session. self.pending_connections -= 1 + + # Count the number of sessions we have registered + self.sessions += 1 end attr_accessor :session_waiter_event # :nodoc: attr_accessor :pending_connections # :nodoc: + attr_accessor :sessions # :nodoc: end diff --git a/lib/msf/core/handler/reverse_http.rb b/lib/msf/core/handler/reverse_http.rb index f3f81594de..35ec8f46ef 100644 --- a/lib/msf/core/handler/reverse_http.rb +++ b/lib/msf/core/handler/reverse_http.rb @@ -160,7 +160,7 @@ module ReverseHttp def stop_handler if self.service self.service.remove_resource("/") - Rex::ServiceManager.stop_service(self.service) if self.pending_connections == 0 + Rex::ServiceManager.stop_service(self.service) if self.sessions == 0 end end @@ -217,6 +217,8 @@ protected uri_match = process_uri_resource(req.relative_resource) + self.pending_connections += 1 + # Process the requested resource. case uri_match when /^\/INITPY/ @@ -252,7 +254,6 @@ protected :comm_timeout => datastore['SessionCommunicationTimeout'].to_i, :ssl => ssl?, }) - self.pending_connections += 1 when /^\/INITJM/ conn_id = generate_uri_checksum(URI_CHECKSUM_CONN) + "_" + Rex::Text.rand_text_alphanumeric(16) @@ -340,6 +341,7 @@ protected resp.code = 200 resp.message = "OK" resp.body = datastore['HttpUnknownRequestResponse'].to_s + self.pending_connections -= 1 end cli.send_response(resp) if (resp)