mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Brute force media decoding
This commit is contained in:
@@ -112,32 +112,41 @@ export async function decodeMediaMessage(message: WAMessageContent, filename: st
|
|||||||
| proto.ImageMessage
|
| proto.ImageMessage
|
||||||
| proto.AudioMessage
|
| proto.AudioMessage
|
||||||
| proto.DocumentMessage
|
| proto.DocumentMessage
|
||||||
// get the keys to decrypt the message
|
|
||||||
const mediaKeys = getMediaKeys(messageContent.mediaKey, type) //getMediaKeys(Buffer.from(messageContent.mediaKey, 'base64'), type)
|
|
||||||
const iv = mediaKeys.iv
|
|
||||||
const cipherKey = mediaKeys.cipherKey
|
|
||||||
const macKey = mediaKeys.macKey
|
|
||||||
|
|
||||||
// download the message
|
// download the message
|
||||||
const fetched = await fetch(messageContent.url, {})
|
const fetched = await fetch(messageContent.url, {})
|
||||||
const buffer = await fetched.buffer()
|
const buffer = await fetched.buffer()
|
||||||
// first part is actual file
|
|
||||||
const file = buffer.slice(0, buffer.length - 10)
|
|
||||||
// last 10 bytes is HMAC sign of file
|
|
||||||
const mac = buffer.slice(buffer.length - 10, buffer.length)
|
|
||||||
|
|
||||||
// sign IV+file & check for match with mac
|
const decryptedMedia = (type: MessageType) => {
|
||||||
const testBuff = Buffer.concat([iv, file])
|
// get the keys to decrypt the message
|
||||||
const sign = hmacSign(testBuff, macKey).slice(0, 10)
|
const mediaKeys = getMediaKeys(messageContent.mediaKey, type) //getMediaKeys(Buffer.from(messageContent.mediaKey, 'base64'), type)
|
||||||
// our sign should equal the mac
|
// first part is actual file
|
||||||
if (sign.equals(mac)) {
|
const file = buffer.slice(0, buffer.length - 10)
|
||||||
const decrypted = aesDecryptWithIV(file, cipherKey, iv) // decrypt media
|
// last 10 bytes is HMAC sign of file
|
||||||
|
const mac = buffer.slice(buffer.length - 10, buffer.length)
|
||||||
const trueFileName = attachExtension ? (filename + '.' + getExtension(messageContent.mimetype)) : filename
|
// sign IV+file & check for match with mac
|
||||||
fs.writeFileSync(trueFileName, decrypted)
|
const testBuff = Buffer.concat([mediaKeys.iv, file])
|
||||||
|
const sign = hmacSign(testBuff, mediaKeys.macKey).slice(0, 10)
|
||||||
return trueFileName
|
// our sign should equal the mac
|
||||||
} else {
|
if (sign.equals(mac)) {
|
||||||
throw new Error('HMAC sign does not match')
|
return aesDecryptWithIV(file, mediaKeys.cipherKey, mediaKeys.iv) // decrypt media
|
||||||
|
} else {
|
||||||
|
throw new Error('HMAC sign does not match')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
const allTypes = [type, ...Object.keys(HKDFInfoKeys)]
|
||||||
|
for (let i = 0; i < allTypes.length;i++) {
|
||||||
|
try {
|
||||||
|
const decrypted = decryptedMedia (allTypes[i] as MessageType)
|
||||||
|
|
||||||
|
if (i > 0) { console.log (`decryption of ${type} media with HKDF key of ${allTypes[i]}`) }
|
||||||
|
|
||||||
|
const trueFileName = attachExtension ? (filename + '.' + getExtension(messageContent.mimetype)) : filename
|
||||||
|
fs.writeFileSync(trueFileName, decrypted)
|
||||||
|
return trueFileName
|
||||||
|
} catch {
|
||||||
|
if (i === 0) { console.log (`decryption of ${type} media with original HKDF key failed`) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error('HMAC sign does not match')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user