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
use {
    crate::{errors::*, misc},
    postgres::NoTls,
    reqwest::header,
    serde::de::DeserializeOwned,
    std::{collections::HashSet, time::Duration},
    url::Url,
};

trait IntoSubdomains {
    fn into_subdomains(self) -> HashSet<String>;
}

impl IntoSubdomains for HashSet<String> {
    #[inline]
    fn into_subdomains(self) -> HashSet<String> {
        self
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsCertSpotter {
    dns_names: Vec<String>,
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsCrtsh {
    name_value: String,
}

#[allow(non_snake_case)]
struct SubdomainsDBCrtsh {
    NAME_VALUE: String,
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsVirustotal {
    id: String,
}

#[derive(Deserialize, Eq, PartialEq)]
struct ResponseDataVirusTotal {
    data: HashSet<SubdomainsVirustotal>,
}

impl IntoSubdomains for ResponseDataVirusTotal {
    fn into_subdomains(self) -> HashSet<String> {
        self.data.into_iter().map(|sub| sub.id).collect()
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsFacebook {
    domains: Vec<String>,
}

#[derive(Deserialize, Eq, PartialEq)]
struct ResponseDataFacebook {
    data: HashSet<SubdomainsFacebook>,
}

impl IntoSubdomains for ResponseDataFacebook {
    fn into_subdomains(self) -> HashSet<String> {
        self.data
            .into_iter()
            .flat_map(|sub| sub.domains.into_iter())
            .collect()
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsSpyse {
    name: String,
}
#[derive(Deserialize, Eq, PartialEq, Hash)]
struct ItemsSpyse {
    items: Vec<SubdomainsSpyse>,
}
#[derive(Deserialize, Eq, PartialEq, Hash)]
struct RootSpyseData {
    data: ItemsSpyse,
}

impl IntoSubdomains for RootSpyseData {
    fn into_subdomains(self) -> HashSet<String> {
        self.data.items.into_iter().map(|sub| sub.name).collect()
    }
}

#[derive(Deserialize)]
#[allow(non_snake_case)]
struct SubdomainsBufferover {
    FDNS_A: HashSet<String>,
}

impl IntoSubdomains for SubdomainsBufferover {
    fn into_subdomains(self) -> HashSet<String> {
        self.FDNS_A
            .iter()
            .map(|sub| sub.split(','))
            .flatten()
            .map(str::to_owned)
            .collect()
    }
}

#[derive(Deserialize)]
struct SubdomainsThreatcrowd {
    subdomains: HashSet<String>,
}

impl IntoSubdomains for SubdomainsThreatcrowd {
    fn into_subdomains(self) -> HashSet<String> {
        self.subdomains.into_iter().collect()
    }
}

#[derive(Deserialize)]
struct SubdomainsVirustotalApikey {
    subdomains: HashSet<String>,
}

impl IntoSubdomains for SubdomainsVirustotalApikey {
    fn into_subdomains(self) -> HashSet<String> {
        self.subdomains.into_iter().collect()
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainUrlscan {
    domain: String,
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct PageVecUrlscan {
    page: SubdomainUrlscan,
}

#[derive(Deserialize)]
struct ResponseDataUrlscan {
    results: HashSet<PageVecUrlscan>,
}

impl IntoSubdomains for ResponseDataUrlscan {
    fn into_subdomains(self) -> HashSet<String> {
        self.results
            .into_iter()
            .map(|sub| sub.page.domain)
            .collect()
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsSecurityTrails {
    subdomains: Vec<String>,
}

#[derive(Deserialize)]
struct SubdomainsThreatminer {
    results: HashSet<String>,
}

impl IntoSubdomains for SubdomainsThreatminer {
    fn into_subdomains(self) -> HashSet<String> {
        self.results.into_iter().collect()
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
struct SubdomainsC99 {
    subdomain: String,
}
#[derive(Deserialize, Eq, PartialEq)]
struct ResponseDataC99 {
    subdomains: HashSet<SubdomainsC99>,
}

impl IntoSubdomains for ResponseDataC99 {
    fn into_subdomains(self) -> HashSet<String> {
        self.subdomains
            .into_iter()
            .map(|sub| sub.subdomain)
            .collect()
    }
}

lazy_static! {
    static ref CLIENT: reqwest::blocking::Client = misc::return_reqwest_client();
}

fn get_from_http_api<T: DeserializeOwned + IntoSubdomains>(
    url: &str,
    name: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    match CLIENT.get(url).send() {
        Ok(data) => {
            if misc::check_http_response_code(&name, &data, quiet_flag) {
                match data.json::<T>() {
                    Ok(json) => Some(json.into_subdomains()),
                    Err(e) => {
                        check_json_errors(e, name, quiet_flag);
                        None
                    }
                }
            } else {
                None
            }
        }
        Err(e) => {
            check_request_errors(e, name, quiet_flag);
            None
        }
    }
}

pub fn get_certspotter_subdomains(
    url_api_certspotter: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("CertSpotter")
    }
    match CLIENT.get(url_api_certspotter).send() {
        Ok(data_certspotter) => {
            if misc::check_http_response_code("CertSpotter", &data_certspotter, quiet_flag) {
                match data_certspotter.json::<HashSet<SubdomainsCertSpotter>>() {
                    Ok(domains_certspotter) => Some(
                        domains_certspotter
                            .into_iter()
                            .flat_map(|sub| sub.dns_names.into_iter())
                            .collect(),
                    ),
                    Err(e) => {
                        check_json_errors(e, "CertSpotter", quiet_flag);
                        None
                    }
                }
            } else {
                None
            }
        }
        Err(e) => {
            check_request_errors(e, "CertSpotter", quiet_flag);
            None
        }
    }
}

pub fn get_crtsh_subdomains(url_api_crtsh: &str, quiet_flag: bool) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Crtsh")
    }
    match CLIENT.get(url_api_crtsh).send() {
        Ok(data_crtsh) => {
            if misc::check_http_response_code("Crtsh", &data_crtsh, quiet_flag) {
                match data_crtsh.json::<HashSet<SubdomainsCrtsh>>() {
                    Ok(domains_crtsh) => Some(
                        domains_crtsh
                            .iter()
                            .flat_map(|sub| sub.name_value.split('\n'))
                            .map(str::to_owned)
                            .collect(),
                    ),
                    Err(e) => {
                        check_json_errors(e, "Crtsh", quiet_flag);
                        None
                    }
                }
            } else {
                None
            }
        }
        Err(e) => {
            check_request_errors(e, "Crtsh", quiet_flag);
            None
        }
    }
}

pub fn get_securitytrails_subdomains(
    url_api_securitytrails: &str,
    target: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("SecurityTrails")
    }
    match CLIENT.get(url_api_securitytrails).send() {
        Ok(data_securitytrails) => {
            if misc::check_http_response_code("SecurityTrails", &data_securitytrails, quiet_flag) {
                match data_securitytrails.json::<SubdomainsSecurityTrails>() {
                    Ok(domains_securitytrails) => Some(
                        domains_securitytrails
                            .subdomains
                            .into_iter()
                            .map(|sub| format!("{}.{}", sub, target))
                            .collect(),
                    ),
                    Err(e) => {
                        check_json_errors(e, "SecurityTrails", quiet_flag);
                        None
                    }
                }
            } else {
                None
            }
        }
        Err(e) => {
            check_request_errors(e, "SecurityTrails", quiet_flag);
            None
        }
    }
}

pub fn get_spyse_subdomains(
    url_api_spyse: &str,
    spyse_token: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Spyse")
    }
    match CLIENT
        .get(url_api_spyse)
        .header(header::ACCEPT, "application/json")
        .header(header::AUTHORIZATION, &format!("Bearer {}", spyse_token))
        .send()
    {
        Ok(data) => {
            if misc::check_http_response_code("Spyse", &data, quiet_flag) {
                match data.json::<RootSpyseData>() {
                    Ok(json) => Some(json.into_subdomains()),
                    Err(e) => {
                        check_json_errors(e, "Spyse", quiet_flag);
                        None
                    }
                }
            } else {
                None
            }
        }
        Err(e) => {
            check_request_errors(e, "Spyse", quiet_flag);
            None
        }
    }
}

pub fn get_crtsh_db_subdomains(
    crtsh_db_query: &str,
    url_api_crtsh: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Crtsh database")
    }
    match postgres::config::Config::new()
        .connect_timeout(Duration::from_secs(5))
        .user("guest")
        .host("crt.sh")
        .port(5432)
        .dbname("certwatch")
        .connect(NoTls)
    {
        Ok(mut crtsh_db_client) => match crtsh_db_client.simple_query(crtsh_db_query) {
            Ok(crtsh_db_subdomains) => Some(
                crtsh_db_subdomains
                    .iter()
                    .map(|row| {
                        if let postgres::SimpleQueryMessage::Row(row) = row {
                            let subdomain = SubdomainsDBCrtsh {
                                NAME_VALUE: row.get("NAME_VALUE").unwrap().to_owned(),
                            };
                            subdomain.NAME_VALUE
                        } else {
                            String::new()
                        }
                    })
                    .collect(),
            ),
            Err(e) => {
                if !quiet_flag {
                    println!(
                    "❌ A error has occurred while querying the Crtsh database. Error: {}. Trying the API method...",
                    e);
                }
                get_crtsh_subdomains(&url_api_crtsh, quiet_flag)
            }
        },
        Err(e) => {
            if !quiet_flag {
                println!(
                "❌ A error has occurred while connecting to the Crtsh database. Error: {}. Trying the API method...",
                e
            );
            }
            get_crtsh_subdomains(&url_api_crtsh, quiet_flag)
        }
    }
}

pub fn get_virustotal_subdomains(
    url_api_virustotal: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Virustotal")
    }
    get_from_http_api::<ResponseDataVirusTotal>(url_api_virustotal, "Virustotal", quiet_flag)
}

pub fn get_sublist3r_subdomains(
    url_api_sublist3r: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Sublist3r")
    }
    get_from_http_api::<HashSet<String>>(url_api_sublist3r, "Sublist3r", quiet_flag)
}

pub fn get_facebook_subdomains(url_api_fb: &str, quiet_flag: bool) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Facebook")
    }
    get_from_http_api::<ResponseDataFacebook>(url_api_fb, "Facebook", quiet_flag)
}

pub fn get_anubisdb_subdomains(
    url_api_anubisdb: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("AnubisDB")
    }
    get_from_http_api::<HashSet<String>>(url_api_anubisdb, "AnubisDB", quiet_flag)
}

pub fn get_bufferover_subdomains(
    url_api_bufferover: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Bufferover")
    }
    get_from_http_api::<SubdomainsBufferover>(url_api_bufferover, "Bufferover", quiet_flag)
}

pub fn get_threatcrowd_subdomains(
    url_api_threatcrowd: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Threatcrowd")
    }
    get_from_http_api::<SubdomainsThreatcrowd>(url_api_threatcrowd, "Threatcrowd", quiet_flag)
}

pub fn get_virustotal_apikey_subdomains(
    url_virustotal_apikey: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        println!("Searching in the Virustotal API using apikey... 🔍");
    }
    get_from_http_api::<SubdomainsVirustotalApikey>(
        url_virustotal_apikey,
        "Virustotal API using apikey",
        quiet_flag,
    )
}

pub fn get_urlscan_subdomains(url_api_urlscan: &str, quiet_flag: bool) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Urlscan.io")
    }
    get_from_http_api::<ResponseDataUrlscan>(url_api_urlscan, "Urlscan.io", quiet_flag)
}

