Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/generate traces #44

Merged
merged 13 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ base64 = { version = "0.22.1" }
clap = { version = "4.5.20", features = ["derive"] }
rand = "0.8.5"
regex_generate = "0.2.3"
toml = "0.8.19"
serde = { version = "1.0.214", features = ["derive"] }

[build-dependencies]
embed-resource = "2.5.0"
Expand Down
19 changes: 0 additions & 19 deletions src/cli.rs

This file was deleted.

38 changes: 38 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2023 The MalwareTracesGenerator development team
//
// SPDX-License-Identifier: GPL-3.0-or-later

pub mod generate;
pub mod traces;

use crate::commands::{generate::Generate, traces::Traces};
use clap::{Parser, Subcommand};
use std::error::Error;

#[derive(Parser)]
#[clap(author, version)]
#[clap(arg_required_else_help = true)]
pub struct Arguments {
#[clap(subcommand)]
pub command: Command,
}

#[derive(Subcommand)]
pub enum Command {
Traces(Traces),
Generate(Generate),
}

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

impl Runnable for Arguments {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Command::Traces(traces) => traces as &dyn Runnable,
Command::Generate(generate) => generate,
}
.run()
}
}
51 changes: 51 additions & 0 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2023 The MalwareTracesGenerator development team
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::commands::{traces::Traces, Runnable};
use clap::Parser;
use serde::Deserialize;
use std::{error::Error, fs::read_to_string, path::PathBuf};
use toml::from_str;

#[derive(Deserialize)]
struct Configuration {
metadata: Metadata,
traces: Vec<Traces>,
}

#[derive(Deserialize)]
struct Metadata {
name: String,
version: String,
references: Vec<String>,
authors: Option<Vec<Author>>,
}

#[derive(Deserialize)]
struct Author {
name: String,
email: Option<String>,
}

#[derive(Parser)]
pub struct Generate {
#[clap(required = true, help = "Path to the configuration file")]
path: PathBuf,
}

impl Runnable for Generate {
fn run(&self) -> Result<(), Box<dyn Error>> {
if !self.path.try_exists()? || !self.path.is_file() {
return Ok(());
}

let configuration: Configuration = from_str(read_to_string(self.path.clone())?.as_str())?;

for trace in configuration.traces {
trace.run()?;
}

Ok(())
}
}
24 changes: 13 additions & 11 deletions src/traces.rs → src/commands/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::traces::{drivers::Drivers, processes::Processes};
use crate::commands::{
traces::{drivers::Drivers, processes::Processes},
Runnable,
};
use clap::{Args, Subcommand};
use serde::Deserialize;
use std::error::Error;

pub mod drivers;
pub mod processes;

#[derive(Debug, Args)]
#[derive(Args, Deserialize)]
pub struct Traces {
#[clap(subcommand)]
pub command: Commands,
#[serde(flatten)]
pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
#[derive(Subcommand, Deserialize)]
#[serde(rename_all = "snake_case", untagged)]
pub enum Command {
Drivers(Drivers),
Processes(Processes),
}

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

impl Runnable for Traces {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Drivers(drivers) => drivers as &dyn Runnable,
Commands::Processes(processes) => processes,
Command::Drivers(drivers) => drivers as &dyn Runnable,
Command::Processes(processes) => processes,
}
.run()
}
Expand Down
15 changes: 9 additions & 6 deletions src/traces/drivers.rs → src/commands/traces/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::traces::{drivers::byovd::Byovd, Runnable};
use crate::commands::{traces::drivers::byovd::Byovd, Runnable};
use clap::{Args, Subcommand};
use serde::Deserialize;
use std::error::Error;

pub mod byovd;

#[derive(Debug, Args)]
#[derive(Args, Deserialize)]
pub struct Drivers {
#[clap(subcommand)]
pub command: Commands,
#[serde(flatten)]
pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
#[derive(Subcommand, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Command {
Byovd(Byovd),
}

impl Runnable for Drivers {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Byovd(byovd) => byovd as &dyn Runnable,
Command::Byovd(byovd) => byovd as &dyn Runnable,
}
.run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::{traces::Runnable, windows::users::is_administrator};
use crate::{commands::Runnable, windows::users::is_administrator};
use clap::Parser;
use serde::Deserialize;
use std::{error::Error, path::PathBuf};
use windows::{
core::{Owned, HSTRING, PCWSTR},
Expand All @@ -17,7 +18,7 @@ use windows::{
},
};

#[derive(Debug, Parser)]
#[derive(Parser, Deserialize)]
pub struct Byovd {
#[clap(required = true, help = "Name of the service")]
service_name: String,
Expand Down
15 changes: 9 additions & 6 deletions src/traces/processes.rs → src/commands/traces/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::traces::{processes::spoofing::Spoofing, Runnable};
use crate::commands::{traces::processes::spoofing::Spoofing, Runnable};
use clap::{Args, Subcommand};
use serde::Deserialize;
use std::error::Error;

pub mod spoofing;

#[derive(Debug, Args)]
#[derive(Args, Deserialize)]
pub struct Processes {
#[clap(subcommand)]
pub command: Commands,
#[serde(flatten)]
pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
#[derive(Subcommand, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Command {
Spoofing(Spoofing),
}

impl Runnable for Processes {
fn run(&self) -> Result<(), Box<dyn Error>> {
match &self.command {
Commands::Spoofing(spoofing) => spoofing as &dyn Runnable,
Command::Spoofing(spoofing) => spoofing as &dyn Runnable,
}
.run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::{traces::Runnable, windows::processes::get_pid};
use crate::{commands::Runnable, windows::processes::get_pid};
use clap::Parser;
use serde::Deserialize;
use std::{
error::Error, ffi::OsString, iter::once, mem::size_of, os::windows::ffi::OsStrExt,
path::PathBuf,
Expand All @@ -21,7 +22,7 @@ use windows::{
},
};

#[derive(Debug, Parser)]
#[derive(Parser, Deserialize)]
pub struct Spoofing {
#[clap(required = true, help = "Path to the executable")]
executable: PathBuf,
Expand Down
12 changes: 3 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

mod cli;
mod traces;
mod commands;
mod windows;

use clap::Parser;
use cli::{Arguments, Commands};
use commands::{Arguments, Runnable};
use std::error::Error;
use traces::Runnable;

fn main() -> Result<(), Box<dyn Error>> {
match Arguments::parse().command {
Commands::Traces(action) => action.run()?,
};

Ok(())
Arguments::parse().run()
}
Loading