# iOS UIPasteboard

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)系列
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

<figure><img src="/files/YM7uBW8YtIhrJWT3a6lG" alt=""><figcaption></figcaption></figure>

{% embed url="<https://websec.nl/>" %}

iOS设备上应用程序内部和跨应用程序之间的数据共享是通过[`UIPasteboard`](https://developer.apple.com/documentation/uikit/uipasteboard)机制实现的，分为两个主要类别：

* **系统范围的通用剪贴板**：用于与**任何应用程序**共享数据，并设计为在iOS 10以后可跨设备重启和应用卸载持久化数据的功能。
* **自定义/命名剪贴板**：专门用于在应用程序内部或与共享相同团队ID的另一个应用程序之间共享数据，并且不会在创建它们的应用程序进程的生命周期之外持久存在，这是iOS 10引入的变化。

在利用剪贴板时，**安全考虑**起着重要作用。例如：

* 用户没有管理应用程序权限访问**剪贴板**的机制。
* 为了减轻未经授权的后台监控剪贴板的风险，访问仅限于应用程序在前台运行时（自iOS 9以来）。
* 由于隐私问题，不鼓励使用持久命名剪贴板，而是推荐使用共享容器。
* iOS 10引入的**通用剪贴板**功能允许通过通用剪贴板在设备之间共享内容，开发人员可以管理数据过期时间并禁用自动内容传输。

确保**敏感信息不会被意外存储**在全局剪贴板中至关重要。此外，应用程序应设计防止全局剪贴板数据被用于意外操作的滥用，鼓励开发人员实施措施防止将敏感信息复制到剪贴板中。

#### 静态分析

对于静态分析，搜索源代码或二进制文件以查找：

* `generalPasteboard` 以识别**系统范围的通用剪贴板**的使用。
* `pasteboardWithName:create:` 和 `pasteboardWithUniqueName` 用于创建**自定义剪贴板**。验证是否启用了持久性，尽管这已被弃用。

#### 动态分析

动态分析涉及挂钩或跟踪特定方法：

* 监视 `generalPasteboard` 以进行系统范围的使用。
* 跟踪 `pasteboardWithName:create:` 和 `pasteboardWithUniqueName` 以进行自定义实现。
* 观察已弃用的 `setPersistent:` 方法调用以检查持久性设置。

要监视的关键细节包括：

* **剪贴板名称**和**内容**（例如，检查字符串、URL、图像）。
* 存在的**项目数量**和**数据类型**，利用标准和自定义数据类型检查。
* 通过检查 `setItems:options:` 方法检查**到期和仅本地选项**。

一个监视工具的示例是**objection的剪贴板监视器**，它每5秒轮询generalPasteboard以查看更改并输出新数据。

以下是一个简单的JavaScript脚本示例，受objection方法启发，每5秒读取并记录剪贴板中的更改：

```javascript
const UIPasteboard = ObjC.classes.UIPasteboard;
const Pasteboard = UIPasteboard.generalPasteboard();
var items = "";
var count = Pasteboard.changeCount().toString();

setInterval(function () {
const currentCount = Pasteboard.changeCount().toString();
const currentItems = Pasteboard.items().toString();

if (currentCount === count) { return; }

items = currentItems;
count = currentCount;

console.log('[* Pasteboard changed] count: ' + count +
' hasStrings: ' + Pasteboard.hasStrings().toString() +
' hasURLs: ' + Pasteboard.hasURLs().toString() +
' hasImages: ' + Pasteboard.hasImages().toString());
console.log(items);

}, 1000 * 5);
```

### 参考资料

* <https://mobile-security.gitbook.io/mobile-security-testing-guide/ios-testing-guide/0x06h-testing-platform-interaction#testing-object-persistence-mstg-platform-8>
* <https://hackmd.io/@robihamanto/owasp-robi>
* <https://mas.owasp.org/MASTG/tests/ios/MASVS-PLATFORM/MASTG-TEST-0073/>

<figure><img src="/files/YM7uBW8YtIhrJWT3a6lG" alt=""><figcaption></figcaption></figure>

{% embed url="<https://websec.nl/>" %}

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>


---

# Agent Instructions: 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:

```
GET https://hacktricks.xsx.tw/mobile-pentesting/ios-pentesting/ios-uipasteboard.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
