Skip to content

Commit 8d943ef

Browse files
committed
Add the ldapwhoami command support
See RFC4532 and ruby-ldap/ruby-net-ldap#425
1 parent 94535bb commit 8d943ef

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

lib/rex/post/ldap/ui/console/command_dispatcher/client.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class Console::CommandDispatcher::Client
3232
#
3333
def commands
3434
cmds = {
35-
'query' => 'Run an LDAP query'
35+
'query' => 'Run an LDAP query',
36+
'getuid' => 'Get the user that the connection is running as'
3637
}
3738

3839
reqs = {}
@@ -102,6 +103,12 @@ def cmd_query_help
102103
print @@query_opts.usage
103104
end
104105

106+
def cmd_getuid
107+
username = client.ldapwhoami
108+
username.delete_prefix!('u:')
109+
print_status("Server username: #{username}")
110+
end
111+
105112
private
106113

107114
def parse_scope(str)

lib/rex/proto/ldap.rb

+39
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
# Update Net::LDAP's initialize and new_connection method to honor a tracking proxies setting
1111
class Net::LDAP
12+
WhoamiOid = '1.3.6.1.4.1.4203.1.11.3'.freeze
13+
14+
# fix the definition for ExtendedResponse
15+
AsnSyntax[Net::BER::TAG_CLASS[:universal] + Net::BER::ENCODING_TYPE[:constructed] + 107] = :string
16+
1217
# Reference the old initialize method, and ensure `reload_lib -a` doesn't attempt to refine the method
1318
alias_method :_old_initialize, :initialize unless defined?(_old_initialize)
1419

@@ -457,6 +462,40 @@ def modify(args)
457462

458463
pdu
459464
end
465+
466+
# Monkeypatch upstream library to support the extended Whoami request. Delete
467+
# this after https://github.com/ruby-ldap/ruby-net-ldap/pull/425 is landed.
468+
# This is not the only occurrence of a patch for this functionality.
469+
def ldapwhoami
470+
ext_seq = [Net::LDAP::WhoamiOid.to_ber_contextspecific(0)]
471+
request = ext_seq.to_ber_appsequence(Net::LDAP::PDU::ExtendedRequest)
472+
473+
message_id = next_msgid
474+
475+
write(request, nil, message_id)
476+
pdu = queued_read(message_id)
477+
478+
if !pdu || pdu.app_tag != Net::LDAP::PDU::ExtendedResponse
479+
raise Net::LDAP::ResponseMissingOrInvalidError, "response missing or invalid"
480+
end
481+
482+
pdu
483+
end
484+
end
485+
486+
class Net::LDAP::PDU
487+
# Monkeypatch upstream library to support the extended Whoami request. Delete
488+
# this after https://github.com/ruby-ldap/ruby-net-ldap/pull/425 is landed.
489+
# This is not the only occurrence of a patch for this functionality.
490+
def parse_extended_response(sequence)
491+
sequence.length.between?(3, 5) or raise Net::LDAP::PDU::Error, "Invalid LDAP result length."
492+
@ldap_result = {
493+
:resultCode => sequence[0],
494+
:matchedDN => sequence[1],
495+
:errorMessage => sequence[2],
496+
}
497+
@extended_response = sequence.last
498+
end
460499
end
461500

462501
module Rex

lib/rex/proto/ldap/client.rb

+10
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ def discover_base_dn
119119
dlog("#{peerinfo} Discovered base DN: #{base_dn}")
120120
base_dn
121121
end
122+
123+
# Monkeypatch upstream library to support the extended Whoami request. Delete
124+
# this after https://github.com/ruby-ldap/ruby-net-ldap/pull/425 is landed.
125+
# This is not the only occurrence of a patch for this functionality.
126+
def ldapwhoami(args = {})
127+
instrument "ldapwhoami.net_ldap", args do |payload|
128+
@result = use_connection(args, &:ldapwhoami)
129+
@result.success? ? @result.extended_response : nil
130+
end
131+
end
122132
end
123133
end
124134
end

0 commit comments

Comments
 (0)