iOS Serialisation and Encoding
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Code and more information in https://mas.owasp.org/MASTG/iOS/0x06h-Testing-Platform-Interaction/#object-persistence.
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.
NSCoding
ImplementationTo 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:
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:
NSKeyedArchiver
NSKeyedArchiver
and its counterpart, NSKeyedUnarchiver
, enable encoding objects into a file and later retrieving them. This mechanism is useful for persisting objects:
Codable
for Simplified SerializationSwift's Codable
protocol combines Decodable
and Encodable
, facilitating the encoding and decoding of objects like String
, Int
, Double
, etc., without extra effort:
This approach supports straightforward serialization to and from property lists and JSON, enhancing data handling in Swift applications.
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.
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.
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)