1
+ # frozen_string_literal: true
1
2
# -*- coding: binary -*-
2
3
3
4
#
6
7
7
8
require 'rex/ui/text/output/buffer/stdout'
8
9
9
-
10
10
module Msf
11
11
module Ui
12
12
module Console
13
13
module CommandDispatcher
14
-
15
14
#
16
15
# {CommandDispatcher} for commands related to background jobs in Metasploit Framework.
17
16
#
18
17
class Jobs
19
-
20
18
include Msf ::Ui ::Console ::CommandDispatcher
21
19
22
20
@@handler_opts = Rex ::Parser ::Arguments . new (
@@ -29,8 +27,6 @@ class Jobs
29
27
"-n" => [ true , "The custom name to give the handler job" ]
30
28
)
31
29
32
-
33
-
34
30
@@jobs_opts = Rex ::Parser ::Arguments . new (
35
31
"-h" => [ false , "Help banner." ] ,
36
32
"-k" => [ true , "Terminate jobs by job ID and/or range." ] ,
@@ -95,7 +91,7 @@ def cmd_rename_job(*args)
95
91
# @param words [Array<String>] the previously completed words on the command line. words is always
96
92
# at least 1 when tab completion has reached this stage since the command itself has been completed
97
93
98
- def cmd_rename_job_tabs ( str , words )
94
+ def cmd_rename_job_tabs ( _str , words )
99
95
return [ ] if words . length > 1
100
96
framework . jobs . keys
101
97
end
@@ -114,49 +110,49 @@ def cmd_jobs_help
114
110
def cmd_jobs ( *args )
115
111
# Make the default behavior listing all jobs if there were no options
116
112
# or the only option is the verbose flag
117
- args . unshift ( "-l" ) if args . length == 0 || args == [ "-v" ]
113
+ args . unshift ( "-l" ) if args . empty? || args == [ "-v" ]
118
114
119
115
verbose = false
120
116
dump_list = false
121
117
dump_info = false
122
118
job_id = nil
123
119
124
120
# Parse the command options
125
- @@jobs_opts . parse ( args ) do |opt , idx , val |
121
+ @@jobs_opts . parse ( args ) do |opt , _idx , val |
126
122
case opt
127
- when "-v"
128
- verbose = true
129
- when "-l"
130
- dump_list = true
123
+ when "-v"
124
+ verbose = true
125
+ when "-l"
126
+ dump_list = true
131
127
# Terminate the supplied job ID(s)
132
- when "-k"
133
- job_list = build_range_array ( val )
134
- if job_list . blank?
135
- print_error ( "Please specify valid job identifier(s)" )
136
- return false
137
- end
138
- print_status ( "Stopping the following job(s): #{ job_list . join ( ', ' ) } " )
139
- job_list . map ( &:to_s ) . each do |job |
140
- if framework . jobs . has_key? ( job )
141
- print_status ( "Stopping job #{ job } " )
142
- framework . jobs . stop_job ( job )
143
- else
144
- print_error ( "Invalid job identifier: #{ job } " )
145
- end
146
- end
147
- when "-K"
148
- print_line ( "Stopping all jobs..." )
149
- framework . jobs . each_key do |i |
150
- framework . jobs . stop_job ( i )
151
- end
152
- when "-i"
153
- # Defer printing anything until the end of option parsing
154
- # so we can check for the verbose flag.
155
- dump_info = true
156
- job_id = val
157
- when "-h"
158
- cmd_jobs_help
128
+ when "-k"
129
+ job_list = build_range_array ( val )
130
+ if job_list . blank?
131
+ print_error ( "Please specify valid job identifier(s)" )
159
132
return false
133
+ end
134
+ print_status ( "Stopping the following job(s): #{ job_list . join ( ', ' ) } " )
135
+ job_list . map ( &:to_s ) . each do |job |
136
+ if framework . jobs . key? ( job )
137
+ print_status ( "Stopping job #{ job } " )
138
+ framework . jobs . stop_job ( job )
139
+ else
140
+ print_error ( "Invalid job identifier: #{ job } " )
141
+ end
142
+ end
143
+ when "-K"
144
+ print_line ( "Stopping all jobs..." )
145
+ framework . jobs . each_key do |i |
146
+ framework . jobs . stop_job ( i )
147
+ end
148
+ when "-i"
149
+ # Defer printing anything until the end of option parsing
150
+ # so we can check for the verbose flag.
151
+ dump_info = true
152
+ job_id = val
153
+ when "-h"
154
+ cmd_jobs_help
155
+ return false
160
156
end
161
157
end
162
158
@@ -177,7 +173,7 @@ def cmd_jobs(*args)
177
173
178
174
if verbose
179
175
mod_opt = Serializer ::ReadableText . dump_advanced_options ( mod , ' ' )
180
- if mod_opt && mod_opt . length > 0
176
+ if mod_opt && ! mod_opt . empty?
181
177
print_line ( "\n Module advanced options:\n \n #{ mod_opt } \n " )
182
178
end
183
179
end
@@ -194,10 +190,8 @@ def cmd_jobs(*args)
194
190
# @param words [Array<String>] the previously completed words on the command line. words is always
195
191
# at least 1 when tab completion has reached this stage since the command itself has been completed
196
192
197
- def cmd_jobs_tabs ( str , words )
198
- if words . length == 1
199
- return @@jobs_opts . fmt . keys
200
- end
193
+ def cmd_jobs_tabs ( _str , words )
194
+ return @@jobs_opts . fmt . keys if words . length == 1
201
195
202
196
if words . length == 2 && ( @@jobs_opts . fmt [ words [ 1 ] ] || [ false ] ) [ 0 ]
203
197
return framework . jobs . keys
@@ -210,7 +204,7 @@ def cmd_kill_help
210
204
print_line "Usage: kill <job1> [job2 ...]"
211
205
print_line
212
206
print_line "Equivalent to 'jobs -k job1 -k job2 ...'"
213
- print @@jobs_opts . usage ( )
207
+ print @@jobs_opts . usage
214
208
end
215
209
216
210
def cmd_kill ( *args )
@@ -224,7 +218,7 @@ def cmd_kill(*args)
224
218
# @param words [Array<String>] the previously completed words on the command line. words is always
225
219
# at least 1 when tab completion has reached this stage since the command itself has been completed
226
220
227
- def cmd_kill_tabs ( str , words )
221
+ def cmd_kill_tabs ( _str , words )
228
222
return [ ] if words . length > 1
229
223
framework . jobs . keys
230
224
end
@@ -233,13 +227,13 @@ def cmd_handler_help
233
227
print_line "Usage: handler [options]"
234
228
print_line
235
229
print_line "Spin up a Payload Handler as background job."
236
- print @@handler_opts . usage ( )
230
+ print @@handler_opts . usage
237
231
end
238
232
239
233
# Allows the user to setup a payload handler as a background job from a single command.
240
234
def cmd_handler ( *args )
241
- #Display the help banner if no arguments were passed
242
- if args . length == 0
235
+ # Display the help banner if no arguments were passed
236
+ if args . empty?
243
237
cmd_handler_help
244
238
return
245
239
end
@@ -252,31 +246,31 @@ def cmd_handler(*args)
252
246
stage_encoder = nil
253
247
254
248
# Parse the command options
255
- @@handler_opts . parse ( args ) do |opt , idx , val |
249
+ @@handler_opts . parse ( args ) do |opt , _idx , val |
256
250
case opt
257
- when "-x"
258
- exit_on_session = true
259
- when "-p"
260
- payload_module = framework . payloads . create ( val )
261
- if payload_module . nil?
262
- print_error "Invalid Payload Name Supplied!"
263
- return
264
- end
265
- when "-P"
266
- port = val
267
- when "-H"
268
- host = val
269
- when "-n"
270
- job_name = val
271
- when "-e"
272
- encoder_module = framework . encoders . create ( val )
273
- if encoder_module . nil?
274
- print_error "Invalid Encoder Name Supplied"
275
- end
276
- stage_encoder = encoder_module . refname
277
- when "-h"
278
- cmd_handler_help
251
+ when "-x"
252
+ exit_on_session = true
253
+ when "-p"
254
+ payload_module = framework . payloads . create ( val )
255
+ if payload_module . nil?
256
+ print_error "Invalid Payload Name Supplied!"
279
257
return
258
+ end
259
+ when "-P"
260
+ port = val
261
+ when "-H"
262
+ host = val
263
+ when "-n"
264
+ job_name = val
265
+ when "-e"
266
+ encoder_module = framework . encoders . create ( val )
267
+ if encoder_module . nil?
268
+ print_error "Invalid Encoder Name Supplied"
269
+ end
270
+ stage_encoder = encoder_module . refname
271
+ when "-h"
272
+ cmd_handler_help
273
+ return
280
274
end
281
275
end
282
276
@@ -294,19 +288,19 @@ def cmd_handler(*args)
294
288
payload_datastore = payload_module . datastore
295
289
296
290
# Set The RHOST or LHOST for the payload
297
- if payload_datastore . has_key ? "LHOST"
291
+ if payload_datastore . key ? "LHOST"
298
292
payload_datastore [ 'LHOST' ] = host
299
- elsif payload_datastore . has_key ? "RHOST"
293
+ elsif payload_datastore . key ? "RHOST"
300
294
payload_datastore [ 'RHOST' ] = host
301
295
else
302
296
print_error "Could not determine how to set Host on this payload..."
303
297
return
304
298
end
305
299
306
300
# Set the RPORT or LPORT for the payload
307
- if payload_datastore . has_key ? "LPORT"
301
+ if payload_datastore . key ? "LPORT"
308
302
payload_datastore [ 'LPORT' ] = port
309
- elsif payload_datastore . has_key ? "RPORT"
303
+ elsif payload_datastore . key ? "RPORT"
310
304
payload_datastore [ 'RPORT' ] = port
311
305
else
312
306
print_error "Could not determine how to set Port on this payload..."
@@ -335,15 +329,12 @@ def cmd_handler(*args)
335
329
336
330
# Customise the job name if the user asked for it
337
331
if job_name . present?
338
- framework . jobs [ " #{ job_id } " ] . send ( :name= , job_name )
332
+ framework . jobs [ job_id . to_s ] . send ( :name= , job_name )
339
333
end
340
334
341
335
print_status "Payload Handler Started as Job #{ job_id } "
342
336
end
343
-
344
-
345
337
end
346
-
347
338
end
348
339
end
349
340
end
0 commit comments