-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRRuntimeTool.m
266 lines (222 loc) · 9.49 KB
/
TRRuntimeTool.m
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
#import "TRRuntimeTool.h"
#include <Foundation/Foundation.h>
#import <SpringBoardServices/SpringBoardServices.h>
#include <dlfcn.h>
#include <mach-o/dyld.h>
#include <mach-o/dyld_images.h>
#include <mach/mach.h>
#include <sys/sysctl.h>
kern_return_t mach_vm_read_overwrite(vm_map_t, mach_vm_address_t, mach_vm_size_t, mach_vm_address_t,
mach_vm_size_t *);
kern_return_t mach_vm_region(vm_map_read_t target_task, mach_vm_address_t *address,
mach_vm_size_t *size, vm_region_flavor_t flavor, vm_region_info_t info,
mach_msg_type_number_t *infoCnt, mach_port_t *object_name);
BOOL readProcessMemory(task_t targetTask, mach_vm_address_t address, mach_vm_size_t size,
unsigned char *buffer) {
mach_vm_size_t outSize = 0;
kern_return_t kr =
mach_vm_read_overwrite(targetTask, address, size, (mach_vm_address_t)buffer, &outSize);
if (kr != KERN_SUCCESS || outSize != size) {
return NO;
}
return YES;
}
static kern_return_t readmem(mach_vm_offset_t *buffer, mach_vm_address_t address,
mach_vm_size_t size, task_t targetTask,
vm_region_basic_info_data_64_t *info) {
kern_return_t kr;
mach_msg_type_number_t info_cnt = sizeof(vm_region_basic_info_data_64_t);
mach_port_t object_name;
mach_vm_size_t size_info;
mach_vm_address_t address_info = address;
kr = mach_vm_region(targetTask, &address_info, &size_info, VM_REGION_BASIC_INFO_64,
(vm_region_info_t)info, &info_cnt, &object_name);
if (kr) {
fprintf(stderr, "[ERROR] mach_vm_region failed with error %d\n", (int)kr);
return KERN_FAILURE;
}
/* read memory - vm_read_overwrite because we supply the buffer */
mach_vm_size_t nread;
kr = mach_vm_read_overwrite(targetTask, address, size, (mach_vm_address_t)buffer, &nread);
if (kr) {
fprintf(stderr, "[ERROR] vm_read failed! %d\n", kr);
return KERN_FAILURE;
} else if (nread != size) {
fprintf(stderr, "[ERROR] vm_read failed! requested size: 0x%llx read: 0x%llx\n", size,
nread);
return KERN_FAILURE;
}
return KERN_SUCCESS;
}
struct segmentRange {
unsigned long long start;
unsigned long long end;
};
struct segmentRange getImageTextSegmentAddr(mach_vm_address_t address, task_t targetTask,
NSMutableString *msg) {
struct segmentRange textSegRange = {0, 0};
struct mach_header_64 header = {0};
vm_region_basic_info_data_64_t region_info = {0};
if (readmem((mach_vm_offset_t *)&header, address, sizeof(struct mach_header_64), targetTask,
®ion_info)) {
[msg appendString:@"[-] can't read header!"];
return textSegRange;
}
if (header.magic != MH_MAGIC_64) {
[msg appendString:@"[-] only arm64 files can be analyzed."];
return textSegRange;
}
struct load_command *lc = (struct load_command *)malloc(header.sizeofcmds);
if (readmem((mach_vm_offset_t *)lc, address + sizeof(struct mach_header_64), header.sizeofcmds,
targetTask, ®ion_info)) {
[msg appendString:@"[-] can't read load_command!"];
free(lc);
return textSegRange;
}
int ncmds = header.ncmds;
struct load_command *lcp = (struct load_command *)((mach_vm_address_t)lc);
while (ncmds--) {
if (lcp->cmd == LC_SEGMENT_64) {
struct segment_command_64 *sc = (struct segment_command_64 *)lcp;
if (!strncmp(sc->segname, "__TEXT", 16)) {
uint32_t aslr_slide = address - sc->vmaddr;
struct section_64 *sect = (struct section_64 *)((mach_vm_address_t)sc +
sizeof(struct segment_command_64));
if (!strncmp(sect->sectname, "__text", 16)) {
textSegRange.start = sect->addr + aslr_slide;
textSegRange.end = textSegRange.start + sect->size;
break;
}
}
}
lcp = (struct load_command *)((mach_vm_address_t)lcp + lcp->cmdsize);
}
free(lc);
return textSegRange;
}
mach_vm_address_t lookup_ptrace_svc(task_t targetTask, mach_vm_address_t target_addr, uint64_t size,
NSMutableString *msg) {
uint64_t read_size = 0x1000;
uint8_t buffer[read_size];
mach_vm_size_t bytesRead;
for (uint64_t offset = 0; offset < size; offset += bytesRead) {
mach_vm_address_t address = target_addr + offset;
kern_return_t kr = mach_vm_read_overwrite(targetTask, address, read_size,
(mach_vm_address_t)buffer, &bytesRead);
if (kr != KERN_SUCCESS || bytesRead == 0) {
break;
}
uint32_t instr;
for (mach_vm_size_t i = 0; i < bytesRead; i += 4) {
if (i + 4 > bytesRead) {
break;
}
instr = *(uint32_t *)(buffer + i);
/*
mov x16, #0x1a -> 0xd2800350
svc #0x80 -> 0xd4001001
*/
if (instr == 0xd2800350) {
if (i + 8 <= bytesRead) {
instr = *(uint32_t *)(buffer + i + 4);
if (instr == 0xd4001001) {
return address + i;
}
}
}
}
}
return -1;
}
@implementation AppRuntimeTool
+ (NSArray *)listProcess {
// 用于存储进程信息的指针
struct kinfo_proc *processes = NULL;
int count, i = 0;
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t size;
if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0) {
return NULL;
}
while (YES) {
size_t previous_size;
processes = realloc(processes, size);
previous_size = size;
if (sysctl(mib, 4, processes, &size, NULL, 0) == 0) break;
if (!(errno == ENOMEM)) return NULL;
size = previous_size * 11 / 10;
}
NSMutableArray *rets = [NSMutableArray array];
count = size / sizeof(struct kinfo_proc);
for (i = 0; i != count; i++) {
struct kinfo_proc *process = &processes[i];
NSString *identifier =
(__bridge NSString *)SBSCopyDisplayIdentifierForProcessID(process->kp_proc.p_pid);
if (identifier != nil) {
if ([identifier isEqualToString:@"com.zznq.trollr2ool"]) continue;
NSString *name =
(__bridge NSString *)SBSCopyLocalizedApplicationNameForDisplayIdentifier(
(__bridge CFStringRef)identifier);
[rets addObject:@{
@"pid" : @(process->kp_proc.p_pid),
@"bundleID" : identifier,
@"name" : name,
}];
}
}
return rets;
}
+ (NSString *)lookupPtraceSvc:(pid_t)pid {
NSMutableString *msg = [[NSMutableString alloc] init];
task_t targetTask;
kern_return_t kr = task_for_pid(mach_task_self(), pid, &targetTask);
if (kr != KERN_SUCCESS) {
[msg appendString:[NSString stringWithFormat:@"[-] failed to get task for pid %d\n", pid]];
return msg;
}
struct task_dyld_info dyld_info;
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
kr = task_info(targetTask, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count);
if (kr != KERN_SUCCESS) {
[msg appendString:@"[-] failed to read dyld_info\n"];
return msg;
}
mach_vm_size_t size = sizeof(struct dyld_all_image_infos);
uint8_t images_addr[size];
if (!readProcessMemory(targetTask, dyld_info.all_image_info_addr, size, images_addr)) {
[msg appendString:@"[-] failed to read dyld_all_image_infos\n"];
return msg;
}
struct dyld_all_image_infos *infos = (struct dyld_all_image_infos *)images_addr;
if (infos->infoArrayCount == 0) {
[msg appendString:@"[-] no images found\n"];
return msg;
}
size = sizeof(struct dyld_image_info) * infos->infoArrayCount;
uint8_t info_addr[size];
if (!readProcessMemory(targetTask, (mach_vm_address_t)infos->infoArray, size, info_addr)) {
[msg appendString:@"[-] failed to read dyld_image_info\n"];
return msg;
}
struct dyld_image_info *info = (struct dyld_image_info *)info_addr;
// main executable image
mach_vm_address_t imageAddress = (mach_vm_address_t)info[0].imageLoadAddress;
[msg appendString:[NSString
stringWithFormat:@"[+] found main image addres: 0x%llx\n", imageAddress]];
struct segmentRange textSegRange = getImageTextSegmentAddr(imageAddress, targetTask, msg);
if (textSegRange.start != 0 || textSegRange.end != 0) {
[msg appendString:
[NSString stringWithFormat:@"[+] found text segment:\nstart(0x%llx)-end(0x%llx)\n",
textSegRange.start, textSegRange.end]];
mach_vm_address_t svc = lookup_ptrace_svc(targetTask, textSegRange.start,
textSegRange.start - textSegRange.end, msg);
if (svc != -1) {
[msg appendString:[NSString stringWithFormat:@"[+] found ptrace svc:\n"
@"\t0x%llx - (mov x16, #0x1a)\n"
@"\t0x%llx - (svc #0x80)",
svc, svc + 4]];
}
}
return msg;
}
@end