|
|
|
|
|
|
IT Security and Insecurity Portal |
|
|
Hack this script - challenge |
|
Posted: Tue Jan 19, 2010 11:25 am |
|
|
gibbocool |
Advanced user |
|
|
Joined: Jan 22, 2008 |
Posts: 208 |
|
|
|
|
|
|
|
Hey all, this is a file upload script I had on my site previously, it has been hacked somehow and I can't figure out how! So here's a challenge - someone was able to upload php files.
Code: |
<?php
$host_name = "http://upload.gibbocool.com/files/";
$target = "upload/files/";
echo "Files uploaded will be <a href=http://upload.gibbocool.com/files>viewable to the public</a>.<br><br>";
$filename = basename( $_FILES['uploaded']['name']) ;
$file_append = rand(1000,1000000);
$file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
$file_ext = substr($filename, strripos($filename, '.'));
$filename = $file_basename . $file_append . $file_ext;
$target = $target . $filename;
$ok=1;
$uploaded_type = $_FILES['uploaded']['type'];
$uploaded_size = $_FILES['uploaded']['size'];
//This is our size condition
if ($uploaded_size > 262144000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type != "image/jpeg" && $uploaded_type != "image/png" && $uploaded_type != "image/jpeg" && $uploaded_type != "text/plain" && $uploaded_type != "image/pjpeg" && $uploaded_type != "image/gif" && $uploaded_type != "application/zip" && $uploaded_type != "application/gzip" && $uploaded_type != "application/rar" && $uploaded_type != "application/x-msdos-program")
{
if($uploaded_size != "") {
echo "Bad filetype<br>";
$ok=0;
}
}
//Save upload attempts
if($filename != "") {
$ref=$_SERVER['HTTP_REFERER'];
$agent=$_SERVER['HTTP_USER_AGENT'];
$ip=$_SERVER['REMOTE_ADDR'];
$time=date('Y-m-d-G:i:s');
$file = fopen("attempts.txt", "a");
$fstring = "File: $filename Type: $uploaded_type Size: $uploaded_size IP: $ip REF: $ref Agent: $agent Time: $time";
fwrite($file, $fstring);
fwrite($file,"\n");
fclose($file);
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "
Upload Successful!
<center>
<table border=0>
<tr>
<td>
<input type=\"text\" onclick=\"highlight(this)\" size=70 value=\"".$host_name.$filename."\"><br><br>
</td>
<td valign=\"right\">
<a href='".$host_name.$filename."'>Direct Link</a>
</td>
</tr>
<tr>
<td>
<input type=\"text\" onclick=\"highlight(this)\" size=70 value=\"[url=http://gibbocool.com][img]".$host_name.$filename."[/img][/url]\"><br><br>
</td>
<td valign=\"right\">
Hotlink for Forums
</td>
</tr>
<tr>
<td>
<input type=\"text\" onclick=\"highlight(this)\" size=70 value=\"<a href="http://gibbocool.com"><img src="".$host_name.$filename."" border="0" alt="Image Hosted by Gibbocool.com"/></a>\"><br><br>
</td>
<td valign=\"right\">
Hotlink for Websites
</td>
</tr>
</table>
</center>
";
}
else
{
echo "Select a file to upload.";
}
}
?>
|
|
|
|
|
|
|
|
|
|
Posted: Tue Jan 19, 2010 2:47 pm |
|
|
waraxe |
Site admin |
|
|
Joined: May 11, 2004 |
Posts: 2407 |
Location: Estonia, Tartu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Posted: Tue Jan 19, 2010 8:29 pm |
|
|
Cyko |
Moderator |
|
|
Joined: Jul 21, 2009 |
Posts: 375 |
|
|
|
|
|
|
|
Heres a quick patch (untested):
Code: | <?php
$host_name = "http://upload.gibbocool.com/files/";
$target = "upload/files/";
echo "Files uploaded will be <a href=http://upload.gibbocool.com/files>viewable to the public</a>.<br><br>";
$filename = basename($_FILES['uploaded']['name']);
$file_append = rand(1000,1000000);
$file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
//improved ext so it looks at the end
$file_ext = strtolower(end(explode('.',$filename)));
$filename = $file_basename . $file_append . $file_ext;
$target = $target . $filename;
$ok=1;
$uploaded_type = $_FILES['uploaded']['type'];
$uploaded_size = $_FILES['uploaded']['size'];
$uploaded_type = $_FILES['uploaded']['name'];
//This is our size condition
if ($uploaded_size > 262144000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//the allowed extensions - i converted the mime to the actual ext
$allowedExtensions = array('jpg','png','gif', 'txt', 'zip', 'gzip', 'rar', 'dll', 'pjpeg');
//not an allowed extension
if(!in_array($file_ext,$allowedExtensions)){
$ok=0;
}
//no size
if($uploaded_size != "") {
$ok=0;
}
//must not contain .php within filename even if extension is valid
if(preg_match("#.php#i", $uploaded_type)) {
$ok=0;
}
//Save upload attempts
if($filename != "") {
$ref=$_SERVER['HTTP_REFERER'];
$agent=$_SERVER['HTTP_USER_AGENT'];
$ip=$_SERVER['REMOTE_ADDR'];
$time=date('Y-m-d-G:i:s');
$file = fopen("attempts.txt", "a");
$fstring = "File: $filename Type: $uploaded_type Size: $uploaded_size IP: $ip REF: $ref Agent: $agent Time: $time";
fwrite($file, $fstring);
fwrite($file,"\n");
fclose($file);
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "
Upload Successful!
<center>
<table border=0>
<tr>
<td>
<input type=\"text\" onclick=\"highlight(this)\" size=70 value=\"".$host_name.$filename."\"><br><br>
</td>
<td valign=\"right\">
<a href='".$host_name.$filename."'>Direct Link</a>
</td>
</tr>
<tr>
<td>
<input type=\"text\" onclick=\"highlight(this)\" size=70 value=\"[url=http://gibbocool.com][img]".$host_name.$filename."[/img][/url]\"><br><br>
</td>
<td valign=\"right\">
Hotlink for Forums
</td>
</tr>
<tr>
<td>
<input type=\"text\" onclick=\"highlight(this)\" size=70 value=\"<a href="http://gibbocool.com"><img src="".$host_name.$filename."" border="0" alt="Image Hosted by Gibbocool.com"/></a>\"><br><br>
</td>
<td valign=\"right\">
Hotlink for Websites
</td>
</tr>
</table>
</center>
";
}
else
{
echo "Select a file to upload.";
}
}
?> |
|
|
Last edited by Cyko on Tue Jan 19, 2010 10:14 pm; edited 2 times in total |
|
|
|
|
|
|
|
Posted: Tue Jan 19, 2010 9:10 pm |
|
|
waraxe |
Site admin |
|
|
Joined: May 11, 2004 |
Posts: 2407 |
Location: Estonia, Tartu |
|
|
|
|
|
|
Your check is case sensitive:
Code: |
if(preg_match("#.php#", $uploaded_type))
|
If we use "1.pHp.2.pjpeg", then checking will be passed,
but Apache still parses file as php script.
I have seen solutions, where all dots "." except last one are converted to underscores. This will effectively counteract such vulnerability. |
|
|
|
|
Posted: Tue Jan 19, 2010 9:29 pm |
|
|
Cyko |
Moderator |
|
|
Joined: Jul 21, 2009 |
Posts: 375 |
|
|
|
|
|
|
|
waraxe wrote: | Your check is case sensitive:
Code: |
if(preg_match("#.php#", $uploaded_type))
|
If we use "1.pHp.2.pjpeg", then checking will be passed,
but Apache still parses file as php script.
I have seen solutions, where all dots "." except last one are converted to underscores. This will effectively counteract such vulnerability. |
Fixed, updated the code. |
|
|
|
|
Posted: Wed Jan 20, 2010 6:36 am |
|
|
gibbocool |
Advanced user |
|
|
Joined: Jan 22, 2008 |
Posts: 208 |
|
|
|
|
|
|
|
nice work, I'm sure people who want a secure upload script will find this useful |
|
|
|
|
www.waraxe.us Forum Index -> Remote file inclusion
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
All times are GMT
Page 1 of 1
|
|
|
Powered by phpBB © 2001-2008 phpBB Group
|
|
|
|
|