8
8
#include < iostream>
9
9
#include < Windows.h>
10
10
#include < Psapi.h>
11
+ #include < TlHelp32.h> // GetProcessByName
11
12
12
13
#pragma region CRT sections
13
14
// This exists to imitate the behavior of the CRT initialization code
@@ -32,6 +33,37 @@ void print(const char *message, size_t length)
32
33
WriteFile (console, message, size, &size, nullptr );
33
34
}
34
35
36
+ DWORD GetProcessByName (PCSTR name)
37
+ {
38
+ DWORD pid = 0 ;
39
+
40
+ WCHAR exe[MAX_PATH] = {};
41
+ mbstowcs_s (NULL , exe, name, MAX_PATH);
42
+
43
+ // Create toolhelp snapshot.
44
+ HANDLE snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
45
+ PROCESSENTRY32 process;
46
+ ZeroMemory (&process, sizeof (process));
47
+ process.dwSize = sizeof (process);
48
+
49
+ // Walkthrough all processes.
50
+ if (Process32First (snapshot, &process))
51
+ {
52
+ do
53
+ {
54
+ if (wcscmp (process.szExeFile , exe) == 0 )
55
+ {
56
+ pid = process.th32ProcessID ;
57
+ break ;
58
+ }
59
+ } while (Process32Next (snapshot, &process));
60
+ }
61
+
62
+ CloseHandle (snapshot);
63
+
64
+ return pid;
65
+ }
66
+
35
67
DWORD CALLBACK remote_main (BYTE *image_base)
36
68
{
37
69
#pragma region Initialize module image
@@ -124,27 +156,46 @@ int main(int argc, char *argv[])
124
156
125
157
if (argc > 1 )
126
158
{
127
- pid = strtoul (argv[1 ], nullptr , 0 );
128
-
129
- if (pid == 0 )
159
+ if (argc > 2 )
130
160
{
131
- STARTUPINFOA startup_info = { sizeof (startup_info) };
132
- PROCESS_INFORMATION process_info = {};
161
+ if (strcmp (argv[1 ]," -a" ) == 0 ) // Attach to running process
162
+ {
163
+ // Is numerical PID
164
+ pid = strtoul (argv[2 ], nullptr , 0 );
133
165
134
- std::string command_line;
135
- for (int i = 1 ; i < argc; ++i, command_line += ' ' )
136
- command_line += argv[i];
166
+ if (pid == 0 )
167
+ {
168
+ // Try to look up PID of running process by name
169
+ pid = GetProcessByName (argv[2 ]);
170
+ }
171
+ }
172
+ }
173
+ else
174
+ {
175
+ // Attach to running process by PID
176
+ pid = strtoul (argv[1 ], nullptr , 0 );
137
177
138
- if (!CreateProcessA (nullptr , command_line.data (), nullptr , nullptr , FALSE , CREATE_NEW_CONSOLE, nullptr , nullptr , &startup_info, &process_info))
178
+ // Launch target application and determine PID
179
+ if (pid == 0 )
139
180
{
140
- std::cout << " Failed to start target application process!" << std::endl;
141
- return GetLastError ();
142
- }
181
+ STARTUPINFOA startup_info = { sizeof (startup_info) };
182
+ PROCESS_INFORMATION process_info = {};
183
+
184
+ std::string command_line;
185
+ for (int i = 1 ; i < argc; ++i, command_line += ' ' )
186
+ command_line += argv[i];
187
+
188
+ if (!CreateProcessA (nullptr , command_line.data (), nullptr , nullptr , FALSE , CREATE_NEW_CONSOLE, nullptr , nullptr , &startup_info, &process_info))
189
+ {
190
+ std::cout << " Failed to start target application process!" << std::endl;
191
+ return GetLastError ();
192
+ }
143
193
144
- pid = process_info.dwProcessId ;
194
+ pid = process_info.dwProcessId ;
145
195
146
- CloseHandle (process_info.hThread );
147
- CloseHandle (process_info.hProcess );
196
+ CloseHandle (process_info.hThread );
197
+ CloseHandle (process_info.hProcess );
198
+ }
148
199
}
149
200
}
150
201
0 commit comments