-
Notifications
You must be signed in to change notification settings - Fork 58
/
cli.cpp
168 lines (153 loc) · 4.37 KB
/
cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <Windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <stdlib.h>
#include <atlbase.h>
#ifndef _WIN64
#pragma comment(lib, "clrinject.lib")
#else
#pragma comment(lib, "clrinject64.lib")
#endif
#include <clrinject.h>
DWORD GetProcessIdByName(const char * processName) {
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (_stricmp(entry.szExeFile, processName) == 0)
{
CloseHandle(snapshot);
return entry.th32ProcessID;
}
}
}
CloseHandle(snapshot);
return 0;
}
void usage() {
fprintf(stderr,
"Usage:\n"
"\tclrinject-cli.exe -p <processId/processName> -a <assemblyFile>\n\n"
"Addition options:\n"
"\t-e\tEnumerate Runtimes and AppDomains.\n"
"\t-d #\tInject only into #-th AppDomain. Default = 0, inject into every AppDomain.\n"
"\t-i X.Y\tCreate an instance of class Y from namespace X.\n"
);
exit(1);
}
int main(int argc, char** argv) {
InjectionOptions options = {};
char * processName = NULL;
char * assemblyFile = NULL;
char * typeName = NULL;
//handle command line arguments
int argi = 0;
while (++argi < argc) {
if (argv[argi][0] == '-') {
switch (argv[argi][1])
{
case 'p':
if (argi + 1 < argc) {
argi++;
processName = argv[argi];
continue;
}
break;
case 'a':
if (argi + 1 < argc) {
argi++;
assemblyFile = argv[argi];
continue;
}
break;
case 'e':
options.enumerate = true;
continue;
case 'd':
if (argi + 1 < argc) {
argi++;
options.appDomainIndex = strtol(argv[argi], NULL, 10);
continue;
}
case 'i':
if (argi + 1 < argc) {
argi++;
typeName = argv[argi];
continue;
}
break;
}
}
fprintf(stderr, "Unexpected argument '%s'!\n\n", argv[argi]);
usage();
}
//check arguments validity
if (!processName || (!assemblyFile && !options.enumerate)) {
fprintf(stderr, "Process or assembly file was not specified!\n\n");
usage();
}
//find processId
DWORD processId = GetProcessIdByName(processName);
if (!processId)
processId = strtol(processName, NULL, 10);
if (!processId) {
fprintf(stderr, "Failed to get id for process name '%s'!\n", processName);
return 1;
}
options.processId = processId;
if(assemblyFile)
lstrcpynW(options.assemblyFile, CA2W(assemblyFile), MAX_PATH);
if(typeName)
lstrcpynW(options.typeName, CA2W(typeName), 256);
InjectionResult result;
int retVal = Inject(&options, &result);
if (options.enumerate) {
printf("Enumeration:\n");
int index = 1;
for (int i = 0; i < result.numRuntimes; i++) {
const Runtime& runtime = result.runtimes[i];
printf("#\tRuntime Version: '%ls', %s\n", runtime.version, runtime.started ? "is started" : "is not started");
for (int j = 0; j < runtime.numAppDomains; j++) {
const AppDomain& appDomain = runtime.appDomains[j];
printf("#%d\t\tAppDomain Name: '%ls'\n", index++, appDomain.friendlyName);
}
}
printf("\n");
}
else {
printf("Injection succeeded for following Runtimes and AppDomains:\n");
for (int i = 0; i < result.numRuntimes; i++) {
const Runtime& runtime = result.runtimes[i];
for (int j = 0; j < runtime.numAppDomains; j++) {
const AppDomain& appDomain = runtime.appDomains[j];
if(appDomain.injected)
printf("\tRuntime '%ls', AppDomain '%ls'\n", runtime.version, appDomain.friendlyName);
}
}
printf("Injection failed for following Runtimes and AppDomains:\n");
int index = 1;
for (int i = 0; i < result.numRuntimes; i++) {
const Runtime& runtime = result.runtimes[i];
for (int j = 0; j < runtime.numAppDomains; j++) {
const AppDomain& appDomain = runtime.appDomains[j];
if (!appDomain.injected && (!options.appDomainIndex || options.appDomainIndex == index))
printf("\tRuntime '%ls', AppDomain '%ls'\n", runtime.version, appDomain.friendlyName);
index++;
}
}
printf("\n");
}
if (retVal) {
if (result.status || result.statusMessage[0])
fprintf(stderr, "Injection failed, return value: 0x%08X, code: 0x%08X, reason: '%s'!\n", retVal, result.status, result.statusMessage);
else
fprintf(stderr, "Injection failed, return value: 0x%08X!\n", retVal);
return 1;
}
if (!options.enumerate)
printf("Injection successful, return value: 0x%08X\n", result.retVal);
return 0;
}