@@ -8,8 +8,23 @@ use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
88use windows:: Win32 :: Foundation :: { GetLastError , FARPROC , HANDLE , HMODULE } ;
99use windows:: Win32 :: System :: Diagnostics :: Debug :: WriteProcessMemory ;
1010use tracing_subscriber:: util:: SubscriberInitExt ;
11- use windows:: core:: { s, HRESULT , PCWSTR , Error } ;
11+ use windows:: core:: { s, PCWSTR , Error } ;
1212use 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
1429fn 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