// examples/inert-sample/BootReceiver.java
//
// =========================================================================
// INERT TEACHING SAMPLE — chapter 69. THIS IS NOT MALWARE.
// It logs and returns. It starts no service, opens no socket, and reads no
// user data. All indicators in this sample are RFC 2606 `.invalid` names.
// =========================================================================
//
// WHAT THIS DEMONSTRATES
//
// The oldest persistence mechanism on Android, and still the most common one
// in triage: a manifest-declared BroadcastReceiver on BOOT_COMPLETED.
//
// Four things about it are worth knowing precisely, because each one is a
// place where an analyst's assumption goes wrong:
//
//   1. BOOT_COMPLETED is a PROTECTED BROADCAST. Only the system can send it.
//      Chapter 29 measured which actions are and are not protected, and found
//      that INSTALL_REFERRER, com.google.android.c2dm.intent.RECEIVE and
//      PACKAGE_CHANGED are NOT protected despite being widely trusted. So an
//      analyst who wants to trigger this receiver in a sandbox cannot simply
//      `am broadcast` BOOT_COMPLETED as shell; they need root, or they need
//      to reboot the emulator. Chapter 29 measured that too: root can send
//      it, uid 2000 cannot.
//
//   2. Since API 26 an app in the STOPPED state receives NOTHING, including
//      BOOT_COMPLETED. An app enters the stopped state on install and leaves
//      it the first time the user launches it. This is why almost every
//      real-world dropper has a launcher Activity even when it has no user
//      interface worth showing: it needs one user tap to become eligible for
//      broadcasts at all. Seeing a launcher Activity with an empty layout is
//      therefore a triage signal, not an oversight.
//
//   3. MY_PACKAGE_REPLACED fires only for the app's OWN package, after it is
//      upgraded. It is how a component that persisted across an update knows
//      to re-arm itself. It is far less watched than BOOT_COMPLETED.
//
//   4. QUICKBOOT_POWERON is not an AOSP action. Several OEMs send it instead
//      of, or in addition to, BOOT_COMPLETED when resuming from their fast
//      shutdown state. An app that filters for it is targeting those OEMs,
//      which is a weak but real geographic and vendor signal. Do not
//      over-read it: many benign SDKs copy it from Stack Overflow.
//
// WHAT A HOSTILE VERSION WOULD DO — DESCRIBED, NOT IMPLEMENTED
//
// It would call startForegroundService() on its own supervisor here, so that
// the process is running before the user has unlocked the phone for the first
// time. Since API 26 a background app cannot start a background service, but
// a BOOT_COMPLETED receiver runs in the foreground procstate for the duration
// of onReceive(), which is a window the platform grants deliberately and
// which malware uses. This class does not take that window. It logs that the
// window exists and returns.

package com.pentest.ch69.inert;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = (intent == null || intent.getAction() == null)
                ? "(null)"
                : intent.getAction();

        Log.i(BootstrapProvider.TAG, "BootReceiver.onReceive  action=" + action
                + "  pid=" + android.os.Process.myPid());

        if (BootstrapProvider.INERT) {
            Log.i(BootstrapProvider.TAG, "INERT=true: a live sample would call "
                    + "startForegroundService(SentinelService) here, using the "
                    + "foreground procstate window that onReceive() grants. "
                    + "This one does not.");
            return;
        }

        // Unreachable by construction. Left as a comment rather than as code,
        // because the repository does not ship working persistence:
        //
        //   Intent svc = new Intent(context, SentinelService.class);
        //   ContextCompat.startForegroundService(context, svc);
    }
}
