-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·536 lines (465 loc) · 18.8 KB
/
index.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?php
/* ------------------------------------------
* Constants used in application
* ------------------------------------------ */
require_once ('./includes/constants.php');
/* ------------------------------------------------------
* INCLUDE CLASS DEFINITION PRIOR TO INITIALIZING SESSION
* ------------------------------------------------------ */
require_once (__SITE_ROOT__.'/classes/EncodingHandler.php');
require_once (__SITE_ROOT__.'/classes/MySQLHandler.php');
require_once (__SITE_ROOT__.'/classes/SQLQueryHandler.php');
require_once (__SITE_ROOT__.'/classes/CustomErrorHandler.php');
require_once (__SITE_ROOT__.'/classes/LogHandler.php');
require_once (__SITE_ROOT__.'/classes/RemoteFileHandler.php');
require_once (__SITE_ROOT__.'/classes/RequiredSoftwareHandler.php');
/* ------------------------------------------
* INITIALIZE SESSION
* ------------------------------------------ */
if (session_status() == PHP_SESSION_NONE){
session_start();
}// end if
if (!isset($_SESSION["security-level"])){
$_SESSION["security-level"] = 0;
}// end if
/* ----------------------------------------------------
* ENFORCE SSL
* ----------------------------------------------------
* If the user would like to enforce the use of SSL,
* redirect any HTTP requests "up to" HTTPS. Otherwise
* keep the URL the same.
* ---------------------------------------------------- */
if (!isset($_SESSION["EnforceSSL"])){
$_SESSION["EnforceSSL"] = "False";
}// end if
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
case "1": // This code is insecure
if ($_SESSION["EnforceSSL"] == "True"){
if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS']!="on"){
$lSecureRedirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location: $lSecureRedirect");
exit();
}//end if
}//end if
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
if ($_SESSION["EnforceSSL"] == "True"){
if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS']!="on"){
require_once('ssl-enforced.php');
exit();
}//end if
}//end if
break;
}// end switch
/* ----------------------------------------------------
* Initialize logged in status
* ----------------------------------------------------
* user is logged out by default
*/
if (!isset($_SESSION["loggedin"])){
$_SESSION['loggedin'] = 'False';
$_SESSION['logged_in_user'] = '';
$_SESSION['logged_in_usersignature'] = '';
}// end if
/* ----------------------------------------------------
* Check if user wants to disregard any detected
* database errors
* ----------------------------------------------------
* user is logged out by default
*/
if (!isset($_SESSION["UserOKWithDatabaseFailure"])) {
$_SESSION["UserOKWithDatabaseFailure"] = "FALSE";
}// end if
/* ----------------------------------------
* initialize showhints session and cookie
* ----------------------------------------
* This code is here to create a simulated vulnerability. Some
* sites put authorication and status tokens in cookies instead
* of the session. This is a mistake. The user controls the
* cookies entirely.
*/
if (isset($_COOKIE["showhints"])){
$l_showhints = $_COOKIE["showhints"];
}else{
$l_showhints = 1;
/*
* If in secure mode, we want the cookie to be protected
* with HTTPOnly flag. There is some irony here. In secure code,
* we are to ignore authorization cookies, so we are protecting
* a cookie we know we are going to ignore. But the point is to
* provide an example to developers of proper coding techniques.
*/
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
case "1": // This code is insecure
$lProtectCookies = FALSE;
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
$lProtectCookies = TRUE;
break;
}// end switch
$l_cookie_options = array (
'expires' => 0, // 0 means session cookie
'path' => '/', // '/' means entire domain
//'domain' => '.example.com', // default is current domain
'secure' => FALSE, // true or false
'httponly' => FALSE, // true or false
'samesite' => 'Lax' // None || Lax || Strict
);
if ($lProtectCookies){
/* The showhints cookie is unprotected on purpose because
* it is used in one of the lab assignments */
//$l_cookie_options['httponly'] = TRUE;
$l_cookie_options['samesite'] = 'Strict';
}// end if
# setcookie accepts array of options starting in PHP 7.3
setcookie('showhints', $l_showhints, $l_cookie_options);
}// end if (isset($_COOKIE["showhints"])){
if (!isset($_SESSION["showhints"]) || ($_SESSION["showhints"] != $l_showhints)){
// make session = cookie
$_SESSION["showhints"] = $l_showhints;
switch ($l_showhints){
case 0: $_SESSION["hints-enabled"] = "Disabled"; break;
case 1: $_SESSION["hints-enabled"] = "Enabled"; break;
}// end switch
}//end if
/* ------------------------------------------
* initialize Encoder
* ------------------------------------------ */
$Encoder = new EncodingHandler();
/* ------------------------------------------
* Test for database availability
* ------------------------------------------ */
function handleError($errno, $errstr, $errfile, $errline, array $errcontext){
/*
restore_error_handler();
restore_exception_handler();
header("Location: database-offline.php", true, 302);
exit();
*/
}// end function
function handleException($exception){
//restore_error_handler();
restore_exception_handler();
header("Location: database-offline.php", true, 302);
exit();
}// end function
if ($_SESSION["UserOKWithDatabaseFailure"] == "FALSE") {
//set_error_handler('handleError', E_ALL & ~E_NOTICE);
set_exception_handler('handleException');
MySQLHandler::databaseAvailable();
//restore_error_handler();
restore_exception_handler();
}//end if
/* ------------------------------------------
* initialize custom error handler
* ------------------------------------------ */
$CustomErrorHandler = new CustomErrorHandler($_SESSION["security-level"]);
/* ------------------------------------------
* initialize log handler
* ------------------------------------------ */
$LogHandler = new LogHandler($_SESSION["security-level"]);
/* ------------------------------------------
* initialize MySQL handler
* ------------------------------------------ */
$MySQLHandler = new MySQLHandler($_SESSION["security-level"]);
$MySQLHandler->connectToDefaultDatabase();
/* ------------------------------------------
* initialize SQL Query handler
* ------------------------------------------ */
$SQLQueryHandler = new SQLQueryHandler($_SESSION["security-level"]);
/* ------------------------------------------
* initialize remote file handler
* ------------------------------------------ */
$RemoteFileHandler = new RemoteFileHandler($_SESSION["security-level"]);
/* ------------------------------------------
* initialize required software handler
* ------------------------------------------ */
$RequiredSoftwareHandler = new RequiredSoftwareHandler($_SESSION["security-level"]);
/* ------------------------------------------
* PROCESS REQUESTS
* ------------------------------------------ */
if (isset($_GET["do"])){
include_once(__SITE_ROOT__.'/includes/process-commands.php');
}// end if
/* ------------------------------------------
* PROCESS LOGIN ATTEMPT (IF ANY)
* ------------------------------------------ */
if (isset($_POST["login-php-submit-button"])){
include_once(__SITE_ROOT__.'/includes/process-login-attempt.php');
}// end if
/* ------------------------------------------
* REACT TO CLIENT SIDE CHANGES
* ------------------------------------------ */
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
case "1": // This code is insecure
/* Use the clients authorization token which is stored in
* the cookie (in this case). Placing authorization tokens
* on the client is fairly ridiculous.
*
* Known Vulnerabilities: SQL Injection, Authorization Bypass, Session Fixation,
* Lack of custom error page, Application Exception
*/
if (isset($_COOKIE['uid'])){
try{
$lQueryResult = $SQLQueryHandler->getUserAccountByID($_COOKIE['uid']);
// Switch to whatever cookie the user sent to simulate sites
// that use client-side authorization tokens. Auth information
// should never be in cookies.
if ($lQueryResult->num_rows > 0) {
$row = $lQueryResult->fetch_object();
$_SESSION['loggedin'] = 'True';
$_SESSION['uid'] = $row->cid;
$_SESSION['logged_in_user'] = $row->username;
$_SESSION['logged_in_usersignature'] = $row->mysignature;
$_SESSION['is_admin'] = $row->is_admin;
if (isset($_SESSION['logged_in_user'])){
header('Logged-In-User: '.$_SESSION['logged_in_user'], true);
}// end if
}// end if ($result->num_rows > 0)
} catch (Exception $e) {
echo $CustomErrorHandler->FormatError($e, $lQueryString);
}// end try
}else{
/*
* Output the user's login name into a custom header
*
* Known Vulnerability: Potential HTTP Response Splitting
* (PHP defends itself against HTTP response splitting by
* filtering "new line" characters)
*/
if (isset($_SESSION['logged_in_user'])){
header('Logged-In-User: '.$_SESSION['logged_in_user'], true);
}// end if
}// end if
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
/* If we are secure, then we do not rely on any client input
* to make authorization decisions. Authorization tokens should
* never be stored on the client. We use the SESSION in secure mode.
*
* Also, when we create an HTTP header, we encode any output to
* prevent response splitting. The critical chars in response splitting
* are CR-LF. Dont fall for filtering. Just encode it all.
*/
if (isset($_SESSION['logged_in_user'])){
header('Logged-In-User: '.$Encoder->encodeForHTML($_SESSION['logged_in_user']), TRUE);
}// end if
break;
}// end switch
/* ------------------------------------------
* END REACT TO CLIENT SIDE CHANGES
* ------------------------------------------ */
/* ------------------------------------------
* Security Headers (Modern Browsers)
* ------------------------------------------ */
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
case "1":
/* Built-in user-agent defenses */
header("X-XSS-Protection: 0;", TRUE);
/* Disable HSTS */
header("Strict-Transport-Security: max-age=0", TRUE);
// HTTP/1.1 cache control
header("Cache-Control: public", TRUE);
/* Referrer Policy */
header("Referrer-Policy: unsafe-url", TRUE);
header_remove("Pragma");
/* Content sniffing */
header_remove("X-Content-Type-Options");
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
/* Built-in user-agent defenses */
header("X-XSS-Protection: 1; mode=block;", TRUE);
/* Enable HSTS - I would like to enable this but the problem is this header caches so messes
* up labs once the user sets the security level back to level 0*/
//header("Strict-Transport-Security: max-age=31536000; includeSubDomains", TRUE);
// HTTP/1.1 cache control
header('Cache-Control: no-store, no-cache', TRUE);
// HTTP/1.0 cache-control
header("Pragma: no-cache", TRUE);
/* Cross-frame scripting and click-jacking */
header('X-FRAME-OPTIONS: DENY', TRUE);
header("Content-Security-Policy: frame-ancestors 'none';", TRUE);
/* Content sniffing */
header("X-Content-Type-Options: nosniff", TRUE);
/* Referrer Policy */
header("Referrer-Policy: no-referrer", TRUE);
/* Server version banners */
header_remove("X-Powered-By");
header_remove("Server");
break;
}// end switch
/* ------------------------------------------
* END Security Headers (Modern Browsers)
* ------------------------------------------ */
/* ------------------------------------------
* Set the HTTP content-type of this page
* ------------------------------------------ */
header("Content-Type: text/html;charset=UTF-8", TRUE);
/* ------------------------------------------
* DISPLAY PAGE
* ------------------------------------------ */
/* ------------------------------------------
* "PAGE" VARIABLE INJECTION
* ------------------------------------------ */
global $lPage;
$lPage = __SITE_ROOT__.'/home.php';
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
case "1": // This code is insecure
// Get the value of the "page" URL query parameter
if (isset($_REQUEST["page"])) {
$lPage = $_REQUEST["page"];
}// end if
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
/* To prevent page injection, we start with the basic priciple
* of "DENY ALL". We decide to allow only the characters abosolutely
* neccesary to spell the Mutillidae file names. This requires
* alpha, hyphen, and period.
*/
// Get the value of the "page" URL query parameter without accepting POST
// to prevent method tampering.
if (isset($_GET["page"])) {
$lPage = $_GET["page"];
}// end if
$lPageIsAllowed = (preg_match("/^[a-zA-Z0-9\.\-\/]+[\.php|\.html]$/", $lPage) == 1);
if (!$lPageIsAllowed){
$lPage = __SITE_ROOT__.'/page-not-found.php';
}// end if
break;
}// end switch
/* ------------------------------------------
* END "PAGE" VARIABLE INJECTION
* ------------------------------------------ */
/* ------------------------------------------
* SIMULATE "SECRET" PAGES
* ------------------------------------------ */
switch ($lPage){
case ".htaccess":
case ".htaccess.php":
case "secret.php":
case "admin.php":
case "_adm.php":
case "_admin.php":
case "root.php":
case "administrator.php":
case "auth.php":
case "hidden.php":
case "console.php":
case "conf.php":
case "_private.php":
case "private.php":
case "access.php":
case "control.php":
case "control-panel.php":
case "bash_history":
case ".history":
case ".htpasswd":
case ".htpasswd.php":
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
case "1": // This code is insecure
$lPage=__SITE_ROOT__.'/phpinfo.php';
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
/* To prevent unauthorized access, we start with the basic priciple
* of "DENY ALL".
*/
$lUserAuthorized = FALSE;
if(isset($_SESSION['is_admin'])){
if($_SESSION['is_admin'] == 'TRUE'){
$lUserAuthorized = TRUE;
}// end if is_admin
}// end if isseet $_SESSION['is_admin']
if($lUserAuthorized){
$lPage=__SITE_ROOT__.'/phpinfo.php';
}else{
$lPage=__SITE_ROOT__.'/authorization-required.php';
}// end if $lUserAuthorized
break;//case 5
}// end switch
break;
default:break;
}//end switch on page
/* ------------------------------------------
* END SIMULATE "SECRET" PAGES
* ------------------------------------------ */
/* ------------------------------------------
* Set Content Security Policy (CSP) if needed
* ------------------------------------------ */
if ($lPage == "content-security-policy.php"){
$lReportToHeader = 'Report-To: {"group": "csp-endpoint", "max_age": 10886400, "endpoints":[{"url": "includes/capture-data.php"}]}';
$CSPNonce = bin2hex(openssl_random_pseudo_bytes(32));
$lCSP = "Content-Security-Policy: " .
"script-src 'self' 'nonce-{$CSPNonce}' mutillidae.localhost;" .
"style-src 'unsafe-inline' 'self' mutillidae.localhost;" .
"img-src 'self' mutillidae.localhost www.paypalobjects.com;" .
"connect-src 'self' mutillidae.localhost;" .
"form-action 'self' mutillidae.localhost;" .
"font-src 'none';" .
"frame-src 'self' mutillidae.localhost;" .
"media-src 'none';" .
"object-src 'none';" .
"default-src 'self';".
"frame-ancestors 'none';".
"report-uri includes/capture-data.php;" .
"report-to csp-endpoint;";
header($lReportToHeader, TRUE);
header($lCSP, TRUE);
}else{
$CSPNonce = "";
}// end if
/* ------------------------------------------
* END Content Security Policy (CSP)
* ------------------------------------------ */
/* ------------------------------------------
* BEGIN OUTPUT RESPONSE
* ------------------------------------------ */
require_once (__SITE_ROOT__."/includes/header.php");
if (strlen($lPage)==0 || !isset($lPage)){
/* Default Page */
require_once(__SITE_ROOT__."/home.php");
}else{
/* All Other Pages */
if (file_exists($lPage) || $RemoteFileHandler->remoteSiteIsReachable($lPage)){
require_once ($lPage);
}else{
if(!$RemoteFileHandler->curlIsInstalled()){
echo $RemoteFileHandler->getNoCurlAdviceBasedOnOperatingSystem();
}//end if
require_once (__SITE_ROOT__."/page-not-found.php");
}//end if
}// end if page variable not set
require_once (__SITE_ROOT__."/includes/information-disclosure-comment.php");
require_once (__SITE_ROOT__."/includes/footer.php");
/* ------------------------------------------
* LOG USER VISIT TO PAGE
* ------------------------------------------ */
include_once (__SITE_ROOT__."/includes/log-visit.php");
/* ------------------------------------------
* CLOSE DATABASE CONNECTION
* ------------------------------------------ */
$MySQLHandler->closeDatabaseConnection();
?>