2011-09-01 82 views
4

我想寫一個簡單的實用程序,從Firefox密碼數據庫中提取密碼(相應文件在配置文件夾中稱爲signons.sqlite)。解密Firefox密碼數據庫

我到目前爲止所做的:使用sqlite打開的數據庫,檢索的加密用戶名,加密的密碼和網站地址(全部存儲爲std::string)。

所以,唯一剩下的就是解密用戶名和密碼字符串。

我嘗試以下(PK11Decrypt應該存儲在plaintext明文密碼):

void Firefox_Importer::PK11Decrypt(string _cipheredBuffer, char **plaintext) { 
    // declarations needed 
    SECItem * request; 
    SECItem * reply; 
    unsigned int len = (unsigned int)_cipheredBuffer.length(); 
    const char* cipheredBuffer = (const char*)_cipheredBuffer.c_str(); 

    // generate request and reply SECItem; seems to work properly 
    reply = SECITEM_AllocItem(NULL, NULL, 0); 
    if (reply == NULL) cout << "Error allocating SECITEM." << endl; 
    request = NSSBase64_DecodeBuffer(NULL, NULL, cipheredBuffer, len); 
    if (request == NULL) cout << "Error decoding buffer." << endl; 

    // the following is not working 
    SECStatus tmp = PK11SDR_Decrypt(request, reply, NULL); 
    if(tmp != SECSuccess) cout << "Something went wrong during decrypting" << endl; 

    *plaintext = (char*)malloc(reply->len + 1); 
    strncpy(*plaintext, (const char*)reply->data, reply->len); 
    (*plaintext)[reply->len] = '\0'; 

    SECITEM_FreeItem(request, true); 
    SECITEM_FreeItem(reply, true); 
} 

PK11Decrypt被調用時,它打印Something went wrong during decrypting,表明調用PK11SDR_Decrypt沒有正常工作。它總是返回SECFailure(對應於-1)。

有沒有人有一些提示或知道我在做什麼錯?

回答

2

即使沒有設置主密碼,也可能是PK11_Authenticate()的呼叫不是可選的,即使沒有設置主密碼(是的,NSS相當混亂)。所以,你可能首先需要做以下幾點:

PK11SlotInfo *slot = PK11_GetInternalKeySlot(); 
if (!slot) cout << "Error getting internal slot" << endl; 

SECStatus tmp = PK11_Authenticate(slot, PR_TRUE, NULL); 
if (tmp != SECSuccess) cout << "Authentication error" << endl; 

注意,我通過NULL的上下文PK11_Authenticate(),是否應該顯示密碼提示的情況下,才需要。

編輯:沒關係,我注意到PK11SDR_Decrypt()將在內部調用這兩個函數。鑑於您得到SECFailure作爲結果,很可能PK11_GetInternalKeySlot()失敗,這將表明NSS未正確初始化。

+0

感謝您的提示。其實我犯了一個愚蠢的錯誤,忘記調用'NSS_Init'。謝謝。 – phimuemue

+0

@AmitKhandelwal:假設您使用的是NSS庫,該代碼可以在任何操作系統上使用。如果你只是想訪問你的機器上的數據庫,你最好在superuser.com上提出一個新問題。 –

+0

如果設置了主密碼,該怎麼辦?可以使用PK11SDR_Decrypt以及已知的主密碼嗎? –

2

Firefox是開源軟件。你可以找到最近的來源here,它是由你來找到他們解密密碼並將其複製到你的應用程序的部分。祝你好運。

+0

這是 - 原則上 - 一個好點。然而,我試圖在http://mxr.mozilla.org/firefox/source/security/manager/ssl/src/nsSDR.cpp#179上構建這個東西,但沒有運氣,因此我在這裏問了一下。 – phimuemue

+0

您可能需要先初始化它。只要看看他們是如何在源頭上做到這一點就不難。 – Listing

+0

「他們解密密碼的部分」是[nsSecretDecoderRing](http://hg.mozilla.org/mozilla-central/file/7d3d1c2c75f8/security/manager/ssl/src/nsSDR.cpp#l249),它在乍一看至少和問題中的代碼一樣。 –