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
use crate::errors::*;

use crate::blobs::Blob;
use crate::cmd::Cmd;
use crate::gfx;
use crate::models::*;
use crate::shell::Shell;
use structopt::StructOpt;
use structopt::clap::AppSettings;
use crate::utils;
use crate::term;
use std::fs;
use std::net;
use ipnetwork;
use std::path::Path;
use walkdir::WalkDir;

#[derive(Debug, StructOpt)]
#[structopt(global_settings = &[AppSettings::ColoredHelp])]
pub struct Args {
    #[structopt(subcommand)]
    subcommand: Target,
    /// Do not actually insert into database
    #[structopt(short="n", long="dry-run")]
    dry_run: bool,
}

#[derive(Debug, StructOpt)]
pub enum Target {
    /// Insert domain into the database
    #[structopt(name="domain")]
    Domain(AddDomain),
    /// Insert subdomain into the database
    #[structopt(name="subdomain")]
    Subdomain(AddSubdomain),
    /// Insert ip address into the database
    #[structopt(name="ipaddr")]
    IpAddr(AddIpAddr),
    /// Insert url into the database
    #[structopt(name="subdomain")]
    Url(AddUrl),
    /// Insert email into the database
    #[structopt(name="email")]
    Email(AddEmail),
    /// Insert phonenumber into the database
    #[structopt(name="phonenumber")]
    PhoneNumber(AddPhoneNumber),
    /// Insert device into the database
    #[structopt(name="device")]
    Device(AddDevice),
    /// Insert network into the database
    #[structopt(name="network")]
    Network(AddNetwork),
    /// Insert account into the database
    #[structopt(name="account")]
    Account(AddAccount),
    /// Insert breach into the database
    #[structopt(name="breach")]
    Breach(AddBreach),
    /// Insert images into the database
    #[structopt(name="image")]
    Image(AddImage),
    /// Insert ip network into the database
    #[structopt(name="netblock")]
    Netblock(AddNetblock),
    /// Insert a crypto currency address into the database
    #[structopt(name="cryptoaddr")]
    CryptoAddr(AddCryptoAddr),
}

impl Cmd for Args {
    fn run(self, rl: &mut Shell) -> Result<()> {
        match self.subcommand {
            Target::Domain(args) => args.insert(rl, self.dry_run),
            Target::Subdomain(args) => args.insert(rl, self.dry_run),
            Target::IpAddr(args) => args.insert(rl, self.dry_run),
            Target::Url(args) => args.insert(rl, self.dry_run),
            Target::Email(args) => args.insert(rl, self.dry_run),
            Target::PhoneNumber(args) => args.insert(rl, self.dry_run),
            Target::Device(args) => args.insert(rl, self.dry_run),
            Target::Network(args) => args.insert(rl, self.dry_run),
            Target::Account(args) => args.insert(rl, self.dry_run),
            Target::Breach(args) => args.insert(rl, self.dry_run),
            Target::Image(args) => args.insert(rl, self.dry_run),
            Target::Netblock(args) => args.insert(rl, self.dry_run),
            Target::CryptoAddr(args) => args.insert(rl, self.dry_run),
        }
    }
}

trait IntoInsert: Sized {
    fn into_insert(self, rl: &mut Shell) -> Result<Insert>;

    fn insert(self, rl: &mut Shell, dry_run: bool) -> Result<()> {
        let insert = self.into_insert(rl)?;
        if !dry_run {
            rl.db().insert_generic(insert)?;
        }
        Ok(())
    }
}

#[derive(Debug, StructOpt)]
pub struct AddDomain {
    domain: Option<String>,
}

