PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Magic Quotes> <Verwendung von Register Globals
Last updated: Fri, 26 Dec 2008

view this page in

Vom Nutzer übermittelte Daten

In vielen PHP Programmen liegt die größte Schwäche nicht an der Sprache selbst, sondern bloß an Code, der nicht mit dem nötigen Augenmerk auf die Sicherheit geschrieben wurde. Deshalb sollten Sie sich immer Zeit nehmen, die Implikationen eines gegebenen Codestücks zu bedenken, um einen möglichen Schaden durch eine eventuell unerwartete übergebene Variable festzustellen.

Beispiel #1 Gefährliche Verwendung von Variablen

<?php
// lösche eine Datei aus dem Benutzer-Verzeichnis...
// oder vielleicht dem eines anderen Benutzers?
unlink ($evil_var);

// Schreibe die Log-Information von deren Zugriff...
// oder vielleicht einen /etc/passwd Eintrag?
fputs ($fp$evil_var);

// Führe etwas triviales aus... oder rm -rf *?
system ($evil_var);
exec ($evil_var);

?>

Sie sollten Ihren Code immer sorgfältig kontrollieren, um eine sorgfältige Prüfung irgendwelcher von einem Web-Browser übertragenen Variablen sicherzustellen, und sich selbst folgende Fragen stellen:

  • Wird sich dieses Skript nur auf die vorgesehenen Dateien auswirken?
  • Kann auf ungewöhnliche oder unerwünschte Daten reagiert werden?
  • Kann dieses Skript auf nicht vorgesehene Art genutzt werden?
  • Kann dies in Verbindung mit anderen Skripten in einer negativen Art benutzt werden?
  • Werden alle Transaktionen ausreichend geloggt?

Wenn Sie sich diese Fragen anstatt danach schon während dem Schreiben des Skriptes stellen, ersparen Sie sich das unangenehme Umschreiben, wenn eine Erhöhung der Sicherheit erforderlich wird. Mit dieser Art zu denken werden Sie die Sicherheit des Systems zwar nicht garantieren, aber sie können helfen, sie zu erhöhen.

Sie könnten auch Überlegungen anstellen, ob Sie nicht register_globals, magic_quotes, oder andere bequeme Einstellungen abschalten, welche Sie in Punkto Gültigkeit, Herkunft, oder Inhalt einer gegebenen Variable durcheinanderbringen könnten. Mit PHP im error_reporting(E_ALL) Modus zu arbeiten kann auch helfen, Sie vor Variablen zu warnen, welche benutzt werden, bevor sie geprüft oder initialisiert wurden (so können Sie verhindern, dass mit ungewöhnlichen Daten gearbeitet wird).



Magic Quotes> <Verwendung von Register Globals
Last updated: Fri, 26 Dec 2008
 
add a note add a note User Contributed Notes
Vom Nutzer übermittelte Daten
SneakyWho_am_i
06-Dec-2008 09:04
@f dot zijlstra:

Actually the shared secret is a very good way to prevent many kinds of cross site request forgery and such. If your site accepts HTTP GET submissions for actions that change things on the server, and doesn't have a shared secret, then I can combine these two vulnerabilities to modify your logged in users's accounts just by showing them images or iframes. By using tinyurl I can completely conceal the attack in source code.

NoScript won't protect your users from this kind of attack.

With the "easysecure" thing, the unique id is stored as a session variable and therefore only sent to the one user. How am I going to obtain it? Why, I'll need a copy of the document that you sent only to your user. This would require some cache trick or a man-in-the-middle attack.
Even if I somehow got the unique ID (which only you and your client know) I'd then need to hijack the client's session to use it.

Basically there's no way that I can create a fake copy of the form, no way that you will accept data that I generate for my victim to send. It is a good way to prevent me from tricking your users into sending arbitrary data and making changes to their accounts.
ffmandu13 at hotmail dot com
24-Aug-2008 08:29
Hi,

Just one little class I made to control user's submitted datas, I thought it could help some people with security.
And if anyone wants to improve it, I'd be glad you do so.

<?php

/**
 * This program is under GNU GPL license.
 *
 * You can contact the author of this program at <ffmandu13@hotmail.com/>.
 */

