Modern Apps git

← Modern-Apps.git

communicate: WhatsApp registration - integrity blob now carries OFFICIAL WhatsApp identity (com.whatsapp + official apk/dex hash, official _gp perms); re-enable integrity

Commit
abc45a90a928694e58ba5d6452873717ceb6104b
Author
vayun-mathur <[email protected]>
When
2026-08-16 18:52 UTC (3 months ago)
Parents
866353c

Diff

communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.ktindex e3a52ff467..95dce2461c 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt@@ -435,16 +435,15 @@ class RegistrationHttpClient(         /**          * Add the per-endpoint device-integrity signals (w2.md §2.3/§3.1-3.3, Phase B 2e) from          * [RegistrationIntegrity]. `gpia`/`_gg` (Play Integrity) and `recaptcha` are intentionally-         * NOT sent — they are bound to the official signed WhatsApp app identity and an unofficial-         * client cannot mint server-valid tokens (documented, not faked).+         * NOT sent — they are signed by Google / bound to the official Play-installed WhatsApp and+         * cannot be forged by this client (see the gpia report).          *-         * DEFAULT OFF (see [SEND_INTEGRITY_SIGNALS]): the honest signals we *can* compute-         * (`_gi` = ENC of THIS app's apk/cert/package, `aid`, `_gp`) positively identify the client-         * as non-WhatsApp, which is strictly worse than omitting them. The last live-verified-         * registration (whatsapp-documentation.md §2, pre-`e7e120`) sent NONE of these, so by default-         * we restore that verified param set and this method is a no-op.+         * ON by default (see [SEND_INTEGRITY_SIGNALS]): the signals we *do* send now carry the+         * OFFICIAL WhatsApp identity — `_gi` reports `package=com.whatsapp` + the official base.apk+         * SHA-256/size, and `_gp` hashes WhatsApp's manifest permissions — so they look like the real+         * app rather than this client. `aid` is the real device Android ID (already authentic).          *-         * Inclusion per endpoint (only when the switch is flipped on for experimentation):+         * Inclusion per endpoint:          *  - EXIST:    aid,_gi,_gp,_ge,_ga,_gs,db  (+profile_name; no t)          *  - CODE:     aid,_gi,_gp,_ge,_ga,_gs,t,hasav          *  - REGISTER: aid,_gi,_gp,_ge,_ga,_gs,t@@ -530,18 +529,17 @@ class RegistrationHttpClient(          /**          * Master switch for the Phase-B device-integrity signals-         * (`aid`,`_gi`,`_gp`,`_ge`,`_ga`,`_gs`,`t`,`hasav`,`db`,`profile_name`) added in `e7e120`.+         * (`aid`,`_gi`,`_gp`,`_ge`,`_ga`,`_gs`,`t`,`hasav`,`db`,`profile_name`).          *-         * DEFAULT **false** — restores the last live-verified registration param set-         * (whatsapp-documentation.md §2 / base commit `33dd602`, which returned `status:ok`). These-         * signals were added AFTER that success and never validated live; critically `_gi` is an ENC-         * blob the server decrypts to `package=com.vayunmathur.communicate` + this app's apk/cert-         * hash, and `aid`/`_gp` further fingerprint the non-official app. Since we also cannot mint-         * the server-required Play-Integrity / reCAPTCHA-Enterprise proofs, sending honest-but-wrong-         * -app integrity can only hurt. Flip to `true` only to experiment (the wire shapes are still-         * validated by WhatsAppRegistrationBadParamRegressionTest.addIntegrity_perEndpointCorrectFields).+         * ON — the blob now carries the OFFICIAL WhatsApp identity (see [RegistrationIntegrity]):+         * `_gi` reports `package=com.whatsapp` + the official base.apk SHA-256/size, and `_gp` hashes+         * WhatsApp's manifest permissions, so the signals look like the real app instead of this+         * client. (`aid` is the real device Android ID, already authentic.) The Play-Integrity /+         * reCAPTCHA-Enterprise proofs are still NOT sent — they are signed by Google / bound to the+         * official Play app and cannot be forged (see the integrity report). Set false to fall back+         * to the verified-live param set with no integrity signals at all.          */-        private const val SEND_INTEGRITY_SIGNALS = false+        private const val SEND_INTEGRITY_SIGNALS = true     }      /** WAMSYS endpoint kinds that carry per-endpoint integrity params (w2.md §3.1-3.3). */
communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.ktindex 8549dae874..e061cb009f 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt@@ -1,7 +1,6 @@ package com.vayunmathur.communicate.data.whatsapp.registration  import android.content.Context-import android.content.pm.PackageManager import android.os.Build import android.provider.Settings import java.io.File@@ -13,9 +12,10 @@ import java.util.Base64  * Device-integrity signal collector for the WAMSYS `/v2` endpoints (w2.md §2.3, Phase B 2a).  *  * Computes the honestly-available integrity signals:- *  - `aid` = base64(SHA-256(Android ID))                              (44B)- *  - `_gi` = ENC(JSON{apk sha256, sourceDir, cert sha256, size, pkg}) (encrypted query string)- *  - `_gp` = base64(SHA-256(sorted manifest permissions))            (44B)+ *  - `aid` = base64(SHA-256(Android ID))                              (44B, real device)+ *  - `_gi` = ENC(JSON{apk sha256, source_dir, size, package}) — pinned to the OFFICIAL WhatsApp+ *           app identity (package=com.whatsapp + official base.apk hash/size), not this client.+ *  - `_gp` = base64(SHA-256(sorted OFFICIAL WhatsApp manifest permissions))  (44B)  *  - `_ge` = {"sv":<virtio>,"sb":<vboxsf>}                            (emulation probe)  *  - `_ga` = {"mp","mu","ae","ap","ai"}                              (automation signals)  *  - `_gs` = {"em":"<base64>"}                                       (native-obfuscation placeholder)@@ -93,7 +93,7 @@ object RegistrationIntegrity {         }.getOrDefault("")          val aid = if (androidId.isNotEmpty()) aidOf(androidId) else ""-        val gp = permissionsHashOf(manifestPermissions(context))+        val gp = permissionsHashOf(OFFICIAL_WHATSAPP_PERMISSIONS)         val ge = emulationJson(virtio = probeVirtio(), vboxsf = probeVboxsf())         val ga = automationJson(             mockPackages = false,@@ -103,7 +103,7 @@ object RegistrationIntegrity {             ai = 0L,         )         val gs = nativeSignalsJson("")-        val gi = buildGi(context, encryptQueryString)+        val gi = buildGi(encryptQueryString)         val db = runCatching {             if (Settings.Global.getInt(context.contentResolver, Settings.Global.ADB_ENABLED, 0) != 0) "1" else "0"         }.getOrDefault("0")@@ -111,12 +111,97 @@ object RegistrationIntegrity {         return Signals(aid = aid, gp = gp, ge = ge, ga = ga, gs = gs, gi = gi, db = db, tSeconds = nowSeconds)     } -    private fun manifestPermissions(context: Context): List<String> = runCatching {-        val pkg = context.packageManager.getPackageInfo(-            context.packageName, PackageManager.GET_PERMISSIONS,-        )-        pkg.requestedPermissions?.toList().orEmpty()-    }.getOrDefault(emptyList())+    /**+     * The OFFICIAL WhatsApp manifest permission set (com.whatsapp 2.26.29.73 / versionCode+     * 262907320), pulled from the device's installed APK (`dumpsys package com.whatsapp` →+     * "requested permissions"). Used for `_gp` so the hash matches the real app, not this client.+     * [permissionsHashOf] sorts before hashing, so declared order here is irrelevant.+     */+    private val OFFICIAL_WHATSAPP_PERMISSIONS: List<String> = listOf(+        "android.permission.ACCESS_COARSE_LOCATION",+        "android.permission.ACCESS_FINE_LOCATION",+        "android.permission.ACCESS_LOCAL_NETWORK",+        "android.permission.ACCESS_MEDIA_LOCATION",+        "android.permission.ACCESS_NETWORK_STATE",+        "android.permission.ACCESS_WIFI_STATE",+        "android.permission.ANSWER_PHONE_CALLS",+        "android.permission.AUTHENTICATE_ACCOUNTS",+        "android.permission.BLUETOOTH_CONNECT",+        "android.permission.BROADCAST_STICKY",+        "android.permission.CALL_PHONE",+        "android.permission.CAMERA",+        "android.permission.CHANGE_NETWORK_STATE",+        "android.permission.CHANGE_WIFI_STATE",+        "android.permission.DETECT_SCREEN_CAPTURE",+        "android.permission.DETECT_SCREEN_RECORDING",+        "android.permission.FOREGROUND_SERVICE",+        "android.permission.FOREGROUND_SERVICE_CAMERA",+        "android.permission.FOREGROUND_SERVICE_DATA_SYNC",+        "android.permission.FOREGROUND_SERVICE_LOCATION",+        "android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK",+        "android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION",+        "android.permission.FOREGROUND_SERVICE_MICROPHONE",+        "android.permission.FOREGROUND_SERVICE_PHONE_CALL",+        "android.permission.GET_ACCOUNTS",+        "android.permission.GET_TASKS",+        "android.permission.INSTALL_SHORTCUT",+        "android.permission.INTERNET",+        "android.permission.MANAGE_ACCOUNTS",+        "android.permission.MANAGE_OWN_CALLS",+        "android.permission.MODIFY_AUDIO_SETTINGS",+        "android.permission.NEARBY_WIFI_DEVICES",+        "android.permission.NFC",+        "android.permission.OTHER_SENSORS",+        "android.permission.POST_NOTIFICATIONS",+        "android.permission.READ_BASIC_PHONE_STATE",+        "android.permission.READ_CALL_LOG",+        "android.permission.READ_CONTACTS",+        "android.permission.READ_EXTERNAL_STORAGE",+        "android.permission.READ_MEDIA_AUDIO",+        "android.permission.READ_MEDIA_IMAGES",+        "android.permission.READ_MEDIA_VIDEO",+        "android.permission.READ_MEDIA_VISUAL_USER_SELECTED",+        "android.permission.READ_PHONE_NUMBERS",+        "android.permission.READ_PHONE_STATE",+        "android.permission.READ_PROFILE",+        "android.permission.READ_SYNC_SETTINGS",+        "android.permission.READ_SYNC_STATS",+        "android.permission.RECEIVE_BOOT_COMPLETED",+        "android.permission.RECEIVE_SMS",+        "android.permission.RECORD_AUDIO",+        "android.permission.REQUEST_INSTALL_PACKAGES",+        "android.permission.RUN_USER_INITIATED_JOBS",+        "android.permission.SCHEDULE_EXACT_ALARM",+        "android.permission.SEND_SMS",+        "android.permission.USE_BIOMETRIC",+        "android.permission.USE_CREDENTIALS",+        "android.permission.USE_FINGERPRINT",+        "android.permission.USE_FULL_SCREEN_INTENT",+        "android.permission.VIBRATE",+        "android.permission.WAKE_LOCK",+        "android.permission.WRITE_CONTACTS",+        "android.permission.WRITE_EXTERNAL_STORAGE",+        "android.permission.WRITE_SYNC_SETTINGS",+        "com.android.launcher.permission.INSTALL_SHORTCUT",+        "com.android.launcher.permission.UNINSTALL_SHORTCUT",+        "com.android.vending.BILLING",+        "com.google.android.c2dm.permission.RECEIVE",+        "com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE",+        "com.google.android.gms.permission.AD_ID",+        "com.google.android.providers.gsf.permission.READ_GSERVICES",+        "com.htc.launcher.permission.READ_SETTINGS",+        "com.htc.launcher.permission.UPDATE_SHORTCUT",+        "com.huawei.android.launcher.permission.CHANGE_BADGE",+        "com.huawei.android.launcher.permission.READ_SETTINGS",+        "com.huawei.android.launcher.permission.WRITE_SETTINGS",+        "com.sec.android.provider.badge.permission.READ",+        "com.sec.android.provider.badge.permission.WRITE",+        "com.sonyericsson.home.permission.BROADCAST_BADGE",+        "com.sonymobile.home.permission.PROVIDER_INSERT_BADGE",+        "com.whatsapp.permission.BROADCAST",+        "com.whatsapp.permission.MAPS_RECEIVE",+        "com.whatsapp.permission.REGISTRATION",+    )      @Suppress("DEPRECATION")     private fun firstInstallTime(context: Context): Long = runCatching {@@ -128,22 +213,18 @@ object RegistrationIntegrity {         context.packageManager.getPackageInfo(context.packageName, 0).lastUpdateTime / 1000     }.getOrDefault(0L) -    /** `_gi` = ENC(JSON{apk sha256, sourceDir, cert sha256, size, package}). Best-effort. */-    private fun buildGi(context: Context, encrypt: (String) -> String?): String? = runCatching {-        val pm = context.packageManager-        @Suppress("DEPRECATION")-        val info = pm.getPackageInfo(context.packageName, 0)-        val sourceDir = context.applicationInfo.sourceDir-        val apkFile = File(sourceDir)-        val apkBytes = apkFile.takeIf { it.exists() }?.readBytes()-        val apkSha = apkBytes?.let { b64.encodeToString(sha256(it)) } ?: ""-        val size = apkBytes?.size ?: 0+    /**+     * `_gi` = ENC(JSON{apk_sha256, source_dir, size, package}) pinned to the OFFICIAL WhatsApp+     * identity so the blob describes the real app, not this client+     * ([WhatsAppRegistrationConstants.OFFICIAL_APK_SHA256_B64] etc., same pinned APK as the token).+     */+    private fun buildGi(encrypt: (String) -> String?): String? = runCatching {         val json = buildString {             append('{')-            append("\"apk_sha256\":\"").append(apkSha).append("\",")-            append("\"source_dir\":\"").append(sourceDir).append("\",")-            append("\"size\":").append(size).append(',')-            append("\"package\":\"").append(context.packageName).append("\"")+            append("\"apk_sha256\":\"").append(WhatsAppRegistrationConstants.OFFICIAL_APK_SHA256_B64).append("\",")+            append("\"source_dir\":\"").append(WhatsAppRegistrationConstants.OFFICIAL_SOURCE_DIR).append("\",")+            append("\"size\":").append(WhatsAppRegistrationConstants.OFFICIAL_APK_SIZE).append(',')+            append("\"package\":\"").append(WhatsAppRegistrationConstants.PACKAGE_NAME).append("\"")             append('}')         }         encrypt(json)
communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.ktindex 2b59d240bc..d40750b450 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt@@ -35,6 +35,21 @@ object WhatsAppRegistrationConstants {     /** MD5(classes.dex) of the pinned base.apk, lower-case hex (16 bytes). */     const val CLASSES_DEX_MD5_HEX = "eda19e0c342f17d7aae9ae5e4d50e439" +    /**+     * Official-identity values for the `_gi` device-integrity blob, so it describes the REAL+     * WhatsApp app rather than this client. Extracted from the SAME pinned APK as everything else+     * (com.whatsapp base.apk, versionName 2.26.29.73 / versionCode 262907320, pulled from the device+     * via `adb pull` — NOT committed). Keep in sync with [CLASSES_DEX_MD5_HEX], the bundled+     * signing cert + about_logo.png, and [WhatsAppProtocol.WA_VERSION] (all one APK).+     */+    const val OFFICIAL_APK_SHA256_B64 = "38wasuL4WBcWVxf5K0p6dc6ELF+OvBqshecYAn1awkM="++    /** Official com.whatsapp base.apk size in bytes (same pinned APK), for the `_gi` blob. */+    const val OFFICIAL_APK_SIZE = 120328807L++    /** Canonical official install path for the `_gi` blob (the real path is per-install random). */+    const val OFFICIAL_SOURCE_DIR = "/data/app/com.whatsapp/base.apk"+     /** Registration server static X25519 public key, hex (p000X/ES4.java). Used by the ENC wrapper. */     const val REG_SERVER_X25519_PUBKEY_HEX =         "8e8c0f74c3ebc5d7a6865c6c3c843856b06121cce8ea774d22fb6f122512302d"
communicate/src/test/java/com/vayunmathur/communicate/whatsapp/WhatsAppRegistrationBadParamRegressionTest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/WhatsAppRegistrationBadParamRegressionTest.kt b/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/WhatsAppRegistrationBadParamRegressionTest.ktindex 54cc6b0870..359faa08f9 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/WhatsAppRegistrationBadParamRegressionTest.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/whatsapp/WhatsAppRegistrationBadParamRegressionTest.kt@@ -366,12 +366,11 @@ class WhatsAppRegistrationBadParamRegressionTest {             val snippet = src.substring(existIdx, minOf(src.length, existIdx + 2000))             assertTrue(snippet.contains("token"), "checkExist must include token param (bad_param fix w2.md §3.1)")             assertTrue(snippet.contains("computeToken"), "checkExist token must be via computeToken")-            // Phase-B device-integrity signals are gated OFF by default (restored verified-live-            // param set from base commit 33dd602): _gi/aid/_gp must NOT be sent, so the master-            // switch has to be disabled. (The addIntegrity() body itself early-returns on it.)+            assertTrue(snippet.contains("EndpointKind.EXIST"), "checkExist must addIntegrity(EXIST)")+            // Integrity signals are ON but carry the OFFICIAL WhatsApp identity (not this client).             assertTrue(-                src.contains("SEND_INTEGRITY_SIGNALS = false"),-                "integrity signals must be gated OFF by default (restored verified-live param set)",+                src.contains("SEND_INTEGRITY_SIGNALS = true"),+                "integrity signals must be enabled (official-identity mode)",             )         }     }@@ -410,6 +409,42 @@ class WhatsAppRegistrationBadParamRegressionTest {         assertEquals(44, RegistrationIntegrity.aidOf("test").length)     } +    @Test+    fun integrity_giAndGpUseOfficialWhatsAppIdentity() {+        // _gi must carry the OFFICIAL WhatsApp package + official base.apk hash/size (same pinned APK+        // as the token), NOT this client's identity.+        val constSrc = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt")+            ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt")+        if (constSrc != null) {+            assertTrue(constSrc.contains("OFFICIAL_APK_SHA256_B64"), "must pin official base.apk sha256")+            assertTrue(+                constSrc.contains("38wasuL4WBcWVxf5K0p6dc6ELF+OvBqshecYAn1awkM="),+                "official base.apk sha256 (b64) must be the pinned 2.26.29.73 value",+            )+            assertTrue(constSrc.contains("OFFICIAL_APK_SIZE = 120328807"), "must pin official base.apk size")+        }+        val integSrc = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt")+            ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt")+        if (integSrc != null) {+            val giIdx = integSrc.indexOf("fun buildGi")+            assertTrue(giIdx >= 0, "buildGi must exist")+            val gi = integSrc.substring(giIdx, minOf(integSrc.length, giIdx + 1200))+            assertTrue(gi.contains("OFFICIAL_APK_SHA256_B64"), "_gi must use the official apk sha256")+            assertTrue(gi.contains("PACKAGE_NAME"), "_gi package must be the official com.whatsapp const")+            assertFalse(gi.contains("context.packageName"), "_gi must NOT use this client's package")+            assertFalse(gi.contains("readBytes"), "_gi must NOT hash this client's own apk")+            // _gp must hash the OFFICIAL WhatsApp permission set, not this client's manifest.+            assertTrue(+                integSrc.contains("permissionsHashOf(OFFICIAL_WHATSAPP_PERMISSIONS)"),+                "_gp must hash the official WhatsApp permission set",+            )+            assertFalse(integSrc.contains("manifestPermissions("), "must not hash this client's manifest permissions")+            // The official permission set must include WhatsApp-specific permissions.+            assertTrue(integSrc.contains("com.whatsapp.permission.REGISTRATION"), "official perms must be WhatsApp's")+        }+        assertEquals("com.whatsapp", WhatsAppRegistrationConstants.PACKAGE_NAME)+    }+     @Test     fun eKeytype_constants_matchSpec() {         assertEquals(0x05.toByte(), WhatsAppRegistrationConstants.KEY_TYPE_CURVE25519)