Current location: Hot Scripts Forums » General Community » Script Requests » Simple htaccess file to disallow script execution


Simple htaccess file to disallow script execution

Reply
  #1 (permalink)  
Old 09-03-06, 01:04 AM
Industriality Industriality is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Simple htaccess file to disallow script execution

I have a writable folder on my server (Linux, Apache) to allow users to upload JPG files. What I want to do is use an .htaccess file to prevent any script execution in that folder, just in case someone is able to upload a PHP script instead of a JPG, for example. Any idea how to do that with htaccess? Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-03-06, 01:21 AM
Industriality Industriality is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Well, after searching more, I found a functional way of doing it, but it's a pretty ridiculous hack and I can't recommend anybody actually use it. You'll be able to see easily that I just copied and pasted part of someone else's htaccess file.

The result is that all php files (presumably without "chat" in the filename) get redirected to a nonexistant file, 404.php, with some arguments at the end. Since this file does not exist on the server, any PHP files executed in the folder where this .htaccess file resides will not actually be executed, but will return a 404 not found error.

If someone has a more logical approach to this, please let me know. Here is the functional code:
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !chat
RewriteRule ^(.*.php)$ 404.php?vbseourl=$1&%{QUERY_STRING} [L]
An ideal .htaccess file for me would be to disallow ANY files to execute no matter what their extension. The folder will only be used for JPGs and should only allow real JPG files to be loaded in a browser.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-03-06, 02:42 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Industriality
An ideal .htaccess file for me would be to disallow ANY files to execute no matter what their extension. The folder will only be used for JPGs and should only allow real JPG files to be loaded in a browser.
Why use an .htaccess file? Simply test the file that is being upload and verify that it is indeed a .jpeg file. If you use Imagemagick, there is a built in function for this.

Hope this helps!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-03-06, 02:54 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
I believe folder permissions would also be helpful. For example 0777 is read, write, and execute. 0666 is read and write only.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-03-06, 04:54 PM
Industriality Industriality is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
First, thanks to both responses.

curbview, I don't have control over the php file(s) that handle the uploads, plus I've had cases of remote scripting being able to upload files to writable folders without an upload php file on the server, so using an .htaccess file would cover both bases without the need to touch any php files.

mab, permissions must be set to 0777 for the folder to accept the JPG uploads. I can't change that. I imagine the upload script could change the permissions of the folder to 0777 before upload, then change back to 0666 when upload is complete, but again I'd prefer not to have to touch the php file(s) themselves. Having an .htaccess file that I could place in all of my writable folders which prevents script execution would be very useful.

Do you see any drawbacks to the somewhat strange way I did it in my second post? Besides throwing off my 404 Not Found logs?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 09-03-06, 05:39 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Industriality
First, thanks to both responses.
permissions must be set to 0777 for the folder to accept the JPG uploads. I can't change that.
*ANY* web site that has a folder set to 777 is hackable. It will not matter what kind of .htaccess file you have in that folder as with the permissions being 777, I can remotely destroy that file, delete it or whatever else I would like to do.

Folders with permissions set to 777 is like putting a dead-bolt lock on the front door but leaving all the ground floor windows wide open. In this example:

dead-bolt lock = .htaccess file
ground floor windows = 777 chmod folder

777 = the entire world may edit, delete, upload a file (BIG SECURITY EXPLOIT)
755 = the entire world may read & execute the file
644 = the entire world may read the file

All this to say that a properly written script should be able to accept file uploads. The script (if the permissions were set correctly on the script itself) should now be able to add the file uploaded to the folder. *every* directory that is available for the public to access should have permissions set no higher than 644 orther than the CGI-BIN which should be set to 755. If you are on a server that requires the cgi-bin to be chmod'd to 775, LEAVE THAT COMPANY as that means (775) that anyone who has an account on that server can access that folder with read, write, execute permissions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 09-03-06, 07:58 PM
Industriality Industriality is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Is there an article or quick reference I can read on how to allow file uploads and create log files in folders that are not 777? I have been following the simple instructions provided by public PHP apps and many of them say to leave folders set to 777 or they won't be writable. What you're saying is new to me and I'd love to avoid cross-server scripting hacks, and I'd love to avoid having to manually CHMOD folders to 777 all the time.

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 09-03-06, 08:02 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 09-03-06, 08:45 PM
Industriality Industriality is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Google is trying only to teach me how to CHMOD. That's not helpful.

I'm wondering if PHP's function CHOWN has anything to do with this. Nothing on the server seems to allow file uploads or even writing a new TXT file with PHP unless the containing folder is 777. Could that be because PHP's user is different than the folder's owner?

I've used many different servers and often come up against things not working unless I CHMOD to 777, but if I can CHOWN a folder to use PHP's user as it's owner, maybe I can leave a folder's permissions to 644 and still be able to write and upload to those folders. Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 09-03-06, 08:56 PM
Industriality Industriality is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Nevermind. Running CHOWN from PHP gives me "Operation not permitted". I'm still left with a non-functional upload script unless I CHMOD the upload folder to 777.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple PHP File Upload / Management Script LiNx88 Script Requests 2 11-02-07 09:07 PM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
File Copying and rename script soundman87 Script Requests 3 05-23-06 12:47 PM
Looking for a file transfer script Bitzy Job Offers & Assistance 2 05-07-06 02:22 PM
looking for simple photo gallery script rookie Script Requests 0 03-01-05 11:38 AM


All times are GMT -5. The time now is 10:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.