//Defined regexps (you can add your own ones).
define('REG_DATE'          , '([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})');
define('REG_DIGIT_SIGNED'  , '^[-[:digit:]]+$');
define('REG_DIGIT_UNSIGNED', '^[[:digit:]]+$');
define('REG_PASSWORD'      , '^[[:alnum:]]+$');
define('REG_TEXT'          , '[[:graph:][:blank:]]+');
define('REG_WORD'          , '^[[:alpha:]]+$');

//Controls contents of the $_REQUEST variable.
final class checkVar{

  private
$tmp; //Secured value of a $_REQUEST key.

  //Check if the variable is set.
 
private function isSet(&$field){
    if(!isset(
$_REQUEST[$field]))
      throw new
Exception("You forgot to fill the $field field.");
    else
      return
true;
  }

 
//Set $tmp and remove threatening characters.
 
private function removeCharsThreats(&$field){
   
$this->tmp = trim($_REQUEST[$field]);
   
$this->tmp = htmlspecialchars($_REQUEST[$field], ENT_QUOTES, 'UTF-8', false); 
  }

 
//Checks if the value is equal to 1.
 
public function securityBool($field){
    if(
$this->isSet($field) && $_REQUEST[$field] != 1)
      throw new
Exception("Unallowed value in $field field.");
    else
      return
true;
  }

 
//Checks if the value is in the allowed ones list ($enum).
 
public function securityEnum($field, $enum){
    if(
$this->isSet($field)){
     
$this->removeCharsThreats($field);
     
$tab = explode(',', $enum);
      if(!
in_array($this->tmp, $tab))
        throw new
Exception("Unallowed value in $field field.");
      else
        return (string)
$this->tmp;
    }
  }

 
//Checks if the value is a numeric one and if it is in the given range.
 
public function securityRange($field, $range){
    if(
$this->isSet($field)){
     
$this->removeCharsThreats($field);
     
$tab = explode('/', $range);
      if(!
is_numeric($this->tmp))
        throw new
Exception("Unallowed characters in $field field.");
      elseif(
$this->tmp < $tab[0] || $this->tmp > $tab[1])
        throw new
Exception('Value must be in range '.$tab[0].'/'.$tab[1]." in $field field.");
      else
        return (int)
$this->tmp;
    }
  }

 
/**
   * Checks if the value respects the defined regexp,
   * and if its length is not superior than the given maxlength.
   */
 
public function securityText($field, $maxlength, $regexp){
    if(
$this->isSet($field)){
     
$this->removeCharsThreats($field);
      if(!
mb_ereg($regexp, $this->tmp))
        throw new
Exception("Unallowed characters in $field field.");
      elseif(
mb_strlen($this->tmp, ENCODING) > $maxlength)
        throw new
Exception("Too long string length for $field field.");
      else
        return
$this->tmp;
    }
  }

}

?>

Here are some examples of how to use the public methods.

<?php

$checkVar
= new checkVar();
$args     = array();

//If $_REQUEST['bbexport'] is not equal to 1, throws a new Exception.
$args['bbexport'] = $checkVar->securityBool('bbexport');

//If $_REQUEST['id'] is not an unsigned integer and/or has more than 4 digits, throws a new Exception.
$args['id']       = (int) $checkVar->securityText('id', 4, REG_DIGIT_UNSIGNED);

//If $_REQUEST['orderBy'] is not equal to 'date' or 'id' or 'name', throws a new Exception.
$args['orderBy']  = $checkVar->securityEnum('orderBy', 'date,id,name');

//If $_REQUEST['ratio'] is not a numeric value (integer or float) and is not between 0 and 10, throws a new Exception.
$args['ratio']    = $checkVar->securityRange('ratio', '0/10');

?>
Viz
12-Jun-2008 07:47
@f dot zijlstra

Actually you should not filter against known bad data, you need to white list your filters. Rather than looking for bad data, you should strip everything but what you expect because it's future proof.

example for a 32 character text input field:
$inputvar=substr(htmlentities($_POST['inputvar']),0,32);
$inputvar=preg_replace('/[^\w\.\-\& ]/', '', $inputvar);

This will strip numbers, newlines, null characters, # and all non-printable characters, only allowing word characters(determined by locale setting), .,-, space and &, as well as truncate the value to the size you accept. You could use preg_match instead and throw an error if there's anything outside the pattern.

This can be a little painful because you might miss a character you need, and have to add it to your code, but it's easier than restoring your database, or explaining to your customers about why their data is now in the hands of identity thieves.

