Repository navigation
Expand file tree
/
Copy pathphperpack.php
More file actions
executable file
·322 lines (297 loc) · 9.37 KB
/
Copy pathphperpack.php
File metadata and controls
executable file
·322 lines (297 loc) · 9.37 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
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
<?php
/**
* PHPpack使用说明
* -m 指定入口文件路径 可以是相当路径 也可以是绝对路径
* -o 指定输出文件位置 及文件名,可以是相对路径,也可以是绝对路径
* --v 开启变量名混淆
* --p 使用模块打包
**/
new Main();
class Main {
static public $startTime;
public $entry;
public $outputPath;
static public $varConfusion = False;
static public $pack = False;
static public $encryption = False;
public function __construct() {
self::$startTime = microtime(true);
$this->_init();
}
public function _init() {
global $argv;
$this->init_before();
$mainIndex = array_search('-m',$argv);
if (array_search('--p', $argv)) {
Main::$pack = True;
}
$mainIndex || die(new Message("警告:对不起进程无法继续,请输入入口文件名称或使用phppack.config.json文件进行配置", 'error'));
$outputIndex = array_search('-o', $argv);
$this->outputPath = $outputIndex ? $this->handlePath($argv[ $outputIndex + 1 ]) : './output.php';
$mainFileName = $argv[$mainIndex + 1];
if (!$mainFileName) {
die(new Message('入口文件输入错误', 'error'));
}
if (array_search('--v', $argv)) {
Main::$varConfusion = True;
}
if (array_search('--e', $argv)) {
Main::$encryption = True;
}
$this->entry = $this->handlePath($mainFileName);
new Handle($this->entry, $this->outputPath);
}
public function handlePath($path) {
if (strpos($path, '/') || strpos($path, '\\')) {
return $path;
}else{
return './' . $path;
}
}
public function init_before() {
echo new Message('-----------------------------------------------');
echo new Message('|------------------👏欢迎使用👏---------------|');
echo new Message('|------------------PHPpack 1.0----------------|');
echo new Message('|------------------作者:张宇童---------------|');
echo new Message('|--------------邮箱:admin@ecuuu.com----------|');
echo new Message('|-----------------PHPpack构建工具-------------|');
echo new Message('-----------------------------------------------');
}
}
class Handle extends Main {
private $preprocessing; // 预处理文件内容
private $afterContent; // 处理后的内容
private $queue = []; // 语法队列
private $queueName = [];
private $varMap = [];
private $fileGetContents = [];
private $keyWordSpace = [
'echo',
'function',
'class',
'private',
'public',
'protected',
'new',
'extends',
'use',
'namespace',
'as'
];
private $en = 'function F88BAF0X(\$str){\$arr=explode("PHPpackJM1.0",\$str);\$ret=[];foreach(\$arr as \$k=>\$v){\$_arr=explode("O",\$v);\$len=array_shift(\$_arr);\$first=array_shift(\$_arr);\$_str="";for(\$i=0;\$i<\$len;\$i++){if(\$i==\$first){\$_str.="1";\$first=array_shift(\$_arr);}else{\$_str.="0";}}array_push(\$ret,\$_str);}foreach(\$ret as &\$v){\$v=pack("H".strlen(base_convert(\$v,2,16)),base_convert(\$v,2,16));}return join("",\$ret);} ';
function __construct($filename, $outputPath) {
$this->handle($filename);
// $this->parse();
$this->output($outputPath);
}
/**
* 预处理文件内容
**/
private function handle($filePath, $randomName = '') {
if (!$randomName) {
$randomName = '_'.(string)mt_rand(1000000,9999999);
}
$this->queueName[] = $randomName;
$this->queue[$randomName] = [];
echo new Message('加载:'.__DIR__.rtrim(ltrim($filePath,'\'.'),'\''));
$content = file_get_contents(__DIR__.rtrim(ltrim($filePath,'\'.'),'\''));
$temp = [];
foreach (token_get_all($content) as $k => $v) {
if (is_array($v)) {
$temp_str = trim(trim($v[1]) == '<?php' ? '' : $v[1]);
$temp_str = $temp_str == '?>'? '' : $temp_str;
if ($temp_str != '') {
if ($v[0] == 320) {
if (Main::$varConfusion) {
$temp_str = $this->varMap[$temp_str] = '$_'.md5($temp_str);
}
array_push($temp, '\\'.$temp_str);
}else if($v[0] == 377) {
}else {
array_push($temp, $temp_str);
}
if ($v[0] == 390) {
array_push($temp, '\\');
}
}
}else {
array_push($temp, trim($v));
}
}
$ret = [];
while(list($k, $v) = each($temp)) {
if (Main::$pack) {
if (
$v == 'include' ||
$v == 'include_once' ||
$v == 'require' ||
$v == 'require_once'
) {
$this->handle_parse_include($temp, $temp, $randomName);
}else if ($v == 'file_get_contents') {
$this->handle_parse_fileGetContent($temp, $temp, $randomName);
}else {
array_push($this->queue[$randomName], $v);
}
}else {
array_push($this->queue[$randomName], $v);
}
$temp_next = $temp;
list(, $vv) = each($temp_next);
if (in_array($vv, $this->keyWordSpace)) {
array_push($this->queue[$randomName], ' ');
}
if (in_array($v, $this->keyWordSpace)) {
array_push($this->queue[$randomName], ' ');
}
}
}
/**
* 处理include and require
**/
function handle_parse_include($arr, &$origin_arr, $randomName) {
list($k, $v) = each($arr);
if ($v == '(') {
$temp_random = '_'.(string)mt_rand(1000000, 9999999);
array_push($this->queue[$randomName], " eval(\\\$$temp_random)");
$this->handle(each($arr)['value'], $temp_random);
for ($i = 0; $i < 3; $i++) {
each($origin_arr);
}
}else {
$temp_random = '_'.(string)mt_rand(1000000, 9999999);
array_push($this->queue[$randomName], " eval(\\\$$temp_random)");
$this->handle($v, $temp_random);
each($origin_arr);
}
}
/**
* 处理file_get_contents
**/
function handle_parse_fileGetContent($arr, &$origin_arr, $randomName) {
each($origin_arr);
each($origin_arr);
list(, $fileName) = each($origin_arr);
if ($fileName[0] == '\'') {
$fileName = trim($fileName, '\'');
}else {
$fileName = trim($fileName, '"');
}
$fileContent = file_get_contents($fileName);
$randomVar = '_'.md5(mt_rand(1000000, 9999999));
$this->fileGetContents[$randomVar] = $fileContent;
array_push($this->queue[$randomName], "\\\${$randomVar}");
each($origin_arr);
}
/**
* 输出文件
**/
function output($outputPath) {
$output_str = "<?php \r\n";
$this->handle_output_fileGetContents($output_str);
$reverse_code = array_reverse($this->queue);
$new_temp_arr = [];
if (Main::$encryption) {
foreach($reverse_code as $k => $v) {
$t = '';
foreach($v as $kk => $vv) {
$t .= $vv;
}
echo $t, '====';
$t = str_replace('\\', '', $t);
$new_temp_arr[$k] = 'eval(F88BAF0X("'.$this->limit($t).'"));';
}
$reverse_code = $new_temp_arr;
}
$len = count($new_temp_arr);
foreach($new_temp_arr as $k => $v) {
$output_str .= "\${$k} = <<<{$k}\r\n";
if (Main::$encryption) {
$len--;
$output_str .= $v;
if (!$len) {
$output_str .= $this->en;
}
}else {
foreach($v as $kk => $vv) {
$output_str .= $vv;
}
}
$output_str .= "\r\n{$k};\r\n";
}
$reverse_name = array_reverse($this->queueName);
$reverse_name = array_reverse($reverse_name);
$output_str .= "eval(\$$reverse_name[0]);";
file_put_contents($outputPath, $output_str);
$consumeTime = (microtime(true) - Main::$startTime) * 100;
echo new Message("👌PHPpack为您构建成功!\r\n🚗构建后文件位置:{$outputPath}\r\n⌚️总耗时:{$consumeTime}s", 'success');
}
/**
* 处理输出的file_get_contents
**/
function handle_output_fileGetContents(&$output_str) {
foreach($this->fileGetContents as $k => $v) {
$output_str.= "\${$k} = <<<{$k}\r\n";
$output_str.= $v."\r\n";
$output_str.="{$k};\r\n";
}
}
/**
*
**/
function limit($str){
$arr = preg_split('/(?<!^)(?!$)/u', $str);
$ret = [];
foreach($arr as &$v){
$temp = unpack('H*', $v);
$v = base_convert($temp[1], 16, 2);
$_str_arr = [];
array_push($_str_arr, strlen($v));
for($i = 0; $i < strlen($v); $i++) {
if ($v[$i] == '1') {
array_push($_str_arr, $i);
}
}
array_push($ret, $_str_arr);
unset($temp);
}
$ret_str = '';
foreach($ret as $k => $v) {
foreach($v as $kk => $vv) {
$ret_str .= $vv . 'O';
}
$ret_str = rtrim($ret_str, 'O');
$ret_str .= 'PHPpackJM1.0';
}
$ret_str = rtrim($ret_str, 'PHPpackJM1.0');
return $ret_str;
}
/**
* xxx
**/
function array_insert(&$array, $position, $insert_array) {
$first_array = array_splice ($array, 0, $position);
$array = array_merge ($first_array, $insert_array, $array);
}
}
class Message {
public function __construct($message, $type = 'info') {
$this->case_type($type, $message);
}
public function case_type ($type, $message) {
switch ($type) {
case 'error':
$this->message = "\033[31m{$message}\033[0m\r\n";
break;
case 'success':
$this->message = "\033[32m{$message}\033[0m\r\n";
break;
case 'info':
$this->message = "\033[36m{$message}\033[0m\r\n";
break;
}
}
public function __toString() {
return $this->message;
}
}