use std::fs;
use std::io::stdin;
use std::io::Write;
use std::path::PathBuf;

fn external(project_folder_path: String,comapny_name: &String, project_name: &String, passtemp: PathBuf){
    let mut general_notes = fs::File::create(format!("{}/general.md", &project_folder_path)).expect("error creating general notes file");
    let mut attack_notes = fs::File::create(format!("{}/attacks.md", &project_folder_path)).expect("error creating attack notes file");
    let mut host_notes = fs::File::create(format!("{}/host_notes.md", &project_folder_path)).expect("error creating host notes file");
    let mut finding_notes = fs::File::create(format!("{}/findings.md", &project_folder_path)).expect("error creating findings notes file");
    // for tagging
    let year = project_name.split("_").collect::<Vec<&str>>()[1];
    let project_type = "External";
    writeln!(&mut general_notes, "#{} #{} #{} #general", comapny_name, project_type, year).expect("error writing to general notes file");
    writeln!(&mut attack_notes, "#{} #{} #{} #attack", comapny_name, project_type, year).expect("error writing tags on attack notes");
    writeln!(&mut host_notes, "##{} #{} #{} #host_notes", comapny_name, project_type, year).expect("error writing tag lin in host notes");
    writeln!(&mut finding_notes, "#{} #{} #{} #findings", comapny_name, project_type, year).expect("error writing tag line on findings");
    writeln!(&mut general_notes, "# Scope").expect("error writing to general notes file");
    writeln!(&mut general_notes, "\n| IP | Third Party | Approval |").expect("error writing to general notes file");
    writeln!(&mut general_notes, "| -- | ----------- | -------- |").expect("error writing to general notes file");
writeln!(&mut general_notes, "# PPC").expect("failed to write general notes");
write!(&mut general_notes, "
Planning call notes:
* methodolgy
* whole month testing window
* start with a vuln scan in nessus pro
* pentesting execution standard framework
* info gathering
* recon
* exlpoitation
* reporting 
* primary objective
    * reasonable and expected protections are in place
    * see if we can get access to the internal network
        * if we do get inside 
            * reach out to the contact and see what they'd want us to know
* questions to ask
* custom objectives
    * 
* testing main website
* password attacks
    * password sprays 3-12 hours
    * lock out policy - 

## Enumeration in progress

### DNS Bruteforcing

| domain name | IP | inscope? |
| ----------- | -- | -------- |

# Attack Surface Enumeration

| IP | host notes | needs? |
| -- | ---------- | ------ |\n").expect("faile to write pentest notes");

// set up the basics for our attack notes
write!(&mut attack_notes,"
# Directory Bruteforcing
- [ ] example.com

----

# Login attacks
## examle.com/login
- [ ] stuffed
- [ ] sprayed
- [ ] bruteforced
- [ ] default

----

# MSOLspray.py
passwords tried:
* password\n
" ).expect("failed to write attack notes template");
let new_spray_path = format!("{}/passwordspray.md", &project_folder_path);
fs::copy(passtemp,new_spray_path).unwrap();
}

fn internal(project_folder_path: String, comapny_name: &String, project_name: &String, passtemp: PathBuf){
    let loot_folder = format!("{}/l00t", project_folder_path);
    fs::create_dir_all(&loot_folder).expect("error creating loot directory");
    let mut general_notes = fs::File::create(format!("{}/general.md", &project_folder_path)).expect("error creating general notes file");
    let mut attack_notes = fs::File::create(format!("{}/attacks.md", &project_folder_path)).expect("error creating attack notes file");
    let mut finding_notes = fs::File::create(format!("{}/findings.md", &project_folder_path)).expect("error creating findings notes file");
    let mut systeminfo = fs::File::create(format!("{}/systeminfo", &project_folder_path)).expect("error creating systeminfo note file");
    let mut netsta = fs::File::create(format!("{}/netstat", &project_folder_path)).expect("error creating netstat file");
    let mut creds_notes = fs::File::create(format!("{}/creds.md", &loot_folder)).expect("error creating creds note");
    let mut todo_notes = fs::File::create(format!("{}/todo.md", &project_folder_path)).expect("error creating todo notes");
    let mut cleanup_notes = fs::File::create(format!("{}/cleanup.md", &project_folder_path)).expect("error creating cleanup notes");
    let mut dump_notes = fs::File::create(format!("{}/dumps.md", &loot_folder)).expect("error creating password spray notes");
    let mut enum_notes = fs::File::create(format!("{}/initial_enum.md", &project_folder_path)).expect("error creating password spray notes");
    let enum_obsidian_path = format!("Hack_Notes/pentest_notes/upcomming/{customer}//initial_enum.md", customer = comapny_name);
    // for tagging notes
    let oyear = project_name.split("_").collect::<Vec<&str>>()[0];
    let year = format!("year-{}", oyear);
    let project_type = "Internal";
    writeln!(&mut creds_notes, "#{} #{} #{} #l00t #creds", comapny_name, project_type, year).expect("error writing creds notes"); 
    writeln!(&mut dump_notes, "#{} #{} #{} #l00t #dumps", comapny_name, project_type, year).expect("error writing creds notes");
    writeln!(&mut cleanup_notes, "#{} #{} #{} #cleanup", comapny_name, project_type, year).expect("error writing to cleanup notes");
    writeln!(&mut general_notes, "#{} #{} #{} #general", comapny_name, project_type, year).expect("error writing to general notes file");
    writeln!(&mut attack_notes, "#{} #{} #{} #attack", comapny_name, project_type, year).expect("error writing attack note tags");
    writeln!(&mut todo_notes,  "#{} #{} #{} #todo", comapny_name, project_type, year).expect("error writing tag line on todo");
    writeln!(&mut finding_notes, "#{} #{} #{} #findings", comapny_name, project_type, year).expect("error writing tags line on findings");
    writeln!(&mut systeminfo, "#{} #{} #{} #general", comapny_name, project_type, year).expect("error writing tag line for system info");
    writeln!(&mut systeminfo, "#{} #{} #{} #Password_sprays", comapny_name, project_type, year).expect("error writing tag line for password spraying");
    writeln!(&mut netsta, "#{} #{} #{} #general", comapny_name, project_type, year).expect("error writing tagline in the netstat file");
    writeln!(&mut enum_notes, "#{} #{} #{} #enum", comapny_name, project_type, year).expect("error writing tagline in the netstat file");
    writeln!(&mut creds_notes, "# CREDS").expect("error writing creds notes");
    writeln!(&mut creds_notes, "\n| System | username | password |").expect("error writing creds notes");
    writeln!(&mut creds_notes, "| ------ | -------- | -------- |").expect("error writing creds notes");
    writeln!(&mut creds_notes, "\n\n").expect("error writing creds notes");
    writeln!(&mut creds_notes, "# HASHES\n\n").expect("error writing creds notes");
    writeln!(&mut creds_notes, "| TYPE | USER | HASH |").expect("error writing creds notes");
    writeln!(&mut creds_notes, "| ---- | ---- | ---- |").expect("error writing creds notes");
    writeln!(&mut cleanup_notes, "- [ ] Breach machine C-temp-fr").expect("error writing to cleanup notes");
    writeln!(&mut cleanup_notes, "- [ ] (continue to add as needed").expect("error writing ot cleanup notes");
    writeln!(&mut general_notes, "# Scope\n").expect("error writing to general notes file");
    writeln!(&mut general_notes, "PASTE SCOPE FROM EXCELL HERE (THE EXCEL TO MARKDOWN TABLE PLUGIN WILL FORMAT FOR YOU").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "# SAM\n\n").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "## system name\n").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "```").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "```\n\n").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "# LSASS\n\n").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "## system name\n").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "```").expect("shouldn't ever fail");
    writeln!(&mut dump_notes, "```").expect("shouldn't ever fail");
    write!(&mut general_notes, "
On the call: 

Introductions 
    Let them know that their primary contact will be the PM and there should be 

Go over general attack strategy/procedure. 
    We will get a beacon payload by the time the test starts 
        The beacon payload should be executed on a domain joined windows system. 
            If the system is not domain joined/no domain - let Seth know as this modifies the standard beacon 
        Select a user based on a department/role that they would like tested (Marketing, Sales, HR, IT) 
            This can be a test system with a cloned user, but then we don't get keylogging or screen grabs 
        The beacon is created using Cobalt Strike and communicates over HTTPS 
        Since Cobalt Strike is very well signatured, remind them that they may need to add an exclusion in antivirus and/or web filter 
    We will look at local privilege escalation, conduct portscans, password sprays, targeted vulnerability scanning (NOT NESSUS), lateral movement opportunities, and escalating to DOMAIN ADMIN privilege.  
    Ask if they want a focus on any particular assets. for example, an old time logging system, or remote access system. 

Confirm On Prem AD vs NoAD or Azure AD

Ask if they have any questions or concerns 

Do they have a specific contact 

Email any follow-up items from the call to the PM 
").expect("error writing PPC text");
    write!(&mut attack_notes,"
# current dat (ex: 7/5)
Got Persistence via (schtasks, bat schtasks, startup folder)

echo WHOAMI > initialenum.txt && echo ``` >> initialenum.txt && whoami /all >> initialenum.txt && echo ``` >> initialenum.txt && echo IPCONFIG >> initialenum.txt && echo ``` >> initialenum.txt && ipconfig /all >> initialenum.txt && echo ``` >> initialenum.txt && echo NETSTAT -ANO >> initialenum.txt && echo ``` >> initialenum.txt && netstat -ano >> initialenum.txt && echo ``` >> initialenum.txt && echo NET ACCOUNTS (LOCAL) >> initialenum.txt && echo ``` >> initialenum.txt && net accounts >> initialenum.txt && echo ``` >> initialenum.txt && echo NET ACCOUNTS (DOMAIN) >> initialenum.txt && echo ``` >> initialenum.txt && net accounts /domain >> initialenum.txt && echo ``` >> initialenum.txt && echo NET LOCALGROUP ADMINISTRATORS >> initialenum.txt && echo ``` >> initialenum.txt && net localgroup administrators >> initialenum.txt && echo ``` >> initialenum.txt && echo NET SHARE >> initialenum.txt && echo ``` >> initialenum.txt && net share >> initialenum.txt && echo ``` >> initialenum.txt && echo NET USE >> initialenum.txt && echo ``` >> initialenum.txt && net use >> initialenum.txt && echo ``` >> initialenum.txt && echo SYSTEMINFO >> initialenum.txt && echo ``` >> initialenum.txt && systeminfo >> initialenum.txt


whoami /all
![[{enum}#WHOAMI]]


ipconfig /all
![[{enum}#IPCONFIG]]


netstat -ano
[[{enum}#NETSTAT]]


net accounts
![[{enum}#NET ACCOUNTS]]


net accounts /domain
![[{enum}#NET ACCOUNTS DOMAIN]]


net localgroup administrators
![[{enum}#LOCAL ADMINS]]

Net group \"domain admins\" /domain
![[{enum}#DOMAIN ADMINS]]


net share
![[{enum}#NET SHARE]]


net use
![[{enum}#NET USE]]


systeminfo
[[{enum}#SYSTEMINFO]]


dsregcmd
[[{enum}#DSREGCMD]]


powerup.ps1/sharpup.exe notes.




    ", enum = enum_obsidian_path).expect("error writing to attack notes for internal tests");
    write!(&mut finding_notes, "
# normal findings

# data exfil
## [Sarting Username]

    ").expect("error writing to findings notes on internal");
    write!(&mut todo_notes, "

- [ ] local checks
- [ ] find shares
- [ ] snaffle
- [ ] bloodhound
- [ ] admin or RDP?
- [ ] certify
- [ ] portscan
- [ ] sql stuff
- [ ] passwords in AD Descriptions?
- [ ] password spray
").expect("error writing todo list");
write!(&mut netsta,"
```

```").expect("error writing code block to system info");
write!(&mut systeminfo,"
```

```").expect("error writing code block to system info");
let new_pass_path =  format!("{}/passwordspray.md", &project_folder_path);
println!("{}  |  {}", passtemp.display(), new_pass_path);
fs::copy(passtemp, new_pass_path).unwrap();
}


pub fn start_pentest(config_path: &PathBuf) {
    let mut pentest_notes = String::new();
    let mut project_files = String::new();
    let mut company_name = String::new();
    let mut project_name = String::new();
    let mut config_file_path_buf = config_path.clone();
    let mut passpray_path = config_file_path_buf.clone();
    passpray_path.push("passwordspray.md");
    config_file_path_buf.push("new_projects.conf");
    let config_string = fs::read_to_string(config_file_path_buf).expect("error reading ");
    if config_string.contains("folder_path") && config_string.contains("notes_path"){
        let config_string_vec: Vec<&str> = config_string.split("\n").collect();
        for line in config_string_vec{
            if line.contains("project_folder_path"){
                let line_vec: Vec<&str> = line.split(":").collect();
                project_files = line_vec[1].to_owned();
            }
            else if line.contains("project_notes_path"){
                let line_vec: Vec<&str> = line.split(":").collect();
                pentest_notes = line_vec[1].to_owned();
            }
        }
    }
    println!("Project files path: {}\nProject Notes path: {}", project_files, pentest_notes);
    println!("Comapny Name?");
    std::io::stdin().read_line(&mut company_name);
    println!("project Name?");
    stdin().read_line(&mut project_name);
    //remove new lines from input
    company_name.trim_end();
    project_name.trim_end();
    let project_folder_path = format!("{}/{}/{}", pentest_notes, company_name,project_name);
    println!("setting folder creation paths...");
    let project_files_folder_path = format!("{}/{}/{}", project_files, company_name, project_name);
    let working_folder = format!("{}/working", &project_files_folder_path);
    let writing_folder = format!("{}/writing", &project_files_folder_path);
    let screeenshot_folder = format!("{}/screenshots",&writing_folder);
    let delivery_folder = format!("{}/delivery", &project_files_folder_path);
    // make the folders for this project's notes and files
    println!("creating directory structure for notes and file folders...");
    fs::create_dir_all(&project_folder_path).expect("Error creating project folder");
    fs::create_dir_all(&project_files_folder_path).expect("Error creating project file folder");
    fs::create_dir_all(&working_folder).expect("error creating working directory");
    fs::create_dir_all(&writing_folder).expect("Error creating writing direcotry");
    fs::create_dir(&screeenshot_folder).expect("error creating screenshots folder");
    fs::create_dir_all(&delivery_folder).expect("Error creating delivery direcotry");
    if project_name.contains("internal"){
        println!("internal pentest type detected, auto populating notes with internal layout...");
        internal(project_folder_path,&company_name, &project_name, passpray_path);
    }
    else if project_name.contains("external") {
        println!("external pentest type detected, auto populating notes with external layout...");
        external(project_folder_path, &company_name, &project_name, passpray_path);
    }
    else if project_name.contains("webapp") {
        println!("not implemented yet sorry");
        println!("default file folder structure used, and empty notes folder created...")
    }
    else{
        println!("unknown project type detected, default folder strucutre and empty note folder created...");
        print!("
Known project types:
Internal Penetration Test
External Penetartion Test

If this test is actually one of these types please include the type in the project name parameter, example: 2023_internal_pentest.
If this test is a common test and you would like a default note structure implemented in this script let Kevin \"Kage\" Gunter know and supply an example markdown note sheet.");
        }
    
}