Prevent direct URL Access of PHP files in the Web Application - php

All of the php files in the application are directly accessible through URL.
Adding this code at the start of my php files works for few of them which are being requested with POST method:
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
die(header( 'location:/webapp/postings' ));
}
But, I do have some php files which are being requested through GET method and the above code doesn't work for them, because of which I came with the following code:
if(!isset($_SERVER['HTTP_REFERER'])){
die(header('location:/webapp/postings'));
}
I know that the HTTP_REFERER coudn't be trusted. Any other options?
can someone please tell me a generic way of preventing direct URL access without altering the code across all the php files.
Note: My Application is running on IIS 7.5 Web server.

Don't do this:
public_html/
includes/
dont_access_me_bro.php
...
index.php
...
Do this instead:
includes/
dont_access_me_bro.php
...
public_html/
index.php
...
Explanation
Keeping your source files outside of the document root guarantees that users will be unable to access them directly by changing the URI on their HTTP request. This will not protect against LFI exploits.
To find out where your document root is, this handy PHP script can help:
var_dump($_SERVER['DOCUMENT_ROOT']);
If this prints out string (25) "C:\htdocs\www\example.com", you don't want to store your files in C:\htdocs\www\example.com or any subdirectory of C:\htdocs\www\example.com.
If you place user-provided files inside your document root, you're creating the risk that someone will access them directly from their browser, and if Apache/nginx/etc. screws something up, their uploaded file may be executed as code.
So you would not want your files to be inside C:\htdocs\www\example.com\uploaded, you would want something like C:\uploads\example.com\.
This is covered in-depth in this article on secure file uploads in PHP.

Related

How can I make a php page inaccessible to all users?

I'm developing an application where I have a configuration page that has some data that must be hidden from anyone who tries to access them directly, I'm currently doing a verification, but I don't know if it really is safe, I'm using this:
if ($_SERVER['REQUEST_METHOD'] == 'GET' && realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
die();
}
In other words, any user who tries to access the page remotely dies, is this safe or is there a better way to do this?
It looks like you're using an include file for PHP. There are three ways you can make it inaccessible via GET request.
Use .inc extension and make the server not serve .inc files and throw Error 404.
Put the file in a non-accessible location, out of www and use the include path, which can include file from any path.
Use .htaccess to limit the file access, i.e., see the below one:
Contents of .htaccess to limit config files.
RewriteRule ^config/.*\.(php|rb|py)$ - [F,L,NC]

Host, PHP, htaccess

I need some help.
I was reading the security recommendations of my hosting service and they say that ideally just put the
index file and files like css, js and img inside my root folder, and that all other files should be placed
off, that is, a level above.
I tried doing this in my tests, and I had some problems. The structure of the hosting folders is:
/
/htdocs
Inside /htdocs I put the index.php file and when accessing it through the url exemple.com/index.php works normally.
But putting other test files out of htdocs is what starts the problem. For example, if I have a file called contact.php
and I try to access it through the url exemple.com/contact.php I get the 404 error message.
So the question I have to ask is:
Is it possible to access url files that are outside of htdocs, or better to put all the files that will be accessed by the url inside
of htdocs and leave only configuration files outside this folder, like class, functions, database connection, etc?
And if it is possible to access the files by url, how would I rewrite these urls in htaccess?
and that all other files should be placed off
Yes, this is good practice. However, you're misunderstanding the implementation.
You can not directly access files outside the document root. But you can indirectly access them. I.e., the web server can't see them, but your programming code can.
Ideally, your site would use the front controller pattern. Here, your index.php file would serve every page of your app by intercepting every request and then routing it to the correct end point. I.e., you would never directly request /contact.php, you'd instead request /contact, which would get funneled to /index.php, which would load the required resources from outside the doc root.

PHP - Send HTTP Request to local script

