Class: Msf::Sessions::LDAP

Inherits:
Object
  • Object
show all
Includes:
Msf::Session::Basic, Scriptable
Defined in:
lib/msf/base/sessions/ldap.rb

Instance Attribute Summary collapse

Attributes included from Msf::Session::Interactive

#rstream

Attributes included from Rex::Ui::Interactive

#completed, #interacting, #next_session, #on_command_proc, #on_print_proc, #on_run_command_error_proc, #orig_suspend, #orig_usr1, #orig_winch

Attributes included from Rex::Ui::Subscriber::Input

#user_input

Attributes included from Rex::Ui::Subscriber::Output

#user_output

Attributes included from Msf::Session

#alive, #db_record, #exploit, #exploit_datastore, #exploit_task, #exploit_uuid, #info, #machine_id, #payload_uuid, #routes, #sid, #sname, #target_host, #target_port, #username, #uuid, #via, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scriptable

#execute_script, included, #legacy_script_to_post_module

Methods included from Msf::Session::Interactive

#_interact_complete, #_interrupt, #_suspend, #_usr1, #abort_foreground, #abort_foreground_supported, #comm_channel, #interactive?, #kill, #run_cmd, #tunnel_local, #tunnel_peer, #user_want_abort?

Methods included from Rex::Ui::Interactive

#_interact_complete, #_interrupt, #_local_fd, #_remote_fd, #_stream_read_local_write_remote, #_stream_read_remote_write_local, #_suspend, #_winch, #detach, #handle_suspend, #handle_usr1, #handle_winch, #interact, #interact_stream, #prompt, #prompt_yesno, #restore_suspend, #restore_usr1, #restore_winch

Methods included from Rex::Ui::Subscriber

#copy_ui

Methods included from Rex::Ui::Subscriber::Input

#gets

Methods included from Rex::Ui::Subscriber::Output

#flush, #print, #print_blank_line, #print_error, #print_good, #print_line, #print_status, #print_warning

Methods included from Msf::Session

#alive?, #comm_channel, #dead?, #inspect, #interactive?, #kill, #log_file_name, #log_source, #name, #name=, #register?, #session_host, #session_host=, #session_port, #session_port=, #session_type, #set_from_exploit, #set_via, #tunnel_local, #tunnel_peer, #tunnel_to_s, #via_exploit, #via_payload

Constructor Details

#initialize(rstream, opts = {}) ⇒ LDAP

@param rstream

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):



29
30
31
32
33
34
# File 'lib/msf/base/sessions/ldap.rb', line 29

def initialize(rstream, opts = {})
  @client = opts.fetch(:client)
  @keepalive_seconds = opts.fetch(:keepalive_seconds)
  self.console = Rex::Post::LDAP::Ui::Console.new(self)
  super(rstream, opts)
end

Instance Attribute Details

#archObject

Returns the value of attribute arch.



22
23
24
# File 'lib/msf/base/sessions/ldap.rb', line 22

def arch
  @arch
end

#clientRex::Proto::LDAP::Client

Returns The LDAP client.

Returns:



15
16
17
# File 'lib/msf/base/sessions/ldap.rb', line 15

def client
  @client
end

#consoleRex::Post::LDAP::Ui::Console

Returns The interactive console.

Returns:



13
14
15
# File 'lib/msf/base/sessions/ldap.rb', line 13

def console
  @console
end

#frameworkObject (readonly)

Returns the value of attribute framework.



23
24
25
# File 'lib/msf/base/sessions/ldap.rb', line 23

def framework
  @framework
end

#keep_alive_threadObject

Returns the value of attribute keep_alive_thread.



17
18
19
# File 'lib/msf/base/sessions/ldap.rb', line 17

def keep_alive_thread
  @keep_alive_thread
end

#keepalive_secondsInteger

Returns Seconds between keepalive requests.

Returns:

  • (Integer)

    Seconds between keepalive requests



20
21
22
# File 'lib/msf/base/sessions/ldap.rb', line 20

def keepalive_seconds
  @keepalive_seconds
end

#platformObject

Returns the value of attribute platform.



22
23
24
# File 'lib/msf/base/sessions/ldap.rb', line 22

def platform
  @platform
end

Class Method Details

.can_cleanup_filesObject



76
77
78
# File 'lib/msf/base/sessions/ldap.rb', line 76

def self.can_cleanup_files
  false
end

.typeObject

Returns the type of session.



72
73
74
# File 'lib/msf/base/sessions/ldap.rb', line 72

def self.type
  'ldap'
end

Instance Method Details

#_interactObject

:category: Msf::Session::Interactive implementors

