Skip to content

Commit dcad2ae

Browse files
committed
Refactoring clipboard options, using constants, changing default values
1 parent 5458ca9 commit dcad2ae

File tree

2 files changed

+43
-45
lines changed
  • lib/rex/post/meterpreter

2 files changed

+43
-45
lines changed

lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def File.upload(dest, *src_files, &stat)
304304
stat.call('Uploading', src, dest) if (stat)
305305

306306
upload_file(dest, src)
307-
stat.call('Completed', src, dest) if (stat)
307+
stat.call(STEP_COMPLETED, src, dest) if (stat)
308308
}
309309
end
310310

@@ -334,7 +334,7 @@ def File.upload_file(dest_file, src_file, &stat)
334334
src_fd.close unless src_fd.nil?
335335
dest_fd.close unless dest_fd.nil?
336336
end
337-
stat.call('Completed', src_file, dest_file) if stat
337+
stat.call(STEP_COMPLETED, src_file, dest_file) if stat
338338
end
339339

340340
def File.is_glob?(name)
@@ -392,11 +392,11 @@ def File.download_file(dest_file, src_file, opts = {}, &stat)
392392
dst_stat = ::File.stat(dest_file)
393393
if (src_stat.size == dst_stat.size && src_stat.mtime == dst_stat.mtime)
394394
src_fd.close
395-
return 'Skipped'
395+
return STEP_SKIPPED
396396
end
397397
if !force_overwrite
398398
src_fd.close
399-
return 'Overwrite'
399+
return STEP_SKIPPED_WOULD_OVERWRITE
400400
else
401401
overwrite_existing= true
402402
end
@@ -489,7 +489,7 @@ def File.download_file(dest_file, src_file, opts = {}, &stat)
489489

490490
# Clone the times from the remote file
491491
::File.utime(src_stat.atime, src_stat.mtime, dest_file)
492-
return overwrite_existing ? 'Overwritten' : 'Completed'
492+
return overwrite_existing ? STEP_COMPLETED_OVERWRITTEN : STEP_COMPLETED
493493
end
494494

495495
#

lib/rex/post/meterpreter/ui/console/command_dispatcher/extapi/clipboard.rb

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ def cmd_clipboard_set_text(*args)
131131
#
132132
@@monitor_start_opts = Rex::Parser::Arguments.new(
133133
"-h" => [ false, "Help banner" ],
134-
"-i" => [ true, "Capture image content when monitoring (default: true)" ]
134+
"--no-capture" => [ true, "Do not capture image content when monitoring" ]
135135
)
136136

137137
#
138138
# Help for the clipboard_monitor_start command.
139139
#
140140
def print_clipboard_monitor_start_usage
141141
print(
142-
"\nUsage: clipboard_monitor_start [-i true|false] [-h]\n\n" +
142+
"\nUsage: clipboard_monitor_start [-h]\n\n" +
143143
"Starts a background clipboard monitoring thread. The thread watches\n" +
144144
"the clipboard on the target, under the context of the current desktop, and when\n" +
145145
"changes are detected the contents of the clipboard are captured. Contents can be\n" +
@@ -156,9 +156,8 @@ def cmd_clipboard_monitor_start(*args)
156156

157157
@@monitor_start_opts.parse(args) { |opt, idx, val|
158158
case opt
159-
when "-i"
160-
# default this to true
161-
capture_images = val.downcase != 'false'
159+
when "--no-capture"
160+
capture_images = false
162161
when "-h"
163162
print_clipboard_monitor_start_usage
164163
return true
@@ -274,9 +273,9 @@ def cmd_clipboard_monitor_resume(*args)
274273
#
275274
@@monitor_dump_opts = Rex::Parser::Arguments.new(
276275
"-h" => [ false, "Help banner" ],
277-
"-i" => [ true, "Indicate if captured image data should be downloaded (default: true)" ],
278-
"-f" => [ true, "Indicate if captured file data should be downloaded (default: true)" ],
279-
"-p" => [ false, "Purge the contents of the monitor once dumped (default: true)" ],
276+
"--no-images" => [ false, "Indicate if captured image data shouldn't be downloaded" ],
277+
"--no-files" => [ false, "Indicate if captured file data shouldn't be downloaded" ],
278+
"-p" => [ false, "Purge the contents of the monitor once dumped" ],
280279
"-d" => [ true, "Download non-text content to the specified folder" ],
281280
'--force' => [false, "Force overwriting existing files"]
282281
)
@@ -286,7 +285,7 @@ def cmd_clipboard_monitor_resume(*args)
286285
#
287286
def print_clipboard_monitor_dump_usage
288287
print(
289-
"\nUsage: clipboard_monitor_dump [-d true|false] [-d downloaddir] [-h]\n\n" +
288+
"\nUsage: clipboard_monitor_dump [-p] [-d downloaddir] [-h]\n\n" +
290289
"Dump the capture clipboard contents to the local machine..\n\n" +
291290
@@monitor_dump_opts.usage + "\n")
292291
end
@@ -295,7 +294,7 @@ def print_clipboard_monitor_dump_usage
295294
# Dump the clipboard monitor contents to the local machine.
296295
#
297296
def cmd_clipboard_monitor_dump(*args)
298-
purge = true
297+
purge = false
299298
download_images = true
300299
download_files = true
301300
download_path = nil
@@ -305,10 +304,10 @@ def cmd_clipboard_monitor_dump(*args)
305304
case opt
306305
when "-d"
307306
download_path = val
308-
when "-i"
309-
download_images = val.downcase != 'false'
310-
when "-f"
311-
download_files = val.downcase != 'false'
307+
when "--no-images"
308+
download_images = false
309+
when "--no-files"
310+
download_files = false
312311
when "-p"
313312
purge = true
314313
when '--force'
@@ -320,7 +319,7 @@ def cmd_clipboard_monitor_dump(*args)
320319
}
321320

322321
if download_path.nil?
323-
print_error("You need to specify download directory.")
322+
print_error("You have to specify destination directory to download loot.")
324323
return true
325324
end
326325

@@ -330,7 +329,7 @@ def cmd_clipboard_monitor_dump(*args)
330329
:purge => false
331330
})
332331