I'm trying to use one PHP script on my server that calls other scripts.
The main script, called call.php, is in the public_html folder, so I can send an HTTP request to it using my_website.com/call.php?action=some_script_name&arg1=value&arg2=some_other_value.
I already have a method to form the new request (and execute it), if the action script is in public_html. For example, if some_script.php was located at /public_html/scripts/some_script.php, my HTTP request would be my_website.com/scripts/some_script.php?arg1=value&arg2=some_other_value.
I have that done already, and it works correctly. However, I want to send requests to scripts that are NOT in public_html (or any subdirectory of that). For example, if I have a script under /lib/otherscript.php, I want to call that as well.
I tried a request such as ../lib/otherscript.php?args_here, but that did not work.
Is this possible, and if so, how can I accomplish this?
Edit:
The actual file structure of the (shared) server looks like this (for this example):
/
public_html/
call.php
scripts/
some_script.php
lib/
otherscript.php
You can't access something outside public_html via HTTP; that's the whole point of the public_html directory. You have a few options:
Create a wrapper that is publicly accessible to call the functionality in public_html. The best way to do this is either a class or a function that takes as parameters the arguments from your URL.
Use the command line interpreter.
Either way, you may need to do some user authentication if the functionality is sensitive. If it's harmless, you can put it in public_html. If it's not, you need authentication/authorization checks.
Edited because I misread your question originally.
If you are using cPanel you can access php files outside public_html folder using absolute url, e.g.: <?php require_once('/home/username/lib/otherscript.php'); ?> now you can post parameters to any script within public_html folder at witch you have included otherscript.php.
Note: You have to use your cPanel user name in absolute address.

Protect php files from direct access

This is what the map of my website looks like:
root:
-index.php
-\actions\ (various php files inside and a .htaccess)
- \includes\ (various php files inside and a .htaccess)
- .htaccess
I know that if I use "deny from all" in actions and includes directories, the files in them will be secured from direct access.
Now, my actions folder has many php files that are called by index.php (forms).
I have "deny from all" in .htaccess inside \actions, but then I have the forbiden access to that files.
So, how can I protect the files from direct url access but have the exception that can be called from index.php?
The easiest way is to place a constant in the index.php and check in the other php files if this constant exists. If not, let the script die.
index.php
define('APP', true);
various.php
if(!defined('APP')) die();
If you want to block using .htaccess then most likely the best way of adding the exception is to add it to the same .htaccess file. If you want to prevent PHP script from working (but not from being "visible"), then simply look for certain condition (like session variable, constant) at the begining of your scripts. Unless these scripts are invoked "the right way", these requirement will not be ment, so it'd be safe to just die() then

using htaccess to hide database passwords

I have a php class that connects to a database which has the password to the database hard coded into it. I do NOT have have access to folders outside the webroot. Reading this forum and others it seemed that creating a htaccess file with
order allow,deny
deny from all
in the directory with my php classes would do the trick. however after doing some quick testing it seems this also blocks the public files which need access to the database to generate the site. to be clear this is the structure i want:
index.php (public file which calls on php classes that access the database)
php_classes/DatabaseConnect.php (contains the password to the database. i want to hide this from everything that is not uploaded onto mysite --- or better yet only to specific files i name)
...
thanks,
brook
Do not place your PHP code in the webroot. Frameworks will typically use this technique where they only put a bootstrap file in the webroot...you can do that same and place your PHP file with sensitve information above your web root so it cannot be browsed.
Your bootstrap file would #require_once '../safe_dir_above_webroot'.
If you're worried about others seeing the login details to your database, rest assure that it cannot be seen if inserted between PHP tags.
.htaccess is a little tricky with some servers. It seems quite a few setups hate overruling which I can understand.
Since you have suggested that you cannot access folders outside of the root directory, you may just want to do something like this.
define("include_allowed", true);
Call that in the leading file, for instance index.php. When a file is included it should check to see if include_allowed has been set true.
if (include_allowed != true) header('HTTP/1.1 404 Not Found');
This checks to see if it has been included by index.php or which ever file that has defined include_allowed true.
If it fails to return true, a 404 error is sent saying not found to trick users! :)
Since your file is PHP , it will processed by the PHP exe, before being rendered to the client. So the password should not be visible. Having said that to use htaccess to stop view a particular file you can do this
<Files php_classes/DatabaseConnect.php>
Deny From All
</Files>

Categories