Repository navigation
Expand file tree
/
Copy pathoracle.cpp
More file actions
344 lines (275 loc) · 6.75 KB
/
Copy pathoracle.cpp
File metadata and controls
344 lines (275 loc) · 6.75 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
#include <algorithm>
#include <asm-generic/errno-base.h>
#include <cerrno>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <fmt/core.h>
#include <ios>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <vector>
#include <charconv>
#include <sys/wait.h>
#include <sys/mman.h>
#include "types.h"
struct all{
all operator*()const{return {};}
all operator++(int){return *this;}
bool operator==(all) const{return false;}
bool operator!=(all) const{return true;}
all operator++(){return *this;}
all begin()const{return {};}
all end()const{return {};}
};
inline constexpr all me;
int g_devnull;
class Ipc
{
public:
int fds[4];
Ipc()
{
if (pipe(this->fds) || pipe(this->fds + 2))
perror("IPC pipe constructor failed");
}
int fatherRead(void *buf, u64 sz)
{
return read(this->fds[0], buf, sz);
}
int workerWrite(void *buf, u64 sz)
{
return write(this->fds[1], buf, sz);
}
int workerRead(void *buf, u64 sz)
{
return read(this->fds[2], buf, sz);
}
int fatherWrite(void *buf, u64 sz)
{
return write(this->fds[3], buf, sz);
}
private:
};
namespace dbg
{
using fmt::print;
}
struct MapEntry
{
u64 start;
u64 end;
u64 size;
MapEntry(u64 start, u64 end) :
start(start), end(end), size(end-start)
{
}
friend std::ostream & operator <<(std::ostream &out, const MapEntry &obj) {
out << fmt::format("MapEntry(start = {:#x}, end = {:#x}, size = {:#x})", obj.start, obj.end, obj.size);
return out;
}
operator std::string() const {
std::ostringstream out;
out << *this;
return out.str();
}
};
u64 fromHex(std::string &s)
{
u64 result;
std::from_chars(s.c_str(), s.c_str() + s.size(), result, 16);
return result;
}
class ProcMap
{
public:
u64 stackAddress;
ProcMap()
{
stackAddress = 0;
}
bool add(std::string line)
{
std::regex rgx("^(.*?)-(.*?)\\s(.*?)\\s(.*?)\\s(.*?)\\s(.*?)\\s(.*)$");
std::smatch match;
std::vector<std::string> vmatch;
if (!std::regex_match(line, match, rgx))
return false;
for (std::string m: match)
vmatch.push_back(m);
auto start = fromHex(vmatch[1]);
auto end = fromHex(vmatch[2]);
this->_entries.push_back(MapEntry(start, end));
if ((vmatch.end() - 1)->find("stack") != std::string::npos)
this->stackAddress = start;
return true;
}
bool has(u64 addr)
{
for (auto e : this->_entries)
{
if (addr >= e.start && addr < e.end)
return true;
}
return false;
}
void clear()
{
this->_entries.clear();
}
u64 getStackAddress()
{
return this->stackAddress;
//if (this->_entries.empty())
// return 0;
//return (this->_entries.end() - 4)->start;
}
friend std::ostream & operator <<(std::ostream &out, const ProcMap &obj) {
//out << fmt::format("{}", obj._entries);
out << "ProcMap:\n";
for (auto e : obj._entries)
{
out << fmt::format("\t{:x} {:#x} {:#x}\n", e.start, e.end, e.size);
}
return out;
}
operator std::string() const {
std::ostringstream out;
out << *this;
return out.str();
}
private:
std::vector<MapEntry> _entries;
pid_t _pid;
};
void readProcMappings(ProcMap &procmap)
{
std::string line;
std::ifstream maps ("/proc/self/maps");
if (!maps.is_open())
return;
while (maps.good())
{
std::getline(maps, line);
procmap.add(line);
}
}
namespace Worker
{
bool isAddressMapped(u64 addr)
{
int wstatus = 0;
if (write(g_devnull, (void*)addr, 1) == -1)
{
if (errno != EFAULT)
{
std::cout << "unexpected write error: " << strerror(errno) << std::endl << std::flush;
}
return false;
}
return true;
}
void worker(Ipc &ipc)
{
for (all t:me)
{
int rv;
u64 addr;
if (rv = ipc.workerRead(&addr, sizeof(addr)), rv != sizeof(addr))
{
fmt::print("workerRead: %d\n", rv);
continue;
}
fmt::print("worker: checking {:#x}\n", addr);
std::cout << std::flush;
bool result = isAddressMapped(addr);
if (rv = ipc.workerWrite(&result, sizeof(result)), rv != sizeof(result))
{
fmt::print("workerWrite: %d\n", rv);
continue;
}
}
}
}
bool fork_isAddressMapped(u64 addr)
{
int wstatus = 0;
pid_t pid = fork();
if (!pid)
{
if (write(g_devnull, (void*)addr, 1) == -1)
{
if (errno == EFAULT)
exit(1);
else
{
std::cout << "isAddressMapped: " << strerror(errno) << std::endl;
exit(2);
}
}
exit(0);
}
while (waitpid(pid, &wstatus, 0), !WIFEXITED(wstatus));
return WEXITSTATUS(wstatus) == 0;
}
pid_t spawnWorker(Ipc &ipc)
{
pid_t pid = fork();
if (!pid)
{
Worker::worker(ipc);
fmt::print("Worker {:d} exiting\n", getpid());
exit(0);
}
return pid;
}
int main(int argc, char **argv, char **envp)
{
ProcMap procmap;
Ipc ipc;
if (open("/dev/null", O_RDWR) == -1)
{
perror("open");
return 1;
}
fmt::print("worker spawned with pid = {:d}\n", spawnWorker(ipc));
procmap.clear();
readProcMappings(procmap);
std::cout << procmap << std::endl;
u64 stack_addr = procmap.getStackAddress();
fmt::print("stack_addr = {:#x}\n", stack_addr);
u64 i;
for( i = 0; fork_isAddressMapped(stack_addr - i*0x1000) == true; ++i);
--i;
fmt::print("i = {:d}, stack = {:#x}, reached = {:#x}\n", i, stack_addr, stack_addr - i*0x1000);
for (all t:me) // undefined behaviour meme
{
u64 addr;
bool response;
std::cout << "Address: " << std::flush;
std::cin >> std::hex >> addr;
// ask the worker
if (ipc.fatherWrite(&addr, sizeof(addr)) != sizeof(addr))
{
perror("fatherWrite");
continue;
}
// get reply
if (ipc.fatherRead(&response, sizeof(response)) != sizeof(response))
{
perror("fatherRead");
continue;
}
std::cout << (response ? "mapped\n" : "not mapped\n") << std::flush; //<< std::endl;
}
return 0;
}