|
| 1 | +<?php |
| 2 | + |
| 3 | +require 'config.php'; // $flag $key |
| 4 | + |
| 5 | + |
| 6 | +function encrypt($data, $key) { |
| 7 | + $length = openssl_cipher_iv_length('aes-256-cbc'); |
| 8 | + $iv = openssl_random_pseudo_bytes($length); |
| 9 | + $cipher = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); |
| 10 | + return base64_encode($iv . $cipher); |
| 11 | +} |
| 12 | + |
| 13 | +function decrypt($data, $key) { |
| 14 | + $length = openssl_cipher_iv_length('aes-256-cbc'); |
| 15 | + $data = base64_decode($data); |
| 16 | + $iv = substr($data, 0, $length); |
| 17 | + $cipher = substr($data, $length); |
| 18 | + return openssl_decrypt($cipher, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +if ($name = @$_POST['name']) { |
| 23 | + $data = serialize([ |
| 24 | + 'name' => $name, |
| 25 | + 'can_see_the_flag' => FALSE, |
| 26 | + ]); |
| 27 | + $session = encrypt($data, $key); |
| 28 | + $_COOKIE['session'] = $session; |
| 29 | + setcookie('session', $session); |
| 30 | + header('Location: .'); |
| 31 | + exit; |
| 32 | +} |
| 33 | + |
| 34 | +$showFlag = FALSE; |
| 35 | +$name = NULL; |
| 36 | + |
| 37 | +if ($session = @$_COOKIE['session']) { |
| 38 | + $data = decrypt($session, $key); |
| 39 | + if ($data === FALSE) { |
| 40 | + die('session error'); |
| 41 | + } |
| 42 | + $data = unserialize($data); |
| 43 | + $name = $data['name']; |
| 44 | + $showFlag = $data['can_see_the_flag']; |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +echo '<title>Find the FLAG</title>'; |
| 49 | +if ($name) { |
| 50 | + echo "<h1>Hi, $name.</h1>"; |
| 51 | + if ($showFlag) { |
| 52 | + echo "This is your flag: <b>$flag</b>"; |
| 53 | + } else { |
| 54 | + echo "You cannot see the flag!"; |
| 55 | + } |
| 56 | +} else { |
| 57 | + echo '<h1>Tell me your name!</h1>'; |
| 58 | + echo '<form method="post">'; |
| 59 | + echo '<input type="text" name="name" placeholder="name">'; |
| 60 | + echo ' '; |
| 61 | + echo '<input type="submit" value="Go">'; |
| 62 | + echo '</form>'; |
| 63 | +} |
0 commit comments