Guest User

Untitled

a guest
Jun 14th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. <?php
  2.  
  3. const SESSION = 'elephant_user';
  4. $flag = file_get_contents('/flag');
  5.  
  6.  
  7. class User {
  8.     public $name;
  9.     private $token;
  10.  
  11.     function __construct($name) {
  12.         $this->name = $name;
  13.         $this->token = md5($_SERVER['REMOTE_ADDR'] . rand());
  14.     }
  15.  
  16.     function canReadFlag() {
  17.         return strcmp($flag, $this->token) == 0;
  18.     }
  19. }
  20.  
  21. if (isset($_GET['logout'])) {
  22.     header('Location: /');
  23.     setcookie(SESSION, NULL, 0);
  24.     exit;
  25. }
  26.  
  27.  
  28. $user = NULL;
  29.  
  30. if ($name = $_POST['name']) {
  31.     $user = new User($name);
  32.     header('Location: /');
  33.     setcookie(SESSION, base64_encode(serialize($user)), time() + 600);
  34.     exit;
  35. } else if ($data = @$_COOKIE[SESSION]) {
  36.     $user = unserialize(base64_decode($data));
  37. }
  38.  
  39.  
  40.  
  41. ?><!DOCTYPE html>
  42. <head>
  43.     <title>Elephant</title>
  44.     <meta charset='utf-8'>
  45.     <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
  46.     <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
  47.     <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  48. </head>
  49. <body>
  50.     <?php if (!$user): ?>
  51.         <div id="login">
  52.             <h3 class="text-center text-white pt-5">Are you familiar with PHP?</h3>
  53.             <div class="container">
  54.                 <div id="login-row" class="row justify-content-center align-items-center">
  55.                     <div id="login-column" class="col-md-6">
  56.                         <div id="login-box" class="col-md-12">
  57.                             <form id="login-form" class="form" action="" method="post">
  58.                                 <h3 class="text-center text-info">What's your name!?</h3>
  59.                                 <div class="form-group">
  60.                                     <label for="name" class="text-info">Name:</label><br>
  61.                                     <input type="text" name="name" id="name" class="form-control">
  62.                                 </div>
  63.                                 <div class="form-group">
  64.                                     <input type="submit" name="submit" class="btn btn-info btn-md" value="let me in">
  65.                                 </div>
  66.                             </form>
  67.                         </div>
  68.                     </div>
  69.                 </div>
  70.             </div>
  71.         </div>
  72.     <?php else: ?>
  73.         <h3 class="text-center text-white pt-5">You may want to read the source code.</h3>
  74.         <div class="container" style="text-align: center">
  75.             <img src="images/elephant2.png">
  76.         </div>
  77.         <hr>
  78.         <div class="container">
  79.             <div class="row justify-content-center align-items-center">
  80.                 <div class="col-md-6">
  81.                     <div class="col-md-12">
  82.                         <h3 class="text-center text-info">Do you know?</h3>
  83.                         <h3 class="text-center text-info">PHP's mascot is an elephant!</h3>
  84.                         Hello, <b><?= $user->name ?></b>!
  85.                         <?php if ($user->canReadFlag()): ?>
  86.                             This is your flag: <b><?= $flag ?></b>
  87.                         <?php else: ?>
  88.                             Your token is not sufficient to read the flag!
  89.                         <?php endif; ?>
  90.                         <a href="?logout">Logout!</a>
  91.                     </div>
  92.                 </div>
  93.             </div>
  94.         </div>
  95.     <?php endif ?>
  96. </body>
Add Comment
Please, Sign In to add comment