CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/ldap/ui/console.rb
Views: 11789
1
# -*- coding: binary -*-
2
3
require 'English'
4
require 'rex/post/session_compatible_modules'
5
6
module Rex
7
module Post
8
module LDAP
9
module Ui
10
###
11
#
12
# This class provides a shell driven interface to the LDAP client API.
13
#
14
###
15
class Console
16
17
include Rex::Ui::Text::DispatcherShell
18
include Rex::Post::SessionCompatibleModules
19
20
# Dispatchers
21
require 'rex/post/ldap/ui/console/command_dispatcher'
22
require 'rex/post/ldap/ui/console/command_dispatcher/core'
23
require 'rex/post/ldap/ui/console/command_dispatcher/client'
24
25
#
26
# Initialize the LDAP console.
27
#
28
# @param [Msf::Sessions::LDAP] session
29
def initialize(session)
30
super('%undLDAP%clr', '>', Msf::Config.ldap_session_history, nil, :ldap)
31
32
# The ldap client context
33
self.session = session
34
self.client = session.client
35
36
# Queued commands array
37
self.commands = []
38
39
# Point the input/output handles elsewhere
40
reset_ui
41
42
enstack_dispatcher(Rex::Post::LDAP::Ui::Console::CommandDispatcher::Client)
43
enstack_dispatcher(Rex::Post::LDAP::Ui::Console::CommandDispatcher::Core)
44
enstack_dispatcher(Msf::Ui::Console::CommandDispatcher::LocalFileSystem)
45
46
# Set up logging to whatever logsink 'core' is using
47
if !$dispatcher['ldap']
48
$dispatcher['ldap'] = $dispatcher['core']
49
end
50
end
51
52
#
53
# Called when someone wants to interact with the LDAP client. It's
54
# assumed that init_ui has been called prior.
55
#
56
def interact(&block)
57
# Run queued commands
58
commands.delete_if do |ent|
59
run_single(ent)
60
true
61
end
62
63
# Run the interactive loop
64
run do |line|
65
# Run the command
66
run_single(line)
67
68
# If a block was supplied, call it, otherwise return false
69
if block
70
block.call
71
else
72
false
73
end
74
end
75
end
76
77
#
78
# Queues a command to be run when the interactive loop is entered.
79
#
80
def queue_cmd(cmd)
81
commands << cmd
82
end
83
84
#
85
# Runs the specified command wrapper in something to catch exceptions.
86
#
87
def run_command(dispatcher, method, arguments)
88
super
89
rescue Timeout::Error
90
log_error('Operation timed out.')
91
rescue Rex::InvalidDestination => e
92
log_error(e.message)
93
rescue ::Errno::EPIPE, ::OpenSSL::SSL::SSLError, ::IOError, Net::LDAP::ResponseMissingOrInvalidError
94
session.kill
95
rescue ::StandardError => e
96
log_error("Error running command #{method}: #{e.class} #{e}")
97
elog(e)
98
end
99
100
# @param [Hash] opts
101
# @return [String]
102
def help_to_s(opts = {})
103
super + format_session_compatible_modules
104
end
105
106
#
107
# Logs that an error occurred and persists the callstack.
108
#
109
def log_error(msg)
110
print_error(msg)
111
112
elog(msg, 'ldap')
113
114
dlog("Call stack:\n#{$ERROR_POSITION.join("\n")}", 'ldap')
115
end
116
117
# @return [Msf::Sessions::LDAP]
118
attr_reader :session
119
120
# @return [Rex::Proto::LDAP::Client]
121
attr_reader :client # :nodoc:
122
123
def format_prompt(val)
124
prompt = session.address.to_s
125
126
substitute_colors("%undLDAP%clr (#{prompt}) > ", true)
127
end
128
129
protected
130
131
attr_writer :session, :client # :nodoc: # :nodoc:
132
attr_accessor :commands # :nodoc:
133
end
134
end
135
end
136
end
137
end
138
139