-
Notifications
You must be signed in to change notification settings - Fork 0
/
melkor.c
269 lines (223 loc) · 5.7 KB
/
melkor.c
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
/*
* melkor.c: Memory Hacking Helper
*
* (C) Copyright 2015
* Author: Eray Arslan <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*/
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <mach/mach_init.h>
#include <mach/mach_traps.h>
#include <mach/mach_types.h>
#include <mach/mach_vm.h>
#include <mach/vm_map.h>
#include <mach/mach.h>
#include <dlfcn.h>
#include <mach-o/fat.h>
#include <mach-o/getsect.h>
#include <mach-o/dyld_images.h>
#include <mach-o/dyld.h>
#include <stdio.h>
#include <stdlib.h>
kern_return_t merror;
bool isRoot() {
if (getuid()) {
if (geteuid()) {
return false;
}
}
return true;
}
bool isProcessValid(mach_port_t process) {
return MACH_PORT_VALID(process);
}
kern_return_t isNoError() {
return merror == KERN_SUCCESS;
}
mach_port_t getProcess(int pid) {
mach_port_t _result;
merror = task_for_pid(mach_task_self(), pid, &_result);
return _result;
}
task_dyld_info_data_t getInfo(mach_port_t process) {
task_dyld_info_data_t dyld_info;
mach_msg_type_number_t task_info_outCnt = TASK_DYLD_INFO_COUNT;
merror = task_info(
process,
TASK_DYLD_INFO,
(task_info_t)&dyld_info,
&task_info_outCnt
);
return dyld_info;
}
uintptr_t getBaseAddress(mach_port_t process) {
vm_map_offset_t vmoffset;
vm_map_size_t vmsize;
uint32_t nesting_depth = 0;
struct vm_region_submap_info_64 vbr;
mach_msg_type_number_t vbrcount = 16;
merror = mach_vm_region_recurse(
process,
&vmoffset,
&vmsize,
&nesting_depth,
(vm_region_recurse_info_t)&vbr,
&vbrcount
);
return vmoffset;
}
uintptr_t getBaseAddressByRegion(mach_port_t process, int region) {
kern_return_t lerror = KERN_SUCCESS;
vm_address_t address = 0;
vm_size_t size = 0;
uint32_t depth = 1;
int region_id = 0;
uintptr_t _result;
while (true) {
struct vm_region_submap_info_64 info;
mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
lerror = vm_region_recurse_64(
process,
&address,
&size,
&depth,
(vm_region_info_64_t)&info,
&count
);
if (lerror == KERN_INVALID_ADDRESS){
break;
}
if (info.is_submap) {
depth++;
} else {
if (region_id++ == region) {
_result = (uintptr_t)address;
}
address += size;
}
}
return _result;
}
mach_vm_address_t doDisableASLR(mach_port_t process) {
vm_address_t iter = 0;
kern_return_t lerror = KERN_SUCCESS;
while (1) {
struct mach_header mh = {0};
vm_address_t addr = iter;
vm_size_t lsize = 0;
uint32_t depth;
mach_vm_size_t bytes_read = 0;
struct vm_region_submap_info_64 info;
mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
if (vm_region_recurse_64(
process,
&addr,
&lsize,
&depth,
(vm_region_info_t)&info,
&count)
) {
break;
}
lerror = mach_vm_read_overwrite(
process,
(mach_vm_address_t)addr,
(mach_vm_size_t)sizeof(struct mach_header),
(mach_vm_address_t)&mh,
&bytes_read
);
if (lerror == KERN_SUCCESS &&
bytes_read == sizeof(struct mach_header)) {
if ((
mh.magic == MH_MAGIC ||
mh.magic == MH_MAGIC_64
) && mh.filetype == MH_EXECUTE) {
return addr;
break;
}
}
iter = addr + lsize;
}
return -1;
}
int detectRegionId(mach_port_t process, uintptr_t pointerAddress) {
kern_return_t lerror = KERN_SUCCESS;
vm_address_t address = 0;
vm_size_t size = 0;
uint32_t depth = 1;
int region_id = 0;
uintptr_t _result;
while (true) {
struct vm_region_submap_info_64 info;
mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
lerror = vm_region_recurse_64(
process,
&address,
&size,
&depth,
(vm_region_info_64_t)&info,
&count
);
if (lerror == KERN_INVALID_ADDRESS){
break;
}
if (info.is_submap) {
depth++;
} else {
if (address >= pointerAddress &&
pointerAddress <= address + size) {
return region_id;
}
address += size;
region_id++;
}
}
return -1;
}
void * readAddress(mach_port_t process, uintptr_t address, int size) {
void **bytes;
unsigned int _result_size;
vm_offset_t dataPointer = 0;
merror = vm_read(process, address, size, &dataPointer, &_result_size);
bytes = (void *)dataPointer;
return *bytes;
}
void writeAddress(mach_port_t process, uintptr_t address, int size, void * value) {
merror = vm_write(process, address, (vm_address_t)value, size);
}
vm_offset_t* readAddressLikeStruct(mach_port_t process, uintptr_t address, int size) {
unsigned int _result_size;
void* infos;
vm_read(process, address, size, (vm_offset_t*)&infos, &_result_size);
return (vm_offset_t*)infos;
}
static void* readAddressLikeOMG(task_port_t target_task, const void* address, size_t len) {
void* result = NULL;
kern_return_t lerror = KERN_SUCCESS;
mach_vm_address_t page_address = (uint32_t)address & (-4096);
mach_vm_address_t last_page_address = ((uint32_t)address + len + 4095) & (-4096);
mach_vm_size_t page_size = last_page_address - page_address;
uint8_t* local_start;
uint32_t local_len;
lerror = vm_read(
target_task,
page_address,
page_size,
(vm_offset_t*)&local_start,
&local_len
);
if (lerror == KERN_SUCCESS) {
result = malloc(len);
if (result != NULL) {
memcpy(result, &local_start[(uint32_t)address - page_address], len);
}
vm_deallocate(mach_task_self(), (uintptr_t)local_start, local_len);
}
return result;
}