Skip to content

Commit

Permalink
refactor: 🔨 Return Result directly and remove all exit code
Browse files Browse the repository at this point in the history
Because Result implements Terminaison trait, the cli will return the right exit code
  • Loading branch information
AntwortEinesLebens committed Sep 16, 2024
1 parent eb903ab commit 2697d86
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ pub enum Commands {
}

pub trait Runnable {
fn run(&self) -> Result<i32, Box<dyn Error>>;
fn run(&self) -> Result<(), Box<dyn Error>>;
}

impl Runnable for Actions {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::AlternateDataStreams(alternate_data_streams) => {
alternate_data_streams as &dyn Runnable
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Runnable for AlternateDataStreams {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Create(create) => create as &dyn Runnable,
}
Expand Down
6 changes: 3 additions & 3 deletions src/actions/ads/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn create_ads(fullpath: String, adsname: String, hex_data: Vec<u8>) -> bool {

impl Runnable for Create {
/* Version 20230908 */
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
println!("Alternate Data Stream");

if !self.filename.is_empty() {
Expand All @@ -77,9 +77,9 @@ impl Runnable for Create {
let payload: Vec<u8> = general_purpose::STANDARD.decode(self.data.as_str())?;
let ret_ads: bool = create_ads(fullname, barrow_ads, payload);

return Ok(!ret_ads as i32);
return Ok(());
}

Ok(1)
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/actions/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Runnable for Drivers {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Create(create) => create as &dyn Runnable,
}
Expand Down
6 changes: 3 additions & 3 deletions src/actions/drivers/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ fn create_driver_service(name: &str, details: &str, path: &str) -> bool {

impl Runnable for Create {
/* Version 20230908 */
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
println!("Bring Your Own Vulnerable Driver");

if !is_administrator()? {
println!("Need to have Administrator right to create the service");
return Ok(1);
return Ok(());
}

// Todo check path is valid or not :)

let result: bool = create_driver_service(&self.internal, &self.display, &self.path);

Ok(!result as i32)
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/actions/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Runnable for Files {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Create(create) => create as &dyn Runnable,
}
Expand Down
6 changes: 3 additions & 3 deletions src/actions/files/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ fn create_file(fullpath: String, hex_data: Vec<u8>) -> bool {
}

impl Runnable for Create {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
if self.admin && !is_administrator()? {
println!("Need to have Administrator right to create the file");
return Ok(1);
return Ok(());
}

let mut generator: Generator<rand::rngs::ThreadRng> =
Expand All @@ -107,6 +107,6 @@ impl Runnable for Create {
let payload: Vec<u8> = general_purpose::STANDARD.decode(self.magicbyte.as_str())?;
let ret: bool = create_file(fullname, payload);

Ok(!ret as i32)
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/actions/mutexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Runnable for Mutexes {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Create(create) => create as &dyn Runnable,
}
Expand Down
4 changes: 2 additions & 2 deletions src/actions/mutexes/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn create_mutex(name: &String, wait: u64) {
}

impl Runnable for Create {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
println!("Create Mutex");

let mut generator: Generator<rand::rngs::ThreadRng> =
Expand All @@ -52,6 +52,6 @@ impl Runnable for Create {

create_mutex(&payload, 2000);

Ok(0)
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/actions/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Runnable for Pipes {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Create(create) => create as &dyn Runnable,
}
Expand Down
4 changes: 2 additions & 2 deletions src/actions/pipes/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn create_name_pipe(name: &String, wait: u64) {
}

impl Runnable for Create {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
println!("Create NamePipe");

let mut generator: Generator<rand::rngs::ThreadRng> =
Expand All @@ -64,6 +64,6 @@ impl Runnable for Create {

create_name_pipe(&payload, 2000);

Ok(0)
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/actions/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Runnable for Processes {
fn run(&self) -> Result<i32, Box<dyn Error>> {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Spoofing(spoofing) => spoofing as &dyn Runnable,
}
Expand Down
6 changes: 2 additions & 4 deletions src/actions/processes/spoofing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ fn spoof(executable: &str, parent_pid: u32) -> Result<(), Box<dyn Error>> {
}

impl Runnable for Spoofing {
fn run(&self) -> Result<i32, Box<dyn Error>> {
spoof(&self.executable, get_pid(&self.parent_executable)?)?;

Ok(0)
fn run(&self) -> Result<(), Box<dyn Error>> {
Ok(spoof(&self.executable, get_pid(&self.parent_executable)?)?)
}
}
16 changes: 5 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod windows;
use actions::Runnable;
use clap::Parser;
use cli::{Arguments, Commands};
use std::error::Error;

fn banner() {
let banner: &str = "
Expand All @@ -23,17 +24,10 @@ fn banner() {
println!("{}", banner);
}

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
banner();

match Arguments::parse().command {
Commands::Actions(action) => match action.run() {
Ok(code) => std::process::exit(code),
Err(error) => {
println!("Error: {}", error);

std::process::exit(1);
}
},
};
Ok(match Arguments::parse().command {
Commands::Actions(action) => action.run()?,
})
}

0 comments on commit 2697d86

Please sign in to comment.