-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTweakGuestApp.m
More file actions
96 lines (85 loc) · 4.04 KB
/
Copy pathTweakGuestApp.m
File metadata and controls
96 lines (85 loc) · 4.04 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
#import "LCKXPCServer.h"
#import "LCTaskForPIDTweak.h"
static NSString *containerLockPath;
static int (*orig_sysctl)(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) = sysctl;
static int hook_sysctl_CTL_KERN_PROC_ALL(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {
// TODO: implement via LCKXPCServer
struct kinfo_proc *procs = (struct kinfo_proc *)oldp;
__block NSArray<NSNumber *> *pids;
[LCKXPCService.sharedClientProxy allRunningProcessesWithReply:^(NSArray<NSNumber *> *p) {
pids = p;
}];
size_t count = 0;
for(NSNumber *pid in pids) {
csops(pid.intValue, 0, NULL, 0);
if(errno == ESRCH) continue;
if(oldp) {
procs[count].kp_eproc.e_ucred.cr_uid = 501; // uid=501
procs[count].kp_eproc.e_pcred.p_rgid = 501; // gid=501
procs[count].kp_eproc.e_ppid = 1; // ppid=1
procs[count].kp_proc.p_pid = pid.intValue;
snprintf(procs[count].kp_proc.p_comm, sizeof(procs[count].kp_proc.p_comm), "LiveProcess");
}
count++;
}
if(oldlenp) *oldlenp = count * sizeof(struct kinfo_proc);
return 0;
}
int hook_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {
if(namelen>=3 && name[0] == CTL_KERN && name[1] == KERN_PROC && name[2] == KERN_PROC_ALL) {
return hook_sysctl_CTL_KERN_PROC_ALL(name, namelen, oldp, oldlenp, newp, newlen);
}
return orig_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
}
kern_return_t hook_task_for_pid(mach_port_name_t target_tport, int pid, mach_port_name_t *t) {
return bootstrap_look_up(bootstrap_port, [LCTaskForPIDTweak taskPortNameForPID:pid].UTF8String, t);
}
int hook_proc_pidpath(int pid, char *buffer, uint32_t buffersize) {
if(buffersize < 1 || !buffer) return 0;
[LCKXPCService.sharedClientProxy proc_pidpath:pid reply:^(NSString *path) {
if(path) {
bzero(buffer, buffersize);
strncpy(buffer, path.UTF8String, MIN(path.length, buffersize));
}
}];
return (int)strlen((char *)buffer);
}
void guest_check_in(void) {
// save old thread states
exception_mask_t masks[32];
mach_msg_type_number_t masksCnt;
exception_handler_t handlers[32];
exception_behavior_t behaviors[32];
thread_state_flavor_t flavors[32];
mach_port_t thread = mach_thread_self();
thread_get_exception_ports(thread, EXC_MASK_BAD_INSTRUCTION, masks, &masksCnt, handlers, behaviors, flavors);
thread_set_exception_ports(thread, EXC_MASK_BAD_INSTRUCTION, exc_port, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, ARM_THREAD_STATE64);
// crash here, borrow genter. this will send my task port to LC
__asm__(".long 0x00201420");
// restore old thread states
for (int i = 0; i < masksCnt; i++){
thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_INSTRUCTION, handlers[i], behaviors[i], flavors[i]);
mach_port_deallocate(mach_task_self(), handlers[i]);
}
// destroy leftover port
mach_port_deallocate(mach_task_self(), exc_port);
exc_port = 0;
// check in with the kernel server
[LCKXPCService.sharedClientProxy checkinWithInfo:@{
@"ProgramArguments": NSProcessInfo.processInfo.arguments
}];
}
void init_guest_apps(void) {
litehook_rebind_symbol = dlsym(RTLD_DEFAULT, "litehook_rebind_symbol");
assert(litehook_rebind_symbol);
containerLockPath = [PrivClass(LCSharedUtils) containerLockPath].path;
kern_return_t kr = bootstrap_look_up(bootstrap_port, LCTaskForPIDTweak.exceptionPortName.UTF8String, &exc_port);
if(kr != KERN_SUCCESS) {
LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: bootstrap_look_up failed: %d", kr]);
return;
}
guest_check_in();
litehook_rebind_symbol(LITEHOOK_REBIND_GLOBAL, sysctl, hook_sysctl, nil);
litehook_rebind_symbol(LITEHOOK_REBIND_GLOBAL, proc_pidpath, hook_proc_pidpath, nil);
litehook_rebind_symbol(LITEHOOK_REBIND_GLOBAL, task_for_pid, hook_task_for_pid, nil);
}