LDAP
Pentesting LDAP - TCP Ports 389,3268,636,3269
Theory
LDAP (Lightweight Directory Access Protocol) is a software protocol for enabling anyone to locate organizations, individuals, and other resources such as files and devices in a network, whether on the public Internet or on a corporate intranet. LDAP is a "lightweight" (smaller amount of code) version of Directory Access Protocol (DAP).
An LDAP directory is organized in a simple "tree" hierarchy consisting of the following levels:
The root directory (the starting place or the source of the tree), which branches out to
Countries, each of which branches out to
Organizations, which branch out to
Organizational units (divisions, departments, and so forth), which branches out to (includes an entry for)
Individuals (which includes people, files, and shared resources such as printers)
It run on port TCP 389 and 636(ldaps). The Global Catalog (LDAP in ActiveDirectory) is available by default on ports 3268, and 3269 for LDAPS.
Practice
A lot of information on an AD domain can be obtained through LDAP. Most of the information can only be obtained with an authenticated bind but metadata (naming contexts, DNS server name, Domain Functional Level (DFL)) can be obtainable anonymously, even with anonymous binding disabled.
UNIX-Like
The ldapsearch command is a shell-accessible interface to the ldap_search_ext(3) library call. It can be used to enumerate essential informations.
Anonymous Enumeration:
Enumerate the base domain.
#Simple bind authentification (-x) as anonymous.
ldapsearch -H ldap://$IP -x -s base namingcontextsDump all readable ldap information as anonymous.
ldapsearch -H ldap://$IP -x -b "DC=contoso,DC=local"Dump ldap information as anonymous and filter.
#With (objectClass=User) as the query and sAMAccountName the filter.
ldapsearch -H ldap://$IP -x -b "DC=contoso,DC=local" '(objectClass=User)' sAMAccountNameAuthenticated Enumeration:
Dump readable ldap informations with NTLM based authentication
#With (objectClass=User) as the query and sAMAccountName the filter.
ldapsearch -H ldap://$IP -x -D "CN=MyUser,CN=Users,DC=contoso,DC=local" -w Password1 -b "DC=contoso,DC=local" '(objectClass=User)' sAMAccountName
ldapsearch -H ldap://$IP -x -D "[email protected]" -w Password1 -b "DC=contoso,DC=local" '(objectClass=User)' sAMAccountNameDump all readable ldap informations with Kerberos based authentication
#Get TGT
kinit [email protected]
#List tickets
klist
#LdapSearch
ldapsearch -H ldap://$IP -Y GSSAPI -b "DC=contoso,DC=local" '(objectClass=User)' sAMAccountNameldeep (Python) can be used to enumerate LDAP informations
# Dump All
ldeep ldap -u $USER -p $PASSWORD -d $DOMAIN -s ldap://$DC_IP all output_folder/my_prefix
# Enumerate users from previous cache
ldeep cache -d output_folder -p my_prefix users
# Enumerate users
ldeep ldap -u $USER -p $PASSWORD -d $DOMAIN -s ldap://$DC_IP usersFor Kerberos, you will need to configure the /etc/krb5.conf
Powerview.py is an alternative for the original PowerView.ps1 script that allow us to perform Powerview commands directly from our attacking host using LDAP.
First we need to authenticate using similar commands
### Authenticate
# Simple
powerview $DOMAIN/$USER:$PASSWORD@$TARGET_IP --dc-ip $DC_IP
# Pass the ticket
powerview $DOMAIN/$USER@$TARGET_IP --dc-ip $DC_IP -k --no-pass
# Pass the key
powerview $DOMAIN/$USER@$TARGET_IP --dc-ip $DC_IP --aes-key $AES_KEY
# Pass the hash
powerview $DOMAIN/$USER@$TARGET_IP --dc-ip $DC_IP -H $NTLM_HASH
### Protocols
# Use LDAP (Port 389)
powerview $DOMAIN/$USER:$PASSWORD@$TARGET_IP --dc-ip $DC_IP --ldap
# Use LDAPS (Port 636)
powerview $DOMAIN/$USER:$PASSWORD@$TARGET_IP --dc-ip $DC_IP --ldap
# Use LDAP Global Catalog (Port 3268)
powerview $DOMAIN/$USER:$PASSWORD@$TARGET_IP --dc-ip $DC_IP --ldap
# Use LDAPS Global Catalog (Port 3269)
powerview $DOMAIN/$USER:$PASSWORD@$TARGET_IP --dc-ip $DC_IP --ldapOnce connected, we should be able to use the powerview.py console. Here are a few examples.
(LDAP)-[10.10.16.2]-[CONTOSO\SimpleUser]
PV > Get-DomainUser -SPN -Select samaccountname,msDS-SupportedEncryptionTypes
DAP)-[10.10.16.2]-[CONTOSO\SimpleUser]
PV > Get-DomainUser -PreAuthNotRequiredNetExec (Python) also has useful modules that can be used to
map information regarding AD-CS (Active Directory Certificate Services)
show subnets listed in AD-SS (Active Directory Sites and Services)
list the users description
print the Machine Account Quota domain-level attribute's value
# list PKIs/CAs
netexec ldap "domain_controller" -d "domain" -u "user" -p "password" -M adcs
# list subnets referenced in AD-SS
netexec ldap "domain_controller" -d "domain" -u "user" -p "password" -M subnets
# machine account quota
netexec ldap "domain_controller" -d "domain" -u "user" -p "password" -M maq
# users description
netexec ldap "domain_controller" -d "domain" -u "user" -p "password" -M get-desc-usersThe PowerShell equivalent to NetExec's subnets modules is the following
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites.Subnetsldapdomaindump is an Active Directory information dumper via LDAP, outputting information in human-readable HTML files.
ldapdomaindump --user 'DOMAIN\USER' --password $PASSWORD --outdir ldapdomaindump $DOMAIN_CONTROLLERWith Impacket's ntlmrelayx (Python), it is possible to gather lots of information regarding the domain users and groups, the computers, ADCS, etc. through a NTLM authentication relayed within an LDAP session.
ntlmrelayx -t "ldap://domaincontroller" --dump-adcs --dump-laps --dump-gmsaWindows
Using PowerShell and .NET classes, we can enumerate the domain using LDAP. This can be very handy if we have compromised a computer in the domain with no administrative access and no RSAT module installed.
To acheive this, we can use the following function.
function LDAPSearch {
param (
[string]$LDAPQuery
)
$PDC = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().PdcRoleOwner.Name
$DN = ([adsi]'').distinguishedName
$DIR_ENTRY = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$PDC/$DN")
$DIR_SEARCHER = New-Object System.DirectoryServices.DirectorySearcher($DIR_ENTRY, $LDAPQuery)
return $DIR_SEARCHER.FindAll()
}# Import the function
. .\ldapsearch.ps1Then, we can use it as in following examples, by specifing our filter.
# Dump all objects
LDAPSearch -LDAPQuery '(objectClass=*)'
# Enum Users
## Filter on Users
LDAPSearch -LDAPQuery "(objectClass=User)"
## Print some properties for each users
$res = foreach ($obj in $(LDAPSearch -LDAPQuery "objectClass=User")) {$obj.properties | select {$_.name}, {$_.memberof}}
$res|fl
# Enum Groups
## Filter on Groups
LDAPSearch -LDAPQuery "(objectClass=Group)"
## Print some properties for each groups
$res = foreach ($obj in $(LDAPSearch -LDAPQuery "objectClass=Group")) {$obj.properties | select {$_.cn}, {$_.member}}
$res|fl
## Query for specific group and print a property
$res = LDAPSearch -LDAPQuery "(&(objectCategory=group)(cn=Domain Admins))"
$res.properties.memberSome PowerView functions use LDAP to retreive information.
# Import it
Import-Module .\PowerView.ps1
# Enum Domain CN
Get-NetDomain
# Enum Groups
Get-NetGroup
Get-NetGroup "Domain Admins"
Get-NetGroup | select cn
# Enum Users
Get-NetUser
Get-NetUser "user01"
Get-NetUser | select cn,pwdlastset,lastlogonFore more powerview commands and enumeration, refer to this page.
Last updated
Was this helpful?