Skip to content

Commit

Permalink
Use a static name for output files.
Browse files Browse the repository at this point in the history
Before it commit Findomain was using a random name for writing to output files, now the output file name is just the name of the target. It mean that you can run Findomain with the -o flag and automatize your workflow easily.

A example of that is just: `findomain -t example.com -o && cat example.com | httprobe` in order to use the httprobe tool.

Fix #12.
  • Loading branch information
Edu4rdSHL committed Sep 16, 2019
1 parent bf4d8c1 commit 8f41477
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 63 deletions.
5 changes: 2 additions & 3 deletions src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ args:
- output:
short: o
long: output
help: Write data to output file in the specified format.
takes_value: true
possible_values: [ txt, csv, json ]
help: Set the flag to write to an output file. The name of the out file is the name of the target, for example if your target is example.com the output file name is example.com. It also applies when findomain read targets from a file.
takes_value: false
89 changes: 46 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ struct SubdomainsVirustotalApikey {
}

lazy_static! {
static ref RNUM: String = rand::thread_rng().gen_range(0, 10000).to_string();
static ref CLIENT: reqwest::Client = reqwest::Client::builder()
.timeout(Duration::from_secs(20))
.build()
.unwrap();
}

pub fn get_subdomains(target: &str, with_ip: &str, with_output: &str, file_format: &str) {
pub fn get_subdomains(target: &str, with_ip: &str, with_output: &str, file_name: &str) {
let target = target
.replace("www.", "")
.replace("https://", "")
Expand Down Expand Up @@ -195,19 +194,17 @@ pub fn get_subdomains(target: &str, with_ip: &str, with_output: &str, file_forma
&target,
&with_ip,
&with_output,
&file_format,
&file_name,
);
if with_ip == "y" && with_output == "y" {
let filename = [&target, "_", &RNUM.to_string(), "-ip", ".", file_format].concat();
println!(
">> 📁 Filename for the target {} was saved in: ./{} 😀",
&target, &filename
&target, &file_name
)
} else if with_output == "y" {
let filename = [&target, "_", &RNUM.to_string(), ".", file_format].concat();
println!(
">> 📁 Filename for the target {} was saved in: ./{} 😀",
&target, &filename
&target, &file_name
)
}
}
Expand All @@ -217,7 +214,7 @@ fn manage_subdomains_data(
target: &str,
with_ip: &str,
with_output: &str,
file_format: &str,
file_name: &str,
) {
let base_target = [".", &target].concat();
if vec_subdomains.is_empty() {
Expand All @@ -237,14 +234,14 @@ fn manage_subdomains_data(
for subdomain in vec_subdomains {
if with_ip == "y" && with_output == "y" {
let ipadress = get_ip(&subdomain);
write_to_file(&subdomain, &target, &ipadress, &file_format, &with_ip);
write_to_file(&subdomain, &ipadress, &file_name, &with_ip);
println!("{},{}", &subdomain, &ipadress);
} else if with_ip == "y" {
let ipadress = get_ip(&subdomain);
println!("{},{}", &subdomain, &ipadress);
} else if with_output == "y" {
let ipadress = "";
write_to_file(&subdomain, &target, &ipadress, &file_format, &with_ip);
write_to_file(&subdomain, &ipadress, &file_name, &with_ip);
println!("{}", &subdomain);
} else {
println!("{}", &subdomain);
Expand Down Expand Up @@ -520,71 +517,60 @@ fn check_json_errors(error: reqwest::Error, api: &str) {
println!("An error ❌ has occurred while parsing the JSON obtained from the {} API. Error description: {}.\n", &api, error.description())
}

pub fn read_from_file(file: &str, with_ip: &str, with_output: &str, file_format: &str) {
if let Ok(f) = File::open(&file) {
let f = BufReader::new(f);
for line in f.lines() {
get_subdomains(
&line.unwrap().to_string(),
&with_ip,
&with_output,
&file_format,
)
pub fn read_from_file(file: &str, with_ip: &str, with_output: &str) {
match File::open(&file) {
Ok(f) => {
let f = BufReader::new(f);
for domain in f.lines() {
let domain = domain.unwrap().to_string();
let file_name = &domain;
check_output_file_exists(&file_name);
get_subdomains(&domain, &with_ip, &with_output, &file_name)
}
}
Err(e) => {
println!("Can't open file 📁 {}. Error: {}", &file, e.description());
}
} else {
println!(
"Error: can't open file 📁 {}, please check the filename and try again.",
&file
);
}
}

fn write_to_file(data: &str, target: &str, subdomain_ip: &str, file_format: &str, with_ip: &str) {
fn write_to_file(data: &str, subdomain_ip: &str, file_name: &str, with_ip: &str) {
if with_ip == "y" {
let data = &[data, ",", subdomain_ip, "\n"].concat();
let with_ip = "-ip";
let filename = &[target, "_", &RNUM, with_ip, ".", file_format].concat();
if Path::new(&filename).exists() {
if Path::new(&file_name).exists() {
let mut output_file = OpenOptions::new()
.append(true)
.open(&filename)
.open(&file_name)
.expect("Can't open file.");
output_file
.write_all(&data.as_bytes())
.expect("Failed writing to file.");
} else {
File::create(&filename).expect("Failed to create file.");
File::create(&file_name).expect("Failed to create file.");
let mut output_file = OpenOptions::new()
.append(true)
.open(&filename)
.open(&file_name)
.expect("Can't open file.");
output_file
.write_all("subdomain,ip\n".as_bytes())
.expect("Failed writing to file.");
output_file
.write_all(&data.as_bytes())
.expect("Failed writing to file.");
}
} else {
let data = &[data, "\n"].concat();
let filename = &[target, "_", &RNUM, ".", file_format].concat();
if Path::new(&filename).exists() {
if Path::new(&file_name).exists() {
let mut output_file = OpenOptions::new()
.append(true)
.open(&filename)
.open(&file_name)
.expect("Can't open file.");
output_file
.write_all(&data.as_bytes())
.expect("Failed writing to file.");
} else {
File::create(&filename).expect("Failed to create file.");
File::create(&file_name).expect("Failed to create file.");
let mut output_file = OpenOptions::new()
.append(true)
.open(&filename)
.open(&file_name)
.expect("Can't open file.");
output_file
.write_all("subdomain\n".as_bytes())
.expect("Failed writing to file.");
output_file
.write_all(&data.as_bytes())
.expect("Failed writing to file.");
Expand Down Expand Up @@ -620,3 +606,20 @@ fn get_resolver() -> Resolver {
},
}
}

pub fn check_output_file_exists(file_name: &str) {
if Path::new(&file_name).exists() && Path::new(&file_name).is_file() {
println!("\nFile {} already exists, deleting it.", &file_name);
match std::fs::remove_file(&file_name) {
Ok(_) => println!("Filename {} deleted.", &file_name),
Err(e) => {
println!(
"A error as occurred while deleting the file {}. Error: {}.",
&file_name,
e.description(),
);
std::process::exit(1)
}
}
}
}
33 changes: 16 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,53 @@
extern crate clap;
use clap::App;

use findomain::{get_subdomains, read_from_file};
use findomain::{check_output_file_exists, get_subdomains, read_from_file};

fn main() {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
if matches.is_present("target") && matches.is_present("output") {
let target: String = matches.values_of("target").unwrap().collect();
let target = matches.value_of("target").unwrap().to_string();
let with_output = "y";
let file_format: String = matches.values_of("output").unwrap().collect();
let file_name = ⌖
check_output_file_exists(&file_name);
if matches.is_present("ip") {
let with_ip = "y";
get_subdomains(&target, &with_ip, &with_output, &file_format)
get_subdomains(&target, &with_ip, &with_output, &file_name)
} else {
let with_ip = "";
get_subdomains(&target, &with_ip, &with_output, &file_format)
get_subdomains(&target, &with_ip, &with_output, &file_name)
}
} else if matches.is_present("target") {
let target: String = matches.values_of("target").unwrap().collect();
let target = matches.value_of("target").unwrap().to_string();
let with_output = "n";
let file_format = "n";
let file_name = "";
if matches.is_present("ip") {
let with_ip = "y";
get_subdomains(&target, &with_ip, &with_output, &file_format)
get_subdomains(&target, &with_ip, &with_output, &file_name)
} else {
let with_ip = "";
get_subdomains(&target, &with_ip, &with_output, &file_format)
get_subdomains(&target, &with_ip, &with_output, &file_name)
}
} else if matches.is_present("file") && matches.is_present("output") {
let with_output = "y";
let file_format: String = matches.values_of("output").unwrap().collect();
let file: String = matches.values_of("file").unwrap().collect();
let file = matches.value_of("file").unwrap().to_string();
if matches.is_present("ip") {
let with_ip = "y";
read_from_file(&file, &with_ip, &with_output, &file_format)
read_from_file(&file, &with_ip, &with_output)
} else {
let with_ip = "";
read_from_file(&file, &with_ip, &with_output, &file_format)
read_from_file(&file, &with_ip, &with_output)
}
} else if matches.is_present("file") {
let with_output = "n";
let file_format = "n";
let file: String = matches.values_of("file").unwrap().collect();
let file = matches.value_of("file").unwrap().to_string();
if matches.is_present("ip") {
let with_ip = "y";
read_from_file(&file, &with_ip, &with_output, &file_format)
read_from_file(&file, &with_ip, &with_output)
} else {
let with_ip = "";
read_from_file(&file, &with_ip, &with_output, &file_format)
read_from_file(&file, &with_ip, &with_output)
}
}
}

0 comments on commit 8f41477

Please sign in to comment.