Skip to content

Commit 2856fac

Browse files
committed
Land rapid7#7765, adds the history command to msfconsole
2 parents d2624ef + e729254 commit 2856fac

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

features/commands/help.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Feature: Help command
2121
getg Gets the value of a global variable
2222
grep Grep the output of another command
2323
help Help menu
24+
history Show commands history
2425
irb Drop into irb scripting mode
2526
load Load a framework plugin
2627
quit Exit the console

lib/msf/ui/console/command_dispatcher/core.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ class Core
8585
"-k" => [ true, "Keep (include) arg lines at start of output." ],
8686
"-c" => [ false, "Only print a count of matching lines." ])
8787

88-
88+
@@history_opts = Rex::Parser::Arguments.new(
89+
"-h" => [ false, "Help banner." ],
90+
"-a" => [ false, "Show all commands in history." ],
91+
"-n" => [ true, "Show the last n commands." ])
8992

9093
@@irb_opts = Rex::Parser::Arguments.new(
9194
"-h" => [ false, "Help banner." ],
@@ -105,6 +108,7 @@ def commands
105108
"getg" => "Gets the value of a global variable",
106109
"grep" => "Grep the output of another command",
107110
"help" => "Help menu",
111+
"history" => "Show commands history",
108112
"irb" => "Drop into irb scripting mode",
109113
"load" => "Load a framework plugin",
110114
"quit" => "Exit the console",
@@ -133,6 +137,7 @@ def initialize(driver)
133137
@cache_payloads = nil
134138
@previous_module = nil
135139
@module_name_stack = []
140+
@history_limit = 100
136141
end
137142

138143
#
@@ -473,6 +478,43 @@ def cmd_exit(*args)
473478

474479
alias cmd_quit cmd_exit
475480

481+
482+
def cmd_history(*args)
483+
484+
limit = @history_limit
485+
length = Readline::HISTORY.length
486+
487+
@@history_opts.parse(args) do |opt, _idx, val|
488+
case opt
489+
when "-a"
490+
limit = length
491+
when "-n"
492+
return cmd_history_help unless !val.nil? && val.match(/\A[-+]?\d+\z/)
493+
limit = val.to_i
494+
limit = length if limit >= length
495+
when "-h"
496+
cmd_history_help
497+
return false
498+
end
499+
end
500+
501+
start = length - limit
502+
(start..length-1).each do |pos|
503+
print "#{pos} #{Readline::HISTORY[pos]}\n"
504+
end
505+
end
506+
507+
def cmd_history_help
508+
print_line "Usage: history [options]"
509+
print_line
510+
print_line "Shows the command history."
511+
print_line "If -n is not set, it will only be shown the last #{@history_limit} commands."
512+
print_line
513+
print @@history_opts.usage
514+
end
515+
516+
517+
476518
def cmd_sleep_help
477519
print_line "Usage: sleep <seconds>"
478520
print_line

0 commit comments

Comments
 (0)