-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.class.php
356 lines (312 loc) · 11.3 KB
/
terminal.class.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
<?php
/**
* Terminal
* Class to interact with the user and the shell process.
* @author Thiago Bocchile <tykoth@gmail.com>
* @package xwiterm
* @subpackage xwiterm-linux
*/
class Terminal {
private $login;
private $password;
static $username;
static $commandsFile = "comandos.txt";
static $totalCommands;
static $process;
static $status;
static $meta;
static $instance;
/**
* Static function to simply autenticate users.
* Can be used for other purposes.
* @param string $login the linux username
* @param string $password password
* @access public
* @return bool - true if login and password is right
*/
public static function autenticate($login, $password) {
self::$username = $login;
$process = new Process("su " . escapeshellarg($login));
usleep(500000);
$process->put($password);
$process->put(chr(13));
usleep(500000);
return (bool) !$process->close();
}
/**
* Get the terminal title.
* @return string
*/
public static function getTitle(){
if(!empty(self::$username)){
$process = new Process("uname -n");
$title = self::$username."@".trim($process->get());
$process->close();
return $title;
} else {
return "not logged shell - how cold it be possible, uh?";
}
}
/**
* Static function to "run" the terminal.
* It must be used at the end of all html output.
* @param string $login
* @param string $password
* @return bool - true if runs
*/
public static function run($login, $password){
self::$instance = new self();
return self::$instance->open($login, $password);
}
/**
* Simple function to "post" a command in the commands file.
* @todo Another way to catch the commands, file is ugly.
* @param string $command
*/
public static function postCommand($command){
file_put_contents(self::$commandsFile, $command."\n", FILE_APPEND);
}
/**
* Open the terminal process.
* @param string $login
* @param string $password
* @return bool - true if runs
*/
private function open($login, $password) {
$this->login = $login;
$this->password = $password;
if(!is_writable(self::$commandsFile)){
$this->output("\r\nNeed permission to write in ".self::$commandsFile."\r\n");
return false;
}
// Clean commands
file_put_contents(self::$commandsFile, "");
$this->startProcess();
do {
$out = self::$process->get();
// Detect "blocking" (wait for stdin)
if(sizeof($out) == 1 && ord($out) == 0) {
$this->listenCommand();
} else {
// Provisorio, meldels. (usuario www-data não tem controle de servico, dude!)
if(preg_match('/-su: no job control in this shell/', $out)) continue;
$this->output($out);
}
usleep(50000);
self::$status = self::$process->getStatus();
self::$meta = self::$process->metaData();
} while(self::$meta['eof'] === false);
return true;
}
/**
* Starts the terminal process
* Uses the class Process.
* @return true if runs
*/
private function startProcess() {
self::$process = new Process("su - {$this->login}");
// self::$process = new Process("vi");
if(!self::$process->isResource()) {
throw new Exception("RESOURCE NOT AVAIBLE");
return false;
}
usleep(500000);
self::$process->put($this->password);
self::$process->put(chr(13));
self::$process->get();
usleep(500000);
self::$status = self::$process->getStatus();
self::$meta = self::$process->metaData();
}
/**
* Simulates the terminal colors :)
* Format the input and returns as html with styles
* Function to be used with preg_replace.
* @param string $code
* @param string $value
* @return string - the html tag with style
*/
private function consoleTag($code, $value){
$attrs = explode(";", $code);
if(sizeof($attrs) == 2 && intval($attrs[0]) > 10){
$attrs[2] = $attrs[1];
$attrs[1] = $attrs[0];
}
if(sizeof($attrs) == 2 && intval($attrs[0]) == 0 && intval($attrs[1]) == 0){
$attrs[0] = 0;
$attrs[1] = 37;
}
$text = array(
'0' => '',
'1' => 'font-weight:bold',
'3' => 'text-decoration:underline',
'5' => 'blink'
);
$colors = array(
'0' => 'black',
'1' => 'red',
'2' => '#89E234', // green
'3' => 'yellow',
'4' => '#729FCF', // blue
'5' => 'magenta',
'6' => 'cyan',
'7' => 'white'
);
$text_decoration = (isset($attrs[0]) && array_key_exists(intval($attrs[0]), $text)) ? $text[intval($attrs[0])] : $text[0];
$color = (isset($attrs[1]) && array_key_exists(intval($attrs[1])-30, $colors)) ? $colors[intval($attrs[1])-30] : $colors[0];
$style = sprintf("%s;color:%s;", $text_decoration, $color);
$style.= (isset($attrs[2]) && array_key_exists((intval($attrs[2])-40), $colors)) ? "background-color:".$colors[(intval($attrs[2])-40)] : '';
return "<tt style=\\\"$style\\\">$value</tt>";
}
/**
* "Hard" output.
* It's not a good practice to echo from class methods, so it's a provisory
* method.
* @param string $output
* @param bool $return - true to return the formatted output
* @param bool $html - true to format html
* @return mixed - if $return is true, returns the output
*/
private function output($output, $return = false, $html = true) {
if(preg_match('/\x08/',$output)) return false;
$output = htmlentities($output);
$output = addslashes($output);
$output = explode("\n", $output);
$output = implode("</span><span>", $output);
$output = sprintf("<span>%s</span>", $output);
$output = preg_replace( "/\r\n|\r|\n/", '\n', $output);
// Removes the first occurrence (on ls)
$output = preg_replace('/\x1B\[0m(\x1B)/', "\x1B", $output);
// Add colors to default coloring sytem
$output = preg_replace('/\x1B\[([^m]+)m([^\x1B]+)\x1B\[0m/e', '$this->consoleTag(\'\\1\',\'\\2\')', $output);
$output = preg_replace('/\x1B\[([^m]+)m([^\x1B]+)\x1B\[m/e', '$this->consoleTag(\'\\1\',\'\\2\')', $output);
// Add colors to grep color system
$output = preg_replace('/\x1B\[([^m]+)m\x1B\[K([^\x1B]+)\x1B\[m\x1B\[K/e', '$this->consoleTag("\\1","\\2")', $output);
// Removes some dumb chars
$output = preg_replace('/\x1B\[m/', '', $output);
$output = preg_replace('/\x07/', '', $output);
if($return === false){
echo "<script>recebe(\"{$output}\");</script>\n"; flush();
} else {
return $output;
}
}
/**
* Formats the output to be used as command suggest (pressing TAB)
* @param string $output
* @return string
*/
private function commandSuggest($output){
$output = preg_replace( "/\n|\r|\r\n/", '', $output);
$output = preg_replace('/'.chr(7).'/', '', $output);
return trim($output);
}
/**
* Listener for incoming commands
*/
private function listenCommand() {
$commands = file(self::$commandsFile);
if(sizeof($commands) > self::$totalCommands) {
self::$totalCommands = sizeof($commands);
$command = $commands[self::$totalCommands-1];
$this->parse($command);
}
}
/**
* Parse the command
* @param string $command - incomming command from terminal
* @return void
*/
private function parse($command) {
switch(trim($command)) {
case chr(3):
// SIGTERM
return $this->sendSigterm();
break;
case chr(4):
self::$process->put("exit");
self::$process->put(chr(13));
break;
case chr(26):
//STOP - experimental
return $this->sendSigstop();
break;
case 'fg':
return $this->sendFg();
break;
default:
// Checks for "TAB"
if(ord(substr($command,-2,1)) == 9){
self::$process->put(trim($command).chr(9));
usleep(500000);
$out = self::$process->get();
// Check if is a "RE-TAB"
if(trim($command) == $this->commandSuggest($out)){
self::$process->put(trim($command).chr(9).chr(9));
self::$process->put(chr(21));
$this->output(self::$process->get());
} else {
echo "<script>recebe(null, '".$this->commandSuggest($out)."')</script>\n"; flush();
}
self::$process->put(chr(21));
} else {
self::$process->put(chr(21));
self::$process->put(trim($command));
self::$process->put(chr(13));
}
usleep(500000);
break;
}
}
/**
* Emulates the SIGTERM sending via CTRL-C
* @return void
*/
private function sendSigterm() {
// SLAYER!!! GRRRRRRRRRR
// http://www.youtube.com/watch?v=VSoh3c7QVyw
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -9 $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
usleep(500000);
}
/**
* Simulates the SIGSTOP sending via CTRL-Z
* @return void
*/
private function sendSigstop() {
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -TSTP $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
self::$process->put(chr(13));
usleep(500000);
}
/**
* Simulates the SIGCONT sending via 'fg'
*/
private function sendFg() {
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -CONT $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
self::$process->put(chr(13));
usleep(500000);
}
}