This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathHUDMainApplication.mm
142 lines (119 loc) · 5.51 KB
/
HUDMainApplication.mm
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
//
// HUDMainApplication.mm
// TrollSpeed
//
// Created by Lessica on 2024/1/24.
//
#import <notify.h>
#import <pthread.h>
#import <mach/mach.h>
#import <mach-o/dyld.h>
#import <objc/runtime.h>
#import "pac_helper.h"
#import "UIEventFetcher.h"
#import "UIEventDispatcher.h"
#import "HUDMainApplication.h"
#import "UIApplication+Private.h"
@implementation HUDMainApplication
- (instancetype)init
{
if (self = [super init])
{
log_debug(OS_LOG_DEFAULT, "- [HUDMainApplication init]");
{
int outToken;
notify_register_dispatch(NOTIFY_DISMISSAL_HUD, &outToken, dispatch_get_main_queue(), ^(int token) {
notify_cancel(token);
// Fade out the HUD window
[UIView animateWithDuration:FADE_OUT_DURATION animations:^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[self.windows firstObject] setAlpha:0.0];
#pragma clang diagnostic pop
} completion:^(BOOL finished) {
// Terminate the HUD app
[self terminateWithSuccess];
}];
});
}
do {
UIEventDispatcher *dispatcher = (UIEventDispatcher *)[self valueForKey:@"eventDispatcher"];
if (!dispatcher)
{
log_error(OS_LOG_DEFAULT, "failed to get ivar _eventDispatcher");
break;
}
log_debug(OS_LOG_DEFAULT, "got ivar _eventDispatcher: %p", dispatcher);
if ([dispatcher respondsToSelector:@selector(_installEventRunLoopSources:)])
{
CFRunLoopRef mainRunLoop = CFRunLoopGetMain();
[dispatcher _installEventRunLoopSources:mainRunLoop];
}
else
{
IMP runMethodIMP = class_getMethodImplementation([self class], @selector(_run));
if (!runMethodIMP)
{
log_error(OS_LOG_DEFAULT, "failed to get - [UIApplication _run] method");
break;
}
uint32_t *runMethodPtr = (uint32_t *)make_sym_readable((void *)runMethodIMP);
log_debug(OS_LOG_DEFAULT, "- [UIApplication _run]: %p", runMethodPtr);
void (*orig_UIEventDispatcher__installEventRunLoopSources_)(id _Nonnull, SEL _Nonnull, CFRunLoopRef) = NULL;
for (int i = 0; i < 0x140; i++)
{
// mov x2, x0
// mov x0, x?
if (runMethodPtr[i] != 0xaa0003e2 || (runMethodPtr[i + 1] & 0xff000000) != 0xaa000000)
continue;
// bl -[UIEventDispatcher _installEventRunLoopSources:]
uint32_t blInst = runMethodPtr[i + 2];
uint32_t *blInstPtr = &runMethodPtr[i + 2];
if ((blInst & 0xfc000000) != 0x94000000)
{
log_error(OS_LOG_DEFAULT, "not a BL instruction: 0x%x, address %p", blInst, blInstPtr);
continue;
}
log_debug(OS_LOG_DEFAULT, "found BL instruction: 0x%x, address %p", blInst, blInstPtr);
int32_t blOffset = blInst & 0x03ffffff;
if (blOffset & 0x02000000)
blOffset |= 0xfc000000;
blOffset <<= 2;
log_debug(OS_LOG_DEFAULT, "BL offset: 0x%x", blOffset);
uint64_t blAddr = (uint64_t)blInstPtr + blOffset;
log_debug(OS_LOG_DEFAULT, "BL target address: %p", (void *)blAddr);
// cbz x0, loc_?????????
uint32_t cbzInst = *((uint32_t *)make_sym_readable((void *)blAddr));
if ((cbzInst & 0xff000000) != 0xb4000000)
{
log_error(OS_LOG_DEFAULT, "not a CBZ instruction: 0x%x", cbzInst);
continue;
}
log_debug(OS_LOG_DEFAULT, "found CBZ instruction: 0x%x, address %p", cbzInst, (void *)blAddr);
orig_UIEventDispatcher__installEventRunLoopSources_ = (void (*)(id _Nonnull __strong, SEL _Nonnull, CFRunLoopRef))make_sym_callable((void *)blAddr);
}
if (!orig_UIEventDispatcher__installEventRunLoopSources_)
{
log_error(OS_LOG_DEFAULT, "failed to find -[UIEventDispatcher _installEventRunLoopSources:]");
break;
}
log_debug(OS_LOG_DEFAULT, "- [UIEventDispatcher _installEventRunLoopSources:]: %p", orig_UIEventDispatcher__installEventRunLoopSources_);
CFRunLoopRef mainRunLoop = CFRunLoopGetMain();
orig_UIEventDispatcher__installEventRunLoopSources_(dispatcher, @selector(_installEventRunLoopSources:), mainRunLoop);
}
UIEventFetcher *fetcher = [[objc_getClass("UIEventFetcher") alloc] init];
[dispatcher setValue:fetcher forKey:@"eventFetcher"];
if ([fetcher respondsToSelector:@selector(setEventFetcherSink:)]) {
[fetcher setEventFetcherSink:dispatcher];
}
else
{
/* Tested on iOS 15.1.1 and below */
[fetcher setValue:dispatcher forKey:@"eventFetcherSink"];
}
[self setValue:fetcher forKey:@"eventFetcher"];
} while (NO);
}
return self;
}
@end