LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 09-11-2010, 01:34 PM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
Preventing Users from Downloading Files from the Sever By Typing the URL


Hi,


Let's consider the following URL typed in a web browser:

Code:
http://192.168.56.10/2010.cal
Output:

The web browser displays the contents of the file requested. It is a simple text file generated by issuing the command:


Code:
cal 2010 2010.cal
Well, how the file is generated or what it contains is not important at this point.

The important question is how to prevent the file from being downloaded and its contents from being displayed in the browser window?


Let's take this example:

Code:
http://192.168.56.10/scanFS.sh
The above file exists on the sever and is a Shell Script file:

Code:
[root@devarishi html]# ls -ld scanFS.sh
-rwxr-xr-x 1 root root 261 Sep 11 23:52 scanFS.sh
[root@devarishi html]#
Since it is not recognized by the web browser so it is downloaded on the system. That way, what the script does is exposed to the outside world.

Okay, I usually keep such scripts in ../cgi-bin/. But for files (text files, in the example) which are being uploaded by a user should not be downloaded by another user.

Any ideas?
 
Old 09-11-2010, 03:57 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

You have some options on this:
1. You can create an index.html page to hide the contents of the webserver directory.

2. Use FilesMatch:
Code:
<FilesMatch "\.(cal|sh|txt)>
Deny from all
</FilesMatch>
3. Use mod_rewrite:
Code:
RewriteEngine on

RewriteRule \.[cal|sh|txt] - [F]
Both 2 and 3 can be placed in a .htaccess file and of course you can add other file extensions if you want

Regards
 
1 members found this post helpful.
Old 09-19-2010, 09:36 AM   #3
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by bathory View Post
Hi,

You have some options on this:
1. You can create an index.html page to hide the contents of the webserver directory.

2. Use FilesMatch:
Code:
<FilesMatch "\.(cal|sh|txt)>
Deny from all
</FilesMatch>
3. Use mod_rewrite:
Code:
RewriteEngine on

RewriteRule \.[cal|sh|txt] - [F]
Both 2 and 3 can be placed in a .htaccess file and of course you can add other file extensions if you want

Regards



I will try the 2nd and the 3rd options. I think they are just what I need to implement. But I didn't get the 1st option:

Quote:
1. You can create an index.html page to hide the contents of the webserver directory.
 
Old 09-19-2010, 09:53 AM   #4
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by bathory View Post
Code:
<FilesMatch "\.(cal|sh|txt)>
Deny from all
</FilesMatch>
Code:
RewriteEngine on

RewriteRule \.[cal|sh|txt] - [F]

I tried both of them one by one and then both of them together:
Code:
-bash-2.05b# pwd
/var/www/html
-bash-2.05b# cat .htaccess
<FilesMatch "\.(cal|sh|txt)>
Deny from all
</FilesMatch>

RewriteEngine on

RewriteRule \.[cal|sh|txt] - [F]
-bash-2.05b#
I restarted the apache / httpd service:

Code:
service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
-bash-2.05b#
Code:
and then accessed the file:

http://host-6-12.linuxzoo.net/year.cal


 September 2010
Su Mo Tu We Th Fr Sa 
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

and the contents god displayed.
 
Old 09-19-2010, 10:01 AM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

I guess the .htaccess is not read by the web server.
You must make sure your have
Code:
AllowOverride All
in httpd.conf, in order for apache to read .htaccess files.

Quote:
But I didn't get the 1st option:

Quote:
1. You can create an index.html page to hide the contents of the webserver directory.
I mean that if you have an index.html in the directory containing the files you want to protect, then the directory contents will not be listed and someone has to know the exact filename of the file to be able to download it.
 
Old 09-19-2010, 11:44 AM   #6
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
[QUOTE=bathory;4102515]
I guess the .htaccess is not read by the web server.
You must make sure your have
Code:
AllowOverride All
in httpd.conf, in order for apache to read .htaccess files.


Code:
# AllowOverride controls what directives may be placed in .htaccess files.
    AllowOverride All
Code:
service httpd restart
Code:
http://host-6-10.linuxzoo.net/year.cal

Forbidden
You don't have permission to access /year.cal on this server.


--------------------------------------------------------------------------------

Apache/2.0.51 (Fedora) Server at host-6-10.linuxzoo.net Port 80

Here is your fees...
 
Old 09-19-2010, 11:47 AM   #7
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by bathory View Post
.htaccess
Do we have some other way to achieve the same result - a better one?


Just found some stuffs on .htaccess thing:


http://httpd.apache.org/docs/1.3/howto/htaccess.html


Quote:
When (not) to use .htaccess files

In general, you should never use .htaccess files unless you don't have access to the main server configuration file. There is, for example, a prevailing misconception that user authentication should always be done in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things.

.htaccess files should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes in .htaccess files for themselves. This is particularly true, for example, in cases where ISPs are hosting multiple user sites on a single machine, and want their users to be able to alter their configuration.

However, in general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a <Directory> section in your main server configuration file.

There are two main reasons to avoid the use of .htaccess files.

The first of these is performance. When AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.

Further note that Apache must look for .htaccess files in all higher-level directories, in order to have a full complement of directives that it must apply. (See section on how directives are applied.) Thus, if a file is requested out of a directory /www/htdocs/example, Apache must look for the following files:

/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess

And so, for each file access out of that directory, there are 4 additional file-system accesses, even if none of those files are present. (Note that this would only be the case if .htaccess files were enabled for /, which is not usually the case.)

The second consideration is one of security. You are permitting users to modify server configuration, which may result in changes over which you have no control. Carefully consider whether you want to give your users this privilege.

Note that it is completely equivalent to put a .htaccess file in a directory /www/htdocs/example containing a directive, and to put that same directive in a Directory section <Directory /www/htdocs/example> in your main server configuration:

.htaccess file in /www/htdocs/example:

AddType text/example .exm

httpd.conf

<Directory /www/htdocs/example>
AddType text/example .exm
</Directory>

However, putting this configuration in your server configuration file will result in less of a performance hit, as the configuration is loaded once when Apache starts, rather than every time a file is requested.

The use of .htaccess files can be disabled completely by setting the AllowOverride directive to "none"

AllowOverride None
 
Old 09-19-2010, 12:37 PM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
If you don't want to use .htaccess, you can put the same FilesMatch or mod_rewrite directives in httpd.conf.
The good thing with .htaccess is that you don't have to restart apache every time you want to change something.

If you think your question is answered, you can mark the thread "Solved", using the "Thread Tools" on top of the page.

Regards
 
1 members found this post helpful.
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
The requested URL/sever-info not found on this server narsimha.v Linux - Newbie 1 01-17-2008 08:54 AM
Apache RewriteEngine issue when typing URL SupremeUK Linux - Server 1 10-26-2007 05:11 AM
Howto forbid anonymous users from downloading files from hot links greeting Programming 13 09-26-2006 12:44 AM
Downloading a file from Url in perl bratieres Programming 2 03-01-2006 12:49 AM
PHP Code to check how many users are downloading files. farmerjoe Programming 8 04-01-2005 03:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration