2017-10-04 7 views
0

私は、開発者がのURLのパラメータ化されたクエリを設定してSQLインジェクションの脆弱性を緩和することができますか?例えばURLのパラメータクエリを設定できますか?

https://example.com/somefile.php?id=1 

どのように開発者がそのためのパラメータ化クエリを行うことができますか?彼らはアプリケーションのパラメータでそうしますか?

答えて

0

はい、できます。パラメータ化されたクエリは簡単で、事前にSQLクエリを定義し、クエリ内のユーザー提供の変数にプレースホルダを使用する必要があります。その後、SQL文が定義された後、各パラメータをクエリに渡すことができ、データベースがSQLコマンドとユーザによって入力されたデータとを区別できるようになります。 SQLコマンドが攻撃者によって入力された場合、パラメータ化されたクエリはこれらを信頼できない入力として扱い、注入されたSQLコマンドは決して実行されません。より多くの理解のために、以下の例を見てください。

if (isset($_GET['id'])){ 
    $id = $_GET['id']; 
    /** 
    * Validate data before it enters the database. In this case, we need to check that 
    * the value of the 'id' GET parameter is numeric 
    */ 
    if (is_numeric($id) == true){ 
    try{ 
     $dbh = new PDO('mysql:host=localhost;dbname=sql_injection_example', 'dbuser', 'dbpasswd'); 

     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     /** 
     * Before executing our SQL statement, we need to prepare it by 'binding' parameters. 
     * We will bind our validated user input (in this case, it's the value of $id) to our 
     * SQL statement before sending it to the database server. 
     * 
     * This fixes the SQL injection vulnerability. 
     */ 
     $q = "SELECT username 
      FROM users 
      WHERE id = :id"; 
     // Prepare the SQL query 
     $sth = $dbh->prepare($q); 
     // Bind parameters to statement variables 
     $sth->bindParam(':id', $id); 
     // Execute statement 
     $sth->execute(); 

     $sth->setFetchMode(PDO::FETCH_ASSOC); 
     // Fetch result 
     $result = $sth->fetchColumn(); 
     /** 
     * HTML encode our result using htmlentities() to prevent stored XSS and print the 
     * result to the page 
     */ 
     print(htmlentities($result)); 

     $dbh = null; 
    } 
    catch(PDOException $e){ 
     /** 
     * You can log PDO exceptions to PHP's system logger, using the Operating System's 
     * system logging mechanism 
     * 
     * For more logging options visit http://php.net/manual/en/function.error-log.php 
     */ 
     error_log('PDOException - ' . $e->getMessage(), 0); 
     /** 
     * Stop executing, return an 'Internal Server Error' HTTP status code (500), 
     * and display an error 
     */ 
     http_response_code(500); 
     die('Error establishing connection with database'); 
    } 
    } else{ 
    /** 
    * If the value of the 'id' GET parameter is not numeric, stop executing, return 
    * a 'Bad request' HTTP status code (400), and display an error 
    */ 
    http_response_code(400); 
    die('Error processing bad or malformed request'); 
    } 
} 
?> 
+1

は、基本的に単純な言語を意味します。つまり、idパラメータに変数を指定するだけです。その変数にはクエリがあります。だから、攻撃者がどこかに格納しようとしたが、元のクエリには影響しません。だから私たちのSQLインジェクションはうまくいかないでしょうか?もし私が間違っていたら私を明確にしてください? –

+0

@ジャンクュアワール正確に何が起こっているのですか –

+0

ああ、おまえの@SuvethanNanthaあなたの説明は本当に感謝します。 –

0

この問題はURLではなく、後でURLで渡されるパラメータをどのように使用するかによって異なります。

ベストな方法は、バインド変数を使用することである:

また、「治療」またはエスケープおよび/または不審なデータを除去することによって、あなたのパラメータを「サニタイズ」することができます。これは通常、間違いの影響を受けやすくするためには非常に難しいことです。したがって、バインド変数は使用するだけで簡単で安全です。

+0

@YoYoありがとうございますが、あまりにも混乱しています。しかし、私はそれを理解しようとします。 –

+0

こんにちは、それを確認してください、私はそれについて私が理解していることを示す下のコメントを投稿しました。 –

+0

あなたはそれをよく説明しました。さらに、受け入れられた回答には、バインド変数の使用方法の良い例がありますが、実際には他の投稿と重複しています。最も顕著なもの - https://stackoverflow.com/a/60496/744133 – YoYo

関連する問題