-
Notifications
You must be signed in to change notification settings - Fork 0
/
serch_demo.php
471 lines (420 loc) · 17.1 KB
/
serch_demo.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
<?php
//Script by TadavomnisT
$c = new SeRCH;
$c->print_intro_message();
$c->print_help();
$c->check_session();
$c->get_command();
class SeRCH
{
private $public_key , $private_key;
public function print_intro_message()
{
echo " \033[33m.;_ :lo;\033[0m .:\033[36m Secure Relay Chat on HTTP\033[0m:.\n \033[33m#:lx:#,ldkOkOkkxdool0K:\033[0m\n \033[33m'KxldkKXXXXXXXXXXXXOol\033[0m \033[91mAUTHOR : \033[0m TadavomnisT (Behrad.B)\n \033[33m,o0XXXXXXXXXXXXXXXXXd\033[0m \033[91mRepo : \033[0m https://github.com/TadavomnisT/SeRCH\n \033[33mdKXXXXXXXXXXXXXXKXXXK#\033[0m \033[91mREPORTING BUGS : \033[0m https://github.com/TadavomnisT/SeRCH/issues\n \033[33m'0XXX0_KXXXXXXXX_kXXXK#\033[0m \033[91mCOPYRIGHT :\033[0m\n \033[33m0XXXXKXX0d::cdKXXXXKc#\033[0m Copyright © 2022 License GPLv3+\n \033[33m#0XXXXXKl.x,o.lXXXK##\033[0m This is free software: you are free to change and redistribute it.\n \033[33m0XXXXkK0.0.0KXXXXXX\033[0m There is NO WARRANTY, to the extent permitted by law.\n \033[96m':lo\033[0m\033[33mdxkkdXXXXXXXX\033[0m\033[0m\033[96mkkkkk:\033[0m \n \033[95m###########\033[0m\033[96m ;kkkkxdllkkkxclcxkkk:\033[0m \033[95m############# ########## ### ### \n \033[95m#### ####\033[0m\033[96m 'kkkkxxdoxxxddxkkkkkdl\033[0m \033[95m## ### #### #### ### ### \n \033[95m### ###\033[0m\033[96mdkkkkkkxc :ddkkkkkkxc\033[0m \033[95m## # ### ## ## ### ### \n \033[95m## . # ###\033[0m\033[96mokkkkkxd;\033[0m\033[95m###\033[0m\033[96modddddddd,\033[0m \033[95m### ### ## . . ### ### # ##### \n \033[95m### . #\033[0m\033[96m,xkkxxddo# ;kkkkkxo#\033[0m \033[95m## . # ## ## . . # #### ### \n \033[95m###. #\033[0m\033[96m'dxdxkd'# #d# ,'\033[0m\033[95m### ## ### ## . . ################### \n \033[95m############ \033[96m;l'\033[0m \033[96m#\033[0m \033[0m\033[95m### ## # ### ## . . #### . #### # \n \033[95m### ## ### ## ### ## . . ### ### \n ### ################## ############# ## . . ### . ### \n ## ### #### . . #### ## ## ### ##### . . ### \n ### ####. #### . . . . #### . ### #### ### ##### . ### \n ############### . . #### . # . ## . ### # #### ### ### . ### \n ####### . ############# .## . ### ####### ##### . ### \033[0m\n";
}
public function check_session()
{
if( !file_exists( "session.srch" ) )
{
echo "Hello Hello:3" .PHP_EOL;
echo "Looks like it's your first time using SeRCH-Client!" .PHP_EOL;
echo "For your own safety, You MUST set a password for your session." .PHP_EOL . PHP_EOL;
while (true) {
$password = $this->prompt_silent("Enter password (will not be echoed):");
if ( $password === $this->prompt_silent("Verify password (will not be echoed):") )
break;
else echo PHP_EOL . "[*]ERROR: Passwords doesn't match! Try again." . PHP_EOL . PHP_EOL;
}
$this->private_key = openssl_pkey_new();
openssl_pkey_export( $this->private_key , $private_key_pem);
$this->public_key = openssl_pkey_get_details($this->private_key)['key'];
$data = json_encode(["private" => $private_key_pem, "public" => $this->public_key]);
$cipher = "aes-256-cbc";
$encryption_key = $password;
$iv = "0000000000000000";
$encrypted_data = openssl_encrypt($data, $cipher, $encryption_key, 0, $iv);
file_put_contents( "session.srch" , $encrypted_data );
echo "[+] Session created successfully." . PHP_EOL . PHP_EOL;
}
else
{
while (true) {
$password = $this->prompt_silent("Enter password please (will not be echoed):");
$cipher = "aes-256-cbc";
$encryption_key = $password;
$iv = "0000000000000000";
$encrypted_data = file_get_contents( "session.srch" );
$data = openssl_decrypt($encrypted_data, $cipher, $encryption_key, 0, $iv);
if( $data ) break;
else echo "[*]ERROR: Wronge password?" . PHP_EOL . PHP_EOL;
}
$json = json_decode($data , true);
if( isset( $json["public"] ) && isset( $json["private"] ) )
{
$this->private_key = openssl_pkey_get_private( $json["private"] );
if ( !$this->private_key ) die("[*]ERROR: Corrupted data in session?");
$this->public_key = $json["public"];
echo "[+] Session decrypted successfully." . PHP_EOL . PHP_EOL;
}
else die("[*]ERROR: Corrupted data in session?");
}
}
public function get_command()
{
while (true) {
echo "Send a command: ";
$command = explode( " " , trim(fread( STDIN , 4096 )));
if( $command[0] === "/getpkey" )
{
echo "Copy below text:" . PHP_EOL . PHP_EOL;
echo $this->public_key . PHP_EOL . PHP_EOL;
}
else if( $command[0] === "/help" )
{
$this->print_help();
}
else if( $command[0] === "/exit" )
{
break;
}
else if( $command[0] === "/file_private_dec" )
{
if ( file_exists($command[1]) ) {
if ( !file_exists($command[2]) ) {
$input = fopen($command[1],'r');
$output = fopen($command[2],'w');
while (true) {
$buffer = fread($input, 256);
if (strlen($buffer) == 0)
break;
$result = openssl_private_decrypt($buffer, $deciphered_data, $this->private_key , OPENSSL_PKCS1_OAEP_PADDING);
fwrite( $output , $deciphered_data );
}
fclose( $input );
fclose( $output );
echo "[+] Successfully decrypted in " . $command[2] . PHP_EOL . PHP_EOL;
} else echo "[*]ERROR: Output file Already exists." . PHP_EOL . PHP_EOL;
} else echo "[*]ERROR: Input file Not found." . PHP_EOL . PHP_EOL;
}
else if( $command[0] === "/file_enc_new_pkey" )
{
if ( file_exists($command[1]) ) {
if ( !file_exists($command[2]) ) {
echo PHP_EOL . "Please enter a new public-key:" . PHP_EOL . PHP_EOL;
$data = "";
while (true) {
$line = fread( STDIN , 4096 );
$data .= $line;
if (trim($line) == "-----END PUBLIC KEY-----")
break;
}
$new_public_key = trim($data) . PHP_EOL;
$input = fopen($command[1],'r');
$output = fopen($command[2],'w');
while (!feof($input)) {
$buffer = fgets($input, 64);
openssl_public_encrypt($buffer, $ciphered_data, $new_public_key , OPENSSL_PKCS1_OAEP_PADDING );
fwrite( $output , $ciphered_data );
}
fclose( $input );
fclose( $output );
echo "[+] Successfully encrypted in " . $command[2] . PHP_EOL . PHP_EOL;
} else echo "[*]ERROR: Output file Already exists." . PHP_EOL . PHP_EOL;
} else echo "[*]ERROR: Input file Not found." . PHP_EOL . PHP_EOL;
}
}
}
function prompt_silent($prompt = "Enter Password:") {
if (preg_match('/^win/i', PHP_OS)) {
$vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
file_put_contents(
$vbscript, 'wscript.echo(InputBox("'
. addslashes($prompt)
. '", "", "password here"))');
$command = "cscript //nologo " . escapeshellarg($vbscript);
$password = rtrim(shell_exec($command));
unlink($vbscript);
return $password;
} else {
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK') {
trigger_error("Can't invoke bash");
return;
}
$command = "/usr/bin/env bash -c 'read -s -p \""
. addslashes($prompt)
. "\" mypassword && echo \$mypassword'";
$password = rtrim(shell_exec($command));
echo "\n";
return $password;
}
}
function print_help()
{
$menu = [
[
"Command" => "/getpkey",
"Description" => "Prints the generated public key in x509 format",
],
[
"Command" => "/private_enc {BASE64_DATA}",
"Description" => "Encrypts base64-encoded data with private key and returns cipher in base64",
],
[
"Command" => "/public_enc {BASE64_DATA}",
"Description" => "Encrypts base64-encoded data with public key and returns cipher in base64",
],
[
"Command" => "/private_dec {BASE64_CIPHER}",
"Description" => "Decrypts base64-encoded cipher data with private key",
],
[
"Command" => "/public_dec {BASE64_CIPHER}",
"Description" => "Decrypts base64-encoded cipher data with public key",
],
[
"Command" => "/file_private_enc [input] [output]",
"Description" => "Encrypts a file with private key",
],
[
"Command" => "/file_public_enc [input] [output]",
"Description" => "Encrypts a file with public key",
],
[
"Command" => "/file_private_dec [input] [output]",
"Description" => "Decrypts a file with private key",
],
[
"Command" => "/file_public_dec [input] [output]",
"Description" => "Decrypts a file with public key",
],
[
"Command" => "/dec_new_pkey",
"Description" => "Decrypts base64-encoded cipher data with a a new public key in x509 format",
],
[
"Command" => "/enc_new_pkey",
"Description" => "Encrypts base64-encoded data data with a a new public key in x509 format",
],
[
"Command" => "/file_dec_new_pkey [input] [output]",
"Description" => "Decrypts a file with a new public key",
],
[
"Command" => "/file_enc_new_pkey [input] [output]",
"Description" => "Encrypts a file with a new public key",
],
[
"Command" => "/help",
"Description" => "Prints this help",
],
[
"Command" => "/exit",
"Description" => "Exits the program",
],
];
$m = new ArrayToTextTable( $menu );
echo $m->render() . PHP_EOL;
}
}
/**
* @author Denis Koronets
*/
class ArrayToTextTable
{
/**
* @var array
*/
private $data;
/**
* @var array
*/
private $columnsList = [];
/**
* @var int
*/
private $maxLineLength = 40;
/**
* @var array
*/
private $columnsLength = [];
/**
* @var array
*/
private $result = [];
/**
* @var string
*/
private $charset = 'UTF-8';
/**
* @var bool
*/
private $renderHeader = true;
public function __construct(array $data)
{
$this->data = $data;
}
/**
* Set custom charset for columns values
*
* @param $charset
*
* @return \dekor\ArrayToTextTable
* @throws \Exception
*/
public function charset($charset)
{
if (!in_array($charset, mb_list_encodings())) {
throw new \Exception(
'This charset `' . $charset . '` is not supported by mbstring.' .
'Please check it: http://php.net/manual/ru/function.mb-list-encodings.php'
);
}
$this->charset = $charset;
return $this;
}
/**
* Set mode to print no header in the table
*
* @return self
*/
public function noHeader()
{
$this->renderHeader = false;
return $this;
}
/**
* @param int $length
*
* @return self
* @throws \Exception
*/
public function maxLineLength($length)
{
if ($length < 3) {
throw new \Exception('Minimum length for cropper is 3 sumbols');
}
$this->maxLineLength = $length;
return $this;
}
/**
* Build your ascii table and return the result
*
* @return string
*/
public function render()
{
if (empty($this->data)) {
return 'Empty';
}
$this->calcColumnsList();
$this->calcColumnsLength();
/** render section **/
$this->renderHeader();
$this->renderBody();
$this->lineSeparator();
/** end render section **/
return str_replace(
['++', '||'],
['+', '|'],
implode(PHP_EOL, $this->result)
);
}
/**
* Calculates list of columns in data
*/
protected function calcColumnsList()
{
$this->columnsList = array_keys(reset($this->data));
}
/**
* Calculates length for string
*
* @param $str
*
* @return int
*/
protected function length($str)
{
return mb_strlen($str, $this->charset);
}
/**
* Calculate maximum string length for each column
*/
private function calcColumnsLength()
{
foreach ($this->data as $row) {
if ($row === '---') {
continue;
}
foreach ($this->columnsList as $column) {
$this->columnsLength[$column] = max(
isset($this->columnsLength[$column])
? $this->columnsLength[$column]
: 0,
$this->length($row[$column]),
$this->length($column)
);
}
}
}
/**
* Append a line separator to result
*/
private function lineSeparator()
{
$tmp = '';
foreach ($this->columnsList as $column) {
$tmp .= '+' . str_repeat('-', $this->columnsLength[$column] + 2) . '+';
}
$this->result[] = $tmp;
}
/**
* @param $columnKey
* @param $value
*
* @return string
*/
private function column($columnKey, $value)
{
return '| ' . $value . ' ' . str_repeat(' ', $this->columnsLength[$columnKey] - $this->length($value)) . '|';
}
/**
* Render header part
*
* @return void
*/
private function renderHeader()
{
$this->lineSeparator();
if (!$this->renderHeader) {
return;
}
$tmp = '';
foreach ($this->columnsList as $column) {
$tmp .= $this->column($column, $column);
}
$this->result[] = $tmp;
$this->lineSeparator();
}
/**
* Render body section of table
*
* @return void
*/
private function renderBody()
{
foreach ($this->data as $row) {
if ($row === '---') {
$this->lineSeparator();
continue;
}
$tmp = '';
foreach ($this->columnsList as $column) {
$tmp .= $this->column($column, $row[$column]);
}
$this->result[] = $tmp;
}
}
}
?>