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/base/sessions/ldap.rb
Views: 11784
# -*- coding: binary -*-12require 'rex/post/ldap'34class Msf::Sessions::LDAP5#6# This interface supports basic interaction.7#8include Msf::Session::Basic9include Msf::Sessions::Scriptable1011# @return [Rex::Post::LDAP::Ui::Console] The interactive console12attr_accessor :console13# @return [Rex::Proto::LDAP::Client] The LDAP client14attr_accessor :client1516attr_accessor :keep_alive_thread1718# @return [Integer] Seconds between keepalive requests19attr_accessor :keepalive_seconds2021attr_accessor :platform, :arch22attr_reader :framework2324# @param[Rex::IO::Stream] rstream25# @param [Hash] opts26# @option opts [Rex::Proto::LDAP::Client] :client27# @option opts [Integer] :keepalive28def initialize(rstream, opts = {})29@client = opts.fetch(:client)30@keepalive_seconds = opts.fetch(:keepalive_seconds)31self.console = Rex::Post::LDAP::Ui::Console.new(self)32super(rstream, opts)33end3435def cleanup36stop_keep_alive_loop37super38end3940def bootstrap(datastore = {}, handler = nil)41session = self42session.init_ui(user_input, user_output)4344@info = "LDAP #{datastore['USERNAME']} @ #{@peer_info}"45end4647def execute_file(full_path, args)48if File.extname(full_path) == '.rb'49Rex::Script::Shell.new(self, full_path).run(args)50else51console.load_resource(full_path)52end53end5455def process_autoruns(datastore)56['InitialAutoRunScript', 'AutoRunScript'].each do |key|57next if datastore[key].nil? || datastore[key].empty?5859args = Shellwords.shellwords(datastore[key])60print_status("Session ID #{sid} (#{tunnel_to_s}) processing #{key} '#{datastore[key]}'")61execute_script(args.shift, *args)62end63end6465def type66self.class.type67end6869# Returns the type of session.70#71def self.type72'ldap'73end7475def self.can_cleanup_files76false77end7879#80# Returns the session description.81#82def desc83'LDAP'84end8586def address87@address ||= client.peerhost88end8990def port91@port ||= client.peerport92end9394##95# :category: Msf::Session::Interactive implementors96#97# Initializes the console's I/O handles.98#99def init_ui(input, output)100self.user_input = input101self.user_output = output102console.init_ui(input, output)103console.set_log_source(log_source)104105super106end107108##109# :category: Msf::Session::Interactive implementors110#111# Resets the console's I/O handles.112#113def reset_ui114console.unset_log_source115console.reset_ui116end117118def exit119console.stop120end121122##123# :category: Msf::Session::Interactive implementors124#125# Override the basic session interaction to use shell_read and126# shell_write instead of operating on rstream directly.127def _interact128framework.events.on_session_interact(self)129framework.history_manager.with_context(name: type.to_sym) do130_interact_stream131end132end133134##135# :category: Msf::Session::Interactive implementors136#137def _interact_stream138framework.events.on_session_interact(self)139140console.framework = framework141# Call the console interaction of the ldap client and142# pass it a block that returns whether or not we should still be143# interacting. This will allow the shell to abort if interaction is144# canceled.145console.interact { interacting != true }146console.framework = nil147148# If the stop flag has been set, then that means the user exited. Raise149# the EOFError so we can drop this handle like a bad habit.150raise EOFError if (console.stopped? == true)151end152153def on_registered154start_keep_alive_loop155end156157# Start a background thread for regularly sending a no-op command to keep the connection alive158def start_keep_alive_loop159self.keep_alive_thread = framework.threads.spawn("LDAP-shell-keepalive-#{sid}", false) do160loop do161if client.last_interaction.nil?162remaining_sleep = @keepalive_seconds163else164remaining_sleep = @keepalive_seconds - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction)165end166sleep(remaining_sleep)167if (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction) > @keepalive_seconds168client.search_root_dse169end170# This should have moved last_interaction forwards171fail if (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction) > @keepalive_seconds172end173end174end175176# Stop the background thread177def stop_keep_alive_loop178keep_alive_thread.kill179end180end181182183