forked from um/web
refactor(decrypt/qmc): typescript qmc mask
This commit is contained in:
parent
15dba7b92f
commit
aca1c11332
@ -1,4 +1,4 @@
|
|||||||
import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
|
import {QmcMask, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
|
||||||
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
|
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
|
||||||
import {
|
import {
|
||||||
AudioMimeType,
|
AudioMimeType,
|
||||||
@ -68,7 +68,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
|
const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
|
||||||
if (handler.detect) reportKeyUsage(keyData, seed.Matrix128,
|
if (handler.detect) reportKeyUsage(keyData, seed.getMatrix128(),
|
||||||
info.artist, info.title, musicMeta.common.album, raw_filename, raw_ext);
|
info.artist, info.title, musicMeta.common.album, raw_filename, raw_ext);
|
||||||
|
|
||||||
let imgUrl = GetCoverFromFile(musicMeta);
|
let imgUrl = GetCoverFromFile(musicMeta);
|
||||||
@ -126,7 +126,7 @@ async function queryKeyInfo(keyData, filename, format) {
|
|||||||
body: JSON.stringify({Format: format, Key: Base64Encode(keyData), Filename: filename, Type: 44}),
|
body: JSON.stringify({Format: format, Key: Base64Encode(keyData), Filename: filename, Type: 44}),
|
||||||
});
|
});
|
||||||
let data = await resp.json();
|
let data = await resp.json();
|
||||||
return QmcMaskCreate58(Base64Decode(data.Matrix44));
|
return new QmcMask(Base64Decode(data.Matrix44));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {BytesEquals, BytesHasPrefix, FLAC_HEADER, OGG_HEADER} from "@/decrypt/utils.ts";
|
import {BytesHasPrefix, FLAC_HEADER, OGG_HEADER} from "@/decrypt/utils.ts";
|
||||||
|
|
||||||
const QMOggPublicHeader1 = [
|
const QMOggPublicHeader1 = [
|
||||||
0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
|
0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
|
||||||
@ -35,96 +35,65 @@ const QMCDefaultMaskMatrix = [
|
|||||||
0xf9, 0xbc, 0x00, 0x11];
|
0xf9, 0xbc, 0x00, 0x11];
|
||||||
|
|
||||||
|
|
||||||
class QmcMask {
|
const AllMapping: number[][] = [];
|
||||||
constructor(matrix, superA, superB) {
|
const Mask128to44: number[] = [];
|
||||||
if (superA === undefined || superB === undefined) {
|
|
||||||
|
(function () {
|
||||||
|
for (let i = 0; i < 128; i++) {
|
||||||
|
let realIdx = (i * i + 27) % 256
|
||||||
|
if (realIdx in AllMapping) {
|
||||||
|
AllMapping[realIdx].push(i)
|
||||||
|
} else {
|
||||||
|
AllMapping[realIdx] = [i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let idx44 = 0
|
||||||
|
AllMapping.forEach(all128 => {
|
||||||
|
all128.forEach(_i128 => {
|
||||||
|
Mask128to44[_i128] = idx44
|
||||||
|
})
|
||||||
|
idx44++
|
||||||
|
})
|
||||||
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
export class QmcMask {
|
||||||
|
private readonly Matrix128: number[];
|
||||||
|
|
||||||
|
constructor(matrix: number[] | Uint8Array) {
|
||||||
|
if (matrix instanceof Uint8Array) matrix = Array.from(matrix)
|
||||||
if (matrix.length === 44) {
|
if (matrix.length === 44) {
|
||||||
this.Matrix44 = matrix
|
this.Matrix128 = this._generate128(matrix)
|
||||||
this.generateMask128from44()
|
} else if (matrix.length === 128) {
|
||||||
} else {
|
|
||||||
this.Matrix128 = matrix
|
this.Matrix128 = matrix
|
||||||
this.generateMask44from128()
|
|
||||||
}
|
|
||||||
this.generateMask58from128()
|
|
||||||
} else {
|
} else {
|
||||||
this.Matrix58 = matrix;
|
throw Error("invalid mask length")
|
||||||
this.Super58A = superA;
|
|
||||||
this.Super58B = superB;
|
|
||||||
this.generateMask128from58();
|
|
||||||
this.generateMask44from128()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generateMask128from58() {
|
getMatrix128() {
|
||||||
if (this.Matrix58.length !== 56) throw "incorrect mask58 matrix length";
|
return this.Matrix128
|
||||||
|
|
||||||
let matrix128 = [];
|
|
||||||
for (let rowIdx = 0; rowIdx < 8; rowIdx += 1) {
|
|
||||||
matrix128 = matrix128.concat(
|
|
||||||
[this.Super58A],
|
|
||||||
this.Matrix58.slice(7 * rowIdx, 7 * rowIdx + 7),
|
|
||||||
[this.Super58B],
|
|
||||||
this.Matrix58.slice(56 - 7 - 7 * rowIdx, 56 - 7 * rowIdx).reverse()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
this.Matrix128 = matrix128;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateMask58from128() {
|
getMatrix44(): number[] {
|
||||||
if (this.Matrix128.length !== 128) throw "incorrect mask128 length";
|
const matrix44: number[] = []
|
||||||
|
|
||||||
const superA = this.Matrix128[0], superB = this.Matrix128[8];
|
|
||||||
let matrix58 = [];
|
|
||||||
|
|
||||||
for (let rowIdx = 0; rowIdx < 8; rowIdx += 1) {
|
|
||||||
let lenStart = 16 * rowIdx;
|
|
||||||
let lenRightStart = 120 - lenStart;
|
|
||||||
if (this.Matrix128[lenStart] !== superA || this.Matrix128[lenStart + 8] !== superB) {
|
|
||||||
throw "decode mask-128 to mask-58 failed"
|
|
||||||
}
|
|
||||||
let rowLeft = this.Matrix128.slice(lenStart + 1, lenStart + 8);
|
|
||||||
let rowRight = this.Matrix128.slice(lenRightStart + 1, lenRightStart + 8).reverse();
|
|
||||||
if (BytesEquals(rowLeft, rowRight)) {
|
|
||||||
matrix58 = matrix58.concat(rowLeft);
|
|
||||||
} else {
|
|
||||||
throw "decode mask-128 to mask-58 failed"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.Matrix58 = matrix58;
|
|
||||||
this.Super58A = superA;
|
|
||||||
this.Super58B = superB;
|
|
||||||
}
|
|
||||||
|
|
||||||
generateMask44from128() {
|
|
||||||
if (this.Matrix128.length !== 128) throw "incorrect mask128 matrix length";
|
|
||||||
let mapping = GetConvertMapping()
|
|
||||||
this.Matrix44 = []
|
|
||||||
let idxI44 = 0
|
let idxI44 = 0
|
||||||
mapping.forEach(it256 => {
|
AllMapping.forEach(it256 => {
|
||||||
let it256Len = it256.length
|
let it256Len = it256.length
|
||||||
for (let i = 1; i < it256Len; i++) {
|
for (let i = 1; i < it256Len; i++) {
|
||||||
if (this.Matrix128[it256[0]] !== this.Matrix128[it256[i]]) {
|
if (this.Matrix128[it256[0]] !== this.Matrix128[it256[i]]) {
|
||||||
throw "decode mask-128 to mask-44 failed"
|
throw "decode mask-128 to mask-44 failed"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.Matrix44[idxI44] = this.Matrix128[it256[0]]
|
matrix44[idxI44] = this.Matrix128[it256[0]]
|
||||||
idxI44++
|
idxI44++
|
||||||
})
|
})
|
||||||
|
return matrix44
|
||||||
}
|
}
|
||||||
|
|
||||||
generateMask128from44() {
|
Decrypt(data: Uint8Array) {
|
||||||
if (this.Matrix44.length !== 44) throw "incorrect mask length"
|
if (!this.Matrix128) throw Error("bad call sequence")
|
||||||
this.Matrix128 = []
|
|
||||||
let idx44 = 0
|
|
||||||
GetConvertMapping().forEach(it256 => {
|
|
||||||
it256.forEach(m => {
|
|
||||||
this.Matrix128[m] = this.Matrix44[idx44]
|
|
||||||
})
|
|
||||||
idx44++
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
Decrypt(data) {
|
|
||||||
let dst = data.slice(0);
|
let dst = data.slice(0);
|
||||||
let index = -1;
|
let index = -1;
|
||||||
let maskIdx = -1;
|
let maskIdx = -1;
|
||||||
@ -140,13 +109,25 @@ class QmcMask {
|
|||||||
}
|
}
|
||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _generate128(matrix44: number[]): number[] {
|
||||||
|
const matrix128: number[] = []
|
||||||
|
let idx44 = 0
|
||||||
|
AllMapping.forEach(it256 => {
|
||||||
|
it256.forEach(m => {
|
||||||
|
matrix128[m] = matrix44[idx44]
|
||||||
|
})
|
||||||
|
idx44++
|
||||||
|
})
|
||||||
|
return matrix128
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function QmcMaskGetDefault() {
|
export function QmcMaskGetDefault() {
|
||||||
return new QmcMask(QMCDefaultMaskMatrix)
|
return new QmcMask(QMCDefaultMaskMatrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function QmcMaskDetectMflac(data) {
|
export function QmcMaskDetectMflac(data: Uint8Array) {
|
||||||
let search_len = Math.min(0x8000, data.length), mask;
|
let search_len = Math.min(0x8000, data.length), mask;
|
||||||
for (let block_idx = 0; block_idx < search_len; block_idx += 128) {
|
for (let block_idx = 0; block_idx < search_len; block_idx += 128) {
|
||||||
try {
|
try {
|
||||||
@ -160,9 +141,9 @@ export function QmcMaskDetectMflac(data) {
|
|||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function QmcMaskDetectMgg(data) {
|
export function QmcMaskDetectMgg(data: Uint8Array) {
|
||||||
if (data.length < 0x100) return
|
if (data.length < 0x100) return
|
||||||
let matrixConfidence = {};
|
let matrixConfidence: { [key: number]: { [key: number]: number } } = {};
|
||||||
for (let i = 0; i < 44; i++) matrixConfidence[i] = {};
|
for (let i = 0; i < 44; i++) matrixConfidence[i] = {};
|
||||||
|
|
||||||
const page2 = data[0x54] ^ data[0xC] ^ QMOggPublicHeader1[0xC];
|
const page2 = data[0x54] ^ data[0xC] ^ QMOggPublicHeader1[0xC];
|
||||||
@ -171,7 +152,7 @@ export function QmcMaskDetectMgg(data) {
|
|||||||
|
|
||||||
for (let idx128 = 0; idx128 < spHeader.length; idx128++) {
|
for (let idx128 = 0; idx128 < spHeader.length; idx128++) {
|
||||||
if (spConf[idx128] === 0) continue;
|
if (spConf[idx128] === 0) continue;
|
||||||
let idx44 = GetMask44Index(idx128);
|
let idx44 = Mask128to44[idx128 % 128];
|
||||||
let _m = data[idx128] ^ spHeader[idx128]
|
let _m = data[idx128] ^ spHeader[idx128]
|
||||||
let confidence = spConf[idx128];
|
let confidence = spConf[idx128];
|
||||||
if (_m in matrixConfidence[idx44]) {
|
if (_m in matrixConfidence[idx44]) {
|
||||||
@ -183,7 +164,7 @@ export function QmcMaskDetectMgg(data) {
|
|||||||
let matrix = [];
|
let matrix = [];
|
||||||
try {
|
try {
|
||||||
for (let i = 0; i < 44; i++)
|
for (let i = 0; i < 44; i++)
|
||||||
matrix[i] = getMaskConfidenceResult(matrixConfidence[i]);
|
matrix[i] = calcMaskFromConfidence(matrixConfidence[i]);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -191,82 +172,34 @@ export function QmcMaskDetectMgg(data) {
|
|||||||
if (!BytesHasPrefix(mask.Decrypt(data.slice(0, OGG_HEADER.length)), OGG_HEADER)) {
|
if (!BytesHasPrefix(mask.Decrypt(data.slice(0, OGG_HEADER.length)), OGG_HEADER)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function QmcMaskCreate128(mask128) {
|
|
||||||
return new QmcMask(mask128)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function QmcMaskCreate58(matrix, superA, superB) {
|
function calcMaskFromConfidence(confidence: { [key: number]: number }) {
|
||||||
return new QmcMask(matrix, superA, superB)
|
console.log(confidence)
|
||||||
}
|
const count = Object.keys(confidence).length
|
||||||
|
if (count === 0) throw "can not match at least one key";
|
||||||
export function QmcMaskCreate44(mask44) {
|
if (count > 1) console.warn("There are 2 potential value for the mask!")
|
||||||
return new QmcMask(mask44)
|
let result = ""
|
||||||
}
|
let conf = 0
|
||||||
|
|
||||||
/**
|
|
||||||
* @param confidence {{}}
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
function getMaskConfidenceResult(confidence) {
|
|
||||||
if (confidence.length === 0) throw "can not match at least one key";
|
|
||||||
if (confidence.length > 1) console.warn("There are 2 potential value for the mask!")
|
|
||||||
let result, conf = 0;
|
|
||||||
for (let idx in confidence) {
|
for (let idx in confidence) {
|
||||||
if (confidence[idx] > conf) {
|
if (confidence[idx] > conf) {
|
||||||
result = idx;
|
result = idx;
|
||||||
conf = confidence[idx];
|
conf = confidence[idx];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return parseInt(result)
|
return Number(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function QmcGenerateOggHeader(page2: number) {
|
||||||
* @return {number}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
const allMapping = [];
|
|
||||||
const mask128to44 = [];
|
|
||||||
|
|
||||||
(function () {
|
|
||||||
for (let i = 0; i < 128; i++) {
|
|
||||||
let realIdx = (i * i + 27) % 256
|
|
||||||
if (realIdx in allMapping) {
|
|
||||||
allMapping[realIdx].push(i)
|
|
||||||
} else {
|
|
||||||
allMapping[realIdx] = [i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let idx44 = 0
|
|
||||||
allMapping.forEach(all128 => {
|
|
||||||
all128.forEach(_i128 => {
|
|
||||||
mask128to44[_i128] = idx44
|
|
||||||
})
|
|
||||||
idx44++
|
|
||||||
})
|
|
||||||
})();
|
|
||||||
|
|
||||||
function GetConvertMapping() {
|
|
||||||
return allMapping;
|
|
||||||
}
|
|
||||||
|
|
||||||
function GetMask44Index(idx128) {
|
|
||||||
return mask128to44[idx128 % 128]
|
|
||||||
}
|
|
||||||
|
|
||||||
function QmcGenerateOggHeader(page2) {
|
|
||||||
let spec = [page2, 0xFF]
|
let spec = [page2, 0xFF]
|
||||||
for (let i = 2; i < page2; i++) spec.push(0xFF)
|
for (let i = 2; i < page2; i++) spec.push(0xFF)
|
||||||
spec.push(0xFF)
|
spec.push(0xFF)
|
||||||
return QMOggPublicHeader1.concat(spec, QMOggPublicHeader2)
|
return QMOggPublicHeader1.concat(spec, QMOggPublicHeader2)
|
||||||
}
|
}
|
||||||
|
|
||||||
function QmcGenerateOggConf(page2) {
|
function QmcGenerateOggConf(page2: number) {
|
||||||
let specConf = [6, 0]
|
let specConf = [6, 0]
|
||||||
for (let i = 2; i < page2; i++) specConf.push(4)
|
for (let i = 2; i < page2; i++) specConf.push(4)
|
||||||
specConf.push(0)
|
specConf.push(0)
|
Loading…
Reference in New Issue
Block a user