/
FirefoxSignonsFile.cs
513 lines (431 loc) · 22.3 KB
/
FirefoxSignonsFile.cs
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* WebSiteAdvantage KeePass to Firefox
* Copyright (C) 2008 - 2012 Anthony James McCreath
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using WebSiteAdvantage.KeePass.Firefox.Gecko;
using System.Diagnostics;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace WebSiteAdvantage.KeePass.Firefox
{
/// <summary>
/// represents the content of a firefox signon file
/// </summary>
public class FirefoxSignonsFile
{
#region Construction and Loading
/// <summary>
/// Quick way to load a new file
/// </summary>
/// <param name="profile">Profile with the signon file</param>
public static FirefoxSignonsFile Create(FirefoxProfile profile, string password)
{
FirefoxSignonsFile file = new FirefoxSignonsFile();
file.Load(profile, password);
return file;
}
private Assembly _SQLite = null;
/// <summary>
/// load this object with signon data related to the supplied profile
/// </summary>
/// <param name="profile"></param>
public void Load(FirefoxProfile profile,string password)
{
Profile = profile;
if (Profile.ProfilePath == null)
throw new Exception("Failed to determine the location of the default Firefox Profile");
SECStatus result1;
try
{
result1 = NSS3.NSS_Init(Profile.ProfilePath); // init for profile
}
catch (Exception ex)
{
throw new Exception("Failed to Init Profile (" + Profile.ProfilePath + "): " + ex.Message, ex);
}
if (result1 != SECStatus.Success)
{
Int32 error = NSPR4.PR_GetError();
string errorName = NSPR4.PR_ErrorToName(error);
throw new Exception("Failed to initialise profile for load at " + Profile.ProfilePath + " reason " + errorName);
}
try
{
IntPtr slot = NSS3.PK11_GetInternalKeySlot(); // get a slot to work with
if (slot == IntPtr.Zero)
throw new Exception("Failed to GetInternalKeySlot");
try
{
SECStatus result2 = NSS3.PK11_CheckUserPassword(slot, password);
if (result2 != SECStatus.Success)
{
Int32 error = NSPR4.PR_GetError();
string errorName = NSPR4.PR_ErrorToName(error);
throw new Exception("Failed to Validate Password: " + errorName);
}
string header = null;
string signonFile = null;
string loginsJsonPath = Path.Combine(Profile.ProfilePath, "logins.json");
if (File.Exists(loginsJsonPath))
{
JObject responseJson = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(loginsJsonPath));
foreach (JObject login in responseJson["logins"])
{
FirefoxSignonSite signonSite = null;
string hostname = login["hostname"].ToString();
foreach (FirefoxSignonSite site in this.SignonSites)
{
if (site.Site == hostname)
{
signonSite = site;
break;
}
}
if (signonSite == null)
{
signonSite = new FirefoxSignonSite();
signonSite.Site = hostname;
this.SignonSites.Add(signonSite);
}
FirefoxSignon signon = new FirefoxSignon();
signonSite.Signons.Add(signon);
signon.UserName = NSS3.DecodeAndDecrypt(login["encryptedUsername"].ToString());
signon.UserNameField = login["usernameField"].ToString();
signon.Password = NSS3.DecodeAndDecrypt(login["encryptedPassword"].ToString());
signon.PasswordField = login["passwordField"].ToString();
signon.LoginFormDomain = login["formSubmitURL"].ToString();
Debug.WriteLine(login["id"].ToString());
Debug.WriteLine(login["httpRealm"].ToString()); // null?
Debug.WriteLine(login["guid"].ToString());
Debug.WriteLine(login["encType"].ToString());
Debug.WriteLine(login["timeCreated"].ToString());
Debug.WriteLine(login["timeLastUsed"].ToString());
Debug.WriteLine(login["timePasswordChanged"].ToString());
Debug.WriteLine(login["timesUsed"].ToString());
}
}
else
{
string signonSqlLitePath = Path.Combine(Profile.ProfilePath, "signons.sqlite");
if (File.Exists(signonSqlLitePath))
{
if (_SQLite == null)
{
if (KeePassUtilities.Is64Bit)
{
if (!File.Exists(Application.StartupPath + @"\WebSiteAdvantageKeePassFirefox-SQLite\4x64\System.Data.SQLite.dll"))
throw new Exception("Failed to find " + Application.StartupPath + @"\WebSiteAdvantageKeePassFirefox-SQLite\4x64\System.Data.SQLite.dll Please re-check the installation process");
try
{
_SQLite = Assembly.LoadFrom(Application.StartupPath + @"\WebSiteAdvantageKeePassFirefox-SQLite\4x64\System.Data.SQLite.dll");
}
catch (Exception ex)
{
throw new Exception("Failed to load SQLite 64: " + ex.Message, ex);
}
}
else
{
if (!File.Exists(Application.StartupPath + @"\WebSiteAdvantageKeePassFirefox-SQLite\4x32\System.Data.SQLite.dll"))
throw new Exception("Failed to find " + Application.StartupPath + @"\WebSiteAdvantageKeePassFirefox-SQLite\4x32\System.Data.SQLite.dll Please re-check the installation process");
try
{
_SQLite = Assembly.LoadFrom(Application.StartupPath + @"\WebSiteAdvantageKeePassFirefox-SQLite\4x32\System.Data.SQLite.dll");
}
catch (Exception ex)
{
throw new Exception("Failed to load SQLite 32: " + ex.Message, ex);
}
}
}
IDbConnection connection = (IDbConnection)_SQLite.CreateInstance("System.Data.SQLite.SQLiteConnection");
connection.ConnectionString = "Data Source=" + signonSqlLitePath + ";Version=3;New=False;Compress=True;";
// SQLiteConnection connection = new SQLiteConnection("Data Source=" + signonSqlLitePath + ";Version=3;New=False;Compress=True;");
try
{
connection.Open();
// SQLiteCommand command = new SQLiteCommand("select id, desc from mains", connection);
Type adapterType = _SQLite.GetType("System.Data.SQLite.SQLiteDataAdapter");
IDataAdapter adapter = (IDataAdapter)Activator.CreateInstance(adapterType, new object[] {"SELECT "+
"id, hostname, httpRealm, formSubmitURL, usernameField, passwordField, encryptedUsername, encryptedPassword, guid, encType "+
"FROM moz_logins ORDER BY hostname", connection});
//SQLiteDataAdapter adapter = new SQLiteDataAdapter(
// "SELECT "+
// "id, hostname, httpRealm, formSubmitURL, usernameField, passwordField, encryptedUsername, encryptedPassword, guid, encType "+
// "FROM moz_logins ORDER BY hostname", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
int rowNumber = 0;
foreach (DataRow row in dataSet.Tables[0].Rows)
{
rowNumber++;
FirefoxSignonSite signonSite = null;
//foreach (DataColumn column in dataSet.Tables[0].Columns)
//{
// Debug.WriteLine(column.ColumnName + "=" + (row.IsNull(column) ? "NULL" : row[column].ToString()));
//}
string hostname = row["hostname"].ToString();
if (signonSite == null || signonSite.Site != hostname)
{
signonSite = new FirefoxSignonSite();
signonSite.Site = hostname;
this.SignonSites.Add(signonSite);
};
string getting = "username";
try
{
FirefoxSignon signon = new FirefoxSignon();
signonSite.Signons.Add(signon);
string u = NSS3.DecodeAndDecrypt(row["encryptedUsername"].ToString());
Debug.WriteLine("U=" + u);
signon.UserName = u;
getting = "usernamefield for " + u;
signon.UserNameField = row.IsNull("usernameField") ? String.Empty : row["usernameField"].ToString();
getting = "password for " + u;
string p = NSS3.DecodeAndDecrypt(row["encryptedPassword"].ToString());
Debug.WriteLine("P=" + p);
signon.Password = p;
getting = "passwordfield for " + u;
signon.PasswordField = row.IsNull("passwordField") ? String.Empty : row["passwordField"].ToString();
getting = "formurl for " + u;
signon.LoginFormDomain = row.IsNull("formSubmitURL") ? String.Empty : row["formSubmitURL"].ToString();
}
catch (Exception ex)
{
throw new Exception("Could not get " + getting + " on " + hostname + " : " + ex.Message, ex);
}
}
}
catch (Exception ex)
{
throw new Exception("Failed to Query Firefox: " + ex.Message, ex);
}
finally
{
connection.Close();
}
}
else
{
int version = 3;
while (signonFile == null && version > 0)
{
string signonPath = Path.Combine(Profile.ProfilePath, SignonFileNames[version]);
if (File.Exists(signonPath))
{
signonFile = signonPath;
header = SignonHeaderValues[version];
}
else
version--;
}
Version = version;
if (version == 0)
throw new Exception("Could not find a signon file to process in " + Profile.ProfilePath);
StreamReader reader = File.OpenText(signonFile);
try
{
// first line is header
string line = reader.ReadLine();
if (line == null)
throw new Exception("signon file is empty");
if (line != header)
throw new Exception("signon file contains an invalid header");
// lines till the first dot are host excludes
while ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
Debug.WriteLine("ExcludeHost: " + line);
this.ExcludeHosts.Add(line);
}
FirefoxSignonSite signonSite = null;
// read each entry
while (line != null)
{
Debug.WriteLine("## " + line);
// here after any dot (.) therefore new site
// all new lines pass through if they contain a dot (.)
signonSite = null;
while ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
// first line is host
// subsequent lines are pairs of name value
// if name starts with * then its a password
// values are encrypted
if (signonSite == null) // site is reset to null after each dot (.)
{
signonSite = new FirefoxSignonSite();
signonSite.Site = line;
this.SignonSites.Add(signonSite);
Debug.WriteLine("Site: " + line);
line = reader.ReadLine(); // move to the next line
Debug.WriteLine("# " + line);
}
// else stick to the same line for the next parser, second site entries dont have a dot (.) nor site line
if (line != null && line != ".")
{
FirefoxSignon signon = new FirefoxSignon();
signonSite.Signons.Add(signon);
// User field
signon.UserNameField = line;
Debug.WriteLine("UserNameField: " + signon.UserNameField);
if ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
// User Value
string u = NSS3.DecodeAndDecrypt(line);
signon.UserName = u;
Debug.WriteLine("UserName: " + signon.UserName);
if ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
// Password field
signon.PasswordField = line.TrimStart(new char[] { '*' });
Debug.WriteLine("PasswordField: " + signon.PasswordField);
if ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
// Password Value
string p = NSS3.DecodeAndDecrypt(line);
signon.Password = p;
Debug.WriteLine("Password: " + signon.Password);
if ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
// Domain
signon.LoginFormDomain = line;
Debug.WriteLine("LoginFormDomain: " + signon.LoginFormDomain);
if ((line = reader.ReadLine()) != null && line != ".")
{
Debug.WriteLine("# " + line);
// Filler
Debug.WriteLine("Filler: " + line);
}
// note: if there is not a dot (.) after this then its a subsequent entry for the same site
}
}
}
}
}
}
}
}
finally
{
reader.Close();
}
}
}
}
finally
{
NSS3.PK11_FreeSlot(slot);
}
}
finally
{
NSS3.NSS_Shutdown();
}
}
#endregion
#region Related Profile
private FirefoxProfile _Profile;
/// <summary>
/// profile related to the signon file
/// </summary>
public FirefoxProfile Profile
{
get { return _Profile; }
set { _Profile = value; }
}
#endregion
#region File Data
private int _Version = 0;
/// <summary>
/// The version of the file
/// </summary>
public int Version
{
get { return _Version; }
set { _Version = value; }
}
private List<FirefoxSignonSite> _SignonSites = new List<FirefoxSignonSite>();
/// <summary>
/// Collection of singons that were stored in the file
/// </summary>
public List<FirefoxSignonSite> SignonSites
{
get { return _SignonSites; }
}
private List<String> _ExcludeHosts = new List<string>();
/// <summary>
/// hosts that have been excluded
/// </summary>
public List<String> ExcludeHosts
{
get { return _ExcludeHosts; }
}
#endregion
#region Information on Versions
private static string[] _SignonFileNames = null;
/// <summary>
/// the file name used for each version of firefox
/// </summary>
public static string[] SignonFileNames
{
get
{
if (_SignonFileNames == null)
{
_SignonFileNames = new string[4];
_SignonFileNames[0] = null;
_SignonFileNames[1] = "signons.txt";
_SignonFileNames[2] = "signons2.txt";
_SignonFileNames[3] = "signons3.txt";
}
return _SignonFileNames;
}
}
private static string[] _SignonHeaderValues = null;
/// <summary>
/// the headers used for each version of firefox
/// </summary>
public static string[] SignonHeaderValues
{
get
{
if (_SignonHeaderValues == null)
{
_SignonHeaderValues = new string[4];
_SignonHeaderValues[0] = null;
_SignonHeaderValues[1] = "#2c";
_SignonHeaderValues[2] = "#2d";
_SignonHeaderValues[3] = "#2e";
}
return _SignonHeaderValues;
}
}
#endregion
}
}