-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
198 lines (156 loc) · 5.43 KB
/
main.cpp
File metadata and controls
198 lines (156 loc) · 5.43 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
#include <iostream>
#include <sstream>
#include <memory>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <dlfcn.h>
#include <elf.h>
#include "Process.h"
#include "ElfMemoryFile64.h"
void WriteHex(const uint8_t* data, int len)
{
for(int i = 0 ; i < len; ++i)
{
std::cout << std::hex;
if(i % 16 == 0)
{
if(i != 0)
{
std::cout << std::endl;
}
std::cout << "0x" << std::setw(std::ceil(std::log(len)/std::log(16.))) << std::setfill('0') << i << ": ";
}
std::cout << std::setw(2) << std::setfill('0') << static_cast<int>(data[i]) << " ";
}
std::cout << std::endl;
}
std::unique_ptr<uint8_t []> ReadProcessData(pid_t pId, uintptr_t offset, size_t len)
{
auto buffer = std::unique_ptr<uint8_t []>(new uint8_t[len]);
for(uintptr_t off = offset; off < offset + len; off += sizeof(uintptr_t))
{
uintptr_t data = ptrace(PTRACE_PEEKDATA, pId, off, 0);
unsigned int copyLen = len - (off - offset);
unsigned int realCopyLen = copyLen > sizeof(uintptr_t) ? sizeof(uintptr_t) : copyLen;
for(unsigned int i = 0; i < realCopyLen; ++i)
{
buffer[i + off - offset] = static_cast<unsigned char>(data >> (i * 8));
}
}
return std::move(buffer);
}
void WriteProcessData(pid_t pId, const uint8_t* data, uintptr_t offset, size_t len)
{
for(uintptr_t off = offset; off < offset + len; off += sizeof(uintptr_t))
{
unsigned int copyLen = len - (off - offset);
unsigned int realCopyLen = copyLen > sizeof(uintptr_t) ? sizeof(uintptr_t) : copyLen;
std::unique_ptr<uint8_t []> extraData;
if(realCopyLen < sizeof(uintptr_t))
{
extraData = ReadProcessData(pId, off, sizeof(uintptr_t));
}
std::array<uint8_t, sizeof(uintptr_t)> writeData;
for(unsigned int i = 0; i < sizeof(uintptr_t); ++i)
{
if(i < realCopyLen)
{
writeData[i] = data[off - offset + i];
}
else
{
writeData[i] = extraData[i];
}
}
ptrace(PTRACE_POKEDATA, pId, off, reinterpret_cast<void*>(*reinterpret_cast<uintptr_t*>(writeData.data())));
}
}
int main(int argc, char** argv)
{
///Shellcode patch. This will load our library into the target process.
uint8_t patch [] =
{
0x48, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8 byte offset to library name */ //movq offset, %rdi
0xbe, 0x02, 0x01, 0x00, 0x00, //movl RLTD_GLOBAL, %rsi
0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //mov dlopen, %rax
0xff, 0xd0, //callq *%rax
0xcc //int 3
};
const int patchLen = 28;
pid_t pId = 28167;
if(argc != 3)
{
std::cerr << "No pId and injection library specified, aborting";
return 0;
}
std::stringstream pIdStream;
pIdStream << argv[1];
pIdStream >> pId;
std::string fileName = argv[2];
//Attach to our process and send SIGSTOP, aborting if it fails
if(ptrace(PTRACE_ATTACH, pId, 0, 0) != 0)
{
std::cerr << "Unable to attach to process " << pId << ", aborting." << std::endl;
return 0;
}
Process process(pId);
auto dlRegions = process.getLibraryRegions("/usr/lib/libdl");
Elf::ElfMemoryFile64 libdl(dlRegions, process);
std::cout << "Attached to process " << pId << std::endl;
const int readLen = patchLen + fileName.length() + 1;
//Wait for the process to stop executing so we can edit the code
int waitStatus = 0;
waitpid(pId, &waitStatus, __WALL);
//Get the current processor register values
user_regs_struct registers;
ptrace(PTRACE_GETREGS, pId, nullptr, ®isters);
std::cout << "Process paused. Instruction pointer = " << std::hex << registers.rip << std::endl;
//Write the library name address into the patch
*reinterpret_cast<uintptr_t*>(&patch[2]) = registers.rip + patchLen;
Elf::ElfExport64 dlOpenExport = libdl.getExport("dlopen");
uintptr_t dlopenPtr = libdl.getMemoryPointerFromFileOffset(dlOpenExport.offset);
//Write the address of dlopen into the patch
*reinterpret_cast<uintptr_t*>(&patch[17]) = dlopenPtr;
//First, read the original shellcode at the instruction pointer
auto data = ReadProcessData(pId, registers.rip, readLen);
//Replace the shellcode with our patch
WriteProcessData(pId, patch, registers.rip, patchLen);
WriteProcessData(pId, reinterpret_cast<const uint8_t*>(fileName.c_str()), registers.rip + patchLen, fileName.length() + 1);
auto data2 = ReadProcessData(pId, registers.rip, readLen);
std::cout << "Code injected successfully... executing stub." << std::endl;
user_regs_struct registers2;
ptrace(PTRACE_CONT, pId, 0, 0);
waitpid(pId, &waitStatus, __WALL);
ptrace(PTRACE_GETREGS, pId, nullptr, ®isters2);
std::cout << "Execution complete, instruction pointer = " << std::hex << registers2.rip << std::endl;
std::cout << "dlopen return value " << registers2.rax << std::endl;
if(registers2.rax == 0)
{
std::cout << "Injection failed!" << std::endl;
}
else
{
std::cout << "Injection success!" << std::endl;
}
std::cout << "Restoring old code..." << std::endl;
//Replace the patched shellcode with the original code
WriteProcessData(pId, data.get(), registers.rip, readLen);
auto data3 = ReadProcessData(pId, registers.rip, readLen);
//Replace the registers to their initial values
ptrace(PTRACE_SETREGS, pId, nullptr, ®isters);
/*WriteHex(data.get(), readLen);
std::cout << std::endl;
WriteHex(data2.get(), readLen);
std::cout << std::endl;
WriteHex(data3.get(), readLen);*/
std::cout << "Detatching..." << std::endl;
ptrace(PTRACE_DETACH, pId, 0, 0);
std::cout << "Done!" << std::endl;
return 0;
}