hacktricks/mobile-pentesting/ios-pentesting/ios-universal-links.md

120 lines
7.3 KiB
Markdown
Raw Normal View History

2024-02-08 03:08:28 +00:00
# iOS Universal Links
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
## Introduction
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
Universal links offer a **seamless redirection** experience to users by directly opening content in the app, bypassing the need for Safari redirection. These links are **unique** and secure, as they cannot be claimed by other apps. This is ensured by hosting a `apple-app-site-association` JSON file on the website's root directory, establishing a verifiable link between the website and the app. In cases where the app is not installed, Safari will take over and direct the user to the webpage, maintaining the app's presence.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
For penetration testers, the `apple-app-site-association` file is of particular interest as it may reveal **sensitive paths**, potentially including ones related to unreleased features.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
### **Analyzing the Associated Domains Entitlement**
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
Developers enable Universal Links by configuring the **Associated Domains** in Xcode's Capabilities tab or by inspecting the `.entitlements` file. Each domain is prefixed with `applinks:`. For example, Telegram's configuration might appear as follows:
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
```xml
2021-05-21 16:38:18 +00:00
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:telegram.me</string>
<string>applinks:t.me</string>
</array>
```
2024-02-08 03:08:28 +00:00
For more comprehensive insights, refer to the [archived Apple Developer Documentation](https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW2).
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
If working with a compiled application, entitlements can be extracted as outlined in [this guide](extracting-entitlements-from-compiled-application.md).
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
### **Retrieving the Apple App Site Association File**
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
The `apple-app-site-association` file should be retrieved from the server using the domains specified in the entitlements. Ensure the file is accessible via HTTPS directly at `https://<domain>/apple-app-site-association`. Tools like the [Apple App Site Association (AASA) Validator](https://branch.io/resources/aasa-validator/) can aid in this process.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
### **Handling Universal Links in the App**
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
The app must implement specific methods to handle universal links correctly. The primary method to look for is [`application:continueUserActivity:restorationHandler:`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623072-application). It's crucial that the scheme of URLs handled is HTTP or HTTPS, as others will not be supported.
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
#### **Validating the Data Handler Method**
2021-05-21 16:38:18 +00:00
2024-02-08 03:08:28 +00:00
When a universal link opens an app, an `NSUserActivity` object is passed to the app with the URL. Before processing this URL, it's essential to validate and sanitize it to prevent security risks. Here's an example in Swift that demonstrates the process:
2021-05-21 16:38:18 +00:00
```swift
func application(_ application: UIApplication, continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
2024-02-08 03:08:28 +00:00
// Check for web browsing activity and valid URL
2021-05-21 16:38:18 +00:00
if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL {
application.open(url, options: [:], completionHandler: nil)
}
return true
}
```
2024-02-08 03:08:28 +00:00
URLs should be carefully parsed and validated, especially if they include parameters, to guard against potential spoofing or malformed data. The `NSURLComponents` API is useful for this purpose, as demonstrated below:
2021-05-21 16:38:18 +00:00
```swift
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
let path = components.path,
let params = components.queryItems else {
return false
}
if let albumName = params.first(where: { $0.name == "albumname" })?.value,
let photoIndex = params.first(where: { $0.name == "index" })?.value {
2024-02-08 03:08:28 +00:00
// Process the URL with album name and photo index
2021-05-21 16:38:18 +00:00
return true
} else {
2024-02-08 03:08:28 +00:00
// Handle invalid or missing parameters
2021-05-21 16:38:18 +00:00
return false
}
}
```
2024-02-08 03:08:28 +00:00
Through **diligent configuration and validation**, developers can ensure that universal links enhance user experience while maintaining security and privacy standards.
2024-06-19 03:11:46 +00:00
## Tools
* [GetUniversal.link](https://getuniversal.link/): simplifies testing and managing universal links for your app. After entering your domain, you can use this tool to verify supported links and use a custom dashboard for triggering and creating test links. It also informs you when Apple will next index your AASA file.
2024-02-08 03:08:28 +00:00
## References
* [https://mas.owasp.org/MASTG/tests/ios/MASVS-PLATFORM/MASTG-TEST-0070/#static-analysis](https://mas.owasp.org/MASTG/tests/ios/MASVS-PLATFORM/MASTG-TEST-0070/#static-analysis)
* [https://mobile-security.gitbook.io/mobile-security-testing-guide/ios-testing-guide/0x06h-testing-platform-interaction#testing-object-persistence-mstg-platform-8](https://mobile-security.gitbook.io/mobile-security-testing-guide/ios-testing-guide/0x06h-testing-platform-interaction#testing-object-persistence-mstg-platform-8)
2022-04-28 16:01:33 +00:00
2024-06-19 03:12:17 +00:00
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>