|
Menu |
|
|
Home |
| |
|
Discussions |
| |
|
Tools |
| |
|
Affiliates |
| |
|
Content |
| |
|
Info |
| | |
|
|
|
|
|
User Info |
|
Membership:
Latest: MichaelSnaRe
New Today: 0
New Yesterday: 0
Overall: 9144
People Online:
Visitors: 46
Members: 0
Total: 46
|
|
|
|
|
|
Full disclosure |
|
|
|
|
|
|
|
|
|
IT Security and Insecurity Portal |
|
|
Upload shell little help |
|
Posted: Tue Aug 28, 2012 1:40 am |
|
|
Frenkie |
Advanced user |
|
|
Joined: Nov 10, 2008 |
Posts: 60 |
|
|
|
|
|
|
|
Hello guys,
I need advice is there any chance someone to upload shell in my website. I use following code to prevent malicious uploads.
First:
Code: | function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
$uploaddir = "./files";
if (($pext != "jpg") && ($pext != "jpeg") && ($pext != "gif") && ($pext != "swf"))
{
?>
Wrong extension. File must be jpg,jpeg,gif or swf.
<?
exit();
}
|
Then
Code: | $final_filename = str_replace(" ", "_", $imgfile_name);
$final_filename2 = "$imgfile_name";
$newfile = $uploaddir . "/$final_filename2";
if (is_uploaded_file($imgfile))
{
if (!copy($imgfile,"$newfile"))
{
print "Error Uploading File.";
exit();
}
} |
If you know way to bypass this please post here.
Thanks |
|
|
|
|
|
|
|
|
Posted: Tue Aug 28, 2012 9:26 am |
|
|
demon |
Moderator |
|
|
Joined: Sep 22, 2010 |
Posts: 485 |
|
|
|
|
|
|
|
a shell with .gif extension easily can be uploaded
just save the shell with this name: shell.php.gif and edit it so the first line in the shell mush be:
and then it will be uploaded without problem
and remember LFI can bypass .htaccess |
|
_________________ Go BIG or go HOME ! |
|
|
|
Posted: Tue Aug 28, 2012 1:24 pm |
|
|
Frenkie |
Advanced user |
|
|
Joined: Nov 10, 2008 |
Posts: 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Posted: Wed Aug 29, 2012 4:07 pm |
|
|
Cyko |
Moderator |
|
|
Joined: Jul 21, 2009 |
Posts: 375 |
|
|
|
|
|
|
|
A few suggestions:
1) You should use move_uploaded_file() instead of copy(), when allowing visitors to upload.
php.net wrote: | This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.
This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system. |
2) When saving files you should rename them (and not save them as they were).
3) You should avoid making custom functions, when they are already functions which exist internally (i.e. for the ext validation).
4) You should add a max file size limit (validate it via filesize).
5) Using exit() is unnecessary; when creating something like this - you should really be using If...Else statements, but it will do.
See below code for example use:
Code: | <?php
//replaced your method of getting the ext with an internal one...
$pext = pathinfo($imgfile_name, PATHINFO_EXTENSION);
$uploaddir = './files';
//replaced your method of validating with a better approach...
if (!in_array($pext, array(
'jpg',
'jpeg',
'gif',
'swf'
))) {
?>
Wrong extension. File must be jpg,jpeg,gif or swf.
<?php
exit();
}
//replace the original filename with a 'unique' alphanumeric one...
$final_filename = md5(uniqid(microtime(), true)) . ".{$pext}";
$newfile = $uploaddir . "/{$final_filename}";
if (is_uploaded_file($imgfile)) {
//used move_uploaded_file() instead of copy()...
if (!move_uploaded_file($imgfile, $newfile)) {
print 'Error Uploading File.';
exit();
}
} |
Untested |
|
Last edited by Cyko on Thu Aug 30, 2012 1:11 am; edited 1 time in total |
|
|
|
|
|
|
|
Posted: Thu Aug 30, 2012 12:50 am |
|
|
Frenkie |
Advanced user |
|
|
Joined: Nov 10, 2008 |
Posts: 60 |
|
|
|
|
|
|
|
Thank you Cyko for your reply. Its really useful.
Also can you tell me is there any way someone to bypass my initial code na upload shell to my website ?
Thanks |
|
|
|
|
Posted: Thu Aug 30, 2012 1:30 am |
|
|
Cyko |
Moderator |
|
|
Joined: Jul 21, 2009 |
Posts: 375 |
|
|
|
|
|
|
|
Frenkie wrote: | Thank you Cyko for your reply. Its really useful.
Also can you tell me is there any way someone to bypass my initial code na upload shell to my website ?
Thanks |
I can't think of any way to bypass your code.
But theres an issue with your code....
By default on apache any file which starts with .ht can not be viewed by the visitor.
So if a visitor uploads a file like .httest.gif, it will pass validation and get uploaded to the server, but you won't be able to view it (therefore can't embed it via img tags).
The code which I provided in my previous post; fixes this issue. |
|
|
|
|
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
|
|
|
|
|
|