Waraxe IT Security Portal
Login or Register
October 16, 2026
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: 1245
Members: 0
Total: 1245
Full disclosure
VestaCP <= 0.9.8-26: authenticated config-parser eval sinks ->root RCE (VESTACP-2026-001)
[SYSS-2026-080]: dcm4che - Path Traversal (CWE-22)
[SYSS-2026-079]: dcm4che - Path Traversal (CWE-22)
[SYSS-2026-078]: dcm4che - Path Traversal (CWE-22)
[SYSS-2026-077]: dcm4che - Path Traversal (CWE-22)
[0day-rubbish] Synametrics Syncrify 6.2 build 1178 authenticated PublicLink chunk-upload path traversal to /etc/cron.d and root command execution (7.5)
[0day-rubbish] South River Technologies Titan MFT Server 26.0.0 first-run administrator setup chained to job post-command injection, NT AUTHORITY\SYSTEM (8.1)
[0day-rubbish] SmartBear Collaborator Server 15.1.15101 authenticated DOORS/Simulink review-file upload zip-slip to anonymous webroot Groovy Server Page execution as uid 1001 (7.5)
[0day-rubbish] PacsOne Server 7.3.9 authenticated DICOMweb STOW-RS SOP Instance UID path traversal to www-data code execution on a PACS host (7.2)
[0day-rubbish] Maian Pal 2.1 product image upload with no extension allow-list to PHP webshell and www-data uid 33 code execution (7.2, PR:H)
[0day-rubbish] Maian Guardian 2.1 license generator projid command injection to www-data uid 33 code execution (7.2, PR:H)
[0day-rubbish] Dataedo Portal 26.2.8 with Dataedo Agent 26.2.8 unrestricted scheduler job-type deserialization to Agent-host command execution (8.1)
[0day-rubbish] CloudPanel 2.5.4-3 site-settings rootDirectory nginx virtual-host injection to host root code execution (8.8)
[0day-rubbish] Auerswald COMmander 6000 / 6000R 8.6D unauthenticated provisioning import zip-slip to root binary hijack and root code execution (9.8)
[0day-rubbish] Abyss Web Server X1 2.16.22.0 first-run console credential takeover to configuration-write log-as-CGI command execution (7.5)
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



PCWizardHub - Helping you fix, build, and optimize your PC life
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.029 Seconds