// examples/inert-sample/InertAccessibilityBridge.java
//
// =========================================================================
// INERT TEACHING SAMPLE — chapter 69. THIS IS NOT MALWARE.
//
// This is a DECLARED accessibility service with an EMPTY implementation.
// It handles no events, performs no gestures, reads no node text, and is
// never requested from the user by any code in this sample. It exists so
// that a triage tool has a real BIND_ACCESSIBILITY_SERVICE declaration to
// find, and so that the reader can see what the declaration looks like.
//
// The repository will not ship a working accessibility abuse primitive.
// The mechanism is described in README section 3.2; the DETECTION side is
// implemented, in ../../scripts/apk-triage.py and in labs 01 and 02.
// =========================================================================
//
// WHY ACCESSIBILITY IS THE CENTRE OF ANDROID MALWARE
//
// The accessibility framework exists so that a person who cannot see the
// screen, or cannot touch it accurately, can still use the device. To do that
// job it must be able to do three things that nothing else in Android can do:
//
//   1. READ the content of any window belonging to any app, as a tree of
//      AccessibilityNodeInfo objects carrying every visible text, every
//      content description, and — with flagReportViewIds — the resource id of
//      every view. This crosses the app sandbox completely. It is not a hole
//      in the sandbox; it is a documented, user-granted capability that the
//      sandbox was designed to permit.
//   2. ACT on any window: performGlobalAction(GLOBAL_ACTION_BACK/HOME/
//      RECENTS/NOTIFICATIONS), and on API 24+ dispatchGesture(), which
//      synthesises taps and swipes at arbitrary coordinates. A synthesised
//      tap is delivered by the system, so it carries the system's authority,
//      not the app's.
//   3. Be RESTARTED by the system whenever it dies, because a disabled
//      screen-reader is a device the user cannot recover. Enabling an
//      accessibility service therefore hands the app a supervised, always-on
//      process for free — which is why modern samples prefer it over the
//      watchdog pairs modelled in SentinelService/GuardService.
//
// Read those three together and the conclusion is unavoidable: a granted
// accessibility service is a GENERAL-PURPOSE remote-control capability. It
// can read what the banking app displays, it can press the buttons the
// banking app draws, and it can dismiss the dialog that would have told the
// user about it. No memory corruption is involved anywhere. This is the
// chapter's thesis in one component: real Android malware works AROUND the
// sandbox, using capability the user granted, not THROUGH it.
//
// WHAT THE CAPABILITY IS ACTUALLY USED FOR, IN PUBLIC REPORTING
//
// Described generically, because this repository does not attribute samples
// it has not analysed:
//
//   * Reading on-screen text to harvest credentials, one-time codes shown in
//     a notification, and recovery phrases. No overlay is needed for this.
//   * Auto-granting the app's own runtime permissions by finding the "Allow"
//     button in the system permission dialog and tapping it.
//   * Preventing uninstall by watching for the Settings "app info" screen and
//     immediately performing GLOBAL_ACTION_BACK.
//   * Re-enabling itself, or enabling a second capability, by driving the
//     Settings UI.
//   * Automating a transaction inside a legitimate banking app that the user
//     already authenticated — which is the reason "device-takeover fraud"
//     replaced credential theft as the dominant model, and the reason
//     server-side behavioural detection matters more than client-side
//     controls.
//
// HOW THE PLATFORM HAS FOUGHT BACK, AND HOW WELL
//
//   API 26   Accessibility button and a warning dialog at enable time.
//   API 30   Play policy (not the platform) restricts the accessibility
//            declaration to apps whose function genuinely needs it. Policy
//            binds the store, not a sideloaded APK.
//   API 31   Services must declare accessibilityFlags honestly; the
//            "accessibility shortcut" is harder to trigger silently.
//   API 33   RESTRICTED SETTINGS. An app installed from a source that did not
//            use the session-based installer API — that is, a sideload — is
//            blocked in the Settings UI from being granted accessibility or
//            notification access, and the user must go to App info and
//            explicitly choose "Allow restricted settings". This is the most
//            effective single mitigation the platform has shipped for this
//            class, and it is why dropper apps now use PackageInstaller
//            sessions instead of ACTION_VIEW on an APK file: a session-
//            installed app is not treated as a sideload for this purpose.
//   API 34   The restriction is extended, and the enable flow carries a
//            stronger warning.
//
// The honest summary for a report: the platform has made this expensive and
// visible, not impossible, and every mitigation depends on a user reading a
// dialog. Do not write "Android prevents this".
//
// HOW TO DETECT IT — the part this file exists to support
//
// Statically, in the manifest:
//   * a <service> with android:permission="android.permission.
//     BIND_ACCESSIBILITY_SERVICE"
//   * an <intent-filter> for android.accessibilityservice.AccessibilityService
//   * a <meta-data android:name="android.accessibilityservice"> pointing at
//     an XML resource
// and then, in that XML resource, the flags that say what it can do:
//   * android:canRetrieveWindowContent="true"      — can read other apps
//   * android:canPerformGestures="true"            — can synthesise input
//   * android:accessibilityEventTypes="typeAllMask" — wants everything
//   * android:packageNames absent                  — wants every app, not one
//
// The last two are the strongest triage signal in the whole declaration. A
// genuine accessibility tool for a specific purpose names the packages it
// serves and subscribes to the event types it needs. `typeAllMask` with no
// package filter, plus canPerformGestures, is an app asking for the whole
// device. See res/xml/accessibility_config.xml in this directory, which is
// deliberately written in exactly that shape so that the triage tool has
// something to flag — and read the comments in it.
//
// Dynamically, on an emulator:
//   adb shell settings get secure enabled_accessibility_services
//   adb shell dumpsys accessibility

package com.pentest.ch69.inert;

import android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

public class InertAccessibilityBridge extends AccessibilityService {

    @Override
    public void onServiceConnected() {
        super.onServiceConnected();
        // If you are reading this line in logcat, you enabled this service by
        // hand on an emulator. Nothing follows from it. There is no event
        // handling below.
        Log.i(BootstrapProvider.TAG, "InertAccessibilityBridge connected — and "
                + "does nothing. No node tree is read, no gesture is "
                + "dispatched, no event type is handled.");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // Deliberately empty.
        //
        // A live sample's body here is the whole attack: getSource(), walk the
        // AccessibilityNodeInfo tree, match on view ids or text, and act. This
        // repository does not ship that code, in any form, defanged or
        // otherwise, because the walk itself is the capability.
        //
        // If you want to see a node tree for your own understanding, use the
        // platform's own tool on an emulator and on an app you own:
        //     adb shell uiautomator dump /sdcard/w.xml && adb pull /sdcard/w.xml
        // That is the same information, obtained through a documented,
        // shell-level interface, without building an app that can do it.
    }

    @Override
    public void onInterrupt() {
        // Deliberately empty.
    }
}
