This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecrypt.php
More file actions
136 lines (124 loc) · 3.47 KB
/
decrypt.php
File metadata and controls
136 lines (124 loc) · 3.47 KB
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
<?php
declare(strict_types=1);
/*
Portable program to decrypt resource packs
*/
if(version_compare(phpversion(), '8.0.0', '<')){
echo "PHP 8.0.0 or higher is required to run this script. You are using PHP ".phpversion().PHP_EOL;
exit(1);
}
//check opelssl ext
if(!extension_loaded("openssl")){
echo "openssl extension is required to run this script.".PHP_EOL;
exit(1);
}
[$path, $contentKey, $outputdir] = readOptions($argv);
$test = file_get_contents($path."contents.json");
$test = substr($test, 0x100);
$content = openssl_decrypt($test, "AES-256-CFB8", $contentKey, OPENSSL_RAW_DATA , substr($contentKey, 0, 16));
try{
$test = json_decode($content, true, flags: JSON_THROW_ON_ERROR);
}catch(\JsonException $e){
echo "final: maybe key incorrect!\n";
exit;
}
foreach($test["content"] as $item){
$target = $item["path"] ?? null;
if($target[0] === "/"||$target[0] === "\\"||str_contains($target, "..")){
throw new \RUntimeException("Invalid path(Unable to write to the parent directory for security reasons): ".$target);
}
if($target === null){
echo "Path not found in ".$item["path"]."\n";
continue;
}
if(!isset($item["key"])){
if($target === "manifest.json"||$target === "pack_icon.png"||$target === "contents.json"){
continue;
}
echo "copy: ".$target."\n";
copy($path.$target, $outputdir.$target);
continue;
}
$key = $item["key"];
$output = $outputdir.$target;
echo $target," with ",$key,"\n";
$test = file_get_contents($path.DIRECTORY_SEPARATOR.$target);
$content1 = openssl_decrypt($test, "AES-256-CFB8", $key, OPENSSL_RAW_DATA , substr($key, 0, 16));
@mkdir(dirname($output), 0777, true);
file_put_contents($output, $content1);
//break;
}
echo "done!\n";
exit;
//readOption
function readOptions(array $argv){
unset($argv[0]);
if(!isset($argv[1])){
help();
}
$argv = array_values($argv);
$key = readShortOpt($argv, "k") ?? null;
$output = readShortOpt($argv, "o") ?? "encrypted";
//key must 32bytes
$path = $argv[0] ?? null;
if($path === null){
echo "Path not specified.\n";
help();
}
$path = realpath($path);
if($path === false||!is_dir($path)){
echo "Path not found.\n";
help();
}
if($key === null){
$key = readKeyFile($path);
if($key === null||strlen($key) === 0){
echo "Key not specified.\n";
help();
}
}
if(strlen($key) !== 32){
echo "Key length should be 32 bytes.\n";
help();
}
$path = rtrim($path, "/\\").DIRECTORY_SEPARATOR;
$output = rtrim($output, "/\\").DIRECTORY_SEPARATOR;
return [$path, $key, $output];
}
exit;
function readShortOpt(array &$argv, $opt): ?string{
$result = null;
foreach($argv as $i => $value){
if(str_starts_with($value, "-".$opt)){
$result = substr($value, 2);
unset($argv[$i]);
if($result === ""||$result === false){
$result = $argv[$i+1] ?? null;
unset($argv[$i+1]);
}
}
}
$argv = array_values($argv);
return $result;
}
function help(){
echo "Usage: php encrypt.php <path> -k <key> -o <output>\n";
exit;
}
//read *.key file with scandir
function readKeyFile(string $path): ?string{
$files = scandir($path);
foreach($files as $file){
if(!str_ends_with($file, ".key")){
continue;
}
$key1 = file_get_contents($path."/".$file);
foreach([$key1, trim($key1)] as $key){
if(strlen($key) !== 32){
continue;
}
return $key;
}
}
return null;
}