Default deny for the long term win. It's the only security panacea that works now and always will. Blacklists are the completely wrong approach because they don't handle future threats you don't know about yet.

Further you should use mysqli and prepared statements for queries which use user data as parameters, after using the mysql filters on the input data. If you really want to go the extra mile, start using stored procedures because it will allow you to remove SELECT, DELETE, UPDATE and INSERT permissions from the web server user. All you need is EXECUTE for stored procs, thereby limiting what can be seen of the database to only the functions you expose(and you can easily expose too much if you aren't careful).

Naturally the big dangers are SQL injection and XSS. A simple filter like this will break them when combined with a prepared statement.

As someone else noted, relying on _anything_ from the client is an extremely bad idea because it can all be spoofed (including HTTP_REFERER) using Paros, Tamper Data or any other client side proxy, by doing a simple man in the middle tamper on yourself.

You can make HTTP_REFERER=http://www.whitehouse.gov if you want.

Sorry to segway into db security, but you can't avoid it when talking about sanitizing input which leads to building queries.

-Viz
f dot zijlstra at gmail dot com
18-May-2008 03:07
I'd like to note that the 'easysecure' thing posted below is NOT a secure way to validate that the form was indeed submitted from a browser. In fact, there is NO way you can guarantee that.

A smart person with bad intentions can easily parse the HTML page to fetch the generated token and pass it on. The only way to secure your forms is to explicitly check every variable against potentially dangerous values.
Livingstone@stonyhills[dot]com
02-Feb-2008 01:51
making sure your form is submitted from your page! Could also be adapted to url, by additing &token to the query string and checking this against session data(or what ever array you like) with $_GET, not that this string is randomly generated and stored. If you like you could build your own array to store the generated string if you dont want to use $_SESSION, say you could make yours like $tokens = array(), and in your easysecure class you store all the stuff in that array!

<?php

class easysecure {
   
    var
$curr_user;
    var
$curr_permission;
    var
$curr_task;
    var
$validpermission;
    var
$error;
   
   
    function &
setVar( $name, $value=null ) {
        if (!
is_null( $value )) {
           
$this->$name = $value;
        }
        return
$this->$name;
    }

    function
maketoken($formname, $id){
       
       
$token = md5(uniqid(rand(), true));
       
       
$_SESSION[$formname.$id] = $token;
       
        return
$token;
    }
   
    function
checktoken($token, $formname, $id){
       
//print_r($_SESSION);
        //echo ($token);
        //if we dont have a valid token, return invalid;
       
if(!$token){
           
$this->setVar('validpermission', 0);
           
$this->setVar('error', 'no token found, security bridgedetected');
            return
false;
        }
       
       
//if we have a valid token check that is is valid
       
$key = $_SESSION[$formname.$id];
        if(
$key !== $token ){
           
$this->setVar('validpermission', 0);
           
$this->setVar('error', 'invalid token');
            return
false;
        }
       
        if(
$this->validpermission !==1){
              echo
'invalid Permissions to run this script';
              return
false;   
        }else{
            return
true;
        }
    }
   
}

?>

<?php $userid = *** //make it what ever id you like ?>
<form name="newform" action="index.php" method="post">
<input type="text" name="potentialeveilfield" value="" size 30 />
<input type="hidden" name="token" value="<?php echo maketoken(newform, $userid); //$userid here could be user profile id ?>" />
<input type="submit" />
</form>

Now when processing the form... check the value of your token

<?php

//well you know the form name
if(!checktoken($_POST['token'], 'newform', $userid))
{
//failed
exit(); //or what ever termination and notification method best suits you.
//you could also design the class your way to get more accurate fail (error messages from the var)
}

//you can now continue with input data clean up (validation)

?>
Uli Kusterer
13-Sep-2005 12:50
One thing I would repeat in the docs here is what information actually comes from the user. Many people think a Cookie, since it's written by PHP, was safe. But the fact is that it's stored on the user's computer, transferred by the user's browser, and thus very easy to manipulate.

So, it'd be handy to mention here again that:

CGI parameters in the URL, HTTP POST data and cookie variables are considered "user data" and thus need to be validated. Session data and SQL database contents only need to be validated if they came from untrustworthy sources (like the ones just mentioned).

Not new, but I would have expected this info under this headline, at least as a short recap plus linlk to the actual docs.

Magic Quotes> <Verwendung von Register Globals
Last updated: Fri, 26 Dec 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites