OWASP 类别: MASVS-CODE: 代码质量
概览
Android 提供了一个强大的框架,称为剪贴板,用于在应用之间复制和粘贴数据。对此功能的不当实现可能会将用户相关数据暴露给未经授权的恶意行为者或应用。
与剪贴板数据暴露相关的具体风险取决于应用的性质及其处理的个人身份信息 (PII)。对于金融应用而言,影响尤其高,因为它们可能暴露支付数据,或者处理双因素身份验证 (2FA) 代码的应用。
用于窃取剪贴板数据的攻击向量因 Android 版本而异。
- 早于 Android 10(API 级别 29)的 Android 版本允许后台应用访问前台应用的剪贴板信息,这可能会让恶意行为者直接访问任何复制的数据。
- 从 Android 12(API 级别 31)开始,每次应用访问剪贴板中的数据并粘贴时,都会向用户显示一个吐司消息,从而使攻击更难不被发现。此外,为了保护 PII,Android 支持
ClipDescription.EXTRA_IS_SENSITIVE
或android.content.extra.IS_SENSITIVE
特殊标志。这允许开发者在键盘 GUI 中视觉上模糊化剪贴板内容的预览,防止复制的数据在明文中视觉显示并可能被恶意应用窃取。不实现上述任一标志实际上可能允许攻击者通过偷窥或通过在后台运行时截屏或录制合法用户活动的恶意应用来窃取复制到剪贴板的敏感数据。
影响
利用不当的剪贴板处理可能导致用户相关的敏感或财务数据被恶意行为者窃取。这可能有助于攻击者进行进一步的操作,如网络钓鱼活动或身份盗窃。
缓解措施
标记敏感数据
此解决方案用于在键盘 GUI 中视觉模糊化剪贴板内容预览。任何可以被复制的敏感数据,例如密码或信用卡数据,在调用 ClipboardManager.setPrimaryClip()
之前都应该标记为 ClipDescription.EXTRA_IS_SENSITIVE
或 android.content.extra.IS_SENSITIVE
。
Kotlin
// If your app is compiled with the API level 33 SDK or higher.
clipData.apply {
description.extras = PersistableBundle().apply {
putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true)
}
}
// If your app is compiled with API level 32 SDK or lower.
clipData.apply {
description.extras = PersistableBundle().apply {
putBoolean("android.content.extra.IS_SENSITIVE", true)
}
}
Java
// If your app is compiled with the API level 33 SDK or higher.
PersistableBundle extras = new PersistableBundle();
extras.putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true);
clipData.getDescription().setExtras(extras);
// If your app is compiled with API level 32 SDK or lower.
PersistableBundle extras = new PersistableBundle();
extras.putBoolean("android.content.extra.IS_SENSITIVE", true);
clipData.getDescription().setExtras(extras);
强制使用最新的 Android 版本
强制应用在 Android 10(API 29)或更高版本上运行可以防止后台进程访问前台应用的剪贴板数据。
要强制应用仅在 Android 10(API 29)或更高版本上运行,请在 Android Studio 项目的 Gradle 构建文件中为版本设置以下值。
Groovy
android {
namespace 'com.example.testapp'
compileSdk [SDK_LATEST_VERSION]
defaultConfig {
applicationId "com.example.testapp"
minSdk 29
targetSdk [SDK_LATEST_VERSION]
versionCode 1
versionName "1.0"
...
}
...
}
...
Kotlin
android {
namespace = "com.example.testapp"
compileSdk = [SDK_LATEST_VERSION]
defaultConfig {
applicationId = "com.example.testapp"
minSdk = 29
targetSdk = [SDK_LATEST_VERSION]
versionCode = 1
versionName = "1.0"
...
}
...
}
...
在特定时间后删除剪贴板内容
如果应用需要在低于 Android 10(API 级别 29)的 Android 版本上运行,任何后台应用都可以访问剪贴板数据。为了降低此风险,最好实现一个函数,在特定时间后清除复制到剪贴板的任何数据。从 Android 13(API 级别 33)开始会自动执行此函数。对于较旧的 Android 版本,可以通过在应用代码中包含以下代码片段来执行此删除操作。
Kotlin
//The Executor makes this task Asynchronous so that the UI continues being responsive
backgroundExecutor.schedule({
//Creates a clip object with the content of the Clipboard
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = clipboard.primaryClip
//If SDK version is higher or equal to 28, it deletes Clipboard data with clearPrimaryClip()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
clipboard.clearPrimaryClip()
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
//If SDK version is lower than 28, it will replace Clipboard content with an empty value
val newEmptyClip = ClipData.newPlainText("EmptyClipContent", "")
clipboard.setPrimaryClip(newEmptyClip)
}
//The delay after which the Clipboard is cleared, measured in seconds
}, 5, TimeUnit.SECONDS)
Java
//The Executor makes this task Asynchronous so that the UI continues being responsive
ScheduledExecutorService backgroundExecutor = Executors.newSingleThreadScheduledExecutor();
backgroundExecutor.schedule(new Runnable() {
@Override
public void run() {
//Creates a clip object with the content of the Clipboard
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = clipboard.getPrimaryClip();
//If SDK version is higher or equal to 28, it deletes Clipboard data with clearPrimaryClip()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
clipboard.clearPrimaryClip();
//If SDK version is lower than 28, it will replace Clipboard content with an empty value
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
ClipData newEmptyClip = ClipData.newPlainText("EmptyClipContent", "");
clipboard.setPrimaryClip(newEmptyClip);
}
//The delay after which the Clipboard is cleared, measured in seconds
}, 5, TimeUnit.SECONDS);