-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
bypass.php
380 lines (324 loc) · 10.2 KB
/
bypass.php
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
*
* Copyright (C) 2002-2024 NekoYuzu (MlgmXyysd) All Rights Reserved.
* Copyright (C) 2013-2024 MeowCat Studio All Rights Reserved.
* Copyright (C) 2020-2024 Meow Mobile All Rights Reserved.
*
*/
/**
*
* Xiaomi HyperOS BootLoader Bypass
*
* https://github.com/MlgmXyysd/Xiaomi-BootLoader-Bypass
*
* Bypass Xiaomi HyperOS community restrictions of BootLodaer unlock account bind.
*
* Environment requirement:
* - PHP 8.0+
* - OpenSSL Extension
* - Curl Extension
* - ADB
*
* @author MlgmXyysd
* @version 1.0
*
* All copyright in the software is not allowed to be deleted
* or changed without permission.
*
*/
/***********************
* Configs Start *
***********************/
// Global flag
// If you are running a Global ROM (Non-China Mainland), set it to true
$useGlobal = false;
/*********************
* Configs End *
*********************/
/***************************************
* WARNING *
* Do NOT modify the codes below *
* WARNING *
***************************************/
// Include php-adb library
// https://github.com/MlgmXyysd/php-adb
require_once __DIR__ . DIRECTORY_SEPARATOR . "adb.php";
use MeowMobile\ADB;
/*************************
* Constants Start *
*************************/
global $api;
global $sign_key;
global $data_pass;
global $data_iv;
$api = $useGlobal ? "https://unlock.update.intl.miui.com/v1/" : "https://unlock.update.miui.com/v1/";
$sign_key = "10f29ff413c89c8de02349cb3eb9a5f510f29ff413c89c8de02349cb3eb9a5f5";
$data_pass = "20nr1aobv2xi8ax4";
$data_iv = "0102030405060708";
$version = "1.0";
/***********************
* Constants End *
***********************/
/*************************
* Functions Start *
*************************/
/**
* Formatted Log
* @param $a ADB required ADB instance
* @return array List of connected adb devices
* @author NekoYuzu (MlgmXyysd)
* @date 2022/03/24 14:01:03
*/
function parseDeviceList(ADB $a): array
{
$s = $a -> refreshDeviceList();
$t = array();
foreach ($s as $d) {
if ($d["status"] === $a::CONNECT_TYPE_DEVICE) {
$t[] = array($d["serial"], $d["transport"]);
}
}
return $t;
}
/**
* Formatted Log
* @param $m string optional Message
* @param $c string optional Color
* @param $p string optional Indicator
* @param $t string optional Type (Level)
* @author NekoYuzu (MlgmXyysd)
* @date 2022/03/24 14:50:01
*/
function logf(string $m = "", string $c = "", string $p = "-", string $t = "I"): void
{
switch (strtoupper($c)) {
case "G":
$c = "\033[32m";
break;
case "R":
$c = "\033[31m";
break;
case "Y":
$c = "\033[33m";
break;
default:
$c = "";
}
switch (strtoupper($t)) {
case "W":
$t = "WARN";
break;
case "E":
$t = "ERROR";
break;
case "I":
default:
$t = "INFO";
}
print(date("[Y-m-d] [H:i:s]") . " [" . $t . "] " . $p . " " . $c . $m . "\033[0m" . PHP_EOL);
}
/**
* Curl HTTP wrapper function
* @param $url string required Target url
* @param $method string required Request method
* @param $fields array optional Request body
* @param $header array optional Request header
* @param $useForm bool optional Treat request body as urlencoded form
* @return array Curl response
* @author NekoYuzu (MlgmXyysd)
* @date 2023/11/20 23:50:39
*/
function http(string $url, string $method, array $fields = array(), array $header = array(), bool $useForm = false): array
{
if ($useForm) {
$fields = http_build_query($fields);
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_MAXREDIRS => 10,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 6,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => $method == "POST",
CURLOPT_POSTFIELDS => $fields,
CURLOPT_HTTPHEADER => $header
));
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$info["errno"] = curl_errno($curl);
$info["error"] = curl_error($curl);
$info["request"] = json_encode($fields);
$info["response"] = $response;
curl_close($curl);
return $info;
}
/**
* HTTP POST wrapper
* @param $_api string required Target endpoint
* @param $data array optional Request body
* @param $header array optional Request header
* @param $useForm bool optional Treat request body as urlencoded form
* @return array Curl response
* @return false Response code is not HTTP 200 OK
* @author NekoYuzu (MlgmXyysd)
* @date 2023/11/20 23:55:41
*/
function postApi(string $_api, array $data = array(), array $header = array(), bool $useForm = false): array|false
{
$response = http($GLOBALS["api"] . $_api, "POST", $data, $header, $useForm);
if ($response["http_code"] != 200) {
return false;
}
return json_decode($response["response"], true);
}
/**
* Sign data using HMAC SHA-1
* @param $data string required Data to sign
* @return string Signed hash
* @author NekoYuzu (MlgmXyysd)
* @date 2023/11/21 00:20:56
*/
function signData(string $data): string
{
return strtolower(bin2hex(hash_hmac("sha1", "POST\n/v1/unlock/applyBind\ndata=" . $data . "&sid=miui_sec_android", $GLOBALS["sign_key"], true)));
}
/**
* Decrypt data using AES/CBC/PKCS5Padding
* @param $data string required Data to decrypt
* @return string Decrypted data
* @return false Failed to decrypt
* @author NekoYuzu (MlgmXyysd)
* @date 2023/11/21 00:15:30
*/
function decryptData(string $data): string|false
{
return openssl_decrypt(base64_decode($data), "AES-128-CBC", $GLOBALS["data_pass"], OPENSSL_RAW_DATA, $GLOBALS["data_iv"]);
}
/***********************
* Functions End *
***********************/
/**********************
* Banner Start *
**********************/
logf("************************************", "g");
logf("* Xiaomi HyperOS BootLoader Bypass *", "g");
logf("* By NekoYuzu Version " . $version . " *", "g");
logf("************************************", "g");
logf("GitHub: https://github.com/MlgmXyysd");
logf("XDA: https://xdaforums.com/m/mlgmxyysd.8430637");
logf("X (Twitter): https://x.com/realMlgmXyysd");
logf("PayPal: https://paypal.me/MlgmXyysd");
logf("My Blog: https://www.neko.ink/");
logf("************************************", "g");
/********************
* Banner End *
********************/
/********************
* Main Logic *
********************/
logf("Starting ADB server...");
$adb = new ADB(__DIR__ . DIRECTORY_SEPARATOR . "libraries");
$devices = parseDeviceList($adb);
$devices_count = count($devices);
while ($devices_count != 1) {
if ($devices_count == 0) {
logf("Waiting for device connection...");
} else {
logf("Only one device is allowed to connect, disconnect others to continue. Current number of devices: " . $devices_count);
}
sleep(1);
$devices = parseDeviceList($adb);
$devices_count = count($devices);
}
$device = $devices[0];
$id = $adb -> getDeviceId($device[1], true);
logf("Processing device " . $device[0] . "(" . $device[1] . ")...");
$adb -> clearLogcat($id);
$adb -> runAdb($id . "shell svc data enable");
logf("Finding BootLoader unlock bind request...");
$focus = $adb -> getCurrentActivity();
if ($focus[0] != "com.android.settings") {
if ($focus[0] != "NotificationShade") {
$adb -> runAdb($id . "shell am start -a android.settings.APPLICATION_DEVELOPMENT_SETTINGS");
}
} else {
if ($focus[1] != "com.android.settings.bootloader.BootloaderStatusActivity") {
$adb -> runAdb($id . "shell am start -a android.settings.APPLICATION_DEVELOPMENT_SETTINGS");
}
}
logf("Now you can bind account in the developer options.", "y", "*");
$args = $headers = null;
$process = proc_open($adb -> bin . " " . $id . "logcat *:S CloudDeviceStatus:V", array(
1 => ["pipe", "w"]
), $pipes);
if (is_resource($process)) {
while (!feof($pipes[1])) {
$output = fgets($pipes[1]);
if (str_contains($output, "CloudDeviceStatus: args:")) {
if (preg_match("/args:(.*)/", $output, $matches)) {
$args = trim($matches[1]);
}
$adb -> runAdb($id . "shell svc data disable");
}
if (str_contains($output, "CloudDeviceStatus: headers:")) {
if (preg_match("/headers:(.*)/", $output, $matches)) {
$headers = trim($matches[1]);
}
logf("Account bind request found! Let's block it.");
break;
}
}
fclose($pipes[1]);
}
logf("Refactoring parameters...");
$data = json_decode(decryptData($args), true);
// V816 is the special identity for HyperOS in MIUI version
$data["rom_version"] = str_replace("V816", "V14", $data["rom_version"]);
$data = json_encode($data);
$sign = signData($data);
$headers = decryptData($headers);
$cookies = null;
if (preg_match("/Cookie=\[(.*)\]/", $headers, $matches)) {
$cookies = trim($matches[1]);
}
logf("Sending POST request...");
$res = postApi("unlock/applyBind", array(
"data" => $data,
"sid" => "miui_sec_android",
"sign" => $sign
), array(
"Cookie: " . $cookies,
"Content-Type: application/x-www-form-urlencoded"
), true);
$adb -> runAdb($id . "shell svc data enable");
if (!$res) {
logf("Fail to send request, check your internet connection.", "r", "!");
exit();
}
switch ($res["code"]) {
case 0:
logf("Target account: " . $res["data"]["userId"], "g");
logf("Account bound successfully, wait time can be viewed in the unlock tool.", "g");
break;
case 401:
logf("Account credentials have expired, re-login to your account in your phone. (401)", "y");
break;
case 20086:
logf("Device credentials expired. (20086)", "y");
break;
case 30001:
logf("Binding failed, this device has been forced to verify the account qualification by Xiaomi. (30001)", "y");
break;
case 86015:
logf("Fail to bind account, invalid device signature. (86015)", "y");
break;
default:
logf($res["descEN"] . " (" . $res["code"] . ")", "y");
}