Newshot

逆向工程解析一款桌游

Translation(model translation, not the source's own text)

逆向工程一款桌游

我最近收到一份礼物,是一款叫 “Hitster” 的游戏。这是一款桌游,你要识别歌曲,并猜测歌曲发行时间、艺术家或乐队是谁等等。每张卡片上都有一个二维码,你可以用 Hitster 应用扫描,然后它会通过 Spotify 或 Apple Music 播放音乐。

遗憾的是,该应用需要 Apple Music 或 Spotify 订阅。在这篇博客文章中,我将深入逆向这个 Android 应用,并绕过他们极其、极其草率地隐藏数据的尝试。

第 0 部分:工作原理

首先,我们需要理解这个游戏是如何运作的。

每张卡片的正面有艺术家或乐队名称、歌曲发行年份和歌曲标题。背面有一个二维码。扫描它会把你带到一个 URL,其路径中有两项内容:游戏区域(我的情况是 DE)和卡片 ID。然后音乐会在应用中或通过 Spotify 开始播放。

这意味着存在从 Card ID ⇒ Spotify 曲目的映射。每个区域也有多个卡片版本,所以仅凭卡片 ID 不足以识别一首歌。Card ID 1 在英国可能是 “Wet Dream”,但在德国可能是 “Tage wie diese”。

card.png
card.png
qr_code.png
qr_code.png

第 1 部分:Android 应用

从某个不太靠谱的 Android APK 下载网站拿到该应用的 APK 后,我用 jadx 得到了反编译后的 Java 代码。

source_code
source_code

翻找了一会儿后,我在 res/raw 下找到了几个 JSON 文件。其中一个名为 gameset_database_4.json,它似乎是加密的。

我运行了一个工具来识别格式,结果返回 Base64。那是一种编码,所以我先对它进行了解码。结果仍然不可读,于是我猜它是用 AES 或类似方式加密的。

我以前从未逆向过 Android 应用,所以我只是全局搜索 AES。你猜怎么着?有一个名为 k43.java 的文件,把整个解密过程都摆在我面前:


        public static String f(String str, String str2) throws o21 {
    str.getClass();
    str2.getClass();
    try {
        byte[] bArrDecode = Base64.decode(str, 0);
        bArrDecode.getClass();
        p(bArrDecode);
        return o(
            or.m(bArrDecode, 0, 12),
            bArrDecode,
            m(bArrDecode.length - 28, str2)
        );
    } catch (o21 e) {
        throw e;
    } catch (Exception e2) {
        Napier.e$default(
            Napier.INSTANCE,
            e2,
            (String) null,
            new s11(24),
            2,
            (Object) null
        );
        throw new o21(pv0.o("Decryption failed: ", e2.getMessage()), e2);
    }
}

public static SecretKeySpec m(int i, String str)
    throws NoSuchAlgorithmException {
    String str2 = i + str;
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    byte[] bytes = str2.getBytes(yj0.a);
    bytes.getClass();
    return new SecretKeySpec(messageDigest.digest(bytes), "AES");
}

public static String o(
    byte[] bArr,
    byte[] bArr2,
    SecretKeySpec secretKeySpec
)
    throws BadPaddingException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(
        2,
        secretKeySpec,
        new GCMParameterSpec(SemanticState.Focused, bArr)
    );
    byte[] bArrDoFinal = cipher.doFinal(bArr2, 12, bArr2.length - 12);
    bArrDoFinal.getClass();
    return new String(bArrDoFinal, yj0.a);
}
      

那我们来一步步分析。

函数 f 接受两个字符串作为输入。第一个 str 包含 Base64 编码的数据,它将其解码:


        byte[] bArrDecode = Base64.decode(str, 0);
bArrDecode.getClass();
p(bArrDecode);
      

然后它会检查解码后的数据是否至少有 28 字节长:


        public static void p(byte[] bArr) throws o21 {
    if (bArr.length < 28) {
        throw new o21(
            gg3.g(
                bArr.length,
                "Encrypted data too small: ",
                " bytes (minimum: 28 bytes)"
            ),
            null
        );
    }
}
      

接下来,它把一堆数据传给函数 o,由 o 处理解密的最后阶段:


        return o(
    or.m(bArrDecode, 0, 12),
    bArrDecode,
    m(bArrDecode.length - 28, str2)
);
      

第一个参数是解码后数据的前 12 个字节。这就是我们的 IV,或者说 nonce。第二个参数是完整的解码字节数组。

