Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service manager fixes #16316

Merged
merged 11 commits into from
Mar 11, 2022
Merged
19 changes: 4 additions & 15 deletions lib/msf/core/exploit/remote/dns/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,11 @@ def start_service
end

#
# Stops the server
# @param destroy [TrueClass,FalseClass] Dereference the server object
def stop_service(destroy = false)
Rex::ServiceManager.stop_service(self.service) if self.service
if destroy
@dns_resolver = nil if @dns_resolver
self.service = nil if self.service
end
end

# Dereference the DNS service
#
# Resets the DNS server
#
def reset_service
stop_service(true)
start_service
def cleanup
super
@dns_resolver = nil if @dns_resolver
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/exploit/remote/http_server/php_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def exploit
rescue ::Interrupt
raise $!
ensure
stop_service
cleanup_service
smashery marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
8 changes: 0 additions & 8 deletions lib/msf/core/exploit/remote/ldap/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ def start_service
rescue ::Errno::EACCES => e
raise Rex::BindFailed, e.message
end

#
# Resets the LDAP server
#
def reset_service
cleanup
start_service
end
end
end
end
6 changes: 3 additions & 3 deletions lib/msf/core/exploit/remote/socket_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def primer
def cleanup
super
if service
stopped = stop_service
stopped = cleanup_service
if stopped
print_status("Server stopped.")
end
Expand All @@ -79,9 +79,9 @@ def start_service(opts = {})
end

#
# Stops the service.
# Cleans up the service; either closing the socket, or deferencing the service
#
def stop_service
def cleanup_service
if service
begin
if self.service.kind_of?(Rex::Service)
Expand Down
4 changes: 1 addition & 3 deletions lib/msf/core/handler/reverse_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ def setup_handler
def stop_handler
if self.service
self.service.remove_resource((luri + "/").gsub("//", "/"))
if self.service.resources.empty? && self.sessions == 0
Rex::ServiceManager.stop_service(self.service)
end
self.service.deref
smashery marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
7 changes: 4 additions & 3 deletions lib/rex/post/meterpreter/packet_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,9 @@ def initialize_passive_dispatcher
'Proc' => Proc.new { |cli, req| on_passive_request(cli, req) },
'VirtualDirectory' => true
)

# Add a reference count to the handler
self.passive_service.ref
end

def shutdown_passive_dispatcher
Expand All @@ -729,9 +732,7 @@ def shutdown_passive_dispatcher
resource_uri = "/" + self.conn_id.to_s.gsub(/(^\/|\/$)/, '') + "/"
self.passive_service.remove_resource(resource_uri) if self.passive_service

if self.passive_service.resources.empty?
Rex::ServiceManager.stop_service(self.passive_service)
end
self.passive_service.deref
self.passive_service = nil
end
super
Expand Down
2 changes: 1 addition & 1 deletion lib/rex/proto/dns/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def default_dispatch_request(cli,data)
# Returns the hardcore alias for the DNS service
#
def self.hardcore_alias(*args)
"#{(args[0] || '')}#{(args[1] || '')}"
"#{(args[0] || '')}-#{(args[1] || '')}-#{args[5] || ''}"
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/rex/proto/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def inspect
# Returns the hardcore alias for the HTTP service
#
def self.hardcore_alias(*args)
"#{(args[0] || '')}#{(args[1] || '')}"
"#{(args[0] || '')}-#{(args[1] || '')}-#{args[4] || ''}"
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/rex/proto/ldap/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def search_ldif(filter, msgid, attrflt = :all)
# Returns the hardcore alias for the LDAP service
#
def self.hardcore_alias(*args)
"#{args[0] || ''}#{args[1] || ''}"
"#{args[0] || ''}-#{args[1] || ''}-#{args[4] || ''}"
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/rex/proto/ssh/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def inspect
# Returns the hardcore alias for the SSH service
#
def self.hardcore_alias(*args)
"#{(args[0])}#{(args[1])}"
"#{(args[0])}-#{(args[1])}-#{args[4] || ''}"
end

#
Expand Down
2 changes: 1 addition & 1 deletion modules/auxiliary/admin/scada/yokogawa_bkbcopyd_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def on_client_data(c)
end

def on_client_close(c)
stop_service
cleanup_service
end
end

