# Webview Attacks

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

## WebView 配置和安全指南

### WebView 漏洞概述

Android 开发的一个关键方面是正确处理 WebViews。本指南重点介绍了用于减轻与 WebView 使用相关风险的关键配置和安全实践。

![WebView 示例](/files/K79qqLArGBIzcmTc1Ifa)

### **WebViews 中的文件访问**

默认情况下，WebViews 允许文件访问。此功能由 `setAllowFileAccess()` 方法控制，自 Android API 级别 3（杯子蛋糕 1.5）起可用。拥有 **android.permission.READ\_EXTERNAL\_STORAGE** 权限的应用程序可以使用文件 URL 方案（`file://path/to/file`）从外部存储器读取文件。

#### **已弃用功能：通用访问和来自 URL 的文件访问**

* **来自文件 URL 的通用访问**：此已弃用功能允许来自文件 URL 的跨源请求，由于潜在的 XSS 攻击风险，构成重大安全风险。针对 Android Jelly Bean 及更新版本的应用程序，默认设置为禁用（`false`）。
* 要检查此设置，请使用 `getAllowUniversalAccessFromFileURLs()`。
* 要修改此设置，请使用 `setAllowUniversalAccessFromFileURLs(boolean)`。
* **来自文件 URL 的文件访问**：此功能也已弃用，控制对其他文件方案 URL 的内容访问。与通用访问一样，其默认设置为禁用以增强安全性。
* 使用 `getAllowFileAccessFromFileURLs()` 进行检查，使用 `setAllowFileAccessFromFileURLs(boolean)` 进行设置。

#### **安全文件加载**

为了在仍然访问资产和资源的同时禁用文件系统访问，使用 `setAllowFileAccess()` 方法。在 Android R 及更高版本中，默认设置为 `false`。

* 使用 `getAllowFileAccess()` 进行检查。
* 使用 `setAllowFileAccess(boolean)` 启用或禁用。

#### **WebViewAssetLoader**

**WebViewAssetLoader** 类是加载本地文件的现代方法。它使用 http(s) URL 访问本地资产和资源，符合同源策略，从而便于 CORS 管理。

### loadUrl

这是在 WebView 中加载任意 URL 的常用函数。

```java
webview.loadUrl("<url here>")
```

### **JavaScript和Intent Scheme处理**

* **JavaScript**：在WebViews中默认禁用，可以通过`setJavaScriptEnabled()`启用。建议谨慎操作，因为未经适当保护启用JavaScript可能会引入安全漏洞。
* **Intent Scheme**：WebViews可以处理`intent`方案，如果管理不慎可能导致利用漏洞。一个示例漏洞涉及一个暴露的WebView参数"support\_url"，可能被利用来执行跨站脚本（XSS）攻击。

![易受攻击的WebView](/files/uD9j30eORW0nCHWjzJ3m)

利用示例使用adb：

{% code overflow="wrap" %}

```bash
adb.exe shell am start -n com.tmh.vulnwebview/.SupportWebView –es support_url "https://example.com/xss.html"
```

{% endcode %}

### JavaScript桥接

Android提供了一项功能，允许WebView中的**JavaScript**调用**本机Android应用功能**。这是通过利用`addJavascriptInterface`方法实现的，该方法将JavaScript与本机Android功能集成，称为\_WebView JavaScript桥接\_。建议谨慎使用此方法，因为该方法允许WebView中的所有页面访问已注册的JavaScript接口对象，如果通过这些接口公开敏感信息，则存在安全风险。

* 针对目标Android版本低于4.2的应用程序，**需要极度谨慎**，因为存在一种漏洞，允许通过恶意JavaScript利用反射进行远程代码执行。

#### 实现JavaScript桥接

* **JavaScript接口**可以与本机代码交互，如示例所示，将类方法暴露给JavaScript：

```javascript
@JavascriptInterface
public String getSecret() {
return "SuperSecretPassword";
};
```

* 通过向WebView添加接口来启用JavaScript Bridge：

```javascript
webView.addJavascriptInterface(new JavascriptBridge(), "javascriptBridge");
webView.reload();
```

* 通过 JavaScript 可能会存在潜在的利用漏洞，例如通过 XSS 攻击，从而可以调用暴露的 Java 方法：

```html
<script>alert(javascriptBridge.getSecret());</script>
```

* 为了减少风险，将**限制 JavaScript 桥接的使用** 仅限于随 APK 一起提供的代码，并防止从远程来源加载 JavaScript。对于较旧的设备，将最低 API 级别设置为 17。

### 基于反射的远程代码执行（RCE）

* 通过执行特定有效负载，可以通过反射实现 RCE 的已记录方法。然而，`@JavascriptInterface` 注解防止未经授权的方法访问，限制了攻击面。

### 远程调试

* 使用 **Chrome 开发者工具** 可以进行 **远程调试**，从而在 WebView 内容中实现交互和任意 JavaScript 执行。

#### 启用远程调试

* 可以通过以下方式为应用程序中的所有 WebView 启用远程调试：

```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
```

* 根据应用程序的可调试状态有条件地启用调试：

```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
```

## 泄露任意文件

* 展示使用XMLHttpRequest泄露任意文件：

```javascript
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('GET', 'file:///data/data/com.authenticationfailure.wheresmybrowser/databases/super_secret.db', true);
xhr.send(null);
```

## 参考资料

* <https://labs.integrity.pt/articles/review-android-webviews-fileaccess-attack-vectors/index.html>
* <https://github.com/authenticationfailure/WheresMyBrowser.Android>
* <https://developer.android.com/reference/android/webkit/WebView>
* <https://medium.com/@justmobilesec/deep-links-webviews-exploitations-part-ii-5c0b118ec6f1>
* <https://www.justmobilesec.com/en/blog/deep-links-webviews-exploitations-part-I>

<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)
* 探索我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](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/android-app-pentesting/webview-attacks.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.
