-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathInjector_UNIX.cpp
More file actions
498 lines (433 loc) · 14.4 KB
/
Copy pathInjector_UNIX.cpp
File metadata and controls
498 lines (433 loc) · 14.4 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "hacklib/ExeFile.h"
#include "hacklib/Injector.h"
#include "hacklib/Memory.h"
#include <algorithm>
#include <dirent.h>
#include <dlfcn.h>
#include <elf.h>
#include <fstream>
#include <climits>
#include <sstream>
#include <cstring>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef ARCH_64BIT
#define USER_REG_IP(u) u.regs.rip
#define USER_REG_SP(u) u.regs.rsp
#define USER_REG_AX(u) u.regs.rax
#else
#define USER_REG_IP(u) u.regs.eip
#define USER_REG_SP(u) u.regs.esp
#define USER_REG_AX(u) u.regs.eax
#endif
class Injection
{
public:
explicit Injection(std::string* error) : m_error(error) {}
Injection(const Injection&) = delete;
Injection& operator=(const Injection&) = delete;
Injection(Injection&&) = delete;
Injection& operator=(Injection&&) = delete;
~Injection()
{
if (m_pid)
{
if (m_remoteLibName)
{
// Free the remote memory for the library name.
call(m_free, m_remoteLibName);
}
if (m_restoreBackup)
{
// Restore registers.
memcpy(&m_regs, &m_regsBackup, sizeof(m_regs));
setRegs();
}
ptrace(PTRACE_DETACH, m_pid, NULL, NULL);
}
}
void writeErr(const std::string& error)
{
if (m_error)
{
*m_error += error;
}
}
bool findFile(const std::string& fileName)
{
if (!realpath(fileName.c_str(), m_fileName))
{
writeErr("Fatal: Could not get the full library name\n");
return false;
}
if (access(m_fileName, F_OK) == -1)
{
writeErr("Fatal: Could not find the library file\n");
return false;
}
return true;
}
bool openProcess(int pid)
{
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0)
{
writeErr("Fatal: Could not attach with ptrace (errno = " + std::to_string(errno) + ")\n");
return false;
}
m_pid = pid;
if (!wait())
return false;
// Verify matching bitness of injector and target.
char regsBuffer[256]; // x86_64 GPRs should be 27*sizeof(uint64_t)=216 bytes.
struct iovec regSet{};
regSet.iov_base = ®sBuffer;
regSet.iov_len = sizeof(regsBuffer);
if (ptrace(PTRACE_GETREGSET, m_pid, NT_PRSTATUS, ®Set) < 0)
{
writeErr("Warning: Could not determine bitness of target process (errno = " + std::to_string(errno) +
")\n");
}
else
{
const bool isTarget64 = regSet.iov_len != 17 * sizeof(uint32_t);
#ifdef ARCH_64BIT
if (!isTarget64)
{
writeErr("Fatal: Can not inject into 32-bit process from 64-bit injector\n");
return false;
}
#else
if (isTarget64)
{
writeErr("Fatal: Can not inject into 64-bit process from 32-bit injector\n");
return false;
}
#endif
}
if (!getRegs())
return false;
// Backup registers for restoring afterwards.
memcpy(&m_regsBackup, &m_regs, sizeof(m_regs));
// Don't touch 128 byte System V ABI redzone. Account for arguments that will be written after the SP.
USER_REG_SP(m_regs) -= 256;
m_restoreBackup = true;
return true;
}
bool findLibs()
{
auto memoryMap = hl::GetMemoryMap(m_pid);
auto findLib = [&](const std::string& libName)
{
return std::ranges::find_if(memoryMap,
[&](const hl::MemoryRegion& r)
{
auto pos = r.name.find(libName);
if (pos != std::string::npos)
{
pos += libName.size();
return r.name[pos] == '.' || r.name[pos] == '-';
}
return false;
});
};
auto itCheck =
std::ranges::find_if(memoryMap, [this](const hl::MemoryRegion& r) { return r.name == m_fileName; });
if (itCheck != memoryMap.end())
{
writeErr("Fatal: The specified module is already loaded\n");
return false;
}
auto libc = findLib("libc");
auto libdl = findLib("libdl");
if (libc == memoryMap.end())
{
writeErr("Fatal: Did not find mapping for libc library\n");
return false;
}
m_libc = *libc;
if (libdl != memoryMap.end())
{
m_libdl = *libdl;
}
return true;
}
bool findApis()
{
hl::ExeFile libc;
if (!libc.loadFromFile(m_libc.name))
{
writeErr("Fatal: Could not load libc ELF file\n");
return false;
}
m_malloc = libc.getExport("malloc");
m_free = libc.getExport("free");
if (!m_malloc || !m_free)
{
writeErr("Fatal: Did not find exports for malloc, free\n");
return false;
}
m_malloc += m_libc.base;
m_free += m_libc.base;
if (m_libdl.status != hl::MemoryRegion::Status::Invalid)
{
hl::ExeFile libdl;
if (!libdl.loadFromFile(m_libdl.name))
{
writeErr("Fatal: Could not load libdl ELF file\n");
return false;
}
m_dlopen = libdl.getExport("dlopen");
m_dlclose = libdl.getExport("dlclose");
m_dlerror = libdl.getExport("dlerror");
}
else
{
// If libdl is not mapped, use the libc exports.
m_dlopen = libc.getExport("dlopen");
m_dlclose = libc.getExport("dlclose");
m_dlerror = libc.getExport("dlerror");
m_libdl.base = m_libc.base;
}
if (!m_dlopen || !m_dlclose || !m_dlerror)
{
writeErr("Fatal: Did not find exports for dlopen, dlclose or dlerror\n");
return false;
}
m_dlopen += m_libdl.base;
m_dlclose += m_libdl.base;
m_dlerror += m_libdl.base;
return true;
}
bool remoteLoadLib()
{
const size_t libNameLen = strlen(m_fileName) + 1;
const size_t paddedLibNameLen = ((libNameLen - 1) & ~(sizeof(uintptr_t) - 1)) + sizeof(uintptr_t);
// Resolve weird state after syscall.
if (!call(0, 0, 0))
return false;
// Allocate memory in the target process.
if (!call(m_malloc, paddedLibNameLen))
return false;
m_remoteLibName = USER_REG_AX(m_regs);
if (!m_remoteLibName)
{
writeErr("Fatal: malloc failed in the remote process\n");
return false;
}
// Copy the library name to the remote process. Assume that PATH_MAX is padded to pointer size boundary.
for (uintptr_t i = 0; i < paddedLibNameLen; i += sizeof(uintptr_t))
{
const uintptr_t value = *(uintptr_t*)&m_fileName[i];
if (ptrace(PTRACE_POKEDATA, m_pid, m_remoteLibName + i, (void*)value) < 0)
{
writeErr("Fatal: ptrace POKEDATA failed\n");
return false;
}
}
// Load the library from the target process.
if (!call(m_dlopen, m_remoteLibName, RTLD_NOW | RTLD_LOCAL))
return false;
// Check whether dlopen succeeded.
if (!USER_REG_AX(m_regs))
{
writeErr("Fatal: Remote process could not load library\n");
if (call(m_dlerror))
{
const uintptr_t remoteStr = USER_REG_AX(m_regs);
if (remoteStr)
{
size_t remoteStrLen = 0;
uintptr_t value = 0;
std::string errorMsg;
auto hasNull = [](uintptr_t word)
{
for (size_t i = 0; i < sizeof(uintptr_t); i++)
{
if ((word & ((uintptr_t)0xff << 8 * i)) == 0)
return true;
}
return false;
};
do
{
value = ptrace(PTRACE_PEEKDATA, m_pid, remoteStr + remoteStrLen, NULL);
if (errno != 0)
{
writeErr("Warning: ptrace PEEKDATA failed when getting dlerror\n");
break;
}
remoteStrLen += sizeof(uintptr_t);
errorMsg.append((char*)&value, sizeof(uintptr_t));
} while (!hasNull(value));
writeErr(" dlerror returned:\n ");
writeErr(errorMsg);
}
}
return false;
}
return true;
}
private:
bool getRegs()
{
if (ptrace(PTRACE_GETREGS, m_pid, NULL, &m_regs) < 0)
{
writeErr("Fatal: ptrace GETREGS failed\n");
return false;
}
return true;
}
bool setRegs()
{
if (ptrace(PTRACE_SETREGS, m_pid, NULL, &m_regs) < 0)
{
writeErr("Fatal: ptrace SETREGS failed\n");
return false;
}
return true;
}
bool call(uintptr_t function, uintptr_t arg1 = 0, uintptr_t arg2 = 0)
{
// The stack must be aligned on 16 byte boundary when a CALL is done.
// Since no CALL is executed and a CALL pushes the return value, increment the SP.
USER_REG_SP(m_regs) &= ~(uintptr_t)0xf;
USER_REG_SP(m_regs) += sizeof(uintptr_t);
// Write zero to top of stack, so that a return from a called function will trigger SIGSEGV.
if (ptrace(PTRACE_POKEDATA, m_pid, USER_REG_SP(m_regs), (void*)0) < 0)
{
writeErr("Fatal: ptrace POKEDATA failed\n");
return false;
}
USER_REG_IP(m_regs) = function;
#ifdef ARCH_64BIT
m_regs.regs.rdi = arg1;
m_regs.regs.rsi = arg2;
#else
if (ptrace(PTRACE_POKEDATA, m_pid, USER_REG_SP(m_regs) + sizeof(uintptr_t), (void*)arg1) < 0)
{
writeErr("Fatal: ptrace POKEDATA failed\n");
return false;
}
if (ptrace(PTRACE_POKEDATA, m_pid, USER_REG_SP(m_regs) + 2 * sizeof(uintptr_t), (void*)arg2) < 0)
{
writeErr("Fatal: ptrace POKEDATA failed\n");
return false;
}
#endif
if (!setRegs())
return false;
if (!resume())
return false;
if (!wait())
return false;
if (!getRegs())
return false;
return true;
}
bool singlestep()
{
if (ptrace(PTRACE_SINGLESTEP, m_pid, NULL, NULL) < 0)
{
writeErr("Fatal: ptrace SINGLESTEP failed\n");
return false;
}
return true;
}
bool resume()
{
if (ptrace(PTRACE_CONT, m_pid, NULL, NULL) < 0)
{
writeErr("Fatal: ptrace continue failed\n");
return false;
}
return true;
}
bool wait()
{
int status = 0;
if (waitpid(m_pid, &status, 0) < 0)
{
writeErr("Fatal: waitpid failed\n");
return false;
}
if (WIFEXITED(status) || WIFSIGNALED(status))
{
writeErr("Fatal: Process is gone\n");
return false;
}
return true;
}
private:
int m_pid = 0;
char m_fileName[PATH_MAX]{};
hl::MemoryRegion m_libc, m_libdl;
uintptr_t m_malloc = 0, m_free = 0, m_dlopen = 0, m_dlclose = 0, m_dlerror = 0;
struct user m_regs{}, m_regsBackup{};
bool m_restoreBackup = false;
uintptr_t m_remoteLibName = 0;
std::string* m_error;
};
bool hl::Inject(int pid, const std::string& libFileName, std::string* error)
{
Injection inj(error);
return inj.findFile(libFileName) && inj.openProcess(pid) && inj.findLibs() && inj.findApis() && inj.remoteLoadLib();
}
std::vector<int> hl::GetPIDsByProcName(const std::string& pname)
{
std::vector<int> result;
DIR* dir = opendir("/proc");
if (dir)
{
struct dirent* entryPID = nullptr;
while ((entryPID = readdir(dir)))
{
bool isPID = true;
auto it = entryPID->d_name;
while (*it)
{
if (!std::isdigit(*it))
{
isPID = false;
break;
}
it++;
}
if (isPID)
{
std::string fileName = "/proc/";
fileName += entryPID->d_name;
fileName += "/cmdline";
std::ifstream file(fileName);
std::string processName;
std::getline(file, processName);
// This code assumes that the process command line in /proc/pid/cmdline was
// not edited or set in weird ways. It is impossible to disambiguate something like:
// /space path/executable with space -argWithSlash/ -argumentBeforeNull\0-otherArg
// Discard everything but the first argument (being the executable path).
auto firstArgEnd = processName.find_first_of("\0\n", 0, 2);
if (firstArgEnd != std::string::npos)
{
processName = processName.substr(0, firstArgEnd);
}
// Remove everything up to the last slash to get the name without path.
// This assumes that there is no slash in the arguments, if the arguments
// are in the first command line argument.
auto slash = processName.find_last_of('/');
if (slash != std::string::npos)
{
processName = processName.substr(slash + 1);
}
if (processName == pname)
{
result.push_back(std::atoi(entryPID->d_name));
}
}
}
closedir(dir);
}
return result;
}