Skip to content

Commit 5be94c1

Browse files
committed
Land rapid7#5602, adds irb -e to core
2 parents 305e35c + 434cffa commit 5be94c1

File tree

2 files changed

+62
-13
lines changed
  • lib
    • msf/ui/console/command_dispatcher
    • rex/post/meterpreter/ui/console/command_dispatcher

2 files changed

+62
-13
lines changed

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class Core
9393
@@go_pro_opts = Rex::Parser::Arguments.new(
9494
"-h" => [ false, "Help banner." ])
9595

96+
@@irb_opts = Rex::Parser::Arguments.new(
97+
"-h" => [ false, "Help banner." ],
98+
"-e" => [ true, "Expression to evaluate." ])
99+
96100
# The list of data store elements that cannot be set when in defanged
97101
# mode.
98102
DefangedProhibitedDataStoreElements = [ "MsfModulePaths" ]
@@ -758,8 +762,8 @@ def cmd_info_tabs(str, words)
758762
def cmd_irb_help
759763
print_line "Usage: irb"
760764
print_line
761-
print_line "Drop into an interactive Ruby environment"
762-
print_line
765+
print_line "Execute commands in a Ruby environment"
766+
print @@irb_opts.usage
763767
end
764768

765769
#
@@ -768,17 +772,34 @@ def cmd_irb_help
768772
def cmd_irb(*args)
769773
defanged?
770774

771-
print_status("Starting IRB shell...\n")
775+
expressions = []
772776

773-
begin
774-
Rex::Ui::Text::IrbShell.new(binding).run
775-
rescue
776-
print_error("Error during IRB: #{$!}\n\n#{$@.join("\n")}")
777+
# Parse the command options
778+
@@irb_opts.parse(args) do |opt, idx, val|
779+
case opt
780+
when '-e'
781+
expressions << val
782+
when '-h'
783+
cmd_irb_help
784+
return false
785+
end
777786
end
778787

779-
# Reset tab completion
780-
if (driver.input.supports_readline)
781-
driver.input.reset_tab_completion
788+
if expressions.empty?
789+
print_status("Starting IRB shell...\n")
790+
791+
begin
792+
Rex::Ui::Text::IrbShell.new(binding).run
793+
rescue
794+
print_error("Error during IRB: #{$!}\n\n#{$@.join("\n")}")
795+
end
796+
797+
# Reset tab completion
798+
if (driver.input.supports_readline)
799+
driver.input.reset_tab_completion
800+
end
801+
else
802+
expressions.each { |expression| eval(expression, binding) }
782803
end
783804
end
784805

lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def initialize(shell)
3030
self.bgjob_id = 0
3131
end
3232

33+
@@irb_opts = Rex::Parser::Arguments.new(
34+
"-h" => [ false, "Help banner." ],
35+
"-e" => [ true, "Expression to evaluate." ])
36+
3337
@@load_opts = Rex::Parser::Arguments.new(
3438
"-l" => [ false, "List all available extensions" ],
3539
"-h" => [ false, "Help menu." ])
@@ -322,16 +326,40 @@ def cmd_interact(*args)
322326

323327
alias cmd_interact_tabs cmd_close_tabs
324328

329+
def cmd_irb_help
330+
print_line "Usage: irb"
331+
print_line
332+
print_line "Execute commands in a Ruby environment"
333+
print @@irb_opts.usage
334+
end
335+
325336
#
326337
# Runs the IRB scripting shell
327338
#
328339
def cmd_irb(*args)
329-
print_status("Starting IRB shell")
330-
print_status("The 'client' variable holds the meterpreter client\n")
340+
expressions = []
341+
342+
# Parse the command options
343+
@@irb_opts.parse(args) do |opt, idx, val|
344+
case opt
345+
when '-e'
346+
expressions << val
347+
when '-h'
348+
return cmd_irb_help
349+
end
350+
end
331351

332352
session = client
333353
framework = client.framework
334-
Rex::Ui::Text::IrbShell.new(binding).run
354+
355+
if expressions.empty?
356+
print_status("Starting IRB shell")
357+
print_status("The 'client' variable holds the meterpreter client\n")
358+
359+
Rex::Ui::Text::IrbShell.new(binding).run
360+
else
361+
expressions.each { |expression| eval(expression, binding) }
362+
end
335363
end
336364

337365
@@set_timeouts_opts = Rex::Parser::Arguments.new(

0 commit comments

Comments
 (0)