impl IntoInsert for AddDomain {
    fn into_insert(self, rl: &mut Shell) -> Result<Insert> {
        let domain = match self.domain {
            Some(domain) => domain,
            _ => utils::question("Domain")?,
        };

        // ensure input is a valid domain
        let dns_name = rl.psl()?.parse_dns_name(&domain)
            .map_err(|e| format_err!("Failed to parse domain: {}", e))?;

        if dns_name.fulldomain.is_some() {
            bail!("Domain has an unexpected subdomain, add as a subdomain instead");
        }

        Ok(Insert::Domain(NewDomain {
            value: domain,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddSubdomain {
    subdomain: Option<String>,
}

impl IntoInsert for AddSubdomain {
    fn into_insert(self, rl: &mut Shell) -> Result<Insert> {
        let subdomain = match self.subdomain {
            Some(subdomain) => subdomain,
            _ => utils::question("Subdomain")?,
        };

        let dns_name = rl.psl()?.parse_dns_name(&subdomain)
            .map_err(|e| format_err!("Failed to parse dns_name: {}", e))?;

        let domain_id = match rl.db().insert_struct(NewDomain {
            value: dns_name.root,
            unscoped: false,
        }, true)? {
            Some((_, domain_id)) => domain_id,
            _ => bail!("Domain is out out of scope"),
        };

        Ok(Insert::Subdomain(NewSubdomain {
            domain_id,
            value: subdomain,
            resolvable: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddIpAddr {
    ipaddr: Option<net::IpAddr>,
}

impl IntoInsert for AddIpAddr {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let ipaddr = match self.ipaddr {
            Some(ipaddr) => ipaddr,
            _ => {
                let ipaddr = utils::question("IP address")?;
                ipaddr.parse()?
            },
        };

        let family = match ipaddr {
            net::IpAddr::V4(_) => "4",
            net::IpAddr::V6(_) => "6",
        };

        Ok(Insert::IpAddr(NewIpAddr {
            family: family.to_string(),
            value: ipaddr.to_string(),
            continent: None,
            continent_code: None,
            country: None,
            country_code: None,
            city: None,
            latitude: None,
            longitude: None,
            asn: None,
            as_org: None,
            description: None,
            reverse_dns: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddUrl {
    url: Option<String>,
}

impl IntoInsert for AddUrl {
    fn into_insert(self, rl: &mut Shell) -> Result<Insert> {
        let url = match self.url {
            Some(url) => url,
            _ => utils::question("URL")?,
        };

        let parts = url::Url::parse(&url)?;
        let subdomain = parts.domain()
            .ok_or_else(|| format_err!("url doesn't have a domain host"))?;

        let dns_name = rl.psl()?.parse_dns_name(&subdomain)
            .map_err(|e| format_err!("Failed to parse dns_name: {}", e))?;

        let domain_id = match rl.db().insert_struct(NewDomain {
            value: dns_name.root,
            unscoped: false,
        }, true)? {
            Some((_, domain_id)) => domain_id,
            _ => bail!("Domain is out out of scope"),
        };

        let subdomain_id = match rl.db().insert_struct(NewSubdomain {
            value: subdomain.to_string(),
            domain_id,
            resolvable: None,
            unscoped: false,
        }, true)? {
            Some((_, subdomain_id)) => subdomain_id,
            _ => bail!("Subdomain is out out of scope"),
        };

        Ok(Insert::Url(InsertUrl {
            subdomain_id,
            value: url,
            status: None,
            body: None,
            online: None,
            title: None,
            redirect: None,
        }.try_into_new()?))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddEmail {
    email: Option<String>,
}

impl IntoInsert for AddEmail {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let email = match self.email {
            Some(email) => email,
            _ => utils::question("Email")?,
        };

        Ok(Insert::Email(NewEmail {
            value: email,
            displayname: None,
            valid: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddPhoneNumber {
    phonenumber: Option<String>,
    name: Option<String>,
}

impl IntoInsert for AddPhoneNumber {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let (phonenumber, name) = match self.phonenumber {
            Some(phonenumber) => (phonenumber, self.name),
            _ => {
                let phonenumber = utils::question("Phone Number")?;
                let name = utils::question_opt("Name")?;
                (phonenumber, name)
            },
        };

        Ok(Insert::PhoneNumber(NewPhoneNumber {
            value: phonenumber,
            name: name,
            valid: None,
            last_online: None,
            country: None,
            carrier: None,
            line: None,
            is_ported: None,
            last_ported: None,
            caller_name: None,
            caller_type: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddDevice {
    mac: Option<String>,
    name: Option<String>,
}

impl IntoInsert for AddDevice {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let (mac, name) = match self.mac {
            Some(mac) => {
                (mac, self.name)
            },
            _ => {
                let mac = utils::question("Mac address")?;
                let name = utils::question_opt("Name")?;
                (mac, name)
            },
        };

        Ok(Insert::Device(NewDevice {
            value: mac,
            name: name,
            hostname: None,
            vendor: None,
            last_seen: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddNetwork {
    network: Option<String>,
    latitude: Option<f32>,
    longitude: Option<f32>,
}

impl IntoInsert for AddNetwork {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let (network, latitude, longitude) = match self.network {
            Some(network) => (network, self.latitude, self.longitude),
            _ => {
                let network = utils::question("Network")?;
                let latitude = utils::question_typed_opt("Latitude")?;
                let longitude = utils::question_typed_opt("Longitude")?;
                (network, latitude, longitude)
            }
        };

        Ok(Insert::Network(NewNetwork {
            value: network,
            latitude,
            longitude,
            description: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddAccount {
    service: Option<String>,
    username: Option<String>,
}

impl IntoInsert for AddAccount {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let service = if let Some(service) = self.service {
            service
        } else {
            utils::question("Service")?
        };

        if service.contains('/') {
            bail!("Service field can't contain `/`");
        }

        let username = if let Some(username) = self.username {
            username
        } else {
            utils::question("Username")?
        };

        let value = format!("{}/{}", service, username);

        Ok(Insert::Account(NewAccount {
            value: value,
            service: service,
            username: username,
            displayname: None,
            email: None,
            url: None,
            last_seen: None,
            birthday: None,
            phonenumber: None,
            profile_pic: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddBreach {
    name: Option<String>,
}

impl IntoInsert for AddBreach {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let name = match self.name {
            Some(name) => name,
            _ => {
                let name = utils::question("Name")?;
                name
            }
        };

        Ok(Insert::Breach(NewBreach {
            value: name,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddImage {
    paths: Vec<String>,
}

impl IntoInsert for AddImage {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        unreachable!()
    }

    fn insert(self, rl: &mut Shell, dry_run: bool) -> Result<()> {
        let paths = if self.paths.is_empty() {
            let path = utils::question("Path")?;
            vec![path]
        } else {
            self.paths
        };

        for path in paths {
            for path in WalkDir::new(path) {
                let path = match path {
                    Ok(path) => path,
                    Err(err) => {
                        let path = err.path().unwrap_or(Path::new("")).display();

                        let err = err.io_error()
                            .map(|err| err.to_string())
                            .unwrap_or_else(|| String::from("walkdir failed"));
                        term::error(&format!("Failed to access entry {:?}: {}", path, err));

                        continue;
                    },
                };

                if path.file_type().is_dir() {
                    debug!("Traversing into directory: {:?}", path);
                    continue;
                }

                debug!("Testing: {:?}", path.path());

                let data = match fs::read(path.path()) {
                    Ok(data) => data,
                    Err(err) => {
                        term::error(&format!("Failed to read {:?}: {}", path, err));
                        continue;
                    },
                };

                // check if image
                let format = match gfx::guess_format(&data) {
                    Ok(format) => format.mime().to_string(),
                    _ => {
                        debug!("Probably not an image, skipping");
                        continue;
                    },
                };
                debug!("Detected image format: {:?}", format);

                let blob = Blob::create(data.into());
                if !dry_run {
                    rl.blobs().save(&blob)?;
                }
                let value = blob.id;

                let filename = path.file_name()
                    .to_string_lossy()
                    .to_string();

                term::info(&format!("{} {:?}", value, path.path()));

                if !dry_run {
                    rl.db().insert_generic(Insert::Image(NewImage {
                        value,

                        filename: Some(filename),
                        mime: Some(format),
                        width: None,
                        height: None,
                        created: None,

                        latitude: None,
                        longitude: None,

                        nudity: None,
                        ahash: None,
                        dhash: None,
                        phash: None,

                        unscoped: false,
                    }))?;
                }
            }
        }

        Ok(())
    }
}

#[derive(Debug, StructOpt)]
pub struct AddNetblock {
    ipnet: Option<ipnetwork::IpNetwork>,
}

impl IntoInsert for AddNetblock {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let ipnet = match self.ipnet {
            Some(ipnet) => ipnet,
            _ => {
                let ipnet = utils::question("IP network")?;
                ipnet.parse()?
            },
        };

        let family = match ipnet {
            ipnetwork::IpNetwork::V4(_) => "4",
            ipnetwork::IpNetwork::V6(_) => "6",
        };

        Ok(Insert::Netblock(NewNetblock {
            family: family.to_string(),
            value: ipnet.to_string(),
            asn: None,
            as_org: None,
            description: None,
            unscoped: false,
        }))
    }
}

#[derive(Debug, StructOpt)]
pub struct AddCryptoAddr {
    address: Option<String>,
}

impl IntoInsert for AddCryptoAddr {
    fn into_insert(self, _rl: &mut Shell) -> Result<Insert> {
        let address = if let Some(address) = self.address {
            address
        } else {
            utils::question("Address")?
        };

        Ok(Insert::CryptoAddr(NewCryptoAddr {
            value: address,
            currency: None,
            denominator: None,
            balance: None,
            received: None,
            first_seen: None,
            last_withdrawal: None,
            unscoped: false,
            description: None,
        }))
    }
}