Stop debuggers, hooks and keyloggers

Most apps never need what’s in this post. If you build a banking app, a wallet, a trading app or anything that moves money, you need all of it, and your security auditor has a checklist that says so. Penetration testers will attach a debugger to your app, hook it with Frida, install a keyboard that logs every key, and write up each one as a finding.

Each defense here is one build hint or a few lines of Java, and they run on the same codebase as the rest of your app.

The attacks, in one picture

flowchart LR A[Attacker] --> D[Attach a debugger
step through your checks] A --> H[Hook with Frida or Xposed
rewrite return values] A --> R[Root the device
read your storage] A --> K[Third-party keyboard
log every key] D --> G1[android.debugGuard] H --> G2[android.fridaDetection] R --> G3[android.rootCheck
ios.detectJailbreak] K --> G4[SENSITIVE fields
keyboard detection
SecureKeyboard] G1 --> S[Enforce on the server
with App Shield attestation] G2 --> S G3 --> S

Refuse to run under a debugger

A debugger lets an attacker pause your app right after the PIN check and flip the result. android.debugGuard=true checks at launch for an attached debugger, Java or native, and exits.

codename1.arg.android.debugGuard=true
codename1.arg.android.debugGuard.checks=debugger,developerOptions,usbDebugging
codename1.arg.android.debugGuard.mode=exit

The default is the debugger alone. Developer Options and USB debugging are opt-in, because plenty of ordinary users switch them on, and so do Google Play’s pre-launch devices. Banks that want them off can say so. mode=warn logs instead of exiting, so you can measure before you enforce. An unknown check name fails the build rather than silently doing nothing.

Two details matter in practice. A debuggable build always only logs, so you can still debug your own app from the IDE. And a tracer running as the app’s own user ID isn’t counted, because commercial protection products attach one to keep debuggers out.

Catch root and hooking tools

Root detection now also looks for the manager apps of Magisk, KernelSU and APatch, the rootless tools that older checks miss. android.fridaDetection=true catches Frida and the Xposed family: classic Xposed, EdXposed, LSPatch, and Riru. On iOS, ios.detectJailbreak=true exits on a jailbroken device.

When you’d rather react in the app than exit, ask:

if (DeviceIntegrity.isDeviceCompromised()) {
    for (String reason : DeviceIntegrity.getCompromiseReasons()) {
        Log.p("Device compromise signal: " + reason);
    }
}

On Android the reasons are root, frida, emulator, debugger, developerOptions and usbDebugging. That lets you block one risky operation, say a transfer to a new payee, require a second factor, or raise a silent alarm, instead of locking the user out of their balance.

Anything that runs on the attacker’s device can in principle be hidden from, which is why the last box in the diagram matters. App Shield attestation lets your server refuse requests that don’t come from a genuine copy of your app on a device with strong integrity. The checks on the device raise the cost of an attack. The check on the server decides it.

You can test all of this without a rooted phone. Simulate, App Shield in the simulator turns on any signal, so you can see how your app behaves when it fires.

Keyloggers live in the keyboard

On Android the keyboard is just an app. Install a third-party one and it sees every key the user types, including the PIN and the card number. Even legitimate keyboards learn what you type and suggest it later, which is how card numbers end up in suggestion bars.

There are three layers of defense, and you pick how far to go.

Keep secrets out of learned words. TextArea.PASSWORD masks a field. The new TextArea.SENSITIVE is for things you want visible but private: card, account, one-time code and ID numbers. Both tell the Android keyboard not to learn from the field, and Gboard, Samsung Keyboard and SwiftKey honor that.

Know which keyboard is listening. DeviceIntegrity.isThirdPartyKeyboardActive tells you when the active keyboard isn’t on your allow list, and getActiveKeyboard() names it. On iOS, DeviceIntegrity.setThirdPartyKeyboardsAllowed(false) from init() makes every field use Apple’s keyboard.

Bring your own keyboard. SecureKeyboard draws a keyboard inside your app, so the platform keyboard never sees a key. Scramble it and a screen recorder learns nothing from the tap positions either.

TextField pin = new TextField("", "PIN", 6, TextArea.NUMERIC | TextArea.PASSWORD);
if (DeviceIntegrity.isThirdPartyKeyboardActive(DeviceIntegrity.wellKnownKeyboards())) {
    SecureKeyboard keyboard = SecureKeyboard.install(pin);
    keyboard.setScrambled(true);
}
The secure number keyboard
A PIN pad the OS never sees.
The secure keyboard with scrambled keys
Scrambled keys.
The full secure text keyboard
A full text keyboard.
The secure keyboard in dark mode
Dark mode.

SecureKeyboard works on every platform, iOS included. Pair it with setSecureScreen(true) and android.tapjackingGuard=true on Android, and ios.disableScreenshots=true on iOS, and the screen, the overlay and the keyboard are all closed.

The full set

Put this week next to what’s already there and the security chapter reads like an auditor’s checklist:

ThreatDefense
Debugger attachedandroid.debugGuard
Rooted or jailbroken deviceandroid.rootCheck, ios.detectJailbreak
Frida, Xposed, LSPatchandroid.fridaDetection
Third-party keyboard logging keysTextArea.SENSITIVE, keyboard detection, SecureKeyboard
Screenshots and screen recordingsetSecureScreen, ios.disableScreenshots
Overlays and tapjackingandroid.tapjackingGuard
Modified or repackaged appApp Shield attestation
Reverse engineeringApp Hardening obfuscation

Each one would be a separate integration and a separate review in two native codebases. Here it’s one line per threat. The security chapter has every hint and its options. Tomorrow: Oracle, SQL Server and your own map server.