pub fn get_threatminer_subdomains(
    url_api_threatminer: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Threatminer")
    }
    get_from_http_api::<SubdomainsThreatminer>(url_api_threatminer, "Threatminer", quiet_flag)
}

pub fn get_c99_subdomains(url_api_c99: &str, quiet_flag: bool) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("C99")
    }
    get_from_http_api::<ResponseDataC99>(url_api_c99, "C99", quiet_flag)
}

pub fn get_archiveorg_subdomains(
    url_api_archiveorg: &str,
    quiet_flag: bool,
) -> Option<HashSet<String>> {
    if !quiet_flag {
        misc::show_searching_msg("Archive.org")
    }
    match reqwest::blocking::Client::builder()
    .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3835.0 Safari/537.36")
    .timeout(std::time::Duration::from_secs(300))
    .build()
    .unwrap()
        .get(url_api_archiveorg)
        .send()
    {
        Ok(data_archiveorg) => {
            if misc::check_http_response_code("Archive.org", &data_archiveorg, quiet_flag) {
                match data_archiveorg.json::<Vec<Vec<String>>>() {
                    Ok(domains_archiveorg) => Some(
                        domains_archiveorg
                            .into_iter()
                            .flatten()
                            .map(|url| match Url::parse(&url) {
                                Ok(host) => host.host_str().unwrap_or_else(|| "").to_string(),
                                _ => String::new(),
                            })
                            .collect()
                   ),
                    Err(e) => {
                        check_json_errors(e, "Archive.org", quiet_flag);
                        None
                    }
                }
            } else {
                None
            }
        }
        Err(e) => {
            check_request_errors(e, "Archive.org", quiet_flag);
            None
        }
    }
}