// examples/inert-sample/InertAdminReceiver.java
//
// =========================================================================
// INERT TEACHING SAMPLE — chapter 69. THIS IS NOT MALWARE.
//
// A declared DeviceAdminReceiver whose policy set is EMPTY and which nothing
// in this sample ever asks the user to activate. There is no
// ACTION_ADD_DEVICE_ADMIN intent anywhere in this app, no wipeData() call,
// no lockNow() call, and no password policy.
// =========================================================================
//
// WHAT DEVICE ADMIN IS, AND WHY MALWARE WANTED IT
//
// Device administration is the API a mobile-device-management product uses to
// enforce corporate policy: require a passcode of a given complexity, lock
// the screen, expire passwords, disable the camera, and — the one that
// matters here — wipe the device.
//
// An ACTIVE device admin cannot be uninstalled. `pm uninstall` refuses and
// the Settings uninstall button is greyed out, because the platform will not
// let a policy enforcer be removed without the policy being deactivated
// first. To remove it, the user must go to Settings, find the device admin
// list, deactivate the admin, and only then uninstall.
//
// That property — uninstall resistance obtained without any exploit — is what
// made device admin the standard persistence mechanism for Android
// ransomware and for early banking trojans, and it is why the mechanism
// appears in essentially every survey of the era. Two refinements followed:
//
//   * Watching for the deactivation screen and dismissing it, either with an
//     overlay drawn on top or with an accessibility service pressing Back.
//   * onDisableRequested(), a callback the platform invokes to let an admin
//     display a warning before deactivation. Ransomware returned a
//     threatening string here. Some samples used the callback as a trigger to
//     lock the device or wipe it.
//
// HOW THE PLATFORM CLOSED IT — this is a genuine success story, state it
//
//   API 24    Device admin deactivation is harder to hide behind an overlay
//             because the Settings screen filters obscured touches.
//   API 26    setPasswordQuality/reset restrictions tightened; several
//             policies deprecated for non-DO/PO admins.
//   API 28-29 Most legacy device-admin policies are deprecated. The APIs
//             remain callable but the platform ignores or restricts several
//             of them for an admin that is not a device owner or profile
//             owner.
//   API 29+   Device owner and profile owner require provisioning through a
//             flow that an ordinary sideloaded app cannot reach: NFC bump,
//             QR at setup, or `dpm set-device-owner` from ADB on a device
//             with no accounts. A user cannot be socially engineered into it
//             through a normal app install.
//   API 33    Restricted settings raises the bar for a sideloaded app to
//             reach the toggles it needs at all.
//
// The net effect is that device admin is now a WEAK and DECLINING signal in
// triage, and this is the sort of thing an analyst gets wrong by repeating an
// old checklist. A BIND_DEVICE_ADMIN declaration in a 2016 sample is a strong
// indicator. In a modern sample it is often either legacy code the author
// never removed, or a genuine MDM agent. Rate it accordingly, and look at
// what the policy XML actually requests: an admin whose <uses-policies>
// contains only force-lock is doing far less than one that requests wipe-data
// and reset-password.
//
// DETECTION — statically
//   * <receiver android:permission="android.permission.BIND_DEVICE_ADMIN">
//   * <meta-data android:name="android.app.device_admin"> and the XML it
//     points at — read the <uses-policies> children, that is the real answer
//   * in the DEX: DevicePolicyManager.lockNow, wipeData, resetPassword,
//     ACTION_ADD_DEVICE_ADMIN, isAdminActive
//
// DETECTION — dynamically, on an emulator
//   adb shell dumpsys device_policy
//   adb shell dpm list-owners
//
// See res/xml/device_admin.xml in this directory. Its policy list is EMPTY,
// which is exactly what an inert declaration looks like, and which the triage
// tool reports as "declared, no policies requested".

package com.pentest.ch69.inert;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class InertAdminReceiver extends DeviceAdminReceiver {

    @Override
    public void onEnabled(Context context, Intent intent) {
        Log.i(BootstrapProvider.TAG, "InertAdminReceiver.onEnabled — the "
                + "policy set is empty, so nothing is enforced. This sample "
                + "never asks to be enabled; if you see this line you "
                + "activated it by hand.");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        // A live ransomware sample returns a threatening string here, and
        // some use this callback as a trigger. This returns a plain factual
        // sentence and takes no action at all.
        return "Inert teaching sample. Deactivating changes nothing.";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        Log.i(BootstrapProvider.TAG, "InertAdminReceiver.onDisabled");
    }
}
