作为一个项目的一部分,我正在try 解密Google Chrome中保存的密码.
我知道我需要读取包含密码的Login Data
文件,并使用保存在Local State
文件中的encrypted_key
解密密码.
我的问题是encrypted_key
.我可以从Local State
读取它,但在使用它作为密码之前,我需要解密它.在我搜索时,它是Base64,并使用CryptoProtectData
函数进行保护.所以我想我应该能够用CryptoUnprotectData
函数来对它进行unprotect
运算.但是该函数返回False
并且不起作用.我的代码如下:
#define DPAPI_PREFIX_LEN 5
static char encoding_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/' };
static char* decoding_table = NULL;
void build_decoding_table() {
decoding_table = malloc(256);
for (int i = 0; i < 64; i++)
decoding_table[(unsigned char)encoding_table[i]] = i;
}
unsigned char* base64_decode(const char* data,
SIZE_T input_length,
SIZE_T* output_length) {
if (decoding_table == NULL) build_decoding_table();
if (input_length % 4 != 0) return NULL;
*output_length = input_length / 4 * 3;
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char* decoded_data = malloc(*output_length);
if (decoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t triple = (sextet_a << 3 * 6)
+ (sextet_b << 2 * 6)
+ (sextet_c << 1 * 6)
+ (sextet_d << 0 * 6);
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
}
return decoded_data;
}
int dpapi_decrypt(unsigned char* encText, unsigned long encTextSize, char* decText)
{
DATA_BLOB in;
DATA_BLOB out;
in.pbData = encText;
in.cbData = encTextSize;
if (CryptUnprotectData(&in, encText, NULL, NULL, NULL, 0, &out))
{
for (int i = 0; i < out.cbData; i++)
decText[i] = out.pbData[i];
decText[out.cbData] = '\0';
return 1;
}
int err = GetLastError();
printf("error: %d\n", err);
return 0;
}
int key_decrypt(unsigned char* keyBase64, int keySize, unsigned char* decKey)
{
int key_decoded_len;
unsigned char* key_decoded = base64_decode(keyBase64, keySize, &key_decoded_len);
if (dpapi_decrypt(key_decoded + DPAPI_PREFIX_LEN, (key_decoded_len - DPAPI_PREFIX_LEN), (decKey)))
{
return 1;
}
return 0;
}
dpapi_decrypt
中的第if (CryptUnprotectData(&in, encText, NULL, NULL, NULL, 0, &out))
行函数没有解密密钥,返回False
和err
是13.我真的不知道出了什么问题.有什么主意吗?
我分析了一些具有相同 struct 的Python和CPP代码,它们都可以工作,但我找不到一个合适的C项目可以工作并且可以帮助我.