Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/ui/console/command_dispatcher/session.rb
Views: 11786
# frozen_string_literal: true12module Msf3module Ui4module Console5module CommandDispatcher6module Session7include Rex::Ui::Text::DispatcherShell::CommandDispatcher89@@irb_opts = Rex::Parser::Arguments.new(10%w[-h --help] => [false, 'Help menu.' ],11'-e' => [true, 'Expression to evaluate.']12)1314@@sessions_opts = Rex::Parser::Arguments.new(15['-h', '--help'] => [ false, 'Show this message' ],16['-i', '--interact'] => [ true, 'Interact with a provided session ID', '<id>' ]17)1819def commands20{21'?' => 'Help menu',22'background' => 'Backgrounds the current session',23'bg' => 'Alias for background',24'exit' => 'Terminate the session',25'help' => 'Help menu',26'irb' => 'Open an interactive Ruby shell on the current session',27'pry' => 'Open the Pry debugger on the current session',28'quit' => 'Terminate the session',29'resource' => 'Run the commands stored in a file',30'uuid' => 'Get the UUID for the current session',31'sessions' => 'Quickly switch to another session'32}33end3435def cmd_background_help36print_line('Usage: background')37print_line38print_line('Stop interacting with this session and return to the parent prompt')39print_line40end4142def cmd_background(*args)43if args.include?('-h') || args.include?('--help')44cmd_background_help45return46end47print_status("Backgrounding session #{session.name}...")48session.interacting = false49end5051alias cmd_bg cmd_background52alias cmd_bg_help cmd_background_help5354#55# Terminates the session.56#57def cmd_exit(*args)58print_status("Shutting down session: #{session.sid}")59session.exit60end6162alias cmd_quit cmd_exit6364def cmd_irb_help65print_line('Usage: irb')66print_line67print_line('Open an interactive Ruby shell on the current session.')68print @@irb_opts.usage69end7071def cmd_irb_tabs(str, words)72return [] if words.length > 17374@@irb_opts.option_keys75end7677#78# Open an interactive Ruby shell on the current session79#80def cmd_irb(*args)81expressions = []8283# Parse the command options84@@irb_opts.parse(args) do |opt, _idx, val|85case opt86when '-e'87expressions << val88when '-h', '--help'89return cmd_irb_help90end91end9293framework = session.framework9495if expressions.empty?96print_status('Starting IRB shell...')97print_status("You are in the session object\n")98framework.history_manager.with_context(name: :irb) do99Rex::Ui::Text::IrbShell.new(session).run100end101else102# XXX: No vprint_status here103if framework.datastore['VERBOSE'].to_s == 'true'104print_status("You are executing expressions in #{binding.receiver}")105end106107expressions.each { |expression| eval(expression, binding) }108end109end110111def cmd_pry_help112print_line 'Usage: pry'113print_line114print_line 'Open the Pry debugger on the current session.'115print_line116end117118#119# Open the Pry debugger on the current session120#121def cmd_pry(*args)122if args.include?('-h') || args.include?('--help')123cmd_pry_help124return125end126127begin128require 'pry'129rescue LoadError130print_error('Failed to load Pry, try "gem install pry"')131return132end133134print_status('Starting Pry shell...')135print_status("You are in the session object\n")136137Pry.config.history_load = false138session.framework.history_manager.with_context(history_file: Msf::Config.pry_history, name: :pry) do139session.pry140end141end142143def cmd_sessions_help144print_line('Usage: sessions [options] or sessions [id]')145print_line146print_line('Interact with a different session ID.')147print(@@sessions_opts.usage)148print_line149end150151def cmd_sessions(*args)152if args.empty?153cmd_sessions_help154return false155end156157sid = nil158159if args.length == 1 && args[0] =~ /-?\d+/160sid = args[0].to_i161else162@@sessions_opts.parse(args) do |opt, _idx, val|163case opt164when '-h', '--help'165cmd_sessions_help166return false167when '-i', '--interact'168sid = val.to_i169else170cmd_sessions_help171return false172end173end174end175176if sid == 0 || sid.nil?177cmd_sessions_help178return false179end180181if sid.to_s == session.name.to_s182print_status("Session #{session.name} is already interactive.")183else184print_status("Backgrounding session #{session.name}...")185# store the next session id so that it can be referenced as soon186# as this session is no longer interacting187session.next_session = sid188session.interacting = false189end190end191192def cmd_resource_help193print_line 'Usage: resource path1 [path2 ...]'194print_line195print_line 'Run the commands stored in the supplied files. (- for stdin, press CTRL+D to end input from stdin)'196print_line 'Resource files may also contain ERB or Ruby code between <ruby></ruby> tags.'197print_line198end199200def cmd_resource(*args)201if args.empty? || args.include?('-h') || args.include?('--help')202cmd_resource_help203return false204end205206args.each do |res|207good_res = nil208if res == '-'209good_res = res210elsif ::File.exist?(res)211good_res = res212elsif [213::Msf::Config.script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter',214::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter'215].each do |dir|216res_path = dir + ::File::SEPARATOR + res217if ::File.exist?(res_path)218good_res = res_path219break220end221end222# let's check to see if it's in the scripts/resource dir (like when tab completed)223end224unless good_res225print_error("#{res} is not a valid resource file")226next227end228229session.console.load_resource(good_res)230end231end232233def cmd_resource_tabs(str, words)234tabs = []235# return tabs if words.length > 1236if (str && str =~ (/^#{Regexp.escape(::File::SEPARATOR)}/))237# then you are probably specifying a full path so let's just use normal file completion238return tab_complete_filenames(str, words)239elsif (!(words[1]) || !words[1].match(%r{^/}))240# then let's start tab completion in the scripts/resource directories241begin242[243::Msf::Config.script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter',244::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter',245'.'246].each do |dir|247next if !::File.exist? dir248249tabs += ::Dir.new(dir).find_all do |e|250path = dir + ::File::SEPARATOR + e251::File.file?(path) and ::File.readable?(path)252end253end254rescue StandardError => e255elog('Problem tab completing resource file names in the scripts/resource directories', error: e)256end257else258tabs += tab_complete_filenames(str, words)259end260261return tabs262end263end264end265end266end267end268269270