-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_terminal.php
More file actions
369 lines (350 loc) · 11.6 KB
/
Copy pathweb_terminal.php
File metadata and controls
369 lines (350 loc) · 11.6 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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/*
* Simple web terminal with login and command execution.
*
* @ Author Fattain Naime
* @ Website: https://iamnaime.info.bd
* @ Github: https://github.com/fattain-naime/js-web-terminal
* @ License: GPL-2.0
*
* Enable terminal access where terminal access is forbidden.
*
* Change const password with yours.
* TERMINAL_PASSWORD_HASH to the MD5 of your own password.
*/
// MD5 hash of the password "123". Replace with md5('your‑password')
const TERMINAL_PASSWORD_HASH = '202cb962ac59075b964b07152d234b70';
// ================== BACKEND (AJAX HANDLER) ==================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
$password = $input['password'] ?? '';
$command = isset($input['command']) ? trim($input['command']) : '';
$action = $input['action'] ?? '';
// ---- LOGIN HANDLER ----
// The client will hit this endpoint with {action: 'login', password: '<pwd>'}
// to validate credentials before enabling the terminal. We do not execute
// any commands for the login action. Instead we just return ok/not ok.
if ($action === 'login') {
if (md5($password) === TERMINAL_PASSWORD_HASH) {
echo json_encode([
'ok' => true,
'output' => "Authentication successful.\n",
]);
} else {
echo json_encode([
'ok' => false,
'output' => "Authentication failed.\n",
]);
}
exit;
}
// ---- COMMAND HANDLER ----
// Reject any request that does not provide the correct password.
if (md5($password) !== TERMINAL_PASSWORD_HASH) {
echo json_encode([
'ok' => false,
'output' => "Authentication failed.\n",
]);
exit;
}
// Command is required for execution
if ($command === '') {
echo json_encode([
'ok' => false,
'output' => "No command provided.\n",
]);
exit;
}
// Run the command without any allow‑list restrictions. This terminal
// intentionally exposes the underlying system to arbitrary commands,
// so be cautious deploying it in production. Output and status code
// are returned to the client.
$output = [];
$returnVar = 0;
exec($command . ' 2>&1', $output, $returnVar);
echo json_encode([
'ok' => $returnVar === 0,
'output' => implode("\n", $output) . "\n",
'statusCode' => $returnVar,
]);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Terminal</title>
<style>
body {
margin: 0;
padding: 0;
background: #0e101a;
color: #f5f5f5;
font-family: 'Courier New', monospace;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.terminal-container {
width: 90%;
max-width: 960px;
height: 80vh;
background: #1a1c29;
border-radius: 8px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.4);
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
}
.terminal-header {
padding: 10px 15px;
background: #2e3047;
color: #9da5b4;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 14px;
}
.terminal-header .title {
font-weight: bold;
}
.terminal-output {
flex: 1;
padding: 10px;
overflow-y: auto;
background: #141622;
color: #d3d7e0;
font-size: 14px;
white-space: pre-wrap;
word-break: break-word;
}
.terminal-input {
display: flex;
padding: 10px;
background: #2e3047;
border-top: 1px solid #3d415c;
}
.terminal-input .prompt {
margin-right: 5px;
color: #6a75a1;
user-select: none;
}
.terminal-input input {
flex: 1;
background: transparent;
border: none;
outline: none;
color: #e5e5e5;
font-family: inherit;
font-size: 14px;
}
/* Login overlay styles */
.login-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.8);
display: flex;
justify-content: center;
align-items: center;
}
.login-box {
background: #1a1c29;
border-radius: 6px;
padding: 20px 30px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
gap: 10px;
width: 320px;
}
.login-box h2 {
margin: 0 0 10px 0;
color: #f5f5f5;
font-size: 20px;
text-align: center;
}
.login-box input[type="password"] {
padding: 8px;
border-radius: 4px;
border: none;
outline: none;
font-size: 14px;
}
.login-box button {
padding: 8px;
border-radius: 4px;
border: none;
background: #4a5fc1;
color: #fff;
font-weight: bold;
cursor: pointer;
transition: background 0.2s ease;
}
.login-box button:hover {
background: #3d51a3;
}
.login-error {
color: #ff6c6c;
font-size: 13px;
display: none;
}
.terminal-footer {
padding: 8px 12px;
background: #2e3047;
color: #9da5b4;
font-size: 13px;
text-align: right;
border-top: 1px solid #3d415c;
user-select: none;
}
.terminal-footer a {
color: #8fb1ff;
text-decoration: none;
font-weight: 600;
}
.terminal-footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="terminal-container">
<div class="terminal-header">
<div class="title">Web Terminal</div>
<div class="status" id="statusText">Not Authenticated</div>
</div>
<div id="terminalOutput" class="terminal-output"></div>
<div class="terminal-input">
<span class="prompt">web@cpanel:~$</span>
<input type="text" id="commandInput" placeholder="Type a command..." disabled />
</div>
<!-- Login overlay layered on top of terminal until login succeeds -->
<div class="login-overlay" id="loginOverlay">
<div class="login-box">
<h2>Login</h2>
<input type="password" id="loginPassword" placeholder="Enter password" autofocus />
<button id="loginButton">Login</button>
<div class="login-error" id="loginError">Invalid password, please try again.</div>
</div>
</div>
<footer class="terminal-footer">Built with ❤️ in Bangladesh by <a href="https://iamnaime.info.bd" target="_blank" rel="noopener noreferrer">Fattain Naime</a></footer>
</div>
<script>
const outputEl = document.getElementById('terminalOutput');
const cmdInput = document.getElementById('commandInput');
const statusText = document.getElementById('statusText');
// Login elements
const loginOverlay = document.getElementById('loginOverlay');
const loginButton = document.getElementById('loginButton');
const loginPassword = document.getElementById('loginPassword');
const loginError = document.getElementById('loginError');
// Cache the password after successful login so we don't store it globally
let passwordCache = '';
let history = [];
let historyIndex = -1;
function appendOutput(text) {
outputEl.textContent += text;
outputEl.scrollTop = outputEl.scrollHeight;
}
// Perform login by sending the password to the server for validation
async function performLogin() {
const pwd = loginPassword.value;
if (!pwd) return;
try {
const res = await fetch(window.location.href, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ password: pwd, action: 'login' })
});
const data = await res.json();
if (data.ok) {
passwordCache = pwd;
loginOverlay.style.display = 'none';
cmdInput.disabled = false;
cmdInput.focus();
statusText.textContent = 'Authenticated';
} else {
loginError.style.display = 'block';
// Clear the wrong password for security
loginPassword.value = '';
}
} catch (e) {
loginError.textContent = 'Error contacting server.';
loginError.style.display = 'block';
}
}
loginButton.addEventListener('click', performLogin);
loginPassword.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
performLogin();
}
});
// Execute commands via AJAX. Use cached password for authentication
async function runCommand(command) {
if (!command.trim()) return;
appendOutput("web@cpanel:~$ " + command + "\n");
try {
const res = await fetch(window.location.href, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ command, password: passwordCache })
});
const data = await res.json();
appendOutput(data.output || '');
if (!data.ok) {
appendOutput("(exit code: " + (data.statusCode ?? 'error') + ")\n");
}
} catch (e) {
appendOutput("Error contacting server.\n");
}
}
cmdInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
const cmd = cmdInput.value;
history.push(cmd);
historyIndex = history.length;
cmdInput.value = '';
runCommand(cmd);
} else if (e.key === 'ArrowUp') {
if (historyIndex > 0) {
historyIndex--;
cmdInput.value = history[historyIndex] || '';
setTimeout(() => cmdInput.setSelectionRange(cmdInput.value.length, cmdInput.value.length), 0);
}
e.preventDefault();
} else if (e.key === 'ArrowDown') {
if (historyIndex < history.length - 1) {
historyIndex++;
cmdInput.value = history[historyIndex] || '';
} else {
historyIndex = history.length;
cmdInput.value = '';
}
e.preventDefault();
}
});
// Allow focusing the input by clicking anywhere in the document after login
document.addEventListener('click', () => {
if (!cmdInput.disabled) {
cmdInput.focus();
}
});
// Initial greeting inside the terminal
appendOutput("Web Terminal (type commands after login)\n\n");
</script>
</body>
</html>