// examples/inert-sample/ConfigVault.java
//
// =========================================================================
// INERT TEACHING SAMPLE — chapter 69. THIS IS NOT MALWARE.
//
// This class holds the two decryption KEYS that ../../scripts/config-decrypt.py
// is meant to recover, and it deliberately never calls a decryption routine
// at runtime. Nothing in this sample reads, decrypts or acts on a config.
// The plaintexts are JSON documents whose every indicator points at a name
// under `.invalid`, which RFC 2606 reserves so that it can never resolve.
// =========================================================================
//
// WHY MALWARE ENCRYPTS ITS CONFIG, AND WHY IT DOES IT BADLY
//
// A config blob is the part of a sample that changes without the code
// changing: the current command-and-control hostnames, the poll interval, the
// list of targeted applications, which overlay asset to show for which
// target, a campaign tag, and a kill switch. It is the highest-value artefact
// in the whole file for an analyst, because it is the part that names things.
//
// Three pressures push the author to encrypt it:
//
//   1. Automated scanners — including Play's — extract strings and match them
//      against blocklists. An unencrypted hostname is a free detection.
//   2. Competing actors read each other's samples. A config in the clear is a
//      free target list for a rival.
//   3. Takedown. A cleartext domain in a public sample is reported within
//      hours; an encrypted one buys days.
//
// And one pressure limits how well they can do it: THE DEVICE MUST BE ABLE TO
// DECRYPT IT WITH NOTHING BUT WHAT IS IN THE FILE. There is no user to type a
// passphrase and no server to fetch a key from before the first connection.
// So the key, or everything needed to derive it, ships in the artefact. That
// is not a mistake the author made; it is a constraint they cannot escape.
//
// This is chapter 34's point restated from the other side. Cryptography here
// is not protecting a secret from you; it is buying time. Your job is to
// spend fifteen minutes rather than a week getting to the plaintext, and the
// way to do that reliably is to recognise the four shapes below.
//
// THE FOUR SHAPES YOU WILL ACTUALLY MEET, in descending order of frequency
//
//   A. REPEATING-KEY XOR with the key as a string literal, or with no key
//      literal at all because it is built from a char array. Cheap, no
//      library imports, invisible to an import-based detector. Recoverable
//      WITHOUT the key if you can guess a few plaintext bytes — see the
//      crib-dragging mode of config-decrypt.py. JSON configs always start
//      with `{"`, which is two known bytes for free.
//
//   B. AES with the key embedded in the same DEX, usually as a 16- or
//      32-character ASCII string passed straight to SecretKeySpec. CBC with a
//      prefixed IV, or ECB with none. The tell is a String constant whose
//      length is exactly 16, 24 or 32 and which is never printed or compared.
//
//   C. AES with the key DERIVED at runtime from something on the device — the
//      package name, the signing certificate hash, the ANDROID_ID. This
//      defeats a naive static extraction and is why an analyst must read the
//      derivation, not hunt for a key. It also makes the sample fragile: a
//      repackaged copy cannot decrypt its own config.
//
//   D. The key fetched from the network on first run, with the config
//      encrypted at rest. This is the only shape that genuinely defeats
//      offline analysis, and it is rare, because it makes the sample useless
//      whenever the server is down.
//
// This sample ships A and B, which between them cover the large majority of
// what triage meets. C and D are described in README section 3.10 with the
// detection approach for each, and are deliberately not implemented here.
//
// WHAT THIS CLASS DOES NOT DO
//
// It does not decrypt anything. There is no Cipher.getInstance() call in this
// sample and no javax.crypto import. The keys are declared, referenced by a
// log statement so that the compiler cannot fold them away entirely, and left
// for the analyst to find. Decryption happens on the ANALYST's machine, with
// config-decrypt.py, which is where a config should be decrypted.

package com.pentest.ch69.inert;

import android.util.Log;

public final class ConfigVault {

    private ConfigVault() {
        // no instances
    }

    /**
     * Shape B, the embedded AES key. Sixteen ASCII characters, used directly
     * as sixteen key bytes — which is exactly what
     *
     *     new SecretKeySpec(AES_KEY.getBytes("UTF-8"), "AES")
     *
     * does, and it is the single most common real-world form. Note what makes
     * it findable in triage: it is a String of length exactly 16, it is
     * `static final`, it is never compared against anything, and it is never
     * shown to a user.
     *
     * config-decrypt.py's `--scan-dex` mode looks for precisely that shape.
     */
    static final String AES_KEY = "6b39ff0e2c7a41d5";

    /**
     * Shape A, the repeating-XOR key. Fourteen bytes. In real samples this is
     * often assembled at runtime from a char array so that no single string
     * literal appears in the DEX at all — see README section 6.4 for what
     * that looks like in Smali, and note that it does not help the author,
     * because the key is still recoverable from the ciphertext alone once you
     * can guess two plaintext bytes.
     */
    static final String XOR_KEY = "ch69-inert-xor";

    /**
     * The asset names. Reading which asset a class names is often faster than
     * finding the decryption call, because the asset name survives obfuscation
     * — it must match a real filename in the APK.
     */
    static final String ASSET_XOR = "cfg-xor.bin";
    static final String ASSET_AES = "cfg-aes.bin";

    /**
     * Referenced from MainActivity so that the constants above are genuinely
     * present in the DEX string pool rather than optimised away. Prints the
     * LENGTHS, not the values, because a sample that prints its own key in
     * logcat would be teaching the wrong lesson about where keys are found.
     */
    static void describe() {
        Log.i(BootstrapProvider.TAG, "ConfigVault: two config assets are "
                + "shipped, " + ASSET_XOR + " and " + ASSET_AES + ". Key "
                + "material is present in this DEX: one " + XOR_KEY.length()
                + "-byte XOR key and one " + AES_KEY.length()
                + "-byte AES key. Neither is used at runtime. Decrypt the "
                + "assets on your own machine with config-decrypt.py.");
    }
}