第三个参数才是关键所在。它取总长度减去 28,然后把结果连同 str2 一起传给函数 m。这 28 个字节对应 12 字节的 nonce 和 16 字节的认证标签。

在 m 内部,剩余长度被转换成字符串,并与 str2 拼接。随后,这个拼接后的字符串会用 SHA-256 进行哈希,以生成 AES 密钥。

所以 str2 并不是 AES 密钥本身。它是用来派生密钥的秘密字符串,而我们还没找到它。

最后,o 执行实际解密:


        public static String o(
    byte[] bArr,
    byte[] bArr2,
    SecretKeySpec secretKeySpec
)
    throws BadPaddingException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(
        2,
        secretKeySpec,
        new GCMParameterSpec(SemanticState.Focused, bArr)
    );
    byte[] bArrDoFinal = cipher.doFinal(bArr2, 12, bArr2.length - 12);
    bArrDoFinal.getClass();
    return new String(bArrDoFinal, yj0.a);
}
      

它用派生出的密钥和 nonce 设置 AES-GCM,然后解密前 12 个字节之后的数据。

解密流程就此搞清楚了。现在只差那个秘密字符串。

第 2 部分:寻找解密密钥

我当时正在 Rust Language Community 服务器的 Discord 通话里做这件事,有人指出我应该去找一个长度为 32 字节的字节数组。

我一开始搜索 byte[32],但没找到有用的东西。然后我试了这个模式:


        grep -P 'byte\[\]\s*\w+\s*=\s*\{' | awk -F',' 'NF==32'
      

这把我引向了 m17.java,里面包含这两个超酷的数组:


        public static final byte[] a = {117, -67, 13, -80, 43, 2, 93, 61, 9, 71, 61, -85, 12, -119, -123, 58, -13, 30, -119, -59, -30, 60, 3, 56, -34, -80, -28, -124, 88, -124, 1, 68};
public static final byte[] b = {45, -28, 93, -4, 29, 49, 31, 87, 102, 16, 119, -31, 71, -54, -10, 66, -72, 40, -80, -73, -86, 14, 77, 86, -85, -55, -121, -55, 46, -73, 98, 29};
      

然后我们试着弄清楚如何把它们变成有用的东西。经过一番试验,有人建议把两个数组进行 XOR。我们把得到的字节解释为文本,得到了这个:


        XYPL63BjoWJJKCsxK69rH2NnuycMv3cY
      

使用这个字符串,我用 Rust 写了一个小小的解密器:


        use aes_gcm::{
    Aes256Gcm, Nonce,
    aead::{Aead, AeadCore, Generate, Key, KeyInit},
};
use base64::Engine;
use sha2::{Digest, Sha256};

const STRING_2: &str = "XYPL63BjoWJJKCsxK69rH2NnuycMv3cY";

fn main() {
    let input = std::fs::read_to_string(
        "/home/<redacted>/Downloads/HITSTER_4.0.3_APKPure.xapk.out/HITSTER_4.0.3_apkcombo.com/resources/nl.jumbo.hitster.apk/res/raw/locales_v4.json",
    );

let data = base64::engine::general_purpose::STANDARD
        .decode(input.unwrap())
        .unwrap();

let iv = &data[..12];
    let ct = &data[12..];

let mut hash = Sha256::new();
    hash.update((data.len() - 28).to_string().as_bytes());
    hash.update(STRING_2);
    let key = hash.finalize();

let cipher = Aes256Gcm::new(&key);
    let plaintext = cipher.decrypt(Nonce::from_slice(iv), ct).unwrap();

std::fs::write("test.txt", plaintext).unwrap();
}
      

我们因此得到了这个很棒的 test.txt 文件,其中包含约 31,000 行映射,覆盖多个地区,将卡片 ID 对应到 Apple Music 和 Spotify 曲目,还有 ISRC。

随后我们又多翻看了一下代码,发现这一切其实根本不需要。

如果我们先搜索一个 HTTPS URL,就会落到这个地址:

https://hitster.jumboplay.com/hitster-assets/

再稍加想象,我们试了:

https://hitster.jumboplay.com/hitster-assets/gameset_database.json

全部内容就在那里。没有任何加密。

我们花了大约 18 个小时追一个文件,而它到头来甚至算不上真正的秘密。

  • 感谢 Rust Language Discord 社区的 Mr.Corrupt 提供的帮助。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