4
4
5
5
constexpr auto HELP_NUMBER_OF_ARGS = 2 ;
6
6
constexpr auto NORMAL_NUMBER_OF_ARGS = 4 ;
7
- constexpr auto SOURCE_EXECUTABLE_ARGS_POSITION = 1 ;
8
- constexpr auto TARGET_EXECUTABLE_ARGS_POSITION = 2 ;
9
- constexpr auto COVER_EXECUTABLE_ARGS_POSITION = 3 ;
7
+ constexpr auto ARGS_POSITION_SOURCE_EXECUTABLE = 1 ;
8
+ constexpr auto ARGS_POSITION_TARGET_EXECUTABLE = 2 ;
9
+ constexpr auto ARGS_POSITION_COVER_EXECUTABLE = 3 ;
10
10
11
11
struct CmdlineArguments {
12
12
std::wstring source_executable;
@@ -19,53 +19,88 @@ std::wstring string_to_wstring(const std::string& string)
19
19
return std::wstring (string.begin (), string.end ());
20
20
}
21
21
22
+ bool is_file_exists (const std::string& path)
23
+ {
24
+ auto dwAttrib = GetFileAttributesA (path.c_str ());
25
+ return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
26
+ }
27
+
22
28
void print_help ()
23
29
{
24
- return ;
30
+ std::cout << std::endl << " ##### Herpaderping v1.0.0 #####" << std::endl;
31
+ std::cout << " Usage: herpaderping.exe source_executable target_executable cover_executable" << std::endl;
32
+ std::cout << std::endl << " source_executable: path to the executable you wish to run." << std::endl;
33
+ std::cout << " target_executable: path to where the source executable will be copied to and run from." << std::endl;
34
+ std::cout << " cover_executable: path to the executable that will be used as a \" cover\" for our process." << std::endl;
35
+ std::cout << std::endl << " Example:" << std::endl;
36
+ std::cout << " Running cmd.exe with the cover of chrome.exe:" << std::endl;
37
+ std::cout << " herpaderping.exe \" C:\\ Windows\\ System32\\ cmd.exe\" \" .\\ target.exe\" \" C:\\ Program Files\\ Google\\ Chrome\\ Application\\ chrome.exe\" " << std::endl;
25
38
}
26
39
27
- // constexpr auto PATH_TO_COVER = L"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
28
40
std::unique_ptr<CmdlineArguments> parse_program_arguments (int argc, char ** argv)
29
41
{
30
42
std::unique_ptr<CmdlineArguments> cmdline_args = nullptr ;
31
43
32
- switch (argc) {
33
- case HELP_NUMBER_OF_ARGS:
34
- // TODO: check if it is -h.
35
- print_help ();
36
- break ;
44
+ std::string source_exe_path = " " ;
45
+ std::string target_exe_path = " " ;
46
+ std::string cover_exe_path = " " ;
37
47
48
+ switch (argc) {
38
49
case NORMAL_NUMBER_OF_ARGS:
50
+ source_exe_path = std::string (argv[ARGS_POSITION_SOURCE_EXECUTABLE]);
51
+ target_exe_path = std::string (argv[ARGS_POSITION_TARGET_EXECUTABLE]);
52
+ cover_exe_path = std::string (argv[ARGS_POSITION_COVER_EXECUTABLE]);
53
+
54
+ if (!is_file_exists (source_exe_path)) {
55
+ throw std::invalid_argument (source_exe_path + " : No such file or directory." );
56
+ }
57
+
58
+ if (!is_file_exists (cover_exe_path)) {
59
+ throw std::invalid_argument (source_exe_path + " : No such file or directory." );
60
+ }
61
+
39
62
cmdline_args = std::make_unique<CmdlineArguments>();
40
- cmdline_args->source_executable = string_to_wstring (std::string (argv[SOURCE_EXECUTABLE_ARGS_POSITION]));
41
- cmdline_args->target_executable = string_to_wstring (std::string (argv[TARGET_EXECUTABLE_ARGS_POSITION]));
42
- cmdline_args->cover_exectuable = string_to_wstring (std::string (argv[COVER_EXECUTABLE_ARGS_POSITION]));
63
+ cmdline_args->source_executable = string_to_wstring (source_exe_path);
64
+ cmdline_args->target_executable = string_to_wstring (target_exe_path);
65
+ cmdline_args->cover_exectuable = string_to_wstring (cover_exe_path);
66
+
43
67
break ;
44
68
45
69
default :
46
- print_help ();
47
- throw std::invalid_argument (" Invalid number of arguments (" + std::to_string (argc)
48
- + " ), expected " + std::to_string (NORMAL_NUMBER_OF_ARGS));
70
+ throw std::invalid_argument (" invalid number of arguments (got "
71
+ + std::to_string (argc) + " while expecting " + std::to_string (NORMAL_NUMBER_OF_ARGS) + " )." );
49
72
}
50
73
51
74
return cmdline_args;
52
75
}
53
76
54
77
int main (int argc, char **argv)
55
78
{
79
+ std::unique_ptr<CmdlineArguments> cmdline_args = nullptr ;
80
+
56
81
try {
57
- auto cmdline_args = parse_program_arguments (argc, argv);
82
+ cmdline_args = parse_program_arguments (argc, argv);
83
+ }
84
+ catch (const std::invalid_argument& exc) {
85
+ std::cout << std::endl << " Error: " + std::string (exc.what ()) << std::endl;
86
+ print_help ();
87
+ goto failure;
88
+ }
58
89
90
+ try {
59
91
auto herpaderping = Herpaderping (cmdline_args->source_executable ,
60
92
cmdline_args->target_executable ,
61
93
cmdline_args->cover_exectuable );
62
94
63
95
herpaderping.run_process_with_cover ();
64
96
}
65
- catch (const std::exception & exc) {
66
- std::cout << " Exception : " + std::string (exc.what ()) << std::endl;
67
- return EXIT_FAILURE ;
97
+ catch (const std::runtime_error & exc) {
98
+ std::cout << std::endl << " Error : " + std::string (exc.what ()) << std::endl;
99
+ goto failure ;
68
100
}
69
101
70
102
return EXIT_SUCCESS;
103
+
104
+ failure:
105
+ return EXIT_FAILURE;
71
106
}
0 commit comments