Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/scripts/meterpreter/service_manager.rb
Views: 11766
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##567# Author: Carlos Perez <carlos_perez [at] darkoperator.com and Shai rod (@NightRang3r)8#-------------------------------------------------------------------------------9################## Variable Declarations ##################1011@client = client12srv_name = nil13returned_value = nil14srv_startup = "Auto"15srv_display_name = ""16srv_command = nil17srv_list = false18srv_start = false19srv_stop = false20srv_create = false21srv_info = false22srv_change_startup = false23srv_delete = false242526@exec_opts = Rex::Parser::Arguments.new(27"-h" => [ false , "Help menu." ],28"-l" => [ false , "List Services"],29"-S" => [ false , "Start Service"],30"-K" => [ false , "Stop Service"],31"-C" => [ false , "Create Service, service will be set to auto start"],32"-c" => [ false , "Change Service StartUp. Default <Auto>" ],33"-i" => [ false , "Get Service Information"],34"-n" => [ true , "Service Name"],35"-s" => [ true , "Startup Parameter for service. Specify Auto, Manual or Disabled"],36"-d" => [ true , "Display Name of Service"],37"-p" => [ true , "Service command"],38"-D" => [ false , "Delete Service"]39)40meter_type = client.platform4142################## Function Declarations ##################4344# Usage Message Function45#-------------------------------------------------------------------------------46def usage47print_line "Meterpreter Script for managing Windows Services."48print_line(@exec_opts.usage)49raise Rex::Script::Completed50end5152# Wrong Meterpreter Version Message Function53#-------------------------------------------------------------------------------54def wrong_meter_version(meter = meter_type)55print_error("#{meter} version of Meterpreter is not supported with this Script!")56raise Rex::Script::Completed57end5859# Check if sufficient privileges are present for certain actions60def priv_check61if not is_uac_enabled? or is_admin?62return true63else64print_error("Insufficient Privileges")65raise Rex::Script::Completed66end6768end6970################## Main ##################71# Check for Version of Meterpreter72wrong_meter_version(meter_type) if meter_type != 'windows'7374@exec_opts.parse(args) { |opt, idx, val|75case opt76when "-h"77usage78when "-l"79srv_list = true80when "-n"81srv_name = val82when "-S"83srv_start = true84when "-K"85srv_stop = true86when "-i"87srv_info = true88when "-c"89srv_change_startup = true90when "-C"91srv_create = true92when "-d"93srv_display_name = val94when "-p"95srv_command = val96when "-D"97srv_delete = true98end99}100101# List Services102if srv_list103print_status("Service List:")104service_list.each do |s|105print_good("\t#{s}")106end107raise Rex::Script::Completed108109# Start a service110elsif srv_start111priv_check112if srv_name113begin114returned_value = service_start(srv_name)115if returned_value == 0116print_good("Service #{srv_name} Started")117elsif returned_value == 1118print_good("Service #{srv_name} already Running")119elsif returned_value == 2120print_error("Service #{srv_name} is Disabled could not be started.")121end122123rescue124print_error("A Service Name must be provided, service names are case sensitive.")125end126else127print_error("No Service Name was provided!")128end129raise Rex::Script::Completed130131# Stop a Service132elsif srv_stop133priv_check134if srv_name135begin136returned_value = service_stop(srv_name)137if returned_value == 0138print_good("Service #{srv_name} Stopped")139elsif returned_value == 1140print_good("Service #{srv_name} already Stopped")141elsif returned_value == 2142print_error("Service #{srv_name} can not be stopped.")143end144145rescue146print_error("A Service Name must be provided, service names are case sensitive.")147end148else149print_error("No Service Name was provided!")150end151raise Rex::Script::Completed152153# Get service info154elsif srv_info155srv_conf = {}156if srv_name157begin158srv_conf = service_info(srv_name)159print_status("Service Information for #{srv_name}:")160print_good("\tName: #{srv_conf['Name']}")161print_good("\tStartup: #{srv_conf['Startup']}")162print_good("\tCommand: #{srv_conf['Command']}")163print_good("\tCredentials: #{srv_conf['Credentials']}")164rescue165print_error("A Service Name must be provided, service names are case sensitive.")166end167else168print_error("No Service Name was provided!")169end170raise Rex::Script::Completed171172# Change startup of a service173elsif srv_change_startup174priv_check175if srv_name176begin177print_status("Changing Service #{srv_name} Startup to #{srv_startup}")178service_change_startup(srv_name,srv_startup)179print_good("Service Startup changed!")180181rescue182print_error("A Service Name must be provided, service names are case sensitive.")183end184else185print_error("No Service Name was provided!")186end187raise Rex::Script::Completed188189# Create a service190elsif srv_create191priv_check192if srv_name and srv_command193begin194print_status("Creating Service #{srv_name}")195service_create(srv_name,srv_display_name,srv_command)196print_good("\tService Created!")197print_good("\tDisplay Name: #{srv_display_name}")198print_good("\tCommand: #{srv_command}")199print_good("\tSet to Auto Star.")200rescue::Exception => e201print_error("Error: #{e}")202end203else204print_error("No Service Name and Service Command where provided!")205end206207# Delete a service208elsif srv_delete209priv_check210if srv_name211begin212print_status("Deleting Service #{srv_name}")213service_delete(srv_name)214print_good("\tService #{srv_name} Delete")215rescue::Exception => e216print_error("A Service Name must be provided, service names are case sensitive.")217print_error("Error: #{e}")218end219else220print_error("No Service Name and Service Command where provided!")221end222raise Rex::Script::Completed223else224usage225end226227228