-
Notifications
You must be signed in to change notification settings - Fork 21
/
functions.php
540 lines (493 loc) · 14.9 KB
/
functions.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
require_once('log.php');
function curlget(string $url, array $headers = null, array $post = null, array $opt_arr = [])
{
//param1 -> url for cURL, param2 -> pass array to be used as header
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
if(isset($headers)) curl_setopt($handle, CURLOPT_HTTPHEADER, $headers) ;
if(!empty($post)) curl_setopt($handle, CURLOPT_POSTFIELDS, $post);
if(!empty($opt_arr)) curl_setopt_array($handle, $opt_arr);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_error($handle);
$response = curl_exec($handle);
curl_close($handle);
$json = json_decode(trim($response));
return (is_object($json) ? $json : $response);
}
function print_usage()
{
global $argv, $options;
echo("Telnetd Ramdisk\n");
echo("Lets you boot your device with a ramdisk and connect to it using telnet\n\n");
echo("OPTIONS:\n");
foreach($options as $opt_short => $option)
{
echo(" -$opt_short\t{$option['help']}\n");
}
echo("\nExample: php {$argv[0]} -d iPhone10,6 -b D221AP -v 13.0 -s 13.0.shsh2\n");
echo("shsh2 file can be from any version but needs to be from your device (ecid). Use: https://shsh.host/\n");
echo("if device parameters aren't specified, the program will attempt to detect DFU devices attached to this machine\n\n");
}
function ask($question){
echo("\e[1m\x1B[31m{$question}\e[0m\e[0m\n");
$h = fopen("php://stdin","r");
$answer = fgets($h);
fclose($h);
return ($answer === "y" . PHP_EOL || $answer === "yes" . PHP_EOL);
}
function mount_rd(){
Log::writeVerboseInfo("Mounting ramdisk.dmg");
$try_mount = execute("hdiutil attach ramdisk.dmg 2>&1",1);
$mount_point = explode(" ",preg_replace('/\s+/', ' ', $try_mount));
unset($mount_point[0]);
unset($mount_point[sizeof($mount_point)]);
$mount_point = implode(" ",$mount_point);
return $mount_point;
}
function unmount($mountpoint){
return execute("hdiutil detach \"{$mountpoint}\" 2>&1",1);
}
function detect_device(): ?object
{
$device_properties = array();
$command = "irecovery -q";
$stdout = execute($command, true);
if(preg_match_all("/(.+?)\:.\s*(?:(0x[0-9a-f]+)|(.+))\n/i", $stdout, $matches))
{
foreach($matches[1] as $index => $match)
{
if(!empty($matches[2][$index]))
{
$number = hexdec(trim($matches[2][$index]));
$device_properties[trim($match)] = $number;
}
else
{
$device_properties[trim($match)] = trim($matches[3][$index]);
}
}
if(empty($device_properties['CPID']) || empty($device_properties['BDID'])) return null;
$device = find_device($device_properties['CPID'], $device_properties['BDID']);
if($device)
{
$device->ecid_raw = $device_properties['ECID'];
$device->ecid = strtoupper(dechex($device_properties['ECID']));
return $device;
}
}
return null;
}
function read_device(): ?object
{
if( !($device = detect_device()) )
{
return null;
}
$device_text = "{$device->name} ({$device->identifier}) with boardConfig {$device->BoardConfig}\n";
$device_text .= "Is this the device you're making the ramdisk for?";
return (ask($device_text) ? $device : null);
}
function find_device(int $cpid, int $bdid): ?object
{
$fw = get_firmware_data();
// lookup in irecovery first
$irecv_device = irecv_lookup_device($cpid, $bdid);
if($irecv_device)
{
$identifier = $irecv_device[0];
if(isset($fw->devices->$identifier))
{
$out_device = $fw->devices->$identifier;
$out_device->BoardConfig = $irecv_device[1];
$out_device->cpid = $irecv_device[3];
$out_device->bdid = $irecv_device[2];
$out_device->identifier = $irecv_device[0];
return $out_device;
}
}
foreach($fw->devices as $identifier => $device)
{
if( ($device->cpid === $cpid) && ($device->bdid === $bdid) )
{
$device->identifier = $identifier;
return $device;
}
}
return null;
}
function execute($command, $show_output = false){
Log::writeVerboseInfo("Executing {$command}");
$h = popen($command . " 2>&1","r");
$o = "";
while (!feof($h)) {
$b = fread($h,1024);
$o .= $b;
}
Log::main()->writeLog($o, LOGTYPE_DEBUG, $show_output);
fclose($h);
return $o;
}
function get_firmware_data(bool $force_refresh = false): stdClass
{
$url = 'https://api.ipsw.me/v2.1/firmwares.json/condensed';
$cache_path = __DIR__ . "/.cache/";
$cache_file = $cache_path."firmwares.json";
if(!is_dir($cache_path)) mkdir($cache_path);
if(!file_exists($cache_file)) $force_refresh = true;
if($force_refresh)
{
execute("wget $url -O $cache_file");
}
if(!file_exists($cache_file)) return null;
$fw_data = file_get_contents($cache_file);
$fw = json_decode($fw_data);
unset($fw_data);
return $fw;
}
function get_ipsw_url(string $device, string $version): ?string
{
Log::writeVerboseInfo("Getting ipsw url from ipsw.me API");
$fw = get_firmware_data();
if(!isset($fw->devices->$device)) return null;
foreach($fw->devices->$device->firmwares as $firmware)
{
if($firmware->version == $version)
{
$url = $firmware->url;
Log::writeInfo("Got url for version: $version");
return $url;
}
}
Log::writeError("Couldn't get IPSW url for $version for $device");
return null;
}
function pls_install_msg($github_project){
return "Dependency missing: Please download, compile (if needed) and install {$github_project} and make sure it's in PATH." . PHP_EOL;
}
function is_installed($cmd){
$dependency_const_name = strtoupper($cmd)."_PATH";
if(shell_exec("which {$cmd}") === NULL){
if(is_executable(__DIR__."/bin/$cmd")){
define($dependency_const_name, __DIR__."/bin/$cmd");
return true;
}
return false;
} else {
define($dependency_const_name, $cmd);
return true;
}
}
function remotezip_file_list(string $url, string $mask = null): array
{
$files = array();
if(!filter_var($url, FILTER_VALIDATE_URL)) return $files;
$cache_file_path = __DIR__.'/.cache/remotezip'.sha1($url);
$cache_data = file_exists($cache_file_path) ? file_get_contents($cache_file_path) : null;
if(!strlen($cache_data))
{
// verbinfo("")
$stdout = execute("/usr/local/bin/remotezip -l $url");
if(strlen($stdout))
{
file_put_contents($cache_file_path, $stdout);
}
}
else
{
$stdout = $cache_data;
}
// var_dump($stdout);
$regex = "/(?<size>\d+?)\s+?(?<date>(?:.+?)\s+?(?:.+?))\s+?(?<path>.+)(?:$|\n)/i";
if(preg_match_all($regex, $stdout, $matches))
{
foreach($matches['path'] as $index => $path)
{
if($mask && (stristr($path, $mask) === false)) continue;
$file = array(
'path' => trim($path),
'size' => (int)$matches['size'][$index],
'date' => trim($matches['date'][$index])
);
$files []= $file;
}
}
return $files;
}
function remotezip_get_files(string $url, string $boardconfig):bool
{
execute("remotezip $url BuildManifest.plist");
$bom = bom_from_buildmanifest('BuildManifest.plist', $boardconfig);
if(!$bom) return false;
// create BoM and download files
foreach($bom as $image_name => $image_path)
{
execute("remotezip $url $image_path");
if(!file_exists($image_path))
{
unlink('BuildManifest.plist');
return false;
}
}
return true;
}
function remotezip_get_files_old(string $url):bool
{
// create BoM and download files
$files = remotezip_file_list($url);
$dmg_files = array();
$files_needed = array(
'iBSS' => array(
'regex' => "/.*iBSS.*\.im4p$/i",
'found' => false,
'file' => null
),
'iBEC' => array(
'regex' => "/.*iBEC.*\.im4p$/i",
'found' => false,
'file' => null
),
'kernelcache' => array(
'regex' => "/^kernelcache.*/i",
'found' => false,
'file' => null
),
'devicetree' => array(
'regex' => sprintf("/.*DeviceTree\.%s\.im4p$/i", BOARDCONFIG),
'found' => false,
'file' => null
)
// 'BuildManifest' => array(
// 'regex' => "/^BuildManifest\.plist$/i",
// 'found' => false,
// 'file' => null
// ),
);
foreach($files as $file)
{
$file_path = $file['path'];
if(preg_match("/.*\.dmg.*/i", $file_path))
{
$dmg_files[$file['size']] = array(
'regex' => null,
'found' => true,
'file' => $file
);
continue;
}
foreach($files_needed as $component => &$file_needed)
{
if($file_needed['found']) continue;
if(preg_match($file_needed['regex'], $file_path))
{
$file_needed['file'] = $file;
$file_needed['found'] = true;
}
}
}
$sizes = array_keys($dmg_files);
rsort($sizes);
unset($dmg_files[current($sizes)]);
$files_needed = array_merge($files_needed, $dmg_files);
// download files
// bad bad php (or I could pick another variable.) $file_needed simply is a reference
// modifying this reference will modify the item in $files_needed array as well
// so free the reference
unset($file_needed);
foreach($files_needed as $file_needed)
{
$file = $file_needed['file'];
if(!$file_needed['found'])
{
return false;
}
execute("remotezip $url {$file['path']}");
if(!file_exists($file['path'])) return false;
}
execute("remotezip $url BuildManifest.plist");
return true;
}
function add_dependency_ldid2(): bool
{
$base_url = 'https://api.github.com';
$user_name = 'xerub';
$repo_name = 'ldid';
$url = "$base_url/repos/$user_name/$repo_name/releases";
// $url = "$base_url/repos/pwn20wndstuff/Undecimus/releases";
$working_path = __DIR__."/dependencies/$repo_name";
$data = curlget($url, ['User-Agent: make_telnet_rd']);
$releases = json_decode($data);
if(empty($releases)) return false;
// var_dump($releases);die();
$latest_release = null;
$iter_cmp_timetamp = 0;
foreach($releases as $release)
{
$release_published_timestamp = strtotime($release->published_at);
if($release_published_timestamp > $iter_cmp_timetamp)
{
$latest_release = $release;
$iter_cmp_timetamp = $release_published_timestamp;
}
}
Log::writeVerboseInfo("Selected {$latest_release->tag_name} of $repo_name");
if(empty($latest_release->assets))
{
return false;
}
if(!is_dir($working_path))
{
execute("mkdir -p $working_path");
}
foreach($latest_release->assets as $asset)
{
$dl_url = $asset->browser_download_url;
$dl_url_pathinfo = pathinfo($dl_url);
$dl_file_path = "$working_path/{$dl_url_pathinfo['basename']}";
if($dl_url_pathinfo['extension'] === 'zip')
{
execute("wget $dl_url -O $dl_file_path");
if(!file_exists($dl_file_path))
{
continue;
}
$z = new ZipArchive();
$z_open_success = $z -> open($dl_file_path);
if($z_open_success === true)
{
if($z->locateName('ldid2') === false)
{
continue;
}
$z_extract_success = $z -> extractTo($working_path, array('ldid2'));
if(!$z_extract_success)
{
continue;
}
$binary_path = "$working_path/ldid2";
if(!is_executable($binary_path))
{
chmod($binary_path, 0755);
if(!is_dir(__DIR__."/bin"))
{
mkdir(__DIR__."/bin");
}
if(rename($binary_path, __DIR__."/bin/ldid2"))
{
return true;
}
}
}
}
}
return false;
}
function bom_from_buildmanifest(string $buildmanifest, string $boardconfig, bool $update_buildidentity = false): ?array
{
$restore_behavior = $update_buildidentity ? "Update" : "Erase";
Log::writeInfo("Finding buildidentity from BuildManifest.plist");
try
{
$bm_plist = new CFPropertyList\CFPropertyList($buildmanifest);
$bm = $bm_plist->toArray();
}
catch (\Exception $e)
{
Log::writeError("Cant parse BuildManifest.plist: " . $e->getMessage());
return null;
}
unset($bm_plist); // it's a quite big object
$buildidentity = null;
foreach($bm['BuildIdentities'] as $index => $iter_buildidentity)
{
// if(
// ($iter_buildidentity['Info']['RestoreBehavior'] === 'Erase') &&
// (hexdec($iter_buildidentity['ApChipID']) === $cpid) &&
// (hexdec($iter_buildidentity['ApBoardID']) === $bdid )
// )
if( ($iter_buildidentity['Info']['RestoreBehavior'] === $restore_behavior) &&
!(strcasecmp($iter_buildidentity['Info']['DeviceClass'], $boardconfig)) )
{
$buildidentity = $iter_buildidentity;
Log::writeInfo("Selected {$buildidentity['ApChipID']} {$buildidentity['ApBoardID']} {$buildidentity['Info']['DeviceClass']}");
unset($bm);
break;
}
}
if(!$buildidentity) return null;
$bom = array(
'RestoreDeviceTree' => null,
'RestoreKernelCache' => null,
'RestoreRamDisk' => null,
'RestoreTrustCache' => null,
'iBEC' => null,
'iBSS' => null,
);
foreach($bom as $img => $_)
{
if(empty($buildidentity['Manifest'][$img]['Info']['Path']))
{
return null;
}
$bom[$img] = $buildidentity['Manifest'][$img]['Info']['Path'];
Log::writeInfo("Found $img -> {$bom[$img]}");
}
return $bom;
}
function getSHSH2(string $ecid, string $device = null, string $boardconfig = null, string $version = null): ?string
{
$base_url = "https://api.arx8x.net/shsh3/list.php?ecid=$ecid";
$selected_files = array();
$data = curlget($base_url);
if(!is_object($data))
{
Log::writeError('shsh.host sent unreadable response');
return null;
}
// 0 is success
if($data->code)
{
Log::writeError("{$data->code}: {$data->message}");
return null;
}
$shsh_files = $data->files;
foreach($shsh_files as $shsh_file)
{
if($device && strcasecmp($shsh_file->device, $device)) continue;
if($boardconfig && strcasecmp($shsh_file->boardconfig, $boardconfig)) continue;
if($version && strcasecmp($shsh_file->version, $version)) continue;
if(($shsh_file->extension === 'shsh2') && $shsh_file->validity)
{
// store all valid files to an array
$selected_files []= $shsh_file;
}
}
if(!count($selected_files)) return null;
// select a random file from valid files
// in case an shsh2 file doesn't work, this will likely get another file upon retrying
// TODO improve this logic by saving state OR checking downloaded files and skipping over them
// $selected_file = $selected_files[array_rand($selected_files)];
$selected_file = end($selected_files);
if(!filter_var($selected_file->url, FILTER_VALIDATE_URL))
{
Log::writeError("The file url is invalid. Re-run the script");
return null;
}
$file_name = basename($selected_file->url);
execute("wget --trust-server-names {$selected_file->url} -O $file_name");
if(!is_file($file_name))
{
Log::writeError("Couldn't download shsh2 file");
return null;
}
return $file_name;
}
function writelog(string $log_entry, int $log_type = 0, $write_stdout = true)
{
$log_level = defined('LOG_LEVEL') ? LOG_LEVEL : 1;
$sym = '+';
}
?>