as - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of as.php

  1. <?php
  2.  
  3. <?php
  4.  
  5. include 'get_flag.php';
  6.  
  7. class User
  8. {
  9.     private $userlevel = 0;
  10.     private $username = '';
  11.  
  12.     function __construct($name, $level)
  13.     {
  14.         $this->username = $name;
  15.         $this->userlevel = $level;
  16.     }
  17.  
  18.     public function getName()
  19.     {
  20.         return $this->username;
  21.     }
  22.  
  23.     public function getBadge()
  24.     {
  25.         // Aha you shall not get the flag easily.
  26.         if ($this->userlevel === 999) $str = getFlag();
  27.         else $str = "Noob";
  28.         return $str;
  29.     }
  30. }
  31.  
  32. function sign($str)
  33. {
  34.     global $secret;
  35.     // This way of producing a signature is actually SUPER secure,
  36.     // because nobody else would know my secret, which is like
  37.     // 32 bytes of random characters. Try guessing that!
  38.     return hash('sha256', $secret . $str);
  39. }
  40.  
  41. function unserialize_safe($str)
  42. {
  43.     // http://php.net/manual/en/function.unserialize.php warns me
  44.     // not to pass untrusted user input to unserialize, but this
  45.     // is ok because I only unserialize data "signed" by me
  46.     return unserialize($str, ['allowed_classes' => ['User']]);
  47. }
  48.  
  49. if (isset($_COOKIE['users']) && isset($_COOKIE['signature'])) {
  50.     $serialized_users = $_COOKIE['users'];
  51.     $signature = $_COOKIE['signature'];
  52.     // http://php.net/manual/en/function.hash-equals.php
  53.     // Aha! Timing attack safe string comparison! Brute force aint gonna work.
  54.     if (hash_equals(sign($serialized_users), $signature) === FALSE) {
  55.         unset($_COOKIE['users']);
  56.     } else {
  57.         $serialized_users = explode('<x>', $serialized_users);
  58.         $users = array_map('unserialize_safe', $serialized_users);
  59.     }
  60. }
  61.  
  62. if (!isset($_COOKIE['users']) || !isset($_COOKIE['signature'])) {
  63.     $users = [new User("John Doe", 10), new User("Peter Parker", 33), new User("Gabe Newell", 87)];
  64.     // http://php.net/manual/en/function.serialize.php
  65.     // Maybe this will help you understand serialization.
  66.     $serialized_users = implode('<x>', array_map('serialize', $users));
  67.     setcookie('users', $serialized_users);
  68.     setcookie('signature', sign($serialized_users));
  69. }
  70. ?>
File Description
  • as
  • PHP Code
  • 20 Feb-2021
  • 2.01 Kb
You can Share it: