# iOS Serialisation and Encoding

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

代码和更多信息请查看<https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence>.

### iOS开发中的对象序列化

在iOS中，**对象序列化**涉及将对象转换为一种可以轻松存储或传输的格式，然后在需要时从该格式重建对象。两个主要协议，**`NSCoding`和`NSSecureCoding`**，为Objective-C或`NSObject`子类提供了便利，允许将对象序列化为\*\*`NSData`\*\*，这是一个包装字节缓冲区的格式。

#### **`NSCoding`** 实现

要实现`NSCoding`，一个类必须继承自`NSObject`或标记为`@objc`。该协议要求实现两个方法来对实例变量进行编码和解码：

```swift
class CustomPoint: NSObject, NSCoding {
var x: Double = 0.0
var name: String = ""

func encode(with aCoder: NSCoder) {
aCoder.encode(x, forKey: "x")
aCoder.encode(name, forKey: "name")
}

required convenience init?(coder aDecoder: NSCoder) {
guard let name = aDecoder.decodeObject(forKey: "name") as? String else { return nil }
self.init(x: aDecoder.decodeDouble(forKey: "x"), name: name)
}
}
```

#### **通过`NSSecureCoding`增强安全性**

为了减轻攻击者向已构建对象中注入数据的漏洞，\*\*`NSSecureCoding`\*\*提供了一个增强的协议。符合`NSSecureCoding`的类在解码过程中必须验证对象的类型，确保只有预期的对象类型被实例化。然而，需要注意的是，虽然`NSSecureCoding`增强了类型安全性，但它并不加密数据或确保数据完整性，因此需要额外的措施来保护敏感信息：

```swift
static var supportsSecureCoding: Bool {
return true
}

let obj = decoder.decodeObject(of: MyClass.self, forKey: "myKey")
```

### 使用 `NSKeyedArchiver` 进行数据归档

`NSKeyedArchiver` 及其对应的 `NSKeyedUnarchiver` 允许将对象编码到文件中，然后在以后检索它们。这种机制对于持久化对象很有用：

```swift
NSKeyedArchiver.archiveRootObject(customPoint, toFile: "/path/to/archive")
let customPoint = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/archive") as? CustomPoint
```

#### 使用 `Codable` 进行简化序列化

Swift 的 `Codable` 协议结合了 `Decodable` 和 `Encodable`，简化了诸如 `String`、`Int`、`Double` 等对象的编码和解码，无需额外努力：

```swift
struct CustomPointStruct: Codable {
var x: Double
var name: String
}
```

这种方法支持直接将数据序列化到属性列表和JSON，增强了Swift应用程序中的数据处理能力。

### JSON和XML编码替代方案

除了原生支持外，还有几个第三方库提供了JSON和XML编码/解码功能，每个库都有其自己的性能特征和安全考虑。特别是要仔细选择这些库，以减轻漏洞，如通过配置解析器来防止外部实体处理的XXE（XML外部实体）攻击。

#### 安全考虑

在序列化数据时，特别是要写入文件系统时，必须警惕可能包含敏感信息。如果序列化数据被拦截或处理不当，可能会导致应用程序面临未经授权的操作或数据泄露等风险。建议对序列化数据进行加密和签名以增强安全性。

### 参考资料

* <https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence>

<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-serialisation-and-encoding.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.