333-
res = parse_dump(dump, download_images, download_files, download_path, force_overwrite)
332+
res = parse_dump(dump, download_images, download_files, download_path, force_overwrite: force_overwrite)
334333
if !res && purge
335334
client.extapi.clipboard.monitor_purge()
336335
end
@@ -342,9 +341,9 @@ def cmd_clipboard_monitor_dump(*args)
342341
#
343342
@@monitor_stop_opts = Rex::Parser::Arguments.new(
344343
"-h" => [ false, "Help banner" ],
345-
"-x" => [ true, "Indicate if captured clipboard data should be dumped (default: true)" ],
346-
"-i" => [ true, "Indicate if captured image data should be downloaded (default: true)" ],
347-
"-f" => [ true, "Indicate if captured file data should be downloaded (default: true)" ],
344+
"--no-dump" => [ false, "Indicate if captured clipboard data shouldn't be dumped" ],
345+
"--no-images" => [ false, "Indicate if captured image data shouldn't be downloaded" ],
346+
"--no-files" => [ false, "Indicate if captured file data shouldn't be downloaded" ],
348347
"-d" => [ true, "Download non-text content to the specified folder" ],
349348
'--force' => [false, "Force overwriting existing files"]
350349
)
@@ -354,7 +353,7 @@ def cmd_clipboard_monitor_dump(*args)
354353
#
355354
def print_clipboard_monitor_stop_usage
356355
print(
357-
"\nUsage: clipboard_monitor_stop [-d true|false] [-x true|false] [-d downloaddir] [-h]\n\n" +
356+
"\nUsage: clipboard_monitor_stop [-d downloaddir] [-h]\n\n" +
358357
"Stops a clipboard monitor thread and returns the captured data to the local machine.\n\n" +
359358
@@monitor_stop_opts.usage + "\n")
360359
end
@@ -373,37 +372,31 @@ def cmd_clipboard_monitor_stop(*args)
373372
case opt
374373
when "-d"
375374
download_path = val
376-
when "-x"
377-
dump_data = val.downcase != 'false'
378-
when "-i"
379-
download_images = val.downcase != 'false'
380-
when "-f"
381-
download_files = val.downcase != 'false'
375+
when "--no-dump"
376+
dump_data = false
377+
when "--no-images"
378+
download_images = false
379+
when "--no-files"
380+
download_files = false
382381
when '--force'
383382
force_overwrite = true
384383
when "-h"
385384
print_clipboard_monitor_stop_usage
386385
return true
387386
end
388387
}
389-
390-
391-
if download_path.nil? && (download_images || download_files)
392-
#allow user to stop monitoring if they don't wish to download anything
393-
client.extapi.clipboard.monitor_stop({
394-
:dump => dump_data,
395-
})
396-
print_good("Clipboard monitor stopped without downloading data. Specify destination folder to download loot.")
397-
return true
398-
end
399-
388+
389+
#you can't download stuff if you don't specify destination directory
390+
# todo: is there more ruby way to do this
391+
download_images = download_images && download_path.nil? ? false : download_images
392+
400393
dump = client.extapi.clipboard.monitor_stop({
401394
:dump => dump_data,
402395
:include_images => download_images
403396
})
404397

405398

406-
parse_dump(dump, download_images, download_files, download_path, force_overwrite) if dump_data
399+
parse_dump(dump, download_images, download_files, download_path, force_overwrite: force_overwrite) if dump_data
407400

408401
print_good("Clipboard monitor stopped")
409402
end
@@ -458,10 +451,15 @@ def download_file( dest_folder, source, force_overwrite=false )
458451
return true, attempted_overwrite
459452
end
460453

461-
def parse_dump(dump, get_images, get_files, download_path, force_overwrite=false)
454+
def parse_dump(dump, get_images, get_files, loot_dir, force_overwrite: false)
462455

463-
loot_dir = download_path || './'
464456
overwrite_attempt = false
457+
458+
if (get_images || get_files ) && loot_dir.nil?
459+
print_error("You have to specify destination directory to download loot.")
460+
return true
461+
end
462+
465463
if (get_images || get_files) && !::File.directory?( loot_dir )
466464
::FileUtils.mkdir_p( loot_dir )
467465
end

0 commit comments

Comments
 (0)