hacktricks/mobile-pentesting/ios-pentesting/ios-serialisation-and-encoding.md

99 lines
6 KiB
Markdown
Raw Normal View History

2022-04-28 16:01:33 +00:00
<details>
2024-01-12 07:54:15 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-12 07:54:15 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-12 07:54:15 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 12:24:06 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-12 07:54:15 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-02-08 03:08:28 +00:00
Code and more information in [https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence](https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence).
2022-04-28 16:01:33 +00:00
2024-02-08 03:08:28 +00:00
## Object Serialization in iOS Development
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
In iOS, **object serialization** involves converting objects into a format that can be easily stored or transmitted, and then reconstructing them from this format when needed. Two main protocols, **`NSCoding`** and **`NSSecureCoding`**, facilitate this process for Objective-C or `NSObject` subclasses, allowing objects to be serialized into **`NSData`**, a format that wraps byte buffers.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
### **`NSCoding`** Implementation
To implement `NSCoding`, a class must inherit from `NSObject` or be marked as `@objc`. This protocol mandates the implementation of two methods for encoding and decoding instance variables:
2021-05-21 16:38:18 +00:00
```swift
class CustomPoint: NSObject, NSCoding {
2024-02-08 03:08:28 +00:00
var x: Double = 0.0
var name: String = ""
2021-05-21 16:38:18 +00:00
func encode(with aCoder: NSCoder) {
aCoder.encode(x, forKey: "x")
aCoder.encode(name, forKey: "name")
}
required convenience init?(coder aDecoder: NSCoder) {
2024-02-08 03:08:28 +00:00
guard let name = aDecoder.decodeObject(forKey: "name") as? String else { return nil }
self.init(x: aDecoder.decodeDouble(forKey: "x"), name: name)
2021-05-21 16:38:18 +00:00
}
}
```
2024-02-08 03:08:28 +00:00
### **Enhancing Security with `NSSecureCoding`**
To mitigate vulnerabilities where attackers inject data into already constructed objects, **`NSSecureCoding`** offers an enhanced protocol. Classes conforming to `NSSecureCoding` must verify the type of objects during decoding, ensuring that only the expected object types are instantiated. However, it's crucial to note that while `NSSecureCoding` enhances type safety, it doesn't encrypt data or ensure its integrity, necessitating additional measures for protecting sensitive information:
2021-05-21 16:38:18 +00:00
```swift
static var supportsSecureCoding: Bool {
2024-02-08 03:08:28 +00:00
return true
2021-05-21 16:38:18 +00:00
}
2024-02-08 03:08:28 +00:00
let obj = decoder.decodeObject(of: MyClass.self, forKey: "myKey")
2021-05-21 16:38:18 +00:00
```
2024-02-08 03:08:28 +00:00
## Data Archiving with `NSKeyedArchiver`
`NSKeyedArchiver` and its counterpart, `NSKeyedUnarchiver`, enable encoding objects into a file and later retrieving them. This mechanism is useful for persisting objects:
2021-05-21 16:38:18 +00:00
```swift
NSKeyedArchiver.archiveRootObject(customPoint, toFile: "/path/to/archive")
2024-02-08 03:08:28 +00:00
let customPoint = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/archive") as? CustomPoint
2021-05-21 16:38:18 +00:00
```
2024-02-08 03:08:28 +00:00
### Using `Codable` for Simplified Serialization
Swift's `Codable` protocol combines `Decodable` and `Encodable`, facilitating the encoding and decoding of objects like `String`, `Int`, `Double`, etc., without extra effort:
2021-05-21 16:38:18 +00:00
```swift
struct CustomPointStruct: Codable {
2024-02-08 03:08:28 +00:00
var x: Double
2021-05-21 16:38:18 +00:00
var name: String
}
```
2024-02-08 03:08:28 +00:00
This approach supports straightforward serialization to and from property lists and JSON, enhancing data handling in Swift applications.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
## JSON and XML Encoding Alternatives
Beyond native support, several third-party libraries offer JSON and XML encoding/decoding capabilities, each with its own performance characteristics and security considerations. It's imperative to carefully select these libraries, especially to mitigate vulnerabilities like XXE (XML External Entities) attacks by configuring parsers to prevent external entity processing.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
### Security Considerations
When serializing data, especially to the file system, it's essential to be vigilant about the potential inclusion of sensitive information. Serialized data, if intercepted or improperly handled, can expose applications to risks such as unauthorized actions or data leakage. Encrypting and signing serialized data is recommended to enhance security.
2021-05-21 16:38:18 +00:00
2022-05-01 12:41:36 +00:00
## References
2024-02-08 03:08:28 +00:00
* [https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence](https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence)
2022-04-28 16:01:33 +00:00
<details>
2024-01-12 07:54:15 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-12 07:54:15 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-12 07:54:15 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 12:24:06 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-12 07:54:15 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>