1

我正在使用 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。

4

2 回答 2

1

在我的代码中 NSS_Init 方法不起作用,因为我没有从路径加载 nss3.dllC:\Program Files\Mozilla Firefox\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);
            }
        }
    }
}
于 2014-03-14T08:13:31.450 回答
1

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

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

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

我希望这将节省专题读者的时间。

于 2018-03-25T15:56:44.487 回答