Skip to content

Commit

Permalink
injector-cli: 'exec' instead of 'create'
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice committed Apr 28, 2022
1 parent 9eb1006 commit 2d4704c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
30 changes: 8 additions & 22 deletions src/injector/injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,23 @@ struct injector {
return res_u8;
}

static std::optional<DWORD> create_process(const std::wstring &path) {
fs::path p(path);

if (p.parent_path().empty()) {
wchar_t realpath[MAX_PATH];

if (SearchPathW(nullptr, p.c_str(), L".exe", MAX_PATH, realpath,
nullptr) == 0) {
return std::nullopt;
}
p = realpath;
}

if (!fs::exists(p)) {
return std::nullopt;
}

static std::optional<DWORD> create_process(const std::wstring &command,
bool new_console = false) {
STARTUPINFO startup_info{};
PROCESS_INFORMATION process_info{};
if (CreateProcessW(p.c_str(), nullptr, nullptr, nullptr, false,
CREATE_NEW_CONSOLE, nullptr, nullptr,
&startup_info, &process_info) == 0) {
if (CreateProcessW(nullptr, std::wstring{command}.data(), nullptr, nullptr,
false, new_console ? CREATE_NEW_CONSOLE : 0, nullptr,
nullptr, &startup_info, &process_info) == 0) {
return std::nullopt;
}

return process_info.dwProcessId;
}

static std::optional<DWORD> create_process(const std::string &path) {
static std::optional<DWORD> create_process(const std::string &path,
bool new_console = false) {
auto wpath = utf8_decode(path);
return create_process(wpath);
return create_process(wpath, new_console);
}
};

Expand Down
19 changes: 13 additions & 6 deletions src/injector/injector_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ int main(int argc, char *argv[]) {
.default_value(vector<string>{})
.append();

parser.add_argument("-c", "--create")
.help("filename of an executable to create a new process and inject "
"proxy (string, i.e. `python` or `C:\\Program Files\\a.exe`)")
parser.add_argument("-e", "--exec")
.help("command line started with an executable to create a new process "
"and inject proxy (string, i.e. `python` or `C:\\Program "
"Files\\a.exe --some-option`)")
.default_value(vector<string>{})
.append();

Expand All @@ -97,6 +98,12 @@ int main(int argc, char *argv[]) {
"`127.0.0.1:1080`)")
.default_value(string{});

parser.add_argument("-w", "--new-console-window")
.help("create a new console window while a new console process is "
"executed in `-e`")
.default_value(false)
.implicit_value(true);

try {
parser.parse_args(argc, argv);
} catch (const runtime_error &err) {
Expand All @@ -107,10 +114,10 @@ int main(int argc, char *argv[]) {

auto pids = parser.get<vector<int>>("-i");
auto proc_names = parser.get<vector<string>>("-n");
auto create_paths = parser.get<vector<string>>("-c");
auto create_paths = parser.get<vector<string>>("-e");

if (pids.empty() && proc_names.empty() && create_paths.empty()) {
cerr << "Expected at least one of `-i`, `-n` or `-c`" << endl;
cerr << "Expected at least one of `-i`, `-n` or `-e`" << endl;
cerr << parser;
return 2;
}
Expand Down Expand Up @@ -165,7 +172,7 @@ int main(int argc, char *argv[]) {
}

for (const auto &file : create_paths) {
if (auto res = injector::create_process(file)) {
if (auto res = injector::create_process(file, parser.get<bool>("-w"))) {
if (server.inject(*res)) {
report_injected(*res);
}
Expand Down
4 changes: 2 additions & 2 deletions src/injector/injector_gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ auto make_controls(injector_server &server, ce::view &view,

auto [process_input, process_input_ptr] = input_box();
auto [input_select, input_select_ptr] =
selection_menu([](auto &&) {}, {"pid", "name", "create"});
selection_menu([](auto &&) {}, {"pid", "name", "exec"});

auto inject_click = [input_select_ptr, process_input_ptr]<typename F>(F &&f) {
auto text = trim_copy(process_input_ptr->get_text());
Expand All @@ -109,7 +109,7 @@ auto make_controls(injector_server &server, ce::view &view,
});
if (!success)
return;
} else if (option == "create") {
} else if (option == "exec") {
auto res = injector::create_process(text);
if (!res) {
return;
Expand Down

0 comments on commit 2d4704c

Please sign in to comment.