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
For 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
Once connected, we should be able to use the powerview.py console. Here are a few examples.
NetExec (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
The PowerShell equivalent to NetExec's subnets modules is the following
ldapdomaindump is an Active Directory information dumper via LDAP, outputting information in human-readable HTML files.
With 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.
Windows
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.
Then, we can use it as in following examples, by specifing our filter.
Last updated
Was this helpful?