From ead71df32b0b6844fbbf3a5b298c45af0b31be96 Mon Sep 17 00:00:00 2001 From: Green-m Date: Mon, 25 Jun 2018 05:29:22 -0400 Subject: [PATCH 01/12] Add config file for persistent job --- lib/msf/base/config.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/msf/base/config.rb b/lib/msf/base/config.rb index d1fa415459..faa3d250c9 100644 --- a/lib/msf/base/config.rb +++ b/lib/msf/base/config.rb @@ -202,6 +202,13 @@ class Config < Hash self.new.history_file end + # Returns the full path to the handler file. + # + # @return [String] path the handler file. + def self.persist_file + self.new.persist_file + end + # Initializes configuration, creating directories as necessary. # # @return [void] @@ -278,6 +285,13 @@ class Config < Hash config_directory + FileSep + "history" end + # Returns the full path to the handler file. + # + # @return [String] path the handler file. + def persist_file + config_directory + FileSep + "persist" + end + # Returns the global module directory. # # @return [String] path to global module directory. From 2802f17cd5d4540ac306f957f94c4e1cbceb53e5 Mon Sep 17 00:00:00 2001 From: Green-m Date: Mon, 25 Jun 2018 05:30:49 -0400 Subject: [PATCH 02/12] Add command persist to make job persistent after msf restart. --- lib/msf/ui/console/command_dispatcher/jobs.rb | 115 +++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/lib/msf/ui/console/command_dispatcher/jobs.rb b/lib/msf/ui/console/command_dispatcher/jobs.rb index 797e0b9192..4d588871e9 100644 --- a/lib/msf/ui/console/command_dispatcher/jobs.rb +++ b/lib/msf/ui/console/command_dispatcher/jobs.rb @@ -38,12 +38,20 @@ module Msf "-S" => [ true, "Row search filter." ], ) + @@persist_opts = Rex::Parser::Arguments.new( + "-h" => [ false, "Help banner." ], + "-C" => [ false, "Clean all persistent jobs." ], + "-a" => [ true, "Add a persistent job by job ID" ], + "-l" => [ false, "List all persistent jobs." ] + ) + def commands { "jobs" => "Displays and manages jobs", "rename_job" => "Rename a job", "kill" => "Kill a job", - "handler" => "Start a payload handler as job" + "handler" => "Start a payload handler as job", + "persist" => "Displays and manages persistent jobs" } end @@ -159,6 +167,7 @@ module Msf cmd_jobs_help return false end + end if dump_list @@ -354,6 +363,110 @@ module Msf } tab_complete_generic(fmt, str, words) end + + def cmd_persist_help + print_line "Usage: persist [options]" + print_line + print_line "Persist job manipulation and interaction." + print @@persist_opts.usage + end + + # + # Displays and manages persistent jobs for the framework. + # + + def cmd_persist(*args) + # Make the default behavior listing all jobs if there were no options + # or the only option is the verbose flag + args.unshift("-l") if args.empty? + + persist_file = Msf::Config.persist_file + verbose = false + dump_list = false + dump_info = false + job_id = nil + persistence = false + + # Parse the command options + @@persist_opts.parse(args) do |opt, _idx, val| + case opt + when "-l" + dump_list = true + # Cleaning all persistent jobs + when "-C" + print_line("Cleaning all persistent jobs...") + File.truncate(persist_file,0) + # Add persistent job + when "-a" + job_id = val + persistence = true + when "-h" + cmd_persist_help + return false + end + end + + if dump_list + persistent_jobs = File.open(persist_file,"r").readlines.map!(&:chomp) rescue nil + print_line("\nPersistent Jobs\n===============\n") + + unless persistent_jobs.blank? + persistent_jobs.map!{|job|JSON.parse(job)} + + persistent_jobs.each do |job| + print_line("#{job['mod_name']} - #{job['mod_options']['Payload']} - #{job['mod_options']["Options"]["lhost"]}:#{job['mod_options']["Options"]["LPORT"]}") + print_line + end + else + print_line("No persistent jobs.\n") + end + end + + if persistence + if job_id && framework.jobs.has_key?(job_id.to_s) + mod = framework.jobs[job_id.to_s].ctx[0].replicant + payload = framework.jobs[job_id.to_s].ctx[1].replicant + + payload_opts = { + 'Payload' => payload.refname, + 'Options' => payload.datastore, + #'LocalInput' => driver.input, + #'LocalOutput' => driver.output, + 'RunAsJob' => true + } + + mod_opts = { + 'mod_name' => mod.fullname, + 'mod_options' => payload_opts + } + + File.open(persist_file,"a+"){|file|file.puts(mod_opts.to_json)} + print_line("Added job #{job_id} as a persistent_job.") + else + print_line("Invalid Job ID") + end + #handler.exploit_simple(payload_opts) + end + end + + + # + # Tab completion for the persist command + # + # @param str [String] the string currently being typed before tab was hit + # @param words [Array] the previously completed words on the command line. words is always + # at least 1 when tab completion has reached this stage since the command itself has been completed + + def cmd_persist_tabs(_str, words) + return @@persist_opts.fmt.keys if words.length == 1 + + if words.length == 2 && (@@persist_opts.fmt[words[1]] || [false])[0] + return framework.jobs.keys + end + + [] + end + end end end From 014574425529ee3ae78370e5b6cb5af838e500cc Mon Sep 17 00:00:00 2001 From: Green-m Date: Mon, 25 Jun 2018 05:32:17 -0400 Subject: [PATCH 03/12] Process persistent job when msf start. --- lib/msf/ui/console/driver.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index 6bc1e7bd9c..a8098e1026 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -178,6 +178,22 @@ class Driver < Msf::Ui::Driver } end + # Process persistent job handler + + persist_file = Msf::Config.persist_file + persistent_handler = File.open(persist_file,"r").readlines.map!(&:chomp) rescue nil + + unless persistent_handler.blank? + persistent_handler.map!{|handler|JSON.parse(handler)} + print_status("Starting persistent handler...") + + persistent_handler.each do |handler_opts| + handler = framework.modules.create(handler_opts['mod_name']) + handler.exploit_simple(handler_opts['options']) + end + end + + # Process any additional startup commands if opts['XCommands'] and opts['XCommands'].kind_of? Array opts['XCommands'].each { |c| From 9806ee327a85b4f3aee1f00a462ac30b4ffa9a2b Mon Sep 17 00:00:00 2001 From: Green-m Date: Mon, 25 Jun 2018 05:50:06 -0400 Subject: [PATCH 04/12] Fix error when load options from persist file. --- lib/msf/ui/console/driver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index a8098e1026..2e47b0c061 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -189,7 +189,7 @@ class Driver < Msf::Ui::Driver persistent_handler.each do |handler_opts| handler = framework.modules.create(handler_opts['mod_name']) - handler.exploit_simple(handler_opts['options']) + handler.exploit_simple(handler_opts['mod_options']) end end From 91a9a248790b7fbe2ae7cbb8c305295c1641b10e Mon Sep 17 00:00:00 2001 From: Green-m Date: Tue, 7 Aug 2018 05:36:57 -0400 Subject: [PATCH 05/12] Update the dump_jobs for persistent job info. --- lib/msf/base/serializer/readable_text.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/msf/base/serializer/readable_text.rb b/lib/msf/base/serializer/readable_text.rb index c8b32d31f7..bae3c4fdc3 100644 --- a/lib/msf/base/serializer/readable_text.rb +++ b/lib/msf/base/serializer/readable_text.rb @@ -754,10 +754,10 @@ class ReadableText # @param col [Integer] the column wrap width. # @return [String] the formatted list of running jobs. def self.dump_jobs(framework, verbose = false, indent = DefaultIndent, col = DefaultColumnWrap) - columns = [ 'Id', 'Name', "Payload", "Payload opts" ] + columns = [ 'Id', 'Name', "Payload", "Payload opts"] if (verbose) - columns += [ "URIPATH", "Start Time", "Handler opts" ] + columns += [ "URIPATH", "Start Time", "Handler opts", "Persist" ] end tbl = Rex::Text::Table.new( @@ -766,6 +766,12 @@ class ReadableText 'Columns' => columns ) + # Get the persistent job info. + if verbose + persist_list = File.open(Msf::Config.persist_file,"r").readlines.map!(&:chomp) rescue nil + persist_list.map!{|handler|JSON.parse(handler)} + end + # jobs are stored as a hash with the keys being a numeric String job_id. framework.jobs.keys.sort_by(&:to_i).each do |job_id| # Job context is stored as an Array with the 0th element being @@ -800,11 +806,17 @@ class ReadableText row[4] = uripath row[5] = framework.jobs[job_id].start_time row[6] = '' + row[7] = 'false' if pinst.respond_to?(:listener_uri) listener_uri = pinst.listener_uri.strip row[6] = listener_uri unless listener_uri == payload_uri end + + persist_list.each do |e| + row[7] = 'true' if e['mod_options']['Options'] == framework.jobs[job_id.to_s].ctx[1].datastore + end + end tbl << row end From 2dee2cf038b3d662275d99fd1f221de2f6ed181f Mon Sep 17 00:00:00 2001 From: Green-m Date: Tue, 7 Aug 2018 05:40:35 -0400 Subject: [PATCH 06/12] Update command job to support for persistent job when msf restart. --- lib/msf/ui/console/command_dispatcher/jobs.rb | 204 +++++++----------- 1 file changed, 77 insertions(+), 127 deletions(-) diff --git a/lib/msf/ui/console/command_dispatcher/jobs.rb b/lib/msf/ui/console/command_dispatcher/jobs.rb index 4d588871e9..57c439ac55 100644 --- a/lib/msf/ui/console/command_dispatcher/jobs.rb +++ b/lib/msf/ui/console/command_dispatcher/jobs.rb @@ -35,14 +35,9 @@ module Msf "-i" => [ true, "Lists detailed information about a running job."], "-l" => [ false, "List all running jobs." ], "-v" => [ false, "Print more detailed info. Use with -i and -l" ], - "-S" => [ true, "Row search filter." ], - ) - - @@persist_opts = Rex::Parser::Arguments.new( - "-h" => [ false, "Help banner." ], - "-C" => [ false, "Clean all persistent jobs." ], - "-a" => [ true, "Add a persistent job by job ID" ], - "-l" => [ false, "List all persistent jobs." ] + "-p" => [ true, "Add a persistent job by job ID" ], + "-P" => [ false, "Persist all running jobs on restart." ], + "-S" => [ true, "Row search filter." ] ) def commands @@ -50,8 +45,7 @@ module Msf "jobs" => "Displays and manages jobs", "rename_job" => "Rename a job", "kill" => "Kill a job", - "handler" => "Start a payload handler as job", - "persist" => "Displays and manages persistent jobs" + "handler" => "Start a payload handler as job" } end @@ -125,7 +119,9 @@ module Msf verbose = false dump_list = false dump_info = false + kill_job = false job_id = nil + job_list = nil # Parse the command options @@jobs_opts.parse(args) do |opt, _idx, val| @@ -137,29 +133,25 @@ module Msf # Terminate the supplied job ID(s) when "-k" job_list = build_range_array(val) - if job_list.blank? - print_error("Please specify valid job identifier(s)") - return false - end - print_status("Stopping the following job(s): #{job_list.join(', ')}") - job_list.map(&:to_s).each do |job| - if framework.jobs.key?(job) - print_status("Stopping job #{job}") - framework.jobs.stop_job(job) - else - print_error("Invalid job identifier: #{job}") - end - end + kill_job = true when "-K" - print_line("Stopping all jobs...") + print_line("Stopping all jobs and persistent jobs ...") framework.jobs.each_key do |i| framework.jobs.stop_job(i) end + File.truncate(Msf::Config.persist_file,0) when "-i" # Defer printing anything until the end of option parsing # so we can check for the verbose flag. dump_info = true job_id = val + when "-p" + job_list = build_range_array(val) + job_list.each{|job_id| add_persist_job(job_id)} + when "-P" + print_line("Making all jobs persistent ...") + job_list = framework.jobs.map{|k,v|v.jid.to_s} + job_list.each{|job_id| add_persist_job(job_id)} when "-S", "--search" search_term = val dump_list = true @@ -195,6 +187,67 @@ module Msf print_line("Invalid Job ID") end end + + if kill_job + if job_list.blank? + print_error("Please specify valid job identifier(s)") + return false + end + + print_status("Stopping the following job(s) and remove their persistence: #{job_list.join(', ')}") + + # Remove the persistent job when match the option of payload. + persist_list = File.open(Msf::Config.persist_file,"r").readlines.map!(&:chomp) rescue nil + persist_list.map!{|handler|JSON.parse(handler)} + + job_list.map(&:to_s).each do |job| + payload_option = framework.jobs[job.to_s].ctx[1].datastore + persist_list.delete_if{|pjob|pjob['mod_options']['Options'] == payload_option} + end + # Write persist job back to config file. + File.truncate(Msf::Config.persist_file,0) + persist_list.each do |pjob| + File.open(Msf::Config.persist_file,"a+"){|file|file.puts(pjob.to_json)} + end + + # Stop the job by job id. + job_list.map(&:to_s).each do |job| + if framework.jobs.key?(job) + print_status("Stopping job #{job}") + framework.jobs.stop_job(job) + else + print_error("Invalid job identifier: #{job}") + end + end + end + + end + + # + # Add a persistent job by job id. + # Persistent job would restore on console restarted. + + def add_persist_job(job_id) + if job_id && framework.jobs.has_key?(job_id.to_s) + mod = framework.jobs[job_id.to_s].ctx[0].replicant + payload = framework.jobs[job_id.to_s].ctx[1].replicant + + payload_opts = { + 'Payload' => payload.refname, + 'Options' => payload.datastore, + 'RunAsJob' => true + } + + mod_opts = { + 'mod_name' => mod.fullname, + 'mod_options' => payload_opts + } + + File.open(Msf::Config.persist_file,"a+"){|file|file.puts(mod_opts.to_json)} + print_line("Added job #{job_id} as a persistent_job.") + else + print_line("Invalid Job ID") + end end # @@ -364,109 +417,6 @@ module Msf tab_complete_generic(fmt, str, words) end - def cmd_persist_help - print_line "Usage: persist [options]" - print_line - print_line "Persist job manipulation and interaction." - print @@persist_opts.usage - end - - # - # Displays and manages persistent jobs for the framework. - # - - def cmd_persist(*args) - # Make the default behavior listing all jobs if there were no options - # or the only option is the verbose flag - args.unshift("-l") if args.empty? - - persist_file = Msf::Config.persist_file - verbose = false - dump_list = false - dump_info = false - job_id = nil - persistence = false - - # Parse the command options - @@persist_opts.parse(args) do |opt, _idx, val| - case opt - when "-l" - dump_list = true - # Cleaning all persistent jobs - when "-C" - print_line("Cleaning all persistent jobs...") - File.truncate(persist_file,0) - # Add persistent job - when "-a" - job_id = val - persistence = true - when "-h" - cmd_persist_help - return false - end - end - - if dump_list - persistent_jobs = File.open(persist_file,"r").readlines.map!(&:chomp) rescue nil - print_line("\nPersistent Jobs\n===============\n") - - unless persistent_jobs.blank? - persistent_jobs.map!{|job|JSON.parse(job)} - - persistent_jobs.each do |job| - print_line("#{job['mod_name']} - #{job['mod_options']['Payload']} - #{job['mod_options']["Options"]["lhost"]}:#{job['mod_options']["Options"]["LPORT"]}") - print_line - end - else - print_line("No persistent jobs.\n") - end - end - - if persistence - if job_id && framework.jobs.has_key?(job_id.to_s) - mod = framework.jobs[job_id.to_s].ctx[0].replicant - payload = framework.jobs[job_id.to_s].ctx[1].replicant - - payload_opts = { - 'Payload' => payload.refname, - 'Options' => payload.datastore, - #'LocalInput' => driver.input, - #'LocalOutput' => driver.output, - 'RunAsJob' => true - } - - mod_opts = { - 'mod_name' => mod.fullname, - 'mod_options' => payload_opts - } - - File.open(persist_file,"a+"){|file|file.puts(mod_opts.to_json)} - print_line("Added job #{job_id} as a persistent_job.") - else - print_line("Invalid Job ID") - end - #handler.exploit_simple(payload_opts) - end - end - - - # - # Tab completion for the persist command - # - # @param str [String] the string currently being typed before tab was hit - # @param words [Array] the previously completed words on the command line. words is always - # at least 1 when tab completion has reached this stage since the command itself has been completed - - def cmd_persist_tabs(_str, words) - return @@persist_opts.fmt.keys if words.length == 1 - - if words.length == 2 && (@@persist_opts.fmt[words[1]] || [false])[0] - return framework.jobs.keys - end - - [] - end - end end end From 3caa3057d8cc3173a62df375f3bad6ad410022ab Mon Sep 17 00:00:00 2001 From: Green-m Date: Tue, 7 Aug 2018 05:41:47 -0400 Subject: [PATCH 07/12] Process persistent job when msf start. --- lib/msf/ui/console/driver.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index 2e47b0c061..fca8695b49 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -179,15 +179,13 @@ class Driver < Msf::Ui::Driver end # Process persistent job handler + persist_handler = File.open(Msf::Config.persist_file,"r").readlines.map!(&:chomp) rescue nil - persist_file = Msf::Config.persist_file - persistent_handler = File.open(persist_file,"r").readlines.map!(&:chomp) rescue nil - - unless persistent_handler.blank? - persistent_handler.map!{|handler|JSON.parse(handler)} + unless persist_handler.blank? + persist_handler.map!{|handler|JSON.parse(handler)} print_status("Starting persistent handler...") - persistent_handler.each do |handler_opts| + persist_handler.each do |handler_opts| handler = framework.modules.create(handler_opts['mod_name']) handler.exploit_simple(handler_opts['mod_options']) end From bdb663b0786070f31add5973a4895da654bf161a Mon Sep 17 00:00:00 2001 From: Green-m Date: Tue, 14 Aug 2018 06:33:44 -0400 Subject: [PATCH 08/12] Make persist list go all in on the JSON format. --- lib/msf/base/serializer/readable_text.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/msf/base/serializer/readable_text.rb b/lib/msf/base/serializer/readable_text.rb index bae3c4fdc3..b737e1d868 100644 --- a/lib/msf/base/serializer/readable_text.rb +++ b/lib/msf/base/serializer/readable_text.rb @@ -768,8 +768,7 @@ class ReadableText # Get the persistent job info. if verbose - persist_list = File.open(Msf::Config.persist_file,"r").readlines.map!(&:chomp) rescue nil - persist_list.map!{|handler|JSON.parse(handler)} + persist_list = JSON.parse(File.read(Msf::Config.persist_file)) rescue [] end # jobs are stored as a hash with the keys being a numeric String job_id. From 2394e92c1c8364a5405ff714051919f402b80762 Mon Sep 17 00:00:00 2001 From: Green-m Date: Tue, 14 Aug 2018 06:37:08 -0400 Subject: [PATCH 09/12] Go all in with JSON format, rename var to get more readable. --- lib/msf/ui/console/driver.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index fca8695b49..108069aa0b 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -179,19 +179,21 @@ class Driver < Msf::Ui::Driver end # Process persistent job handler - persist_handler = File.open(Msf::Config.persist_file,"r").readlines.map!(&:chomp) rescue nil + begin + restore_handlers = JSON.parse(File.read(Msf::Config.persist_file)) + rescue Errno::ENOENT, JSON::ParserError + restore_handlers = nil + end - unless persist_handler.blank? - persist_handler.map!{|handler|JSON.parse(handler)} - print_status("Starting persistent handler...") + unless restore_handlers.nil? + print_status("Starting persistent handler(s)...") - persist_handler.each do |handler_opts| + restore_handlers.each do |handler_opts| handler = framework.modules.create(handler_opts['mod_name']) handler.exploit_simple(handler_opts['mod_options']) end end - # Process any additional startup commands if opts['XCommands'] and opts['XCommands'].kind_of? Array opts['XCommands'].each { |c| From 97b6425315ac512738def1e870d4ffb1e3ffb57a Mon Sep 17 00:00:00 2001 From: Green-m Date: Tue, 14 Aug 2018 06:39:56 -0400 Subject: [PATCH 10/12] Make persist list go all in on the JSON format. --- lib/msf/ui/console/command_dispatcher/jobs.rb | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/msf/ui/console/command_dispatcher/jobs.rb b/lib/msf/ui/console/command_dispatcher/jobs.rb index 57c439ac55..f1a1b9d432 100644 --- a/lib/msf/ui/console/command_dispatcher/jobs.rb +++ b/lib/msf/ui/console/command_dispatcher/jobs.rb @@ -35,7 +35,7 @@ module Msf "-i" => [ true, "Lists detailed information about a running job."], "-l" => [ false, "List all running jobs." ], "-v" => [ false, "Print more detailed info. Use with -i and -l" ], - "-p" => [ true, "Add a persistent job by job ID" ], + "-p" => [ true, "Add persistence to job by job ID" ], "-P" => [ false, "Persist all running jobs on restart." ], "-S" => [ true, "Row search filter." ] ) @@ -135,7 +135,7 @@ module Msf job_list = build_range_array(val) kill_job = true when "-K" - print_line("Stopping all jobs and persistent jobs ...") + print_line("Stopping all jobs...") framework.jobs.each_key do |i| framework.jobs.stop_job(i) end @@ -194,21 +194,21 @@ module Msf return false end - print_status("Stopping the following job(s) and remove their persistence: #{job_list.join(', ')}") + print_status("Stopping the following job(s): #{job_list.join(', ')}") # Remove the persistent job when match the option of payload. - persist_list = File.open(Msf::Config.persist_file,"r").readlines.map!(&:chomp) rescue nil - persist_list.map!{|handler|JSON.parse(handler)} + begin + persist_list = JSON.parse(File.read(Msf::Config.persist_file)) + rescue Errno::ENOENT, JSON::ParserError + persist_list = [] + end job_list.map(&:to_s).each do |job| payload_option = framework.jobs[job.to_s].ctx[1].datastore persist_list.delete_if{|pjob|pjob['mod_options']['Options'] == payload_option} end # Write persist job back to config file. - File.truncate(Msf::Config.persist_file,0) - persist_list.each do |pjob| - File.open(Msf::Config.persist_file,"a+"){|file|file.puts(pjob.to_json)} - end + File.open(Msf::Config.persist_file,"w"){|file| file.puts(JSON.pretty_generate(persist_list))} # Stop the job by job id. job_list.map(&:to_s).each do |job| @@ -243,8 +243,10 @@ module Msf 'mod_options' => payload_opts } - File.open(Msf::Config.persist_file,"a+"){|file|file.puts(mod_opts.to_json)} - print_line("Added job #{job_id} as a persistent_job.") + persist_list = JSON.parse(File.read(Msf::Config.persist_file)) rescue [] + persist_list << mod_opts + File.open(Msf::Config.persist_file,"w"){|file| file.puts(JSON.pretty_generate(persist_list))} + print_line("Added persistence to job #{job_id}.") else print_line("Invalid Job ID") end From 028799299c7e7a3a8ce18c094b47f9bb6b729ed7 Mon Sep 17 00:00:00 2001 From: Green-m Date: Wed, 15 Aug 2018 22:23:04 -0400 Subject: [PATCH 11/12] Update for style requirements. --- lib/msf/ui/console/command_dispatcher/jobs.rb | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/msf/ui/console/command_dispatcher/jobs.rb b/lib/msf/ui/console/command_dispatcher/jobs.rb index f1a1b9d432..d8777cda15 100644 --- a/lib/msf/ui/console/command_dispatcher/jobs.rb +++ b/lib/msf/ui/console/command_dispatcher/jobs.rb @@ -147,11 +147,17 @@ module Msf job_id = val when "-p" job_list = build_range_array(val) - job_list.each{|job_id| add_persist_job(job_id)} + job_list.each do |job_id| + add_persist_job(job_id) + end when "-P" print_line("Making all jobs persistent ...") - job_list = framework.jobs.map{|k,v|v.jid.to_s} - job_list.each{|job_id| add_persist_job(job_id)} + job_list = framework.jobs.map do |k,v| + v.jid.to_s + end + job_list.each do |job_id| + add_persist_job(job_id) + end when "-S", "--search" search_term = val dump_list = true @@ -208,7 +214,9 @@ module Msf persist_list.delete_if{|pjob|pjob['mod_options']['Options'] == payload_option} end # Write persist job back to config file. - File.open(Msf::Config.persist_file,"w"){|file| file.puts(JSON.pretty_generate(persist_list))} + File.open(Msf::Config.persist_file,"w") do |file| + file.puts(JSON.pretty_generate(persist_list)) + end # Stop the job by job id. job_list.map(&:to_s).each do |job| @@ -243,9 +251,15 @@ module Msf 'mod_options' => payload_opts } - persist_list = JSON.parse(File.read(Msf::Config.persist_file)) rescue [] + begin + persist_list = JSON.parse(File.read(Msf::Config.persist_file)) + rescue Errno::ENOENT, JSON::ParserError + persist_list = [] + end persist_list << mod_opts - File.open(Msf::Config.persist_file,"w"){|file| file.puts(JSON.pretty_generate(persist_list))} + File.open(Msf::Config.persist_file,"w") do |file| + file.puts(JSON.pretty_generate(persist_list)) + end print_line("Added persistence to job #{job_id}.") else print_line("Invalid Job ID") From 1475f205d4fd7f293a7fdb67117ac07d9ec0e0ee Mon Sep 17 00:00:00 2001 From: Green-m Date: Wed, 15 Aug 2018 22:24:20 -0400 Subject: [PATCH 12/12] Update for style requirements. --- lib/msf/base/serializer/readable_text.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/msf/base/serializer/readable_text.rb b/lib/msf/base/serializer/readable_text.rb index b737e1d868..29333be511 100644 --- a/lib/msf/base/serializer/readable_text.rb +++ b/lib/msf/base/serializer/readable_text.rb @@ -768,7 +768,11 @@ class ReadableText # Get the persistent job info. if verbose - persist_list = JSON.parse(File.read(Msf::Config.persist_file)) rescue [] + begin + persist_list = JSON.parse(File.read(Msf::Config.persist_file)) + rescue Errno::ENOENT, JSON::ParserError + persist_list = [] + end end # jobs are stored as a hash with the keys being a numeric String job_id.