# DPAPI 数据保护应用接口

DPAPI，即数据保护应用接口，是一个提供加解密数据块的 Windows组件。它使用与**指定用户或主机**绑定的密码学密钥并且允许原生 Windows 功能以及第三方应用来透明地保护/解除保护给用户的数据。DPAPI 被 **Windows 凭证管理器**所使用于储存保存的秘密，凭证管理器数据块存放于用户的 **AppData** 文件夹中。

那么，DPAPI 保护哪些呢？例如：

```
Internet Explorer、Google Chrome 中的密码和表单自动填充数据
Outlook、Windows Mail、Windows Mail 等中的电子邮件帐户密码。
共享文件夹和资源访问密码
无线网络帐户密钥和密码
Windows CardSpace 和 Windows Vault 中的加密密钥
远程桌面连接密码，.NET Passport
凭据管理器中的网络密码
使用 API 函数 CryptProtectData 以编程方式保护的任何应用程序中的个人数据。 例如，在 Skype、Windows Rights Management Services、Windows Media、MSN messenger、Google Talk 等
...
```

我们可以使用原生工具 **vaultcmd** 来列举 **AppData** 中的数据块

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/SZL5Od34lhCLrEYp-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/SZL5Od34lhCLrEYp-image.png)

或者使用 **mimikatz** 中的 **vault::list** 命令。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/JLjVpj6K8bzW1UQd-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/JLjVpj6K8bzW1UQd-image.png)

以及Seatbelt 中的 **WindowsVault 模块** 也可以枚举出：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/ZaciAH4AolMYkGMw-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/ZaciAH4AolMYkGMw-image.png)

如果前往**控制面板 -&gt; 凭据管理器**，并且选择 **Windows 凭据**，我们会看到这些凭据是如何出现在用户面前的。而如果打开RDP，这些凭据会被自动填充。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/G1yexRwUyTy8KpZb-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/G1yexRwUyTy8KpZb-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/MDuY7wYyc2IJ1Rb3-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/MDuY7wYyc2IJ1Rb3-image.png)

**加密后的凭证文件**存在于 **C:\\Users\\username\\AppData\\Local\\Microsoft\\Credentials** 文件夹下。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/KRwL6fE0TVSrFJz3-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/KRwL6fE0TVSrFJz3-image.png)

我们也可以使用 Seatbelt 的 **WindowsCredentialFiles 模块** 来列举：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/XgP4k7V7l4MSLUb6-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/XgP4k7V7l4MSLUb6-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/FgMWP6p9ofvnsL7A-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/FgMWP6p9ofvnsL7A-image.png)

为了解密这些凭证文件，我们需要找到**主加密密钥**，即 **MasterKey**。上图的输出我们能看到 MasterKey 的 **GUID**。而 MasterKey 文件存储在 **%APPDATA%\\Microsoft\\Protect\\{SID}** 目录下，其中 **{SID}** 是该用户的**安全标识符**，这些文件依旧是被加密的。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/2VUMLAnjJYR7r4ck-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/2VUMLAnjJYR7r4ck-image.png)

只有解密 MasterKey 获得实际的 AES256/128 密钥后，我们方能使用该密钥解密凭证文件。有以下两种方法来解密：

##### **LSASS 注入**

当有本地管理员权限的时候并且密钥缓存 (近期访问或解密过凭证) 在 LSASS 中，我们可以通过mimikatz 的 !sekurlsa::dpapi 命令读取 MasterKey，注意与 GUID 的一一对应。我们得到的 MasterKey 为 **137c32458ea484baaa62214a46caec2b0a24d0f793275ac7cfc85cde5939a9ba084a1156c31cd262ae547c27be8d22f2dc34483dd0168957dc58438bc5750f9a**

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/YJ6lkEcZTAS0bR47-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/YJ6lkEcZTAS0bR47-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/3UgDrnBXk5IJy8zc-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/3UgDrnBXk5IJy8zc-image.png)

因为会与 LSASS 交互，所以会有 OPSEC 的风险。

##### **MS-BKRP**

MS-BKRP 是**微软备份密钥远程协议**，可用于向域控制器请求 MasterKey，不需要提升特权以及 LSASS 交互。这是为了以防用户修改或忘记密码，以及支持智能卡功能。

我们需要在目标用户的上下文中运行以下命令之一 (取决于是否知道明文密码)。

```
dpapi::masterkey /in:C:\users\<用户名>\AppData\Roaming\Microsoft\Protect\<用户SID>\<MasterKey GUID> /rpc
dpapi::masterkey /in:C:\users\<用户名>\AppData\Roaming\Microsoft\Protect\<用户SID>\<MasterKey GUID> /sid:<用户SID> /password:<密码> /protected
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/cJElYIDOZeDxynPg-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/cJElYIDOZeDxynPg-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/3C67VyO2QvsfpolW-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/3C67VyO2QvsfpolW-image.png)

在获得主密钥之后，就可以进行最终的解密了，我们可以通过如下的 mimikatz 的命令进行解密。

```
 mimikatz dpapi::cred /in:C:\Users\<用户>\AppData\Local\Microsoft\Credentials\<凭证文件> /masterkey:<MasterKey>
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/5NWOE5SClNSDHigl-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/5NWOE5SClNSDHigl-image.png)

我们之前发现 serveradm 的目录下有 3 个凭证文件，我们并不知道哪个平整文件是我们想要的，这没有关系，分别查看一下。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/WF9mA2mMFGsv0yXg-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/WF9mA2mMFGsv0yXg-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-05/scaled-1680-/Qvg2HrEA7YHyHhTR-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-05/Qvg2HrEA7YHyHhTR-image.png)

最终，我们获得了 serveradm 在 RDP 客户端中缓存的凭证，虽然 alice 的凭证我们已经之前知道了，但这是通过 DPAPI 得到的。此外，我们还知道了 Web02 上的 MSSQL sa 帐号的明文密码。