Override the basic session interaction to use shell_read and shell_write instead of operating on rstream directly.



128
129
130
131
132
133
# File 'lib/msf/base/sessions/ldap.rb', line 128

def _interact
  framework.events.on_session_interact(self)
  framework.history_manager.with_context(name: type.to_sym) do
    _interact_stream
  end
end

#_interact_streamObject

:category: Msf::Session::Interactive implementors

Raises:

  • (EOFError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/msf/base/sessions/ldap.rb', line 138

def _interact_stream
  framework.events.on_session_interact(self)

  console.framework = framework
  # Call the console interaction of the ldap client and
  # pass it a block that returns whether or not we should still be
  # interacting.  This will allow the shell to abort if interaction is
  # canceled.
  console.interact { interacting != true }
  console.framework = nil

  # If the stop flag has been set, then that means the user exited.  Raise
  # the EOFError so we can drop this handle like a bad habit.
  raise EOFError if (console.stopped? == true)
end

#addressObject



87
88
89
# File 'lib/msf/base/sessions/ldap.rb', line 87

def address
  @address ||= client.peerhost
end

#bootstrap(datastore = {}, handler = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/msf/base/sessions/ldap.rb', line 41

def bootstrap(datastore = {}, handler = nil)
  session = self
  session.init_ui(user_input, user_output)

  @info = "LDAP #{datastore['USERNAME']} @ #{@peer_info}"
end

#cleanupObject



36
37
38
39
# File 'lib/msf/base/sessions/ldap.rb', line 36

def cleanup
  stop_keep_alive_loop
  super
end

#descObject

Returns the session description.



83
84
85
# File 'lib/msf/base/sessions/ldap.rb', line 83

def desc
  'LDAP'
end

#execute_file(full_path, args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/msf/base/sessions/ldap.rb', line 48

def execute_file(full_path, args)
  if File.extname(full_path) == '.rb'
    Rex::Script::Shell.new(self, full_path).run(args)
  else
    console.load_resource(full_path)
  end
end

#exitObject



119
120
121
# File 'lib/msf/base/sessions/ldap.rb', line 119

def exit
  console.stop
end

#init_ui(input, output) ⇒ Object

:category: Msf::Session::Interactive implementors

Initializes the console’s I/O handles.



100
101
102
103
104
105
106
107
# File 'lib/msf/base/sessions/ldap.rb', line 100

def init_ui(input, output)
  self.user_input = input
  self.user_output = output
  console.init_ui(input, output)
  console.set_log_source(log_source)

  super
end

#on_registeredObject



154
155
156
# File 'lib/msf/base/sessions/ldap.rb', line 154

def on_registered
  start_keep_alive_loop
end

#portObject



91
92
93
# File 'lib/msf/base/sessions/ldap.rb', line 91

def port
  @port ||= client.peerport
end

#process_autoruns(datastore) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/msf/base/sessions/ldap.rb', line 56

def process_autoruns(datastore)
  ['InitialAutoRunScript', 'AutoRunScript'].each do |key|
    next if datastore[key].nil? || datastore[key].empty?

    args = Shellwords.shellwords(datastore[key])
    print_status("Session ID #{sid} (#{tunnel_to_s}) processing #{key} '#{datastore[key]}'")
    execute_script(args.shift, *args)
  end
end

#reset_uiObject

:category: Msf::Session::Interactive implementors

Resets the console’s I/O handles.



114
115
116
117
# File 'lib/msf/base/sessions/ldap.rb', line 114

def reset_ui
  console.unset_log_source
  console.reset_ui
end

#start_keep_alive_loopObject

Start a background thread for regularly sending a no-op command to keep the connection alive



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/msf/base/sessions/ldap.rb', line 159

def start_keep_alive_loop
  self.keep_alive_thread = framework.threads.spawn("LDAP-shell-keepalive-#{sid}", false) do
    loop do
      if client.last_interaction.nil?
        remaining_sleep = @keepalive_seconds
      else
        remaining_sleep = @keepalive_seconds - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction)
      end
      sleep(remaining_sleep)
      if (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction) > @keepalive_seconds
        client.search_root_dse
      end
      # This should have moved last_interaction forwards
      fail if (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction) > @keepalive_seconds
    end
  end
end

#stop_keep_alive_loopObject

Stop the background thread



178
179
180
# File 'lib/msf/base/sessions/ldap.rb', line 178

def stop_keep_alive_loop
  keep_alive_thread.kill
end

#typeObject



66
67
68
# File 'lib/msf/base/sessions/ldap.rb', line 66

def type
  self.class.type
end