diff --git a/src/decrypt/qmc.test.ts b/src/decrypt/qmc.test.ts new file mode 100644 index 0000000..5bb2af9 --- /dev/null +++ b/src/decrypt/qmc.test.ts @@ -0,0 +1,32 @@ +import fs from "fs"; +import {QmcDecoder} from "@/decrypt/qmc"; +import {BytesEqual} from "@/decrypt/utils"; + +function loadTestDataDecoder(name: string): { + cipherText: Uint8Array, + clearText: Uint8Array +} { + const cipherBody = fs.readFileSync(`./testdata/${name}_raw.bin`); + const cipherSuffix = fs.readFileSync(`./testdata/${name}_suffix.bin`); + const cipherText = new Uint8Array(cipherBody.length + cipherSuffix.length); + cipherText.set(cipherBody); + cipherText.set(cipherSuffix, cipherBody.length); + return { + cipherText, + clearText: fs.readFileSync(`testdata/${name}_target.bin`) + } +} + +test("qmc: real file", async () => { + const cases = ["mflac0_rc4", "mflac_map", "mgg_map", "qmc0_static"] + for (const name of cases) { + const {clearText, cipherText} = loadTestDataDecoder(name) + const c = new QmcDecoder(cipherText) + const buf = c.decrypt() + + expect(BytesEqual(buf, clearText)).toBeTruthy() + } +}) + + + diff --git a/src/decrypt/qmc.ts b/src/decrypt/qmc.ts index a593cd7..91a652e 100644 --- a/src/decrypt/qmc.ts +++ b/src/decrypt/qmc.ts @@ -1,4 +1,4 @@ -import {QmcStaticCipher} from "./qmc_cipher"; +import {QmcMapCipher, QmcRC4Cipher, QmcStaticCipher, StreamCipher} from "./qmc_cipher"; import { AudioMimeType, GetArrayBuffer, @@ -16,6 +16,7 @@ import {DecryptQMCv2} from "./qmcv2"; import iconv from "iconv-lite"; import {DecryptResult} from "@/decrypt/entity"; import {queryAlbumCover} from "@/utils/api"; +import {QmcDecryptKey} from "@/decrypt/qmc_key"; interface Handler { ext: string @@ -137,3 +138,63 @@ async function getCoverImage(title: string, artist?: string, album?: string): Pr } return "" } + +export class QmcDecoder { + file: Uint8Array + size: number + decoded: boolean = false + audioSize?: number + cipher?: StreamCipher + + constructor(file: Uint8Array) { + this.file = file + this.size = file.length + this.searchKey() + } + + decrypt(): Uint8Array { + if (!this.cipher) { + throw new Error("no cipher found") + } + if (!this.audioSize || this.audioSize <= 0) { + throw new Error("invalid audio size") + } + const audioBuf = this.file.subarray(0, this.audioSize) + + if (!this.decoded) { + this.cipher.decrypt(audioBuf, 0) + this.decoded = true + } + + return audioBuf + } + + private searchKey() { + const last4Byte = this.file.slice(this.size - 4); + const textEnc = new TextDecoder() + if (textEnc.decode(last4Byte) === 'QTag') { + const sizeBuf = this.file.slice(this.size - 8, this.size - 4) + const sizeView = new DataView(sizeBuf.buffer, sizeBuf.byteOffset) + const keySize = sizeView.getUint32(0, false) + this.audioSize = this.size - keySize - 8 + const rawKey = this.file.subarray(this.audioSize, this.size - 8) + const keyEnd = rawKey.findIndex(v => v == ','.charCodeAt(0)) + const keyDec = QmcDecryptKey(rawKey.subarray(0, keyEnd)) + this.cipher = new QmcRC4Cipher(keyDec) + } else { + const sizeView = new DataView(last4Byte.buffer, last4Byte.byteOffset); + const keySize = sizeView.getUint32(0, true) + if (keySize < 0x300) { + this.audioSize = this.size - keySize - 4 + const rawKey = this.file.subarray(this.audioSize, this.size - 4) + const keyDec = QmcDecryptKey(rawKey) + this.cipher = new QmcMapCipher(keyDec) + } else { + this.audioSize = this.size + this.cipher = new QmcStaticCipher() + } + } + } + + +} diff --git a/src/decrypt/qmc_key.test.ts b/src/decrypt/qmc_key.test.ts index 4584f9e..327732a 100644 --- a/src/decrypt/qmc_key.test.ts +++ b/src/decrypt/qmc_key.test.ts @@ -1,4 +1,4 @@ -import {DecryptKey, simpleMakeKey,} from "@/decrypt/qmc_key"; +import {QmcDecryptKey, simpleMakeKey,} from "@/decrypt/qmc_key"; import fs from "fs"; test("key dec: make simple key", () => { @@ -23,7 +23,7 @@ test("key dec: real file", async () => { const cases = ["mflac_map", "mgg_map", "mflac0_rc4"] for (const name of cases) { const {clearText, cipherText} = loadTestDataKeyDecrypt(name) - const buf = DecryptKey(cipherText) + const buf = QmcDecryptKey(cipherText) expect(buf).toStrictEqual(clearText) } diff --git a/src/decrypt/qmc_key.ts b/src/decrypt/qmc_key.ts index ba487ba..748d4e2 100644 --- a/src/decrypt/qmc_key.ts +++ b/src/decrypt/qmc_key.ts @@ -83,8 +83,9 @@ function decryptTencentTea(inBuf: Uint8Array, key: Uint8Array): Uint8Array { return outBuf } -export function DecryptKey(raw: Uint8Array): Uint8Array { - const rawDec = Buffer.from(raw.toString(), 'base64') +export function QmcDecryptKey(raw: Uint8Array): Uint8Array { + const textDec = new TextDecoder() + const rawDec = Buffer.from(textDec.decode(raw), 'base64') let n = rawDec.length; if (n < 16) { throw Error("key length is too short") diff --git a/src/decrypt/utils.ts b/src/decrypt/utils.ts index cef54f6..81692b3 100644 --- a/src/decrypt/utils.ts +++ b/src/decrypt/utils.ts @@ -32,13 +32,20 @@ export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean { }) } +export function BytesEqual(a: Uint8Array, b: Uint8Array,): boolean { + if (a.length != b.length) return false + return a.every((val, idx) => { + return val === b[idx]; + }) +} + export function SniffAudioExt(data: Uint8Array, fallback_ext: string = "mp3"): string { if (BytesHasPrefix(data, MP3_HEADER)) return "mp3" if (BytesHasPrefix(data, FLAC_HEADER)) return "flac" if (BytesHasPrefix(data, OGG_HEADER)) return "ogg" if (data.length >= 4 + M4A_HEADER.length && - BytesHasPrefix(data.slice(4), M4A_HEADER)) return "m4a" + BytesHasPrefix(data.slice(4), M4A_HEADER)) return "m4a" if (BytesHasPrefix(data, WAV_HEADER)) return "wav" if (BytesHasPrefix(data, WMA_HEADER)) return "wma" if (BytesHasPrefix(data, AAC_HEADER)) return "aac" diff --git a/testdata/qmc0_static_raw.bin b/testdata/qmc0_static_raw.bin new file mode 100644 index 0000000..80f65f9 Binary files /dev/null and b/testdata/qmc0_static_raw.bin differ diff --git a/testdata/qmc0_static_suffix.bin b/testdata/qmc0_static_suffix.bin new file mode 100644 index 0000000..e69de29 diff --git a/testdata/qmc0_static_target.bin b/testdata/qmc0_static_target.bin new file mode 100644 index 0000000..f3ff0a4 Binary files /dev/null and b/testdata/qmc0_static_target.bin differ