Skip to content

Instantly share code, notes, and snippets.

@nijjwal
Last active May 25, 2020 02:20
Show Gist options
  • Save nijjwal/9d6ad053d8b8411a85f2 to your computer and use it in GitHub Desktop.
Save nijjwal/9d6ad053d8b8411a85f2 to your computer and use it in GitHub Desktop.
PHP - SQL Injection - 1
<?php
require 'pdo.php';
//1. Create an instance of connection
$pdo_obj = new Connection();
//2. Connect to the server + db
try
{
$connection = $pdo_obj->connect();
}catch(Exception $e){
echo $e->getMessage();
}
//3. Imagine that you are getting input from user
//We will use this string for injecting vulnerable code
$id = "8' OR '1' = '1";
//4. Prepare sql query
$sql = "SELECT * FROM USER WHERE id = '$id' ";
//5. Prepare, execute, and display
$stmt = $connection->prepare($sql);
$stmt->execute();
$arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table border='1px'>\n";
echo "<tr>\n";
foreach ($arrValues[0] as $key => $useless){
echo "<td>$key</td>";
}
echo "</tr>\n";
//display data
foreach($arrValues as $rows)
{
echo "<tr>";
foreach($rows as $key=>$value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment