Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Php e Sql Injections

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    30

    Php e Sql Injections

    Salve a tutti,
    Una domanda che potrebbe sembrare un pò cretina:
    Volendo evitare che in un $_GET possa essere passato stringhe che siano poi dannose non è possible fare la cosa seguente?:
    Codice PHP:
    define(SECRET_KEY,"una qualunques stringa con numeri e()6778a888a890");
    function 
    create_hash($array)
             {
             
    $data '';

             
    /* Construct the string with our key/value pairs */
             
    foreach ($array as $key => $value) {
                      
    $data .= $key $value;
                      }
             
    $h = new Crypt_HMAC(SECRET_KEY'md5');
             
    $hash $h->hash($data);
             return 
    $hash;
             }
    function 
    BuiltLink($uri,$params)
             {
             return (
    $uri.'?'.$params);

             }
    function 
    create_parameters($array)
             {
             
    $data '';
             
    $ret = array();
             
    /* Construct the string with our key/value pairs */
             
    foreach ($array as $key => $value) {
                      
    $data .= $key $value;
                      
    $ret[] = "$key=$value";
                      }
             
    $h = new Crypt_HMAC(SECRET_KEY'md5');
             
    $hash $h->hash($data);
             
    $ret[] = "hash=$hash";
             return 
    join ("&"$ret);
             }
    function 
    verify_parameters($array)
             {
             
    $data '';
             
    $ret = array();
             
    /* Store the hash in a separate variable and unset the hash from
             * the array itself (as it was not used in constructing the hash
             */

             
    $hash $array['hash'];
             unset (
    $array['hash']);
             
    /* Construct the string with our key/value pairs */
             
    foreach ($array as $key => $value) {
                      
    $data .= $key $value;
                      
    $ret[] = "$key=$value";
                      }
             
    $h = new Crypt_HMAC(SECRET_KEY'md5');
             if (
    $hash != $h->hash($data)) {
                 return 
    FALSE;
                 } else {
                 return 
    TRUE;
                 }
             } 
    In ogni script
    il primo comando è:
    Codice PHP:
    if (tep_is_null($_GET) {
    ........
    } else
    if (!
    verify_parameters($_GET)) { die(PARAMETRI_NOT_OK);} 
    se nelle varie pagine dove faccio i link costruisco il link nel modo seguente:
    Codice PHP:
    $k = array ("page" => "nome",
                       
    "application" => $_SESSION["application"],
                       
    "action" => "begin",
                       
    "id" => Get_session_id());
    $link=BuiltLink (INDEX,create_parameters($k)); 
    Evito o no qualsiasi possibilità di trasformare i miei get con attraverso il Cross-site scripting ?
    Grazie per l'aiuto
    Paolo

  2. #2
    Tralasciando l'overhead introdotto dal sistema, non appena deve essere l'utente a dover inserire dei dati (es. form di ricerca) il tutto diventa inutile.

  3. #3

    Re: Php e Sql Injections

    Volendo evitare che in un $_GET possa essere passato stringhe che siano poi dannose
    Se la variabile deve essere un intero, basta:

    codice:
    $variabile = intval($_GET['variabile']);
    E, usando sempre $variabile, sei a posto per tutto lo script.

    Se deve essere una stringa, e usi MySQL:

    codice:
    $variabile = mysql_real_escape_string($_GET['variabile']);
    Non vedo il bisogno di complicarti la vita in quel modo... pulisci tutte le variabili che possono essere direttamente modificate da un utente (get, form, e via dicendo) e sei a posto.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.