Firefox 27:NSS_Init失败

时间:2014-03-11 11:22:35

标签: firefox nss

我正在使用nss3.dll进行密码恢复工具。我已加载nss3.dll但NSS_Init函数失败。这是我的代码。

typedef enum _SECStatus {
    SECWouldBlock = -2,
    SECFailure = -1,
    SECSuccess = 0
} SECStatus;
typedef struct SECItemStr SECItem;
typedef struct PLArenaPool      PLArenaPool;
typedef SECStatus      (*NSS_Init) (const char *configdir);
void init()
{
   HINSTANCE LoadMe ;
   LoadMe = LoadLibrary(L"nss3.dll"); //Library gets loaded successfully.

   NSS_Init                NSSInit1;
   NSSInit1               = (NSS_Init) GetProcAddress(LoadMe, "NSS_Init");

   const char * path = "C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\0rhq2lpu.default";

  SECStatus result = (*NSSInit1) (path);
  if( result != SECSuccess )
  {
  // fails here..  
    return ;
  }

 //Here will code of password decryption...
}

我总是在调用NSS_Init时获得SECFailure。

2 个答案:

答案 0 :(得分:1)

在我的代码中,NSS_Init方法无效,因为我没有从路径C:\Program Files\Mozilla Firefox\nss3.dll加载nss3.dll 我正在将nss3.dll复制到我的解决方案文件夹并加载它。因为nss3.dll需要其他支持dll才能工作。

这是我的工作代码。

#include<Windows.h>
#include<atlenc.h>


typedef enum {
    siBuffer = 0,
    siClearDataBuffer = 1,
    siCipherDataBuffer = 2,
    siDERCertBuffer = 3,
    siEncodedCertBuffer = 4,
    siDERNameBuffer = 5,
    siEncodedNameBuffer = 6,
    siAsciiNameString = 7,
    siAsciiString = 8,
    siDEROID = 9,
    siUnsignedInteger = 10,
    siUTCTime = 11,
    siGeneralizedTime = 12,
    siVisibleString = 13,
    siUTF8String = 14,
    siBMPString = 15
} SECItemType;

typedef struct SECItemStr SECItem;

struct SECItemStr {
    SECItemType type;
    unsigned char *data;
    unsigned int len;
};

typedef enum _SECStatus {
    SECWouldBlock = -2,
    SECFailure = -1,
    SECSuccess = 0
} SECStatus;

typedef int PRBool;
typedef unsigned int PRUint32;
/* /security/nss/lib/pk11wrap/secmodti.h */
/* /security/nss/lib/softoken/secmodt.h*/
/* typedef struct PK11SlotInfoStr PK11SlotInfo; */
typedef void PK11SlotInfo;      /* self defined */

/* /security/nss/lib/nss/nss.h */
typedef SECStatus (__cdecl *NSS_InitFunc)(const char *configdir);
typedef SECStatus (__cdecl *NSS_ShutdownFunc)(void);
/* /security/nss/lib/pk11wrap/pk11pub.h */
typedef PK11SlotInfo *(__cdecl *PK11_GetInternalKeySlotFunc)(void);
typedef void (__cdecl *PK11_FreeSlotFunc)(PK11SlotInfo *slot);
typedef SECStatus (__cdecl *PK11_AuthenticateFunc)(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
/* /security/nss/lib/pk11wrap/pk11sdr.h */
typedef SECStatus (__cdecl *PK11SDR_DecryptFunc)(SECItem *data, SECItem *result, void *cx);
/* /security/nss/lib/pk11wrap/pk11pub.h */
typedef SECStatus (__cdecl *PK11_CheckUserPasswordFunc)(PK11SlotInfo *slot, const char *pw);
/* /security/nss/lib/util/secitem.h */
typedef void (__cdecl *SECITEM_ZfreeItemFunc)(SECItem *zap, PRBool freeit);

typedef void (*SECITEM_AllocItem)(SECItem & item, int len);


void decryptFirefoxPassword(char *dest,const char *encoded_cred)
{
    // Check to see if the library was loaded successfully 
    HINSTANCE nss3_handle;
    HINSTANCE glueLib;

    glueLib = LoadLibrary(L"C:\\Program Files\\Mozilla Firefox\\mozglue.dll");
    nss3_handle = LoadLibrary(L"C:\\Program Files\\Mozilla Firefox\\nss3.dll");
    // Check to see if the library was loaded successfully 
    if(glueLib && nss3_handle)
    {
        SECStatus init_status;
        if ((NSS_Init = (NSS_InitFunc)
            GetProcAddress(nss3_handle, "NSS_Init")) &&
            (NSS_Shutdown = (NSS_ShutdownFunc)
            GetProcAddress(nss3_handle, "NSS_Shutdown")) &&
            (PK11_GetInternalKeySlot = (PK11_GetInternalKeySlotFunc)
            GetProcAddress(nss3_handle, "PK11_GetInternalKeySlot")) &&
            (PK11_FreeSlot = (PK11_FreeSlotFunc)
            GetProcAddress(nss3_handle, "PK11_FreeSlot")) &&
            (PK11_Authenticate = (PK11_AuthenticateFunc)
            GetProcAddress(nss3_handle, "PK11_Authenticate")) &&
            (PK11SDR_Decrypt = (PK11SDR_DecryptFunc)
            GetProcAddress(nss3_handle, "PK11SDR_Decrypt")) &&
            (PK11_CheckUserPassword = (PK11_CheckUserPasswordFunc)
            GetProcAddress(nss3_handle, "PK11_CheckUserPassword")) &&
            (SECITEM_ZfreeItem = (SECITEM_ZfreeItemFunc)
            GetProcAddress(nss3_handle, "SECITEM_ZfreeItem")))
        {
            init_status = NSS_Init(m_strDefaultProfilePath.toStdString().c_str());

            if(init_status == SECSuccess)
            {
                size_t cred_len;
                int pnDestLen=MAX_CRED_LENGTH;
                unsigned char decoded_cred[MAX_CRED_LENGTH];

                PK11SlotInfo *slot;


                cred_len = strlen(encoded_cred);
                if (! Base64Decode(encoded_cred,cred_len,decoded_cred,&pnDestLen))
                    return;

                if (!(slot = PK11_GetInternalKeySlot()))
                    return;
                if (PK11_Authenticate(slot, TRUE, NULL) == SECSuccess)
                {
                    SECItem data, result;
                    result.data = NULL;
                    result.len = 0;
                    data.data = decoded_cred;
                    data.len = decoded_size(encoded_cred);

                    if (PK11SDR_Decrypt(&data, &result, NULL) == SECSuccess) 
                    {
                        strncpy_s(dest, MAX_CRED_LENGTH, (char *)result.data, result.len);
                        SECITEM_ZfreeItem(&result, FALSE);
                    }
                }
                PK11_FreeSlot(slot);
            }
        }
    }
}

答案 1 :(得分:0)

老问题,但由于我有同样的问题,请记住,除了OP的问题,你应该记住,你可能会遇到同样的问题,因为:

1)您可能需要管理员权限才能加载dll。

2)您的程序需要与dll具有相同的位。例如,如果您的程序是32位且firefox的nss3.dll是64位,则库加载将失败。

我希望这可以节省功能读者的时间。