> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web/business-logic/authentication-bypass-via-encryption-oracle.md).

# 通过加密预言机绕过身份验证

通过加密预言机绕过身份验证，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

### 通过加密预言机绕过认证

**实验描述**

本实验存在一个逻辑缺陷，暴露了一个 \*\*服务器端加密\*\* 预言机。利用该弱点，可以伪造一个有效的加密值，从而绕过认证，访问管理员面板并删除用户 **carlos**.

提供的标识符：

* 用户： `wiener`
* 密码： `peter`

**初步观察**

在登录页面， **保持登录** 选项可用。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1c8f2b6704708a6182e65cff57da7ca746fc47ca%2F%7B3E36DA00-915F-4E99-A834-06AC0A639BAC%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

使用时，会生成一个以加密方式 **Base64** 并存储在客户端。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-06479314aece8e4eae54a0445e0bf7e609922eb5%2F%7B45D84F5E-FC68-4377-AE0C-264F6B775F2D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

```bash
echo '0HE6oMYJL//S1fLwttqR7WMF3EX4KJmvfFRI53qpVnk=' | base64 -d

# 解码后的值是二进制密文，不是可读文本。
```

### 加密预言机的识别

当提交有效评论时，没有观察到特定响应。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-df90ea91b514f51d437eb3dba0cdaabe68d82b75%2F%7BCA4664B4-97B1-454D-BAF5-98D4D7777D0E%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

然而，如果评论引发应用程序错误（例如无效的电子邮件地址），服务器：

* 返回一个显示在屏幕上的错误消息；

{% code overflow="wrap" %}

```bash
csrf=cFMQUjAyzETtEr8TLsoir7V5p4C3m4ZG&postId=4&comment=test&name=test&email=test&website=https://localhost
```

{% endcode %}

* 生成一个新的 cookie `通知`\*\*，已加密。

显示的内容例如：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8773a3dd5c9ca7ffa97fa91971e0f8bc47bbe38c%2F%7B98731CE0-7072-4F6F-820E-A8ABAAFFE738%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

这表明：

* 服务器\*\*会解密\*\*该 `通知` cookie；

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-170f9cd9ea3b411e2911b413fd030255fc7344f8%2F%7BA516BF0D-8173-48BA-9854-D8E725E90EF8%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

因此，服务器充当一个 **加密和解密预言机**.

* 解密后的文本是 **重新注入到 HTML 响应中**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-16c05cc800cb2d282e9caf9ed7058823d79f5741%2F%7B6FA85698-84B5-498A-B262-9B0CAF4181DA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 与 Cookie 的关联

通过注入该 cookie 的加密值 \*\*

观察到的格式是：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-da4fa9c84fc71b8d2faf60d1ca2fb080634116b5%2F%7B2E30F536-1AB8-430A-9F77-F7E116B0D862%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

这显然对应于：

* 用户名；
* 时间戳。

#### 伪造管理员身份

目标是让服务器加密以下字符串：

```bash
administrator:1766913113564
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3594dd843ec91657d6fd3109c0b8caf088cf6df8%2F%7BB99DE7CF-7BCE-4058-9E02-DF9F6A451DF9%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

为此，将该值注入到易受攻击的字段（无效评论/电子邮件）中，从而导致：

* 显示包含该字符串的错误消息；

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-71b56cb8b65bd1996936e932e33fc8d8de9c0d99%2F%7BF469F5A4-6D70-41BA-9326-889C2BC22AFA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

生成一个新的 `通知` cookie，其中包含注入的文本。

{% code overflow="wrap" %}

```bash
Set-Cookie: notification=VQbs3ex4CbvKjTm0LM5wuZa79pnAzKI9MEsa1O0MbX%2fJpfl9baj6yL3ryxh4u6nyiWHVwxP7%2fVDPyft%2f1qAbbQ%3d%3d; HttpOnly
```

{% endcode %}

### 加密算法识别

通过任意截断加密值，服务器返回以下错误

```bash
PTBLGtTtDG1/yaX5fW2o%2bsi968sYeLup8olh1cMT%2b/1Qz8n7f9agG20%3d
```

`使用带填充的密码进行解密时，输入长度必须是 16 的倍数`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6edc2449f91bef9d278bd1e005590eaf0fc64ca7%2F%7B86526481-7898-40D5-8113-809AAE3C7710%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

这清楚地表明服务器端使用了 **带填充的 AES**，以 **16 字节**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-921a3d43ea0db8951b4b6ffbee9ae35a884766ff%2F%7BA6A7EDD6-E0F9-4DDC-99CE-CA4AFBE3E432%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 密文块操纵

遇到的问题是在消息末尾存在无效的填充字符。/ 为了解决这个问题，在注入的链之前添加填充字符，以正确对齐各个块：

```
xxxxxxxxxadministrator:1766913113564
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-db48f1a39a887b49244698967f194e57043ef5d4%2F%7B3FB22BF1-1A82-41BE-966D-0E22EFD0D949%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

随后生成一个新的加密 cookie。

VQbs3ex4CbvKjTm0LM5wuZJqwlVboMZ2IzNuiTfVH6iHj9RZUT59d5kGoS64tVE5wWiEfmp6VbJG%2bb%2fNW5%2bblA%3d%3d

### 截取并重组 Cookie

通过删除前面的 **两个加密块** （对应于填充部分），只保留包含：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-53b11c36be65a667931372eed1d9916d85e8d95c%2F%7BB155871E-4388-4A10-BEB4-7B2665F6EF57%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a85d9e6c8504608250675b0e1e0a6c2633a84516%2F%7BC52B4886-B120-45A9-B85C-755072EB80DD%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

服务器接受这个最终加密值。

```
h4/UWVE%2bfXeZBqEuuLVROcFohH5qelWyRvm/zVufm5Q%3d
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0b23a0e83ce8947384e9e367f5fe9fe3b619ac7e%2F%7B9B975810-3156-49D5-BBE6-87F32FCBF48B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 绕过认证

最后步骤：

1. 删除现有的会话 cookie。
2. 替换 cookie \*\* 的值
3. 刷新页面。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cb818e32f157dec337db5f2539aeb3f9668ba4d7%2F%7BF7D93958-A273-4244-8FE3-5A0243528964%7D.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/web/business-logic/authentication-bypass-via-encryption-oracle.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
