|
| 1 | +# Challenge |
| 2 | +```php |
| 3 | +<?php |
| 4 | + |
| 5 | +$SECRET = `../read_secret`; |
| 6 | +$SANDBOX = "../data/" . md5($SECRET. $_SERVER["REMOTE_ADDR"]); |
| 7 | +$FILEBOX = "../file/" . md5("K0rz3n". $_SERVER["REMOTE_ADDR"]); |
| 8 | +@mkdir($SANDBOX); |
| 9 | +@mkdir($FILEBOX); |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +if (!isset($_COOKIE["session-data"])) { |
| 14 | + $data = serialize(new User($SANDBOX)); |
| 15 | + $hmac = hash_hmac("md5", $data, $SECRET); |
| 16 | + setcookie("session-data", sprintf("%s-----%s", $data, $hmac)); |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +class User { |
| 21 | + public $avatar; |
| 22 | + function __construct($path) { |
| 23 | + $this->avatar = $path; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +class K0rz3n_secret_flag { |
| 29 | + protected $file_path; |
| 30 | + function __destruct(){ |
| 31 | + if(preg_match('/(log|etc|session|proc|read_secret|history|class)/i', $this->file_path)){ |
| 32 | + die("Sorry Sorry Sorry"); |
| 33 | + } |
| 34 | + include_once($this->file_path); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +function check_session() { |
| 40 | + global $SECRET; |
| 41 | + $data = $_COOKIE["session-data"]; |
| 42 | + list($data, $hmac) = explode("-----", $data, 2); |
| 43 | + if (!isset($data, $hmac) || !is_string($data) || !is_string($hmac)){ |
| 44 | + die("Bye"); |
| 45 | + } |
| 46 | + if ( !hash_equals(hash_hmac("md5", $data, $SECRET), $hmac) ){ |
| 47 | + die("Bye Bye"); |
| 48 | + } |
| 49 | + $data = unserialize($data); |
| 50 | + |
| 51 | + if ( !isset($data->avatar) ){ |
| 52 | + die("Bye Bye Bye"); |
| 53 | + } |
| 54 | + return $data->avatar; |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +function upload($path) { |
| 59 | + if(isset($_GET['url'])){ |
| 60 | + if(preg_match('/^(http|https).*/i', $_GET['url'])){ |
| 61 | + $data = file_get_contents($_GET["url"] . "/avatar.gif"); |
| 62 | + if (substr($data, 0, 6) !== "GIF89a"){ |
| 63 | + die("Fuck off"); |
| 64 | + } |
| 65 | + file_put_contents($path . "/avatar.gif", $data); |
| 66 | + die("Upload OK"); |
| 67 | + }else{ |
| 68 | + die("Hacker"); |
| 69 | + } |
| 70 | + }else{ |
| 71 | + die("Miss the URL~~"); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +function show($path) { |
| 77 | + if ( !is_dir($path) || !file_exists($path . "/avatar.gif")) { |
| 78 | + |
| 79 | + $path = "/var/www"; |
| 80 | + } |
| 81 | + header("Content-Type: image/gif"); |
| 82 | + die(file_get_contents($path . "/avatar.gif")); |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +function check($path){ |
| 87 | + if(isset($_GET['c'])){ |
| 88 | + if(preg_match('/^(ftp|php|zlib|data|glob|phar|ssh2|rar|ogg|expect)(.|\\s)*|(.|\\s)*(file)(.|\\s)*/i',$_GET['c'])){ |
| 89 | + die("Hacker Hacker Hacker"); |
| 90 | + }else{ |
| 91 | + $file_path = $_GET['c']; |
| 92 | + list($width, $height, $type) = @getimagesize($file_path); |
| 93 | + die("Width is :" . $width." px<br>" . |
| 94 | + "Height is :" . $height." px<br>"); |
| 95 | + } |
| 96 | + }else{ |
| 97 | + list($width, $height, $type) = @getimagesize($path."/avatar.gif"); |
| 98 | + die("Width is :" . $width." px<br>" . |
| 99 | + "Height is :" . $height." px<br>"); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +function move($source_path,$dest_name){ |
| 105 | + global $FILEBOX; |
| 106 | + $dest_path = $FILEBOX . "/" . $dest_name; |
| 107 | + if(preg_match('/(log|etc|session|proc|root|secret|www|history|file|\.\.|ftp|php|phar|zlib|data|glob|ssh2|rar|ogg|expect|http|https)/i',$source_path)){ |
| 108 | + die("Hacker Hacker Hacker"); |
| 109 | + }else{ |
| 110 | + if(copy($source_path,$dest_path)){ |
| 111 | + die("Successful copy"); |
| 112 | + }else{ |
| 113 | + die("Copy failed"); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +$mode = $_GET["m"]; |
| 122 | + |
| 123 | +if ($mode == "upload"){ |
| 124 | + upload(check_session()); |
| 125 | +} |
| 126 | +else if ($mode == "show"){ |
| 127 | + show(check_session()); |
| 128 | +} |
| 129 | +else if ($mode == "check"){ |
| 130 | + check(check_session()); |
| 131 | +} |
| 132 | +else if($mode == "move"){ |
| 133 | + move($_GET['source'],$_GET['dest']); |
| 134 | +} |
| 135 | +else{ |
| 136 | + |
| 137 | + highlight_file(__FILE__); |
| 138 | +} |
| 139 | + |
| 140 | +include("./comments.html"); |
| 141 | + |
| 142 | +``` |
| 143 | +# Refference |
| 144 | ++ Lctf 2018 T4lk 1s ch34p,sh0w m3 the sh31l |
0 commit comments