Waraxe IT Security Portal
Login or Register
July 27, 2024
Menu
Home
Logout
Discussions
Forums
Members List
IRC chat
Tools
Base64 coder
MD5 hash
CRC32 checksum
ROT13 coder
SHA-1 hash
URL-decoder
Sql Char Encoder
Affiliates
y3dips ITsec
Md5 Cracker
User Manuals
AlbumNow
Content
Content
Sections
FAQ
Top
Info
Feedback
Recommend Us
Search
Journal
Your Account
User Info
Welcome, Anonymous
Nickname
Password
(Register)

Membership:
Latest: MichaelSnaRe
New Today: 0
New Yesterday: 0
Overall: 9144

People Online:
Visitors: 230
Members: 0
Total: 230
Full disclosure
CyberDanube Security Research 20240722-0 | Multiple Vulnerabilities in Perten/PerkinElmer ProcessPlus
[KIS-2024-06] XenForo <= 2.2.15 (Template System) Remote Code Execution Vulnerability
[KIS-2024-05] XenForo <= 2.2.15 (Widget::actionSave) Cross-Site Request Forgery Vulnerability
CVE-2024-33326
CVE-2024-33327
CVE-2024-33328
CVE-2024-33329
CyberDanube Security Research 20240703-0 | Authenticated Command Injection in Helmholz Industrial Router REX100
SEC Consult SA-20240627-0 :: Local Privilege Escalation via MSI installer in SoftMaker Office / FreeOffice
SEC Consult SA-20240626-0 :: Multiple Vulnerabilities in Siemens Power Automation Products
Novel DoS Vulnerability Affecting WebRTC Media Servers
APPLE-SA-06-25-2024-1 AirPods Firmware Update 6A326, AirPods Firmware Update 6F8, and Beats Firmware Update 6F8
40 vulnerabilities in Toshiba Multi-Function Printers
17 vulnerabilities in Sharp Multi-Function Printers
SEC Consult SA-20240624-0 :: Multiple Vulnerabilities allowing complete bypass in Faronics WINSelect (Standard + Enterprise)
Log in Register Forum FAQ Memberlist Search
IT Security and Insecurity Portal

www.waraxe.us Forum Index -> Remote file inclusion -> Hack this script - challenge
Post new topicReply to topic View previous topic :: View next topic
Hack this script - challenge
PostPosted: Tue Jan 19, 2010 11:25 am Reply with quote
gibbocool
Advanced user
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=\"&lt;a href=&quot;http://gibbocool.com&quot;&gt;&lt;img src=&quot;".$host_name.$filename."&quot; border=&quot;0&quot; alt=&quot;Image Hosted by Gibbocool.com&quot;/&gt;&lt;/a&gt;\"><br><br>
</td>
<td valign=\"right\">
Hotlink for Websites
</td>
</tr>
</table>
</center>
";
}
else
{
echo "Select a file to upload.";
}
}


?>

_________________
http://www.gibbocool.com
View user's profile Send private message Visit poster's website
PostPosted: Tue Jan 19, 2010 2:47 pm Reply with quote
waraxe
Site admin
Site admin
Joined: May 11, 2004
Posts: 2407
Location: Estonia, Tartu




This script is affected by old Apache feature. It appears, that Apache will try to parse files with ".php" in filename as php files, if MIME is unknown to webserver.
Some years ago RAR files were usable, now Apache recognizes them.
Still, pjpeg is unknown to Apache, so:

1. write php script and name it as "1.php.2.pjpeg"
2. upload to the server
3. access via http:

http://localhost/test/1.php.2669917.pjpeg

Wink

Some real world examples:


http://securityreason.com/wlb_show/WLB-2010010050
http://www.waraxe.us/advisory-57.html
View user's profile Send private message Send e-mail Visit poster's website
PostPosted: Tue Jan 19, 2010 8:29 pm Reply with quote
Cyko
Moderator
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=\"&lt;a href=&quot;http://gibbocool.com&quot;&gt;&lt;img src=&quot;".$host_name.$filename."&quot; border=&quot;0&quot; alt=&quot;Image Hosted by Gibbocool.com&quot;/&gt;&lt;/a&gt;\"><br><br>
</td>
<td valign=\"right\">
Hotlink for Websites
</td>
</tr>
</table>
</center>
";
}
else
{
echo "Select a file to upload.";
}
}


?>


Smile


Last edited by Cyko on Tue Jan 19, 2010 10:14 pm; edited 2 times in total
View user's profile Send private message
PostPosted: Tue Jan 19, 2010 9:10 pm Reply with quote
waraxe
Site admin
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.
View user's profile Send private message Send e-mail Visit poster's website
PostPosted: Tue Jan 19, 2010 9:29 pm Reply with quote
Cyko
Moderator
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.
View user's profile Send private message
PostPosted: Wed Jan 20, 2010 6:36 am Reply with quote
gibbocool
Advanced user
Advanced user
Joined: Jan 22, 2008
Posts: 208




nice work, I'm sure people who want a secure upload script will find this useful

_________________
http://www.gibbocool.com
View user's profile Send private message Visit poster's website
Hack this script - challenge
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

Post new topicReply to topic


Powered by phpBB © 2001-2008 phpBB Group



Space Raider game for Android, free download - Space Raider gameplay video - Zone Raider mobile games
All logos and trademarks in this site are property of their respective owner. The comments and posts are property of their posters, all the rest (c) 2004-2024 Janek Vind "waraxe"
Page Generation: 0.265 Seconds