Skip to content

Commit c14fd3d

Browse files
committed
Switch over to clap for command line parsing
1 parent 3945c78 commit c14fd3d

File tree

3 files changed

+166
-19
lines changed

3 files changed

+166
-19
lines changed

Cargo.lock

Lines changed: 129 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ windows = { version = "0.58.0", features = [
1919
"Win32_Security"
2020
] }
2121
tracing = { version = "0.1.40", features = ["max_level_debug"] }
22-
tracing-subscriber = { version = "0.3.18", features = ["ansi"] }
22+
tracing-subscriber = { version = "0.3.18", features = ["ansi"] }
23+
clap = { version = "4.5.20", features = ["derive"] }

src/main.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@ use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
88
use windows::Win32::Foundation::{GetLastError, FARPROC, HANDLE, HMODULE};
99
use windows::Win32::System::Diagnostics::Debug::WriteProcessMemory;
1010
use tracing_subscriber::util::SubscriberInitExt;
11-
use windows::core::{s, HRESULT, PCWSTR, Error};
11+
use windows::core::{s, PCWSTR, Error};
1212
use tracing::{error, info, warn};
13+
use clap::Parser;
14+
15+
#[derive(Parser, Debug)]
16+
#[command(version, about, long_about = None)]
17+
struct Args {
18+
/// The name of the target process
19+
#[arg(required = true)]
20+
target: OsString,
21+
/// The path to the DLL file that will be injected
22+
#[arg(required = true)]
23+
dll: OsString,
24+
/// Enables verbose logging for events
25+
#[arg(short, long)]
26+
verbose: bool,
27+
}
1328

1429
fn main() -> Result<(), Error> {
1530
tracing_subscriber::fmt()
@@ -18,27 +33,27 @@ fn main() -> Result<(), Error> {
1833
.finish()
1934
.init();
2035

21-
let mut args = std::env::args_os().collect::<Vec<OsString>>();
36+
let args = Args::parse();
2237

23-
if args.len() < 3 || args.len() > 3 {
24-
println!("Usage: dll_injector [TARGET_NAME] [PATH_TO_DLL]");
25-
return Err(Error::new(HRESULT::default(), "Provide one item per field"))
26-
}
38+
let mut critical_args = [args.target, args.dll];
2739

28-
for arg in &mut args {
40+
for arg in &mut critical_args {
2941
arg.push("\0")
3042
}
3143

32-
let dll_name = args[2].clone();
44+
let dll_name = critical_args[1].clone();
3345

34-
let target_handle = process_enumerate_and_search(PCWSTR::from_raw(
35-
args[1].clone()
36-
.to_string_lossy()
37-
.replace('\u{FFFD}', "")
38-
.encode_utf16()
39-
.collect::<Vec<u16>>()
40-
.as_mut_ptr()
41-
))?;
46+
let target_handle = process_enumerate_and_search(
47+
PCWSTR::from_raw(
48+
critical_args[0].clone()
49+
.to_string_lossy()
50+
.replace('\u{FFFD}', "")
51+
.encode_utf16()
52+
.collect::<Vec<u16>>()
53+
.as_mut_ptr()
54+
),
55+
args.verbose
56+
)?;
4257

4358
inject_dll(
4459
PCWSTR::from_raw(
@@ -55,7 +70,7 @@ fn main() -> Result<(), Error> {
5570
Ok(())
5671
}
5772

58-
fn process_enumerate_and_search(process_name: PCWSTR) -> Result<HANDLE, Error> {
73+
fn process_enumerate_and_search(process_name: PCWSTR, verbose: bool) -> Result<HANDLE, Error> {
5974
let mut process_handle: Option<HANDLE> = None;
6075
let snapshot_handle: HANDLE;
6176
let mut process_entry: PROCESSENTRY32 = unsafe { std::mem::zeroed() };
@@ -83,7 +98,9 @@ fn process_enumerate_and_search(process_name: PCWSTR) -> Result<HANDLE, Error> {
8398
CStr::from_ptr(process_entry.szExeFile.clone().as_ptr() as *mut c_char)
8499
};
85100

86-
info!("Checking: {sz_exe_file:?}");
101+
if verbose {
102+
info!("Checking: {sz_exe_file:?}");
103+
}
87104

88105
if unsafe { PCWSTR::from_raw({
89106
let mut str = sz_exe_file

0 commit comments

Comments
 (0)