Skip to content

Commit c94f22c

Browse files
committed
Add in fixes from discussion and also update documentation to correctly note what functions can raise
1 parent b292586 commit c94f22c

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

lib/msf/core/post/windows/services.rb

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def service_exists?(service)
294294
# Mode is a string with either auto, manual or disable for the
295295
# corresponding setting. The name of the service is case sensitive.
296296
#
297+
# @raise [RuntimeError] if an invalid startup mode is provided in the mode parameter
297298
#
298299
def service_change_startup(name, mode, server=nil)
299300
if mode.is_a? Integer
@@ -338,6 +339,8 @@ def service_change_startup(name, mode, server=nil)
338339
#
339340
# @return [GetLastError] 0 if the function succeeds
340341
#
342+
# @raise [RuntimeError] if OpenSCManagerA failed
343+
#
341344
def service_change_config(name, opts, server=nil)
342345
open_sc_manager(:host=>server, :access=>"SC_MANAGER_CONNECT") do |manager|
343346
open_service_handle(manager, name, "SERVICE_CHANGE_CONFIG") do |service_handle|
@@ -369,6 +372,8 @@ def service_change_config(name, opts, server=nil)
369372
#
370373
# @return [GetLastError] 0 if the function succeeds
371374
#
375+
# @raise [RuntimeError] if OpenSCManagerA failed
376+
#
372377
def service_create(name, opts, server=nil)
373378
access = "SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SC_MANAGER_QUERY_LOCK_STATUS"
374379
open_sc_manager(:host=>server, :access=>access) do |manager|
@@ -465,6 +470,8 @@ def service_stop(name, server=nil)
465470
#
466471
# @param (see #service_start)
467472
#
473+
# @raise [RuntimeError] if OpenServiceA failed
474+
#
468475
def service_delete(name, server=nil)
469476
open_sc_manager(:host=>server) do |manager|
470477
open_service_handle(manager, name, "DELETE") do |service_handle|
@@ -483,7 +490,6 @@ def service_delete(name, server=nil)
483490
#
484491
# @raise (see #service_start)
485492
#
486-
#
487493
def service_status(name, server=nil)
488494
ret = nil
489495

@@ -513,53 +519,41 @@ def service_status(name, server=nil)
513519
#
514520
# @return [Boolean] indicating success
515521
#
516-
#
517-
def service_restart(name, start_type=START_TYPE_AUTO, server=nil)
518-
tried = false
522+
def service_restart(name, start_type=START_TYPE_AUTO, server=nil, should_retry=true)
523+
status = service_start(name, server)
519524

520-
begin
521-
status = service_start(name, server)
525+
if status == Error::SUCCESS
526+
vprint_good("[#{name}] Service started")
527+
return true
528+
end
522529

523-
if status == Error::SUCCESS
524-
vprint_good("[#{name}] Service started")
525-
return true
526-
else
527-
raise status.to_s
528-
end
529-
rescue RuntimeError => s
530-
if tried
531-
vprint_error("[#{name}] Unhandled error: #{s}")
532-
return false
530+
531+
case status
532+
when Error::ACCESS_DENIED
533+
vprint_error("[#{name}] Access denied")
534+
when Error::INVALID_HANDLE
535+
vprint_error("[#{name}] Invalid handle")
536+
when Error::PATH_NOT_FOUND
537+
vprint_error("[#{name}] Service binary could not be found")
538+
when Error::SERVICE_ALREADY_RUNNING
539+
vprint_status("[#{name}] Service already running attempting to stop and restart")
540+
stopped = service_stop(name, server)
541+
if ((stopped == Error::SUCCESS) || (stopped == Error::SERVICE_NOT_ACTIVE))
542+
service_restart(name, start_type, server, false) if should_retry
533543
else
534-
tried = true
544+
vprint_error("[#{name}] Service disabled, unable to change start type Error: #{stopped}")
535545
end
536-
537-
case s.message.to_i
538-
when Error::ACCESS_DENIED
539-
vprint_error("[#{name}] Access denied")
540-
when Error::INVALID_HANDLE
541-
vprint_error("[#{name}] Invalid handle")
542-
when Error::PATH_NOT_FOUND
543-
vprint_error("[#{name}] Service binary could not be found")
544-
when Error::SERVICE_ALREADY_RUNNING
545-
vprint_status("[#{name}] Service already running attempting to stop and restart")
546-
stopped = service_stop(name, server)
547-
if ((stopped == Error::SUCCESS) || (stopped == Error::SERVICE_NOT_ACTIVE))
548-
retry
549-
else
550-
vprint_error("[#{name}] Service disabled, unable to change start type Error: #{stopped}")
551-
end
552-
when Error::SERVICE_DISABLED
553-
vprint_status("[#{name}] Service disabled attempting to set to manual")
554-
if (service_change_config(name, {:starttype => start_type}, server) == Error::SUCCESS)
555-
retry
556-
else
557-
vprint_error("[#{name}] Service disabled, unable to change start type")
558-
end
546+
when Error::SERVICE_DISABLED
547+
vprint_status("[#{name}] Service disabled attempting to set to manual")
548+
if (service_change_config(name, {:starttype => start_type}, server) == Error::SUCCESS)
549+
service_restart(name, start_type, server, false) if should_retry
559550
else
560-
vprint_error("[#{name}] Unhandled error: #{s}")
561-
return false
551+
vprint_error("[#{name}] Service disabled, unable to change start type")
562552
end
553+
else
554+
status = WindowsError::Win32.find_by_retval(s).first
555+
vprint_error("[#{name}] Unhandled error: #{status.name}: #{status.description}")
556+
return false
563557
end
564558
end
565559

0 commit comments

Comments
 (0)