UntitledPotato

natas20

Nov 10th, 2022
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.76 KB | Cybersecurity | 0 0
  1.  <html>
  2. <head>
  3. <!-- This stuff in the header has nothing to do with the level -->
  4. <link rel="stylesheet" type="text/css" href="http://natas.labs.overthewire.org/css/level.css">
  5. <link rel="stylesheet" href="http://natas.labs.overthewire.org/css/jquery-ui.css" />
  6. <link rel="stylesheet" href="http://natas.labs.overthewire.org/css/wechall.css" />
  7. <script src="http://natas.labs.overthewire.org/js/jquery-1.9.1.js"></script>
  8. <script src="http://natas.labs.overthewire.org/js/jquery-ui.js"></script>
  9. <script src=http://natas.labs.overthewire.org/js/wechall-data.js></script><script src="http://natas.labs.overthewire.org/js/wechall.js"></script>
  10. <script>var wechallinfo = { "level": "natas20", "pass": "<censored>" };</script></head>
  11. <body>
  12. <h1>natas20</h1>
  13. <div id="content">
  14. <?php
  15.  
  16. function debug($msg) { /* {{{ */
  17.     if(array_key_exists("debug", $_GET)) {
  18.         print "DEBUG: $msg<br>";
  19.     }
  20. }
  21. /* }}} */
  22. function print_credentials() { /* {{{ */
  23.     if($_SESSION and array_key_exists("admin", $_SESSION) and $_SESSION["admin"] == 1) {
  24.     print "You are an admin. The credentials for the next level are:<br>";
  25.     print "<pre>Username: natas21\n";
  26.     print "Password: <censored></pre>";
  27.     } else {
  28.     print "You are logged in as a regular user. Login as an admin to retrieve credentials for natas21.";
  29.     }
  30. }
  31. /* }}} */
  32.  
  33. /* we don't need this */
  34. function myopen($path, $name) {
  35.     //debug("MYOPEN $path $name");
  36.     return true;
  37. }
  38.  
  39. /* we don't need this */
  40. function myclose() {
  41.     //debug("MYCLOSE");
  42.     return true;
  43. }
  44.  
  45. function myread($sid) {
  46.     debug("MYREAD $sid");
  47.     if(strspn($sid, "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-") != strlen($sid)) {
  48.     debug("Invalid SID");
  49.         return "";
  50.     }
  51.     $filename = session_save_path() . "/" . "mysess_" . $sid;
  52.     if(!file_exists($filename)) {
  53.         debug("Session file doesn't exist");
  54.         return "";
  55.     }
  56.     debug("Reading from ". $filename);
  57.     $data = file_get_contents($filename);
  58.     $_SESSION = array();
  59.     foreach(explode("\n", $data) as $line) {
  60.         debug("Read [$line]");
  61.     $parts = explode(" ", $line, 2);
  62.     if($parts[0] != "") $_SESSION[$parts[0]] = $parts[1];
  63.     }
  64.     return session_encode();
  65. }
  66.  
  67. function mywrite($sid, $data) {
  68.     // $data contains the serialized version of $_SESSION
  69.     // but our encoding is better
  70.     debug("MYWRITE $sid $data");
  71.     // make sure the sid is alnum only!!
  72.     if(strspn($sid, "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-") != strlen($sid)) {
  73.     debug("Invalid SID");
  74.         return;
  75.     }
  76.     $filename = session_save_path() . "/" . "mysess_" . $sid;
  77.     $data = "";
  78.     debug("Saving in ". $filename);
  79.     ksort($_SESSION);
  80.     foreach($_SESSION as $key => $value) {
  81.         debug("$key => $value");
  82.         $data .= "$key $value\n";
  83.     }
  84.     file_put_contents($filename, $data);
  85.     chmod($filename, 0600);
  86. }
  87.  
  88. /* we don't need this */
  89. function mydestroy($sid) {
  90.     //debug("MYDESTROY $sid");
  91.     return true;
  92. }
  93. /* we don't need this */
  94. function mygarbage($t) {
  95.     //debug("MYGARBAGE $t");
  96.     return true;
  97. }
  98.  
  99. session_set_save_handler(
  100.     "myopen",
  101.     "myclose",
  102.     "myread",
  103.     "mywrite",
  104.     "mydestroy",
  105.     "mygarbage");
  106. session_start();
  107.  
  108. if(array_key_exists("name", $_REQUEST)) {
  109.     $_SESSION["name"] = $_REQUEST["name"];
  110.     debug("Name set to " . $_REQUEST["name"]);
  111. }
  112.  
  113. print_credentials();
  114.  
  115. $name = "";
  116. if(array_key_exists("name", $_SESSION)) {
  117.     $name = $_SESSION["name"];
  118. }
  119.  
  120. ?>
  121.  
  122. <form action="index.php" method="POST">
  123. Your name: <input name="name" value="<?=$name?>"><br>
  124. <input type="submit" value="Change name" />
  125. </form>
  126. <div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
  127. </div>
  128. </body>
  129. </html>
  130.  
Advertisement
Add Comment
Please, Sign In to add comment