0

I am looking to find a way I can secure admin area, especially the folder itself from outside access (These include folders with images and css). I have read a lot of suggestions but they all feel rather a compromise or work around than a bullet proof method or I am not understanding which is best for security and hidden from outside world, I want to be the only one that knows about it or access it. Hoping someone can shed some light what they would use, when they want the area completely hidden from outside world, whilst still accessible to you.

Some of the methods I have come across involve:

  1. Moving folder outside of root
  2. Using Htaccess Deny all. (also means I can't login unless I apply a static IP address which I do not have)
  3. Another way I thought of could be to use session variable to store admin, recognize and grant access based on session ID. (This does mean all other css files and image folders are viewable).
  4. Adding an index page in the folder which I see alot of sites do.

I currently have my login script to redirect me to my admin area, so is there anyway for the whole folder to recognize it's me and grant access and serve files on if a logged in admin php file is requesting it?, if not to decline access including images and css etc?

Can't figure out how best to protect this area? Is using session a secure way of identifying an admin?

David Smith
  • 117
  • 3
  • 10
  • CSS and JS files don't have anything confidential in them (if they do, you have a big security issue here, security by obscurity is bad). –  Jul 07 '14 at 18:58
  • Let it run in a separate virtual host which is only accessible via localhost and forward the port via SSH. – Gumbo Jul 07 '14 at 19:03
  • Of course they won't have any confidential data I agree. But even so, I do not want people finding the css, js, be able to view in source code, the folder it is attached and start understanding some of the folder structure. Of course as a last resort I can just heavily restrict access. But ideally i'd like it, as if though it doesn't even exist. Is this something that can't be done for impossible? – David Smith Jul 07 '14 at 19:04
  • What is the specific question you are having? What are you trying to prevent in specific? What is wring with options 1 - 4? – PeeHaa Jul 07 '14 at 19:21
  • 1. Possibly no issues but I'd like to know it achieves complete secrecy and causes no issues with hosting, as I have read when they update their panels etc, the folder outside of root gets deleted in the update process. 4. This method means that users can identify the folder, possibly gain access to folder structure through view source. Personally I'd like to keep these out of my area completely. – David Smith Jul 07 '14 at 20:11

3 Answers3

3

The easiest way to ensure content is not exposed to the web is to place it above the site folder in your directory structure.

so for example in your Apache configuration mount the site at

/var/www/sites/site/content/

and place the restricted content at

/var/www/sites/site/

that way the content will not be exposed but php can still read it if required.

Obviously this will not stop users from seeing what is in your css files if php reads them and echoes them out but I dont see why a css file should need to be secure

Edit

Supposing you have a folder on your server at /var/www/sites/site/content/some_folder

and you enter www.yoursite.com/some_folder into a browser, assuming you have indexes open in your site you will see a list of files in some_folder

But how can you get to /var/www/sites/site/ from a web brower ? ... you can't!!

but what you can do is some thing like this:

And this would be a php file inside the main site folder (visible to public)

<?php

session_start();
if(isset($_SESSION['admin_logged_in'])){
   include '/var/www/sites/site/secret_content.php';
}
andrew
  • 8,614
  • 7
  • 25
  • 56
  • I suppose it's just me being very insecure, but from days when I was learning WP, I used source codes alot to figure out what files were being served from where. Given the admin folder will have no direct access, it will be accessed from root>folder, I was hoping that maybe the folder can be completely locked out. If a php file has granted it access, only files will be served to me. Being logged out, you will not see anything what so ever – David Smith Jul 07 '14 at 19:11
  • in the example i posted above there is no way you can navigate to `www.yoursite.com/whatever` and gain access to `/var/www/sites/site` because the site is mounted at `/var/www/sites/site/content/` the secure files will need to be served with `file_get_contents` or similar in php. Which can be administered as you choose – andrew Jul 07 '14 at 19:15
  • I am thinking this might be the possible root. So realistically if login script redirects me to this folder, then nobody except for me should be able to access or view it right? even css and js files? Maybe I'm being too paranoid but does this cause an issue with the way some host providers set up their folder access? – David Smith Jul 07 '14 at 19:20
1

The first step would indeed be to move all files you want to prevent public access to to outside the document root. This way there is no way to access the files directly through your webserver.

If you are looking to prevent access for all resources (including images, scripts, stylesheets etc) you could implement a "proxy" which is responsible for serving the files (after checking whether the user is authorized).

The easiest and most flexible way to do this is to have a single entry point in the application. Using apache this can easily be achieved using the following rewrite rule:

RewriteEngine On
RewriteRule ^(.*)$ index.php [L,QSA]

This will make sure every request will go through your index.php file.

No you can easiy check whether you are allowed to access the resources using e.g.:

<?php

session_start();

if (!isset($_SESSION['user'])) {
    header('HTTP/1.0 403 Forbidden');
    exit; // important to prevent further execution of the script
}

// user is allowed access, do shit

The above is a very simplified example. Normally you may want to render an actual nice looking page telling the user he is not allowed to access you stuff / rendering a login page.

Now to render the protected resources you could do something like:

Directory structure

  • Project
    • public (docroot)
    • index.php
    • index.php
    • other protected files

index.php in docroot

<?php

require_once __DIR__ . '/../index.php';

index.php in project

<?php

session_start();

if (!isset($_SESSION['user'])) {
    header('HTTP/1.0 403 Forbidden');
    exit; // important to prevent further execution of the script
}

$file = $_SERVER['REQUEST_URI']; // important to sanitize or possible check against whitelist the requested resource

$ext = pathinfo($path, PATHINFO_EXTENSION);

switch ($ext) {
    case 'jpg':
    case 'jpeg':
        header('Content-type: image/jpeg');
        imagejpeg('/path/to/protected/resources/' . $file);
        break;
}

Now you will lhave total control over what you serve and to whom.

Note that whether it is secure depends entirely on what your implementation looks like, but in general:

  • Always place your non public files outside of the document root
  • Always sanitize / whitelist user input
  • Always secure your data

Some generic, but related reads:

Community
  • 1
  • 1
PeeHaa
  • 66,697
  • 53
  • 182
  • 254
  • Ok so let me ask you a question. So say this is the structure login page is held in root. This page processes login users and redirects them according to their access. So for admin it would be root>outside root, for staff it would be root>staff folder. Given that the login is processed on root and redirected to outside root folder. Any images, css or files should only be visible to me right? Because even as admin I am not accessing the folder directly, the login script is redirecting me there? If that makes sense. Would that work to hide the admin area? – David Smith Jul 07 '14 at 20:18
  • The point of the above example is that the *every* request goes through the `index.php` file so you have total control what you serve to which users. – PeeHaa Jul 07 '14 at 20:19
  • Just thought of another problem, are files outside of root able to access and make changes to database or use config files, classes or anything else that exists in the root folder? – David Smith Jul 07 '14 at 20:24
  • Yes the are able to do that. Normal rules apply. They just aren;t publicly accessible. – PeeHaa Jul 07 '14 at 20:25
  • Do I need to saitize and whitelist if I am the only one accessing that folder? Also I tried looking for a tutorial about the usage of syntax to redirect to another folder outside htdocs, couldn't find any. Can you only include files from outside root or can you be redirected there to a index.php in the folder? What syntax would I use to be redirected to the folder outside root? – David Smith Jul 07 '14 at 21:22
0

Yes, you should move the content out of the document root. You could try using .htaccess to protect your files, but allowing overrides by .htaccess can itself be a security problem. It's certainly a performance problem.

Simply point your 404 handler at something like....

<?php
define('REQUEST_PATH', '/secure');
define('SECURED_CONTENT', '/var/www/restricted');

$req=parse_url($_SERVER["REQUEST_URI"]);

if ((0===strpos($req['path'],REQUEST_PATH)) 
    && $_SESSION['authenticated']) {
       if (is_readable(SECURED_CONTENT . $req['path']) 
           && is_file(SECURED_CONTENT . $req['path'])) {
              header('Content-type: ' 
                 . mime_content_type(SECURED_CONTENT . $req['path']);
              include(SECURED_CONTENT . $req_path);
       } else {
             header('HTTP/1.0 404 Not Found');
       }
       exit;
} 
header('HTTP/1.0 403 Forbidden');
symcbean
  • 45,607
  • 5
  • 49
  • 83
  • "I am looking to find a way I can secure admin area" - if you can't trust your administrators then YOU HAVE NO SECURITY – symcbean Jul 08 '14 at 09:28
  • Yeah because website administrators are always the same people as the sysadmins... The above is just vulnerable and is a potential hazard for future readers imho. Also, pro tip: security is all about distrust. – PeeHaa Jul 08 '14 at 10:00