Webview Attacks
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)
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.
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
).
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)
.
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.
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)
.
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.
This is a common function used to load arbitrary URLs in a webviwe:
Ofc, a potential attacker should never be able to control the URL that an application is going to load.
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.
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.
Exploitation example using adb:
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.
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.
JavaScript interfaces can interact with native code, as shown in the examples where a class method is exposed to JavaScript:
JavaScript Bridge is enabled by adding an interface to the WebView:
Potential exploitation through JavaScript, for instance, via an XSS attack, enables the calling of exposed Java methods:
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.
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.
Remote debugging is possible with Chrome Developer Tools, enabling interaction and arbitrary JavaScript execution within the WebView content.
Remote debugging can be enabled for all WebViews within an application by:
To conditionally enable debugging based on the application's debuggable state:
Demonstrates the exfiltration of arbitrary files using an XMLHttpRequest:
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)