OWASP 類別: MASVS-PLATFORM:平台互動
總覽
如果廣播接收器實作不當,攻擊者可能會傳送惡意意圖,讓易受攻擊的應用程式執行非外部呼叫端的動作。
這個安全漏洞通常是指在 AndroidManifest 中設定 android:exported="true"
,或以程式輔助方式建立廣播接收器,導致接收器預設為公開的情況下,不小心匯出廣播接收器。如果接收方不包含任何意圖篩選器,預設值為 "false"
,但如果接收方至少包含一個意圖篩選器,android:exported 的預設值則為 "true"
。
如果開發人員不希望所有應用程式呼叫廣播接收器,但卻刻意匯出廣播接收器而未設定適當的存取控制,就可能遭到濫用。
影響
攻擊者可濫用未安全實作的廣播接收器,取得未經授權的存取權,在應用程式中執行開發人員不打算向第三方公開的行為。
因應措施
完全避免問題
如要完全解決這個兩難問題,請將 exported
設為 false
:
<receiver android:name=".MyReceiver" android:exported="false">
<intent-filter>
<action android:name="com.example.myapp.MY_ACTION" />
</intent-filter>
</receiver>
使用呼叫和回呼
如果您使用廣播接收器用於應用程式內部用途 (例如事件完成通知),可以重新調整程式碼,改為傳遞會在事件完成後觸發的回呼。
事件完成事件監聽器
Kotlin
interface EventCompletionListener {
fun onEventComplete(data: String)
}
Java
public interface EventCompletionListener {
public void onEventComplete(String data);
}
安全工作
Kotlin
class SecureTask(private val listener: EventCompletionListener?) {
fun executeTask() {
// Do some work...
// Notify that the event is complete
listener?.onEventComplete("Some secure data")
}
}
Java
public class SecureTask {
final private EventCompletionListener listener;
public SecureTask(EventCompletionListener listener) {
this.listener = listener;
}
public void executeTask() {
// Do some work...
// Notify that the event is complete
if (listener != null) {
listener.onEventComplete("Some secure data");
}
}
}
主要活動
Kotlin
class MainActivity : AppCompatActivity(), EventCompletionListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val secureTask = SecureTask(this)
secureTask.executeTask()
}
override fun onEventComplete(data: String) {
// Handle event completion securely
// ...
}
}
Java
public class MainActivity extends AppCompatActivity implements EventCompletionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SecureTask secureTask = new SecureTask(this);
secureTask.executeTask();
}
@Override
public void onEventComplete(String data) {
// Handle event completion securely
// ...
}
}
使用權限保護廣播接收器
請只為受保護的廣播訊息 (只有系統層級應用程式才能傳送的廣播訊息) 或自行宣告的簽章層級權限註冊動態接收器。