使用Python/pycryptodome为Android备份解密Titanium备份
pytibade的Python项目详细描述
py tibade:Python钛备份解密
作者:李泰宇
这是一个基于Python/pycryptodome的TitaniumBackupDecrypt实现, 它最初是由布莱恩T.哈弗撰写的。它将解密备份 通过Titanium Backup for Android。在
另一种Python实现是TiBUdecrypter 由大卫·奥鲁克创作。在
安装
这在两个PyPi上都可用:
pip install pytibade
pytibade --version
康达:
^{pr2}$使用
positional arguments:
inputfiles Input file(s), either a list of filenames or as an
unexpanded glob wildcards that is expanded internally
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
-s SUFFIX, --suffix SUFFIX
Suffix to add to base filename of decrypted files,
appended to stem before any extensions (default:
-decrypted)
-m MATCH_BASENAME, --match_basename MATCH_BASENAME
Regular expression to match base filename of a file,
applied to filename without directory; first capturing
group should be base filename; an error is produced if
files don't match this pattern, which can be ignored
with the `-c` option (default:
(^.*-[0-9\-]+)[.]([a-z]{3})([.a-z]{0,4}))
-p PASSPHRASE, --passphrase PASSPHRASE
Passphrase used to encrypt the backup files, if not
present then user will be prompted to enter one; same
passphrase used for all files. (default: None)
-c, --continue Continue processing next file even if error
encountered; by default script will stop on first
error (default: False)
-v, --verbose Increase logging verbosity, available levels 1 to 3
with `-v` to `-vvv` (default: 0)
最简单的用法:pytibade com.my.favourite.app-20200426-111127.tar.gz
然后,脚本将提示输入密码短语。在
脚本可以通过指定multiple来解密一批文件
inputfiles
或{passphrase
对于所有文件。--continue
选项对于持续尝试
解密所有指定的文件,尽管之前的失败(有用,例如,如果
有加密和未加密文件的混合)。在
钛备份文件格式
信息源
这些信息最初是从克里斯蒂安·爱格的G+页面上的一篇文章中获取的 that has now disappeared。 它已经被David O'Rourke重新格式化为降价。在
文件格式
"TB_ARMOR_V1" '\n'
pass_hmac_key '\n'
pass_hmac_result '\n'
public_key '\n'
enc_privkey_spec '\n'
enc_sesskey_spec '\n'
data
格式说明
5个“变量”(pass_hmac_key
,pass_hmac_result
,
public_key
,enc_privkey_spec
,enc_sesskey_spec
)存储在
不带换行符的Base64格式(当然),可以用以下格式解码:
Base64.decode(pass_hmac_key, Base64.NO_WRAP)
然后,用户提供的密码短语(String
)可以进行如下验证:
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(pass_hmac_key, "HmacSHA1"));
byte[] sigBytes = mac.doFinal(passphrase.getBytes("UTF-8"));
boolean passphraseMatches = Arrays.equals(sigBytes, pass_hmac_result);
然后使用SHA-1独立散列密码短语。我们附加[12]0x00
字节到160位结果,构成256位AES密钥,用于
解密enc_privkey_spec
(使用[16]0x00
字节的IV)。[解密
在CBC模式下使用AES-256并执行块大小为16的PKCS7取消添加。]
然后我们构建KeyPair对象,如下所示:
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey2 = keyFactory.generatePrivate(
new PKCS8EncodedKeySpec(privateKey)
);
PublicKey public_key2 = keyFactory.generatePublic(
new X509EncodedKeySpec(public_key)
);
KeyPair keyPair = new KeyPair(public_key2, privateKey2);
然后我们将会话密钥解密如下:
Cipher rsaDecrypt = Cipher.getInstance("RSA/NONE/PKCS1Padding");
rsaDecrypt.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(baos, rsaDecrypt);
cos.write(enc_sesskey_spec); cos.close();
byte[] sessionKey = baos.toByteArray();
最后,我们用会话密钥(可以是
128位、192位或256位密钥)和0x00
IV.[记住
块大小为16]的PKCS7 unpad。在
虽然从安全角度来看,“零”IV是次优的,但它允许 文件的编码速度更快-因为每一个比特都很重要,尤其是 当我们用LZO压缩存储备份时。在
Author | Tet Woo Lee |
---|---|
Created | 2020-04-26 |
Copyright | © 2020 Tet Woo Lee |
License | GPLv3 |
Dependencies | pycryptodome, tested with v3.8.2 |
更改日志
- 版本1.0.dev1 2020-04-26
工作版本
- 项目
标签: