Skip to content

Commit

Permalink
Add option to reload all libs when running a module
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Sep 18, 2023
1 parent 1378bfb commit 1499e4c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/msf/ui/console/command_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ def active_session=(mod)
driver.active_session = mod
end

#
# Gets the dispatcher of a specific class
# Returns nil if there are no dispatchers of the specific class in the stack,
# otherwise returns the first dispatcher of class klass.
#
def dispatcher(klass)
driver.dispatcher_stack.find { |d| d.instance_of?(klass) }
end

#
# Logs an error message to the screen and the log file. The callstack is
# also printed.
Expand Down
2 changes: 2 additions & 0 deletions lib/msf/ui/console/command_dispatcher/auxiliary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def cmd_run(*args, action: nil)
return false
end

dispatcher(::Msf::Ui::Console::CommandDispatcher::Developer)&.cmd_reload_lib('-a') if args[:reload_libs]

begin
# Check if this is a scanner module or doesn't target remote hosts
if rhosts.blank? || mod.class.included_modules.include?(Msf::Auxiliary::Scanner)
Expand Down
3 changes: 3 additions & 0 deletions lib/msf/ui/console/command_dispatcher/evasion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def cmd_run(*args)
'LocalOutput' => driver.output
}

dispatcher(::Msf::Ui::Console::CommandDispatcher::Developer)&.cmd_reload_lib('-a') if args[:reload_libs]

begin
mod.run_simple(opts)
rescue ::Interrupt
Expand Down Expand Up @@ -64,6 +66,7 @@ def cmd_run_tabs(str, words)
'-n' => [ framework.nops.map { |refname, mod| refname } ],
'-o' => [ true ],
'-p' => [ framework.payloads.map { |refname, mod| refname } ],
'-r' => [ nil ],
'-t' => [ true ],
'-z' => [ nil ]
}
Expand Down
2 changes: 2 additions & 0 deletions lib/msf/ui/console/command_dispatcher/exploit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def cmd_run_tabs(str, words)
'-n' => [ framework.nops.map { |refname, mod| refname } ],
'-o' => [ true ],
'-p' => [ framework.payloads.map { |refname, mod| refname } ],
'-r' => [ nil ],
'-t' => [ true ],
'-z' => [ nil ]
}
Expand Down Expand Up @@ -138,6 +139,7 @@ def cmd_exploit(*args)
return false
end

dispatcher(::Msf::Ui::Console::CommandDispatcher::Developer)&.cmd_reload_lib('-a') if args[:reload_libs]

if rhosts && has_rhosts_option
rhosts_walker = Msf::RhostsWalker.new(rhosts, mod_with_opts.datastore)
Expand Down
2 changes: 2 additions & 0 deletions lib/msf/ui/console/command_dispatcher/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def cmd_run(*args, action: nil)
jobify = true
end

dispatcher(::Msf::Ui::Console::CommandDispatcher::Developer)&.cmd_reload_lib('-a') if args[:reload_libs]

begin
mod.run_simple(
'Action' => args[:action],
Expand Down
15 changes: 9 additions & 6 deletions lib/msf/ui/console/module_argument_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ module ModuleArgumentParsing

# Options which are standard and predictable across all modules
@@module_opts = Rex::Parser::Arguments.new(
['-h', '--help'] => [ false, 'Help banner.' ],
['-j', '--job'] => [ false, 'Run in the context of a job.' ],
['-J', '--foreground'] => [ false, 'Force running in the foreground, even if passive.' ],
['-o', '--options'] => [ true, 'A comma separated list of options in VAR=VAL format.', '<options>' ],
['-q', '--quiet'] => [ false, 'Run the module in quiet mode with no output' ]
['-h', '--help'] => [ false, 'Help banner.' ],
['-j', '--job'] => [ false, 'Run in the context of a job.' ],
['-J', '--foreground'] => [ false, 'Force running in the foreground, even if passive.' ],
['-o', '--options'] => [ true, 'A comma separated list of options in VAR=VAL format.', '<options>' ],
['-q', '--quiet'] => [ false, 'Run the module in quiet mode with no output' ],
['-r', '--reload-libs'] => [ false, 'Reload all libraries before running.' ]
)

@@module_opts_with_action_support = @@module_opts.merge(
Expand All @@ -41,7 +42,7 @@ def parse_check_opts(args)
help_cmd = proc do |_result|
cmd_check_help
end
parse_opts(@@module_opts_with_action_support, args, help_cmd: help_cmd)&.slice(:datastore_options)
parse_opts(@@module_opts_with_action_support, args, help_cmd: help_cmd)&.slice(:datastore_options, :reload_libs)
end

def parse_run_opts(args, action: nil)
Expand Down Expand Up @@ -127,6 +128,8 @@ def parse_opts(opts, args, help_cmd:, action: nil)
end
when '-p'
result[:payload] = val
when '-r'
result[:reload_libs] = true
when '-t'
result[:target] = val.to_i
when '-z'
Expand Down
2 changes: 2 additions & 0 deletions lib/msf/ui/console/module_command_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ def cmd_check(*args)
return false
end

dispatcher(::Msf::Ui::Console::CommandDispatcher::Developer)&.cmd_reload_lib('-a') if args[:reload_libs]

begin
check_multiple(mod_with_opts)
rescue ::Interrupt
Expand Down

0 comments on commit 1499e4c

Please sign in to comment.