Skip to content

Commit c732fed

Browse files
committed
Feedback from code review
1 parent 22cf3f0 commit c732fed

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

lib/msf/base/sessions/ldap.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ class Msf::Sessions::LDAP
1515
attr_accessor :client
1616

1717
attr_accessor :keep_alive_thread
18+
19+
# @return [Integer] Seconds between keepalive requests
20+
attr_accessor :keepalive_seconds
1821

1922
attr_accessor :platform, :arch
2023
attr_reader :framework
2124

2225
# @param[Rex::IO::Stream] rstream
2326
# @param [Hash] opts
2427
# @option opts [Rex::Proto::LDAP::Client] :client
28+
# @option opts [Integer] :keepalive
2529
def initialize(rstream, opts = {})
2630
@client = opts.fetch(:client)
31+
@keepalive_seconds = opts.fetch(:keepalive_seconds)
2732
self.console = Rex::Post::LDAP::Ui::Console.new(self)
2833
super(rstream, opts)
2934
end
@@ -152,20 +157,19 @@ def on_registered
152157

153158
# Start a background thread for regularly sending a no-op command to keep the connection alive
154159
def start_keep_alive_loop
155-
self.keep_alive_thread = framework.threads.spawn('LDAP-shell-keepalive', false) do
156-
keep_alive_timeout = 10 * 60 # 10 minutes
160+
self.keep_alive_thread = framework.threads.spawn("LDAP-shell-keepalive-#{sid}", false) do
157161
loop do
158162
if client.last_interaction.nil?
159-
remaining_sleep = keep_alive_timeout
163+
remaining_sleep = @keepalive_seconds
160164
else
161-
remaining_sleep = keep_alive_timeout - (Time.now - client.last_interaction)
165+
remaining_sleep = @keepalive_seconds - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction)
162166
end
163167
sleep(remaining_sleep)
164-
if (Time.now - client.last_interaction) > keep_alive_timeout
168+
if (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction) > @keepalive_seconds
165169
client.search_root_dse
166170
end
167171
# This should have moved last_interaction forwards
168-
fail if (Time.now - client.last_interaction) > keep_alive_timeout
172+
fail if (Process.clock_gettime(Process::CLOCK_MONOTONIC) - client.last_interaction) > @keepalive_seconds
169173
end
170174
end
171175
end

lib/msf/core/exploit/remote/ldap.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def resolve_connect_opts(connect_opts)
168168
# the target LDAP server.
169169
def ldap_new(opts = {})
170170
ldap = Rex::Proto::LDAP::Client.new(resolve_connect_opts(get_connect_opts.merge(opts)))
171-
mutex = Mutex.new
172171

173172
# NASTY, but required
174173
# monkey patch ldap object in order to ignore bind errors
@@ -185,9 +184,7 @@ def ldap_new(opts = {})
185184
# @param args [Hash] A hash containing options for the ldap connection
186185
def ldap.use_connection(args)
187186
if @open_connection
188-
mutex.synchronize do
189-
yield @open_connection
190-
end
187+
yield @open_connection
191188
register_interaction
192189
else
193190
begin

lib/rex/proto/ldap/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(args)
2525
end
2626

2727
def register_interaction
28-
@last_interaction = Time.now
28+
@last_interaction = Process.clock_gettime(Process::CLOCK_MONOTONIC)
2929
end
3030

3131
# @return [Array<String>] LDAP servers naming contexts

modules/auxiliary/scanner/ldap/ldap_login.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def initialize(info = {})
3636
OptBool.new(
3737
'APPEND_DOMAIN', [true, 'Appends `@<DOMAIN> to the username for authentication`', false],
3838
conditions: ['LDAP::Auth', 'in', [Msf::Exploit::Remote::AuthOption::AUTO, Msf::Exploit::Remote::AuthOption::PLAINTEXT]]
39-
)
39+
),
40+
OptInt.new('SessionKeepalive', [true, 'Time (in seconds) for sending protocol-level keepalive messages', 10 * 60])
4041
]
4142
)
4243

@@ -48,6 +49,7 @@ def initialize(info = {})
4849
else
4950
# Don't give the option to create a session unless ldap sessions are enabled
5051
options_to_deregister << 'CreateSession'
52+
options_to_deregister << 'SessionKeepalive'
5153
end
5254

5355
deregister_options(*options_to_deregister)
@@ -175,7 +177,7 @@ def session_setup(result)
175177
return unless result.connection && result.proof
176178

177179
# Create a new session
178-
my_session = Msf::Sessions::LDAP.new(result.connection, { client: result.proof })
180+
my_session = Msf::Sessions::LDAP.new(result.connection, { client: result.proof, keepalive_seconds: datastore['SessionKeepalive'] })
179181

180182
merge_me = {
181183
'USERPASS_FILE' => nil,

0 commit comments

Comments
 (0)