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 中以视觉方式混淆剪贴板内容预览。任何可以复制的敏感数据(例如
密码或信用卡数据)都应在使用
ClipDescription.EXTRA_IS_SENSITIVE或android.content.extra.IS_SENSITIVE
调用ClipboardManager.setPrimaryClip()之前进行标记。
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 上运行,可防止后台进程访问前台应用中的剪贴板数据。
如需强制应用仅在 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);