LDAP Exploitation
A primer into LDAP enumeration and exploitation
Enumeration
Manual simple enumeration with ldap3
Manual LDAP Enumeration Attackers can perform manual LDAP enumeration using Python. Follow the steps given below to perform manual LDAP enumeration using Python.
Using Nmap, check whether the target LDAP server is listening on the port
389
for LDAP and port636
for secure LDAP.If the target server is listening on the specified ports, initiate the enumeration process by installing LDAP using the following command: pip3 install ldap3
As shown in the code given below, create a server object (server), and specify the target IP address or hostname and port number. If the target server is listening on secure LDAP, specify
use_ssl = True
.Retrieve the Directory System Agent (DSA)–specific entry (DSE) naming contexts by specifying
get_info = ldap3.ALL
.Now, create a connection object,
connection
, and initiate a call tobind()
.If the connection is successful, True is displayed on the screen as follows:
Now, one can fetch information such as the domain name and naming context using the following script:
After obtaining the naming context, retrieve all the directory objects using the script given below:
Now, use the following script to dump the entire LDAP:
This connection
object can also be used to write data.
Using nmap
Attackers use the ldap-brute
NSE script to brute-force LDAP authentication. By default, it uses the built-in username and password lists.
The userdb
and passdb
script arguments can be employed to use custom lists.
Using ldapsearch
The ldapsearch
is a shell-accessible interface for the ldap_search_ext(3)
library call.
The ldapsearch
opens a connection to an LDAP server, binds it, and performs a search using the specified parameters.
The -x option specifies simple authentication. The -b option specifies the domain string.
Extract users:
Extract computers:
Extract my info:
Extract Domain Admins:
Extract Domain Users:
Extract Enterprise Admins:
Extract Administrators:
Extract Remote Desktop Group:
To see if you have access to any password you can use grep after executing one of the queries:
You can extract everything from a domain using:
Using windapsearch
Windapsearch is a Python script useful to enumerate users, groups, and computers from a Windows domain by utilizing LDAP queries.
Dump information with ldapdomaindump using valid credentials
Exploitation
Write Data with ldap3 and python
Changing the "sshPublicKey" information of a user
Anonymous Login by bypassing TLS SNI check
According to this writeup just by accessing the LDAP server with an arbitrary domain name (like company.com) he was able to contact the LDAP service and extract information as an anonymous user.
Using ldapsearch
Using Rogue JNDI
Java Naming and Directory Interface is a Common interface to interact with Naming and Directory Services.
To know more read this.
Bash reverse shell using rogue jndi
Base 64 encoded reverse shell payload:
Start a netcat listener on the PORT.
Use base 64 encoded payload for Rogue JNDI
Credits: Hacktricks (https://book.hacktricks.xyz/)
Last updated