-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsptula.cs
324 lines (281 loc) · 11.8 KB
/
sptula.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using PuppeteerSharp;
partial class Program
{
private static readonly string[] PuppeteerArgs =
[
"--disable-extensions",
"--disable-gpu",
"--no-sandbox"
];
static readonly HashSet<string> allEmails = new();
static async Task Main(string[] args)
{
Banner.Show();
/* --- *SECTION - Ask Proxy --- */
Class_Color.Console_Colors.WriteWithColor("\nDo you want to use a proxy?: ", ConsoleColor.White);
string? useProxy = Console.ReadLine()?.ToLower();
string? proxy = null;
if (useProxy == "y")
{
Class_Color.Console_Colors.WriteWithColor("Enter proxy (format: type://host:port, e.g., socks5://127.0.0.1:1080): ", ConsoleColor.White);
proxy = Console.ReadLine();
if (string.IsNullOrEmpty(proxy))
{
Class_Color.Console_Colors.WriteWithColor("Invalid proxy settings.", ConsoleColor.Red);
return;
}
bool isProxyValid = await IsProxyValidAsync(proxy);
if (!isProxyValid)
{
Class_Color.Console_Colors.WriteWithColor("The proxy is not valid or not operational.", ConsoleColor.Yellow);
return;
}
}
/* --- *NOTE - Scan from file --- */
Class_Color.Console_Colors.WriteWithColor("\nDo you want to use URLs from a file? (y/n): ", ConsoleColor.White);
string? useFile = Console.ReadLine()?.ToLower();
List<string> urls = [];
if (useFile == "y")
{
Class_Color.Console_Colors.WriteWithColor("Enter the file path: ", ConsoleColor.White);
string? filePath = Console.ReadLine();
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
urls.AddRange(File.ReadAllLines(filePath));
}
else
{
Class_Color.Console_Colors.WriteWithColor("File not found or invalid path.", ConsoleColor.Red);
return;
}
}
else
{
Class_Color.Console_Colors.WriteWithColor("\nEnter url: ", ConsoleColor.White);
string? url = Console.ReadLine();
if (!string.IsNullOrEmpty(url))
{
urls.Add(url);
}
else
{
Class_Color.Console_Colors.WriteWithColor("The URI is empty.", ConsoleColor.Yellow);
return;
}
}
Class_Color.Console_Colors.WriteWithColor("Enter the limit: ", ConsoleColor.White);
if (!int.TryParse(Console.ReadLine(), out int limit))
{
Class_Color.Console_Colors.WriteLineWithColor("Invalid limit, default: 15.", ConsoleColor.Blue);
limit = 15;
}
string chromePath = @"./../../../../../usr/bin/chromium";
if (!File.Exists(chromePath))
{
Class_Color.Console_Colors.WriteLineWithColor($"Chromium not found at {chromePath}", ConsoleColor.Red);
return;
}
var argsList = PuppeteerArgs.ToList();
/* --- Add proxy if needed --- */
if (useProxy == "y" && !string.IsNullOrEmpty(proxy))
{
argsList.Add($"--proxy-server={proxy}");
}
var launchOptions = new LaunchOptions
{
Headless = true,
ExecutablePath = chromePath,
Args = [.. argsList] // Convert argument list back to an array
};
var browser = await Puppeteer.LaunchAsync(launchOptions);
try
{
foreach (string url in urls)
{
var visitedUrls = new HashSet<string>();
var queue = new Queue<string>();
queue.Enqueue(url);
string baseDomain = new Uri(url).Host;
Console.CancelKeyPress += async (sender, e) =>
{
e.Cancel = true;
ShowEmailsFound();
await browser.CloseAsync();
};
int visitedCount = 0;
while (queue.Count > 0 && visitedCount < limit)
{
string? currentUrl = queue.Dequeue();
if (visitedUrls.Contains(currentUrl))
continue;
visitedUrls.Add(currentUrl);
Class_Color.Console_Colors.WriteLineWithColor($"Navigating to: {currentUrl}", ConsoleColor.Cyan);
visitedCount++;
var page = await browser.NewPageAsync();
await page.SetRequestInterceptionAsync(true);
page.Request += async (sender, e) =>
{
if (e.Request.ResourceType == ResourceType.Image ||
e.Request.ResourceType == ResourceType.StyleSheet ||
e.Request.ResourceType == ResourceType.Font)
{
await e.Request.AbortAsync();
}
else
{
await e.Request.ContinueAsync();
}
};
try
{
var navigationOptions = new NavigationOptions
{
Timeout = 29000,
WaitUntil = [ WaitUntilNavigation.Networkidle2 ]
};
await page.GoToAsync(currentUrl, navigationOptions);
string pageSource = await page.GetContentAsync();
List<string> emails = ExtractEmails(pageSource);
foreach (string email in emails)
{
allEmails.Add(email);
}
var hiddenEmails = await page.EvaluateFunctionAsync<string[]>(@"
() => {
let emails = [];
let emailRegex = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
let elements = document.getElementsByTagName('*');
for (let element of elements) {
if (element.textContent.match(emailRegex)) {
let matches = element.textContent.match(emailRegex);
emails.push(...matches);
}
}
return emails;
}
");
foreach (string hiddenEmail in hiddenEmails)
{
allEmails.Add(hiddenEmail);
}
var links = await page.EvaluateExpressionAsync<string[]>("Array.from(document.querySelectorAll('a[href]')).map(a => a.href)");
foreach (string link in links)
{
if (Uri.TryCreate(link, UriKind.Absolute, out Uri? uri) &&
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) &&
IsSameDomain(baseDomain, uri.Host))
{
string normalizedUrl = uri.GetLeftPart(UriPartial.Path);
if (!visitedUrls.Contains(normalizedUrl))
{
queue.Enqueue(normalizedUrl);
}
}
}
}
catch (Exception ex)
{
Class_Color.Console_Colors.WriteLineWithColor($"\nAn error occurred while navigating to {currentUrl}: {ex.Message}", ConsoleColor.Red);
}
finally
{
await page.CloseAsync();
}
}
}
ShowEmailsFound();
/* --- Save Emails --- */
if (allEmails.Count > 0)
{
Class_Color.Console_Colors.WriteWithColor("Do you want to save the emails to a file? (y/n): ", ConsoleColor.Blue);
string? saveToFile = Console.ReadLine()?.ToLower();
if (saveToFile == "y")
{
Class_Color.Console_Colors.WriteWithColor("Enter the file path: ", ConsoleColor.White);
string? filePath = Console.ReadLine();
if (!string.IsNullOrEmpty(filePath))
{
EmailStorage.SaveEmailsToFile(allEmails, filePath);
}
else
{
Class_Color.Console_Colors.WriteWithColor("Invalid file path.", ConsoleColor.Yellow);
}
}
}
}
catch (Exception ex)
{
Class_Color.Console_Colors.WriteLineWithColor($"\nAn error occurred: {ex.Message}", ConsoleColor.DarkRed);
}
finally
{
await browser.CloseAsync();
}
}
static async Task<bool> IsProxyValidAsync(string proxy)
{
try
{
var handler = new HttpClientHandler
{
Proxy = new WebProxy(proxy),
UseProxy = true
};
using var httpClient = new HttpClient(handler);
httpClient.Timeout = TimeSpan.FromSeconds(10); // Time of waiting 10s
// Hacemos una solicitud a un sitio web conocido
var response = await httpClient.GetAsync("http://www.google.com");
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
static List<string> ExtractEmails(string input)
{
var emails = new List<string>();
string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
// Check if the match does not end with a file extension
if (!MyRegex().IsMatch(match.Value))
{
emails.Add(match.Value);
}
}
return emails;
}
static void ShowEmailsFound()
{
if (allEmails.Count > 0)
{
Class_Color.Console_Colors.WriteLineWithColor("\nEmails Found:", ConsoleColor.White);
foreach (string email in allEmails)
{
Class_Color.Console_Colors.WriteLineWithColor($"{email}", ConsoleColor.DarkMagenta);
}
Class_Color.Console_Colors.WriteLineWithColor($"\nTotal Emails Found: {allEmails.Count}", ConsoleColor.White);
}
else
{
Class_Color.Console_Colors.WriteLineWithColor("\nNo emails found.", ConsoleColor.Blue);
}
}
static bool IsSameDomain(string baseDomain, string host)
{
return host.EndsWith(baseDomain) || (baseDomain.StartsWith("www.") && host.EndsWith(baseDomain[4..]));
}
[GeneratedRegex(@"\.(png|webp|jpg|jpeg|gif|pdf|docx|xlsx)$", RegexOptions.IgnoreCase, "")]
private static partial Regex MyRegex();
}