Websec

Websec.fr Level 17
The code for level17 is here. <?php include "flag.php"; function sleep_rand() { /* I wish php5 had random_int() */ $range = 100000; $bytes = (int) (log($range, 2) / 8) + 1; do { /* Side effect: more random cpu cycles wasted ;) */ $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); } while ($rnd >= $range); usleep($rnd); } ?> <!DOCTYPE html> <html> ....... <?php if (isset ($_POST['flag'])): sleep_rand(); /* This makes timing-attack impractical. */ ?> <br> <div class="container"> <div class="row"> <?php if (! strcasecmp ($_POST['flag'], $flag)) echo '<div class="alert alert-success">Here is your flag: <mark>' . $flag . '</mark>.</div>'; else echo '<div class="alert alert-danger">Invalid flag, sorry.</div>'; ?> </div> </div> <?php endif ?> ........ </html> After reading the code, we can ignore the first part of the PHP code because it’s impossible to break this function. But in the second part, the only thing that can lead to a vulnerability is strcasecmp. This function checks if the input matches the flag. Reading strcasecmp php documentation we can see that :
websec
Websec.fr Level 08
The source code of level8 is here. <?php $uploadedFile = sprintf('%1$s/%2$s', '/uploads', sha1($_FILES['fileToUpload']['name']) . '.gif'); if (file_exists ($uploadedFile)) { unlink ($uploadedFile); } if ($_FILES['fileToUpload']['size'] <= 50000) { if (getimagesize ($_FILES['fileToUpload']['tmp_name']) !== false) { if (exif_imagetype($_FILES['fileToUpload']['tmp_name']) === IMAGETYPE_GIF) { move_uploaded_file ($_FILES['fileToUpload']['tmp_name'], $uploadedFile); echo '<p class="lead">Dump of <a href="/level08' . $uploadedFile . '">'. htmlentities($_FILES['fileToUpload']['name']) . '</a>:</p>'; echo '<pre>'; include_once($uploadedFile); echo '</pre>'; unlink($uploadedFile); } else { echo '<p class="text-danger">The file is not a GIF</p>'; } } else { echo '<p class="text-danger">The file is not an image</p>'; } } else { echo '<p class="text-danger">The file is too big</p>'; } ?> With this code we have 2 problems to bypass:
websec file-upload
Websec.fr Level 04
For the Level Four challenge, there are two sources: here and here. Only the PHP code is shown here because the HTML is not important. source1.php <?php include 'connect.php'; $sql = new SQL(); $sql->connect(); $sql->query = 'SELECT username FROM users WHERE id='; if (isset ($_COOKIE['leet_hax0r'])) { $sess_data = unserialize (base64 _decode ($_COOKIE['leet_hax0r'])); try { if (is_array($sess_data) && $sess_data['ip'] != $_SERVER['REMOTE_ADDR']) { die('CANT HACK US!!!'); } } catch(Exception $e) { echo $e; } } else { $cookie = base64_encode (serialize (array ( 'ip' => $_SERVER['REMOTE_ADDR']))) ; setcookie ('leet_hax0r', $cookie, time () + (86400 * 30)); } if (isset ($_REQUEST['id']) && is_numeric ($_REQUEST['id'])) { try { $sql->query .= $_REQUEST['id']; } catch(Exception $e) { echo ' Invalid query'; } } ?> source2.php
websec insecure-deserialization
Websec.fr Level 02
The source code for level2 is here. <?php ini_set('display_errors', 'on'); class LevelTwo { public function doQuery($injection) { $pdo = new SQLite3('leveltwo.db', SQLITE3_OPEN_READONLY); $searchWords = implode (['union', 'order', 'select', 'from', 'group', 'by'], '|'); $injection = preg_replace ('/' . $searchWords . '/i', '', $injection); $query = 'SELECT id,username FROM users WHERE id=' . $injection . ' LIMIT 1'; $getUsers = $pdo->query ($query); $users = $getUsers->fetchArray (SQLITE3_ASSOC); if ($users) { return $users; } return false; } } if (isset ($_POST['submit']) && isset ($_POST['user_id'])) { $lt = new LevelTwo (); $userDetails = $lt->doQuery ($_POST['user_id']); } ?> <!DOCTYPE html> <html> .... </html> We can see that these two lines were problematic, all words in this dictionary are removed from the payload.
websec sqli
Websec.fr Level 01
Websec.fr Level 1 level01 - 1 point - 2564 solves The source code for level1 is shown here. Only the PHP code is included because the vulnerability is only in the PHP code. <?php session_start (); ini_set('display_errors', 'on'); ini_set('error_reporting', E_ALL); include 'anti_csrf.php'; init_token (); class LevelOne { public function doQuery($injection) { $pdo = new SQLite3('database.db', SQLITE3_OPEN_READONLY); $query = 'SELECT id,username FROM users WHERE id=' . $injection . ' LIMIT 1'; $getUsers = $pdo->query($query); $users = $getUsers->fetchArray(SQLITE3_ASSOC); if ($users) { return $users; } return false; } } if (isset ($_POST['submit']) && isset ($_POST['user_id'])) { check_and_refresh_token(); $lo = new LevelOne (); $userDetails = $lo->doQuery ($_POST['user_id']); } ?> <!DOCTYPE html> <html> ........ </html> The problem here is this line $query = 'SELECT id,username FROM users WHERE id=' . $injection . ' LIMIT 1'; it’s a sqli .
websec sqli

Tedsig42

Another infosec enthusiast blog