@@ -22,8 +22,9 @@ class Console::CommandDispatcher::Extapi::Service
22
22
#
23
23
def commands
24
24
{
25
- "service_enum" => "Enumerate all registered Windows services" ,
26
- "service_query" => "Query more detail about a specific Windows service"
25
+ "service_enum" => "Enumerate all registered Windows services" ,
26
+ "service_query" => "Query more detail about a specific Windows service" ,
27
+ "service_control" => "Control a single service (start/pause/resume/stop/restart)"
27
28
}
28
29
end
29
30
@@ -33,6 +34,32 @@ def commands
33
34
def name
34
35
"Extapi: Service Management"
35
36
end
37
+
38
+ #
39
+ # Initialize the instance
40
+ #
41
+ def initialize ( shell )
42
+ super
43
+
44
+ @status_map = {
45
+ 1 => "Stopped" ,
46
+ 2 => "Starting" ,
47
+ 3 => "Stopping" ,
48
+ 4 => "Running" ,
49
+ 5 => "Continuing" ,
50
+ 6 => "Pausing" ,
51
+ 7 => "Paused"
52
+ }
53
+
54
+ @start_type_map = {
55
+ 0 => "Boot" ,
56
+ 1 => "System" ,
57
+ 2 => "Automatic" ,
58
+ 3 => "Manual" ,
59
+ 4 => "Disabled"
60
+ }
61
+ end
62
+
36
63
#
37
64
# Options for the service_enum command.
38
65
#
@@ -44,7 +71,7 @@ def name
44
71
# Query a single service for more detail.
45
72
#
46
73
def cmd_service_enum ( *args )
47
- @@service_enum_opts . parse ( args ) { |opt , idx , val |
74
+ @@service_enum_opts . parse ( args ) do |opt , idx , val |
48
75
case opt
49
76
when "-h"
50
77
print (
@@ -55,17 +82,7 @@ def cmd_service_enum(*args)
55
82
"able to interact with the desktop.\n \n " )
56
83
return true
57
84
end
58
- }
59
-
60
- status_map = {
61
- 1 => "Stopped" ,
62
- 2 => "Starting" ,
63
- 3 => "Stopping" ,
64
- 4 => "Running" ,
65
- 5 => "Continuing" ,
66
- 6 => "Pausing" ,
67
- 7 => "Paused"
68
- }
85
+ end
69
86
70
87
services = client . extapi . service . enumerate
71
88
@@ -78,14 +95,14 @@ def cmd_service_enum(*args)
78
95
]
79
96
)
80
97
81
- services . each { |s |
98
+ services . each do |s |
82
99
table << [
83
100
s [ :pid ] ,
84
- status_map [ s [ :status ] ] ,
101
+ @ status_map[ s [ :status ] ] ,
85
102
s [ :interactive ] ? "Y" : "N" ,
86
103
"#{ s [ :name ] . downcase } (#{ s [ :display ] } )"
87
104
]
88
- }
105
+ end
89
106
90
107
print_line
91
108
print_line ( table . to_s )
@@ -107,9 +124,9 @@ def cmd_service_enum(*args)
107
124
# Query a single service for more detail.
108
125
#
109
126
def cmd_service_query ( *args )
110
- args << "-h" if args . length == 0
127
+ args . unshift ( "-h" ) if args . length != 1
111
128
112
- @@service_query_opts . parse ( args ) { |opt , idx , val |
129
+ @@service_query_opts . parse ( args ) do |opt , idx , val |
113
130
case opt
114
131
when "-h"
115
132
print (
@@ -119,25 +136,18 @@ def cmd_service_query(*args)
119
136
"binary path, DACL, load order group, start type and more.\n \n " )
120
137
return true
121
138
end
122
- }
139
+ end
123
140
124
141
service_name = args . shift
125
142
126
- start_type_map = {
127
- 0 => "Boot" ,
128
- 1 => "System" ,
129
- 2 => "Automatic" ,
130
- 3 => "Manual" ,
131
- 4 => "Disabled"
132
- }
133
-
134
143
detail = client . extapi . service . query ( service_name )
135
144
136
145
print_line
137
146
print_line ( "Name : #{ service_name } " )
138
147
print_line ( "Display : #{ detail [ :display ] } " )
139
148
print_line ( "Account : #{ detail [ :startname ] } " )
140
- print_line ( "Start Type : #{ start_type_map [ detail [ :starttype ] ] } " )
149
+ print_line ( "Status : #{ @status_map [ detail [ :status ] ] } " )
150
+ print_line ( "Start Type : #{ @start_type_map [ detail [ :starttype ] ] } " )
141
151
print_line ( "Path : #{ detail [ :path ] } " )
142
152
print_line ( "L.O. Group : #{ detail [ :logroup ] } " )
143
153
print_line ( "Interactive : #{ detail [ :interactive ] ? "Yes" : "No" } " )
@@ -146,6 +156,39 @@ def cmd_service_query(*args)
146
156
147
157
end
148
158
159
+ #
160
+ # Options for the service_control command.
161
+ #
162
+ @@service_control_opts = Rex ::Parser ::Arguments . new (
163
+ "-h" => [ false , "Help banner" ]
164
+ )
165
+
166
+ #
167
+ # Query a single service for more detail.
168
+ #
169
+ def cmd_service_control ( *args )
170
+ args . unshift ( "-h" ) if args . length != 2
171
+
172
+ @@service_control_opts . parse ( args ) do |opt , idx , val |
173
+ case opt
174
+ when "-h"
175
+ print (
176
+ "\n Usage: service_control [-h] <servicename> <op>\n " +
177
+ " <servicename> : The name of the service to control.\n " +
178
+ " <op> : The operation to perform on the service.\n " +
179
+ " Valid ops: start pause resume stop restart.\n \n " )
180
+ return true
181
+ end
182
+ end
183
+
184
+ service_name = args [ 0 ]
185
+ op = args [ 1 ]
186
+
187
+ client . extapi . service . control ( service_name , op )
188
+
189
+ print_good ( "Operation #{ op } succeeded." )
190
+ end
191
+
149
192
end
150
193
151
194
end
0 commit comments