2 changes: 1 addition & 1 deletion modules/auxiliary/gather/safari_file_url_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def cleanup
super

# Kill FTP
stop_service
cleanup_service

# clear my resource, deregister ref, stop/close the HTTP socket
begin
smashery marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
13 changes: 10 additions & 3 deletions modules/auxiliary/server/dns/native_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ def initialize(info = {})
},
'Author' => 'RageLtMan <rageltman[at]sempervictus>',
'License' => MSF_LICENSE,
'References' => []
'References' => [],
'Actions' =>
[
[ 'Service', 'Description' => 'Serve DNS entries' ]
],
'PassiveActions' =>
[
'Service'
],
'DefaultAction' => 'Service'
))
end

Expand All @@ -37,8 +46,6 @@ def run
service.wait
rescue Rex::BindFailed => e
print_error "Failed to bind to port #{datastore['RPORT']}: #{e.message}"
ensure
stop_service(true)
end
end

Expand Down
4 changes: 1 addition & 3 deletions modules/auxiliary/server/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ def initialize(info = {})
end

#
# Wrapper for service execution and cleanup
# Wrapper for service execution
#
def run
start_service
service.wait
rescue Rex::BindFailed => e
print_error "Failed to bind to port #{datastore['SRVPORT']}: #{e.message}"
ensure
stop_service
end

#
Expand Down
22 changes: 17 additions & 5 deletions modules/auxiliary/spoof/dns/native_spoofer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ def initialize(info = {})
},
'Author' => 'RageLtMan <rageltman[at]sempervictus>',
'License' => MSF_LICENSE,
'References' => []
'References' => [],
'Actions' =>
[
[ 'Service', 'Description' => 'Serve DNS entries' ]
],
'PassiveActions' =>
[
'Service'
],
'DefaultAction' => 'Service'

))

register_options(
Expand All @@ -47,13 +57,15 @@ def run
service.wait
rescue Rex::BindFailed => e
print_error "Failed to bind to port #{datastore['RPORT']}: #{e.message}"
ensure
@capture_thread.kill if @capture_thread
close_pcap
stop_service(true)
end
end

def cleanup
super
@capture_thread.kill if @capture_thread
close_pcap
end

#
# Generates reply with src and dst reversed
# Maintains original packet structure, proto, etc, changes ip_id
Expand Down
11 changes: 7 additions & 4 deletions modules/exploits/linux/http/vestacp_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,13 @@ def payload_implant

def exploit
start_http_server
payload_implant
login
start_backup_and_trigger_payload
stop_service
begin
payload_implant
login
start_backup_and_trigger_payload
ensure
cleanup_service
end
smashery marked this conversation as resolved.
Show resolved Hide resolved
end

def on_request_uri(cli, _request)
Expand Down
2 changes: 1 addition & 1 deletion modules/exploits/multi/http/jboss_maindeployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def exploit
end

print_status("Shutting down the web service...")
stop_service
cleanup_service


#
Expand Down
2 changes: 1 addition & 1 deletion modules/exploits/multi/http/mutiny_subnetmask_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def wait_linux_payload
#select(nil, nil, nil, 20) unless session_created?

print_status("Shutting down the web service...")
stop_service
cleanup_service
smashery marked this conversation as resolved.
Show resolved Hide resolved
end

# Handle incoming requests from the target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ def on_request_uri(cli, request)
# sleep 1
# waited += 1
# if waited > datastore['HTTP_DELAY']
# stop_service
# cleanup_service
# return Exploit::CheckCode::Safe
# end
# end
#
# stop_service
# cleanup_service
# return Exploit::CheckCode::Vulnerable
# end

Expand Down
4 changes: 2 additions & 2 deletions modules/exploits/multi/http/ubiquiti_unifi_log4shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def check
wait_until { @search_received }
@search_received ? Exploit::CheckCode::Vulnerable : Exploit::CheckCode::Unknown('No LDAP search query was received.')
ensure
stop_service
cleanup_service
end

def build_ldap_search_response_payload
Expand Down Expand Up @@ -150,6 +150,6 @@ def exploit
wait_until { @search_received && (!handler_enabled? || session_created?) }
handler
ensure
cleanup
cleanup_service
smashery marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading