|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: May 2006
Location: Amsterdam
Posts: 286
|
User Uploads & Unix Folder/File Permissions
I've had a hard time figuring out the best way to handle this issue ... I once set an upload folder to 777 only to have a hacker set-up a bank phishing page in the directory.
GOAL The functionality I'm looking for is the ability to allow a user to upload an image via an html form <input type="file"> and then have the image displayed on certain public and private web pages. All of this without a security loop-hole. WHAT I"VE GOT I've got the form working fine. I've got the PHP script working fine. The upload is successful (when the directory is set at 777 or PHP owns the folder). The image appears where I want it. UPLOAD FOLDER /public_html/uploads/ WHAT I TRIED After the phishing expedition I took the 777 away and made it 755 but the PHP script wouldn't write to the directory. I discovered that this happens because the PHP script doesn't run under my website's uid but under its own. So I deleted the upload folder and had the script create the folder so that the folder would have the PHP uid as its creator - this solved the problem but then I read-up on phpsuexec and get the impression that it's possible for another PHP script on the server to "dive-into" that folder and do what it wants since the uid PHP is using is "nobody". QUESTIONS
Thanks in advance for your help! Dan Last edited by danNL; Dec 3, 2006 at 13:18.. |
|
|
|
|
|
#2 |
|
SitePoint Wizard
![]() Join Date: Mar 2006
Posts: 6,132
|
yes, when apache is running php as a module, php often runs as the same user as the webserver. so all "websites" on that server run php as the same user. this can mean everyone can read/write everyones files if using php. (same with your session files! scandir() your session.save_path if you wanna scare yourself)
php's safe_mode and open_basedir attempt to solve this. but, they only affect php, not other languages. i dont know of any real solutions other than moving to a webhost that runs php as phpsuexec or suphp, or a vps etc... maybe its possible to use a cgi script to do the reading and writing to a directory where only your user can. but im not sure how you would execute this cgi script and make it run as your user, ive never looked into it. however, that still will not solve others being able to read your php files, which you must leave world readable at least if you want php to work. fyi- im definately not an expert on this. take my words with some salt.
__________________
1 |
|
|
|
|
|
#3 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: May 2006
Location: Amsterdam
Posts: 286
|
Hey clamcrusher thanks for your response. I'll continue with what I've found/done thus far but please don't feel obliged to post a response ...
I did a bit of reading about safe_mode and open_basedir, http://php.net/manual/en/features.safe-mode.php, and checked the web server's PHP settings with phpinfo() to verify the open_basedir limitation - it does exist and is limiting my access to my directory and the user library. My session tmp folder is in my home directory so I'm hoping this limits others' PHP access to session info - I'll look into that one a bit later.
Any thoughts? |
|
|
|
|
|
|
#4 |
|
Hosting
![]() ![]() ![]() ![]() Join Date: Feb 2002
Location: Auckland
Posts: 11,782
|
dan,
Interesting thread - thanks! The Apache/PHP/MySQL user must be the group assigned to Apache, NOT you! You don't want to be the only person to upload files, obviously, so that's out of the question. Uploads. They're uploaded to temporary directory and, IF UPLOADED (yes, there is a PHP command that checks that), it is then moved to the directory location you specify. IMHO, you MUST check the extension of the file before doing so. Because "nasties" can be embedded in image files, the truly paranoid among us would also ask GD to open the file in the correct format (GIF, JPG, PNG) and, if there is an error detected, DELETE the file rather than risk "a bad case of the nasties." If you want to allow uploads, that's about the best you can do - except make sure that the uploaded files are not 777 (universally executable) as image files should NOT be executable! Did I miss anything else in your posts? Regards, DK
__________________
David K. Lynn Data Koncepts Article: Setup, Config, Test & Write mod_rewrite regex (updated 21 Mar 2010) |
|
|
|
|
|
#5 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: May 2006
Location: Amsterdam
Posts: 286
|
Thanks for your reply DK.
I am using the PHP is_uploaded_file() to test for an actual uploaded file and then a check on the extension for a valid graphic extension as well as $_FILES['uploaded_file_name']['type'] to check the mime type. I haven't used GD to test the image but will look into how to do that - after the phishing trip I became a bit paranoid ![]() I still have a few questions (hope you don't mind) ...
|
|
|
|
|
|
#6 | |
|
Hosting
![]() ![]() ![]() ![]() Join Date: Feb 2002
Location: Auckland
Posts: 11,782
|
dan,
I'd be paranoid, too! ![]() Quote:
DK
__________________
David K. Lynn Data Koncepts Article: Setup, Config, Test & Write mod_rewrite regex (updated 21 Mar 2010) |
|
|
|
|
|
|
#7 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: May 2006
Location: Amsterdam
Posts: 286
|
Hey DK,
Thanks again for your continued input. ![]() For now I'll focus on what I imagine is the 'normal' set-up on a webserver:
Quote:
|
|
|
|
|
|
|
#8 |
|
Hosting
![]() ![]() ![]() ![]() Join Date: Feb 2002
Location: Auckland
Posts: 11,782
|
dan,
What you're telling me seems to be off-beat just a bit. PHP has it's own upload directory which you don't have to bother with (it's set in php.ini and will be beyond your control on a hosted server). All you need to do is use the move_uploaded_file() with two arguements, the $_FILES['file']['tmp_name'] of the uploaded file and the destination path/filename. For PHP to MOVE the file, all that's required is for it to find the 'tmp_name' of the file in its uploaded files directory and to be able to write ('x4x') to the path/filename (it WILL overwrite an existing file without warning!). From what you're saying, it can't write to your path unless it's a 777 and that's just not right (the 7 says read, write and EXECUTE which you do NOT want to allow!). I can't explain why you're only successful with 777. I have a couple of code bits in my sites that allow file uploads and will provide them via PM (if requested). Regards, DK
__________________
David K. Lynn Data Koncepts Article: Setup, Config, Test & Write mod_rewrite regex (updated 21 Mar 2010) |
|
|
|
|
|
#9 | ||
|
SitePoint Addict
![]() ![]() ![]() Join Date: May 2006
Location: Amsterdam
Posts: 286
|
Hey DK,
I set-up the script to work as you mentioned thinking it would work with the x4x permission but it didn't. I e-mailed the web host asking why the folder had to have 777 permission in order for a file upload to work and received this reply: Quote:
Quote:
With this in mind it seems that it would be prudent to do the following after a file has been succesfully moved from the temp folder to the upload folder:
Thanks again for sticking with me on this o Dan |
||
|
|
|
|
|
#10 |
|
Hosting
![]() ![]() ![]() ![]() Join Date: Feb 2002
Location: Auckland
Posts: 11,782
|
dan,
First, it sounds like your host is opening the door to security problems (i.e., a good host is recommended in place of these people). Remember that the file is uploaded to some random (hidden) directory (with a RANDOM filename) from which you move it to your own directory where you can do your testing then move again to the image directory. Code:
<Files *.*> order allow,deny deny from all </Files> <FilesMatch "\.(gif|jpe?g|png)$"> order allow,deny allow from all </FilesMatch> Code:
RewriteRule !\.(gif|jpe?g|png)$ - [F] Regards, DK
__________________
David K. Lynn Data Koncepts Article: Setup, Config, Test & Write mod_rewrite regex (updated 21 Mar 2010) |
|
|
|
|
|
#11 |
|
SitePoint Member
Join Date: Dec 2006
Posts: 1
|
I had the same problem. Only a dir with 777 would allow php to move the image to its final directory.
(Ask your ISP to) switch on Mod_suphp (thats substitue user). This will substitute the 'nobody' to ur own UID. Now you can set the dir to 755. You need to chmod the image files though to 644. Hope this helps... |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:25.












Thanks again for your continued input. 


Linear Mode
