-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParsePlugin.cs
368 lines (303 loc) · 15.2 KB
/
ParsePlugin.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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Metacrack
{
public class ParsePlugin : PluginBase
{
public static void Process(ParseOptions options)
{
var currentDirectory = Directory.GetCurrentDirectory();
var fileEntries = Directory.GetFiles(currentDirectory, options.InputPath);
if (fileEntries.Length == 0)
{
WriteMessage($"File(s) {options.InputPath} not found.");
return;
}
var names = new List<string>();
if (options.ParseType == "log" && options.Names.Count() == 2)
{
foreach (var name in options.Names) names.Add($"{name}:");
}
else
{
names.Add("Username:");
names.Add("Password:");
}
if (options.ParseType == "log") WriteMessage($"Using names {String.Join(",", names)}.");
var size = GetFileEntriesSize(fileEntries);
var progressTotal = 0L;
//Determine columns;
int[] columns = (options.Columns.Count() == 0) ? new int[] { 1 } : Array.ConvertAll(options.Columns.ToArray(), s => int.Parse(s));
int maxColumn = columns.Max();
int[] datecolumns = (options.DateColumns.Count() == 0) ? new int[] { 1 } : Array.ConvertAll(options.DateColumns.ToArray(), s => int.Parse(s));
foreach (var filePath in fileEntries)
{
var output = new List<string>();
var notparsed = new List<string>();
var fileInfo = new FileInfo(filePath);
var dict = new Dictionary<string, int>();
//Create a version based on the file size, so that the hash and dict are bound together
var fileName = Path.GetFileNameWithoutExtension(filePath);
var filePathName = $"{currentDirectory}\\{fileName}";
var outputPath = $"{filePathName}.parse.txt";
var outputNotParsedPath = $"{filePathName}.noparse.txt";
//Check that there are no output files
if (!CheckForFiles(new string[] { outputPath, outputNotParsedPath }))
{
WriteHighlight($"Skipping {filePathName}.");
progressTotal += fileInfo.Length;
continue;
}
var lineCount = 0;
//Loop through and check if each email contains items from the lookup, if so add them
using (var reader = new StreamReader(filePath))
{
var email = "";
//Use a json parser instead
if (options.ParseType.ToLower() == "json")
{
JsonSerializer serializer = new JsonSerializer();
using (JsonReader jr = new JsonTextReader(reader))
{
while (jr.Read())
{
lineCount++;
// deserialize only when there's "{" character in the stream
if (jr.TokenType == JsonToken.StartObject)
{
var o = serializer.Deserialize(jr);
if (o is JObject)
{
var jobject = (JObject)o;
//Try get columns out of jobject
var builder = new StringBuilder();
var skip = false;
foreach (var column in options.ColumnNames)
{
if (jobject.ContainsKey(column))
{
if (builder.Length > 0) builder.Append(":");
builder.Append(jobject[column]);
}
else
{
skip = true;
break;
}
}
if (!skip) output.Add(builder.ToString());
}
}
//Update the percentage
if (lineCount % 1000 == 0) WriteProgress($"Parsing {fileName} as JSON", reader.BaseStream.Position, size);
//Write out buffer
if (output.Count > 1000000)
{
if (output.Count > 0) File.AppendAllLines(outputPath, output);
output.Clear();
}
}
}
//Write final
if (output.Count > 0) File.AppendAllLines(outputPath, output);
continue;
}
while (!reader.EndOfStream)
{
//Read using email hash parser and full text for backwards compatibility
var fullLine = reader.ReadLineAsEmailHash();
var line = fullLine.Text;
lineCount++;
progressTotal += line.Length + 2;
if (options.ParseType.ToLower() == "tsv")
{
var splits = line.Split("\t");
var builder = new StringBuilder();
var skip = false;
foreach (var column in columns)
{
if (splits.Length < column)
{
notparsed.Add(line);
skip = true;
break;
}
if (builder.Length > 0) builder.Append(":");
builder.Append(splits[column]);
}
if (!skip) output.Add(builder.ToString());
}
else if (options.ParseType.ToLower() == "log")
{
//Password must follow line after email, or everythign is reset
if (line.StartsWith(names[0]))
{
var token = line.Split(names[0], StringSplitOptions.TrimEntries)[1];
if (ValidateEmail(token, out var emailSteam))
{
email = emailSteam;
}
else
{
notparsed.Add(line);
}
}
else if (line.StartsWith(names[1]) && email != "")
{
var password = line.Split(names[1], StringSplitOptions.TrimEntries)[1];
output.Add($"{email}:{password}");
email = "";
}
else
{
email = "";
notparsed.Add(line);
}
}
//Split by delimiter
else if (options.ParseType == "delimited")
{
var splits = line.Split(options.Delimiter, StringSplitOptions.TrimEntries);
var values = new List<string>();
if (options.Validate && !ValidateEmail(splits[0], out var emailSteam))
{
notparsed.Add(line);
}
else
{
if (splits.Length > maxColumn)
{
foreach (var column in columns)
{
//Check if we need to parse out the date
if (datecolumns.Contains(column))
{
if (DateOnly.TryParse(splits[column], out var date))
{
//We are only interested in the yyyy year
values.Add(date.Year.ToString());
}
else
{
values.Add("");
}
}
else
{
values.Add(splits[column]);
}
}
output.Add(String.Join(":", values));
}
else
{
notparsed.Add(line);
}
}
}
else if (options.ParseType == "edmodo")
{
var splits = line.Split(':', 2);
if (splits.Length == 2 && splits[1].Length > 64)
{
var hash = splits[1];
var i = 0;
var builder = new System.Text.StringBuilder();
while (i < 66)
{
builder.Append(hash[i]);
i += 2;
}
builder.Append(hash.Substring(65));
var final = builder.ToString();
output.Add($"{splits[0]}:{final}");
}
else
{
notparsed.Add(line);
}
}
else if (options.ParseType == "hextochar")
{
var splits = line.Split(':');
if (splits.Length == 2)
{
var hash = splits[1];
var final = Encoding.UTF8.GetString(FromHex(splits[1]));
output.Add($"{splits[0]}:{final}");
}
else
{
notparsed.Add(line);
}
}
else if (options.ParseType == "vodafone")
{
var splits = line.Split(':');
if (splits.Length == 2)
{
var hash = splits[1];
var final = hash.Substring(0,60);
output.Add($"{splits[0]}:{final}");
}
else
{
notparsed.Add(line);
}
}
//Special top level domain wordlist mode
else if (options.ParseType == "tldw")
{
var splits = line.Split(':');
var tldPlain = splits[columns[1]];
if (ValidateEmail(splits[columns[0]], out var emailStem))
{
//Does the email end with the tld?
if (emailStem.EndsWith(options.Delimiter))
{
output.Add(tldPlain);
}
else
{
notparsed.Add(tldPlain);
}
}
else
{
notparsed.Add(tldPlain);
}
}
//Update the percentage
if (lineCount % 1000 == 0) WriteProgress($"Parsing {fileName}", progressTotal, size);
//Write out buffer
if ((output.Count > 1000000 || notparsed.Count > 1000000) && !options.Deduplicate)
{
if (output.Count > 0) File.AppendAllLines(outputPath, options.Deduplicate ? output.Distinct() : output);
if (notparsed.Count > 0) File.AppendAllLines(outputNotParsedPath, notparsed);
output.Clear();
notparsed.Clear();
}
}
}
//Check if we must deduplicate
if (output.Count > 0) File.AppendAllLines(outputPath, options.Deduplicate ? output.Distinct() : output);
if (notparsed.Count > 0) File.AppendAllLines(outputNotParsedPath, notparsed);
}
}
//private static string DoAdobeKeyFinder(string encryptedBase64, string password)
//{
// TripleDESCryptoServiceProvider desCryptoProvider = new TripleDESCryptoServiceProvider();
// MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider();
// byte[] byteHash;
// byte[] byteBuff;
// desCryptoProvider.Mode = CipherMode.ECB; //CBC, CFB
// byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
// desCryptoProvider.Key = byteHash;
// byteBuff = Encoding.UTF8.GetBytes(password);
// byte[] encoded = desCryptoProvider.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length);
// return encoded;
//}
}
}