forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathservice_manager.rb
221 lines (201 loc) · 5.67 KB
/
service_manager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# $Id$
# $Revision$
# Author: Carlos Perez <carlos_perez [at] darkoperator.com and Shai rod (@NightRang3r)
#-------------------------------------------------------------------------------
################## Variable Declarations ##################
@client = client
srv_name = nil
returned_value = nil
srv_startup = "Auto"
srv_display_name = ""
srv_command = nil
srv_list = false
srv_start = false
srv_stop = false
srv_create = false
srv_info = false
srv_change_startup = false
srv_delete = false
@exec_opts = Rex::Parser::Arguments.new(
"-h" => [ false , "Help menu." ],
"-l" => [ false , "List Services"],
"-S" => [ false , "Start Service"],
"-K" => [ false , "Stop Service"],
"-C" => [ false , "Create Service, service will be set to auto start"],
"-c" => [ false , "Change Service StartUp. Default <Auto>" ],
"-i" => [ false , "Get Service Information"],
"-n" => [ true , "Service Name"],
"-s" => [ true , "Startup Parameter for service. Specify Auto, Manual or Disabled"],
"-d" => [ true , "Display Name of Service"],
"-p" => [ true , "Service command"],
"-D" => [ false , "Delete Service"]
)
meter_type = client.platform
################## Function Declarations ##################
# Usage Message Function
#-------------------------------------------------------------------------------
def usage
print_line "Meterpreter Script for managing Windows Services."
print_line(@exec_opts.usage)
raise Rex::Script::Completed
end
# Wrong Meterpreter Version Message Function
#-------------------------------------------------------------------------------
def wrong_meter_version(meter = meter_type)
print_error("#{meter} version of Meterpreter is not supported with this Script!")
raise Rex::Script::Completed
end
# Check if sufficient privileges are present for certain actions
def priv_check
if not is_uac_enabled? or is_admin?
return true
else
print_error("Insuficient Privileges")
raise Rex::Script::Completed
end
end
################## Main ##################
# Check for Version of Meterpreter
wrong_meter_version(meter_type) if meter_type !~ /win32|win64/i
@exec_opts.parse(args) { |opt, idx, val|
case opt
when "-h"
usage
when "-l"
srv_list = true
when "-n"
srv_name = val
when "-S"
srv_start = true
when "-K"
srv_stop = true
when "-i"
srv_info = true
when "-c"
srv_change_startup = true
when "-C"
srv_create = true
when "-d"
srv_display_name = val
when "-p"
srv_command = val
when "-D"
srv_delete = true
end
}
# List Services
if srv_list
print_status("Service List:")
service_list.each do |s|
print_good("\t#{s}")
end
raise Rex::Script::Completed
# Start a service
elsif srv_start
priv_check
if srv_name
begin
returned_value = service_start(srv_name)
if returned_value == 0
print_good("Service #{srv_name} Started")
elsif returned_value == 1
print_good("Service #{srv_name} already Running")
elsif returned_value == 2
print_error("Service #{srv_name} is Disabled could not be started.")
end
rescue
print_error("A Service Name must be provided, service names are case sensitive.")
end
else
print_error("No Service Name was provided!")
end
raise Rex::Script::Completed
# Stop a Service
elsif srv_stop
priv_check
if srv_name
begin
returned_value = service_stop(srv_name)
if returned_value == 0
print_good("Service #{srv_name} Stopped")
elsif returned_value == 1
print_good("Service #{srv_name} already Stopped")
elsif returned_value == 2
print_error("Service #{srv_name} can not be stopped.")
end
rescue
print_error("A Service Name must be provided, service names are case sensitive.")
end
else
print_error("No Service Name was provided!")
end
raise Rex::Script::Completed
# Get service info
elsif srv_info
srv_conf = {}
if srv_name
begin
srv_conf = service_info(srv_name)
print_status("Service Information for #{srv_name}:")
print_good("\tName: #{srv_conf['Name']}")
print_good("\tStartup: #{srv_conf['Startup']}")
print_good("\tCommand: #{srv_conf['Command']}")
print_good("\tCredentials: #{srv_conf['Credentials']}")
rescue
print_error("A Service Name must be provided, service names are case sensitive.")
end
else
print_error("No Service Name was provided!")
end
raise Rex::Script::Completed
# Change startup of a service
elsif srv_change_startup
priv_check
if srv_name
begin
print_status("Changing Service #{srv_name} Startup to #{srv_startup}")
service_change_startup(srv_name,srv_startup)
print_good("Service Startup changed!")
rescue
print_error("A Service Name must be provided, service names are case sensitive.")
end
else
print_error("No Service Name was provided!")
end
raise Rex::Script::Completed
# Create a service
elsif srv_create
priv_check
if srv_name and srv_command
begin
print_status("Creating Service #{srv_name}")
service_create(srv_name,srv_display_name,srv_command)
print_good("\tService Created!")
print_good("\tDisplay Name: #{srv_display_name}")
print_good("\tCommand: #{srv_command}")
print_good("\tSet to Auto Star.")
rescue::Exception => e
print_error("Error: #{e}")
end
else
print_error("No Service Name and Service Command where provided!")
end
# Delete a service
elsif srv_delete
priv_check
if srv_name
begin
print_status("Deleting Service #{srv_name}")
service_delete(srv_name)
print_good("\tService #{srv_name} Delete")
rescue::Exception => e
print_error("A Service Name must be provided, service names are case sensitive.")
print_error("Error: #{e}")
end
else
print_error("No Service Name and Service Command where provided!")
end
raise Rex::Script::Completed
else
usage
end