<meta charset="utf-8">

Android Studio 运行程序时,报以下错误:

因为在 Android 12 中包含 <intent-filter> 的 activity 、 service 或 receiver 必须为这些应用组件显示声明 android:exported 属性.
解决方法如下:
在AndroidManifest.xml中找到对应的Activity,加上android:exported属性:

 <activity
            android:name=".EnterActivity"
            android:exported="true"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

即可。

为什么在 Android 12 上需要显示声明 android:exported 属性

android:exported 属性的默认值取决于是否包含 <intent-filter>,如果包含 <intent-filter> 那么默认值为 true,否则 false。

  • android:exported="true" 时,如果不做任何处理,可以接受来自其他 App 的访问
  • android:exported="false" 时,限制为只接受来自同一个 App 或一个具有相同 user ID 的 App 的访问

正因为 android:exported 的属性的默认值的问题,Twicca App 发生过一次安全性问题,因为另一个没有访问 SD 卡或网络权限的 App,可以通过 Twicca App 将存储在 SD 卡上的图片或电影上传到 Twicca 用户的 Twitter 账户上的社交网络上。

产生问题的代码如下所示:

<activity android:configChanges="keyboard|keyboardHidden|orientation" 
          android:name=".media.yfrog.YfrogUploadDialog" 
          android:theme="@style/Vulnerable.Dialog" 
          android:windowSoftInputMode="stateAlwaysHidden">           
    <intent-filter android:icon="@drawable/yfrog_icon" 
                   android:label="@string/YFROG">
        <action android:name="jp.co.vulnerable.ACTION_UPLOAD" />                
        <category android:name="android.intent.category.DEFAULT" />                
        <data android:mimeType="image/*" />                
        <data android:mimeType="video/*" />            
    </intent-filter>        
</activity>

因为添加了 intent-filter 所以 android:exported 的属性的默认值为 true,因此可以接受来自其他 App 的访问,进而造成了上述问题(通过 Twicca App 将存储在 SD 卡上的图片或电影上传到 Twicca 用户的 Twitter 账户上的社交网络上),而解决方案有两个:

  • 方案一:添加 android:exported="false" 属性

<activity  android:exported="false"
            android:configChanges="keyboard|keyboardHidden|orientation" 
            android:name=".media.yfrog.YfrogUploadDialog"  
            android:theme="@style/ VulnerableTheme.Dialog"
            android:windowSoftInputMode="stateAlwaysHidden" >    
</activity>

  • 方案二: Twicca App 没有使用方式一,而是检查调用者的包名是否与自身的包名相同

public void onCreate(Bundle arg5) {
    super.onCreate(arg5);
    ...
    ComponentName v0 = this.getCallingActivity();
    if(v0 == null) {
        this.finish();
    } else if(!jp.r246.twicca.equals(v0.getPackageName())) {
        this.finish();
        } else {
            this.a = this.getIntent().getData();
            if(this.a == null) {
                this.finish();
            }
            ...
        }
    }
}

这种方案也是可行的,因为在一台设备上,不可能会出现两个包名相同的应用,更多详细的信息可以前往查看 Restrict access to sensitive activities

这仅仅是关于 activity安全漏洞的其中一个,在不同的场景下利用这些漏洞做的事情也可能不一样。当然还有 servicereceiver 组件也都是一样,存在安全性问题

参考:
https://juejin.cn/post/7043582179103735821

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