Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 27, 2023 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/312cf100572a23d6b84426f4d6ab9fd1 to your computer and use it in GitHub Desktop.
Save code-boxx/312cf100572a23d6b84426f4d6ab9fd1 to your computer and use it in GitHub Desktop.
SQL Injection & Prevention In PHP MYSQL

SQL INJECTION & PREVENTION IN PHP MYSQL

https://code-boxx.com/php-mysql-injection-prevention/

NOTES

  1. Create a test database and import 1-products.sql.
  2. Change the database settings in 2-db.php to your own.
  3. Access 3a-bad.php in the browser and do an injection search " OR 1=1 OR name LIKE " - This will show all entries.
  4. Access 3b-good.php, the same " OR 1=1 OR name LIKE " search will no longer work.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) PRODUCTS TABLE
CREATE TABLE `products` (
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `products`
ADD PRIMARY KEY (`id`),
ADD KEY `status` (`status`);
ALTER TABLE `products`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
-- (B) DUMMY DATA
INSERT INTO `products` (`id`, `name`, `status`) VALUES
(1, 'Apple', 1),
(2, 'Beet', 1),
(3, 'Carrot', 0),
(4, 'Dill', 1),
(5, 'Eggplant', 1),
(6, 'Feijoa', 0),
(7, 'Grape', 0),
(8, 'Hazelnut', 1),
(9, 'Icaco', 1),
(10, 'Jalapeno', 1);
<?php
// (A) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (B) CONNECT TO DATABASE
$pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (C) RUN SQL SEARCH
$stmt = $pdo->prepare($sql);
$stmt->execute($data);
$results = $stmt->fetchAll();
$stmt = null;
$pdo = null;
// (D) OUTPUT HTML RESULTS
echo "<code class='sql'>$sql</code>";
if (count($results)==0) { echo "<div class='row'>No results</div>"; }
else { foreach($results as $r) {
printf("<div class='row'>%u %s</div>", $r["status"], $r["name"]);
}}
<!DOCTYPE html>
<html>
<head>
<title>PHP MYSQL Injection Demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<!-- (A) SEARCH FORM -->
<form method="post">
<p>* Search for any product normally - Will only extract products with <code>status=1</code>.</p>
<p>* Then try <code>" OR 1=1 OR `name` LIKE "</code> - This will extract everything.</p>
<input type="text" name="search">
<input type="submit" value="Search">
</form>
<!-- (B) SEARCH RESULTS -->
<div id="results"><?php
if (isset($_POST["search"])) {
$sql = "SELECT * FROM `products` WHERE `status`=1 AND `name` LIKE \"%".$_POST["search"]."%\"";
$data = null;
require "2-db.php";
}
?></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>PHP MYSQL Injection Demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<!-- (A) SEARCH FORM -->
<form method="post">
<p><code>" OR 1=1 OR `name` LIKE "</code> no longer works.</p>
<input type="text" name="search">
<input type="submit" value="Search">
</form>
<!-- (B) SEARCH RESULTS -->
<div id="results"><?php
if (isset($_POST["search"])) {
$sql = "SELECT * FROM `products` WHERE `status`=1 AND `name` LIKE ?";
$data = ["%".$_POST["search"]."%"];
require "2-db.php";
}
?></div>
</body>
</html>
<?php
// (A) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_USER", "root");
define("DB_PASSWORD", "");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno) { exit($mysqli->connect_error); }
// (B) PREPARE & BIND
$stmt = $mysqli->prepare("SELECT * FROM `products` WHERE `status`=1 AND `name` LIKE ?");
$search = "%".$_POST["search"]."%";
$stmt->bind_param("s", $search);
// (C) FETCH
$stmt->execute();
$stmt->bind_result($a, $b, $c);
while ($stmt->fetch()) { echo "<div>$a $b $c</div>"; }
mysqli_close($mysqli);
/* NOT IMPORTANT - CSS COSMETICS */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
form, #results {
width: 400px;
padding: 10px;
}
form {
border: 1px solid #efefef;
background: #f2f2f2;
}
code {
font-family: Consolas, monospace;
padding: 1px;
background: #fff255;
}
input {
display: block;
width: 100%;
padding: 10px;
}
input[type=text] {
border: 1px solid #b9b9b9;
}
input[type=submit] {
margin-top: 20px;
border: 0;
color: #fff;
background: #b62323;
cursor: pointer;
}
#results { margin-top: 20px; }
#results .sql {
display: block;
width: 100%;
margin-bottom: 30px;
font-weight: 700;
}
#results .row {
padding: 10px;
background: #eee;
}
#results .row:nth-child(odd) {
background: #f7f7f7;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment