I am setting up a website with SQL injection vulnerabilities for testing purposes. However, I want to configure a Blind SQL injection only. I have this PHP code:
<?php
$news_id = $_GET["id"];
$conn = mysqli_connect("localhost","root","","db");
$result = mysqli_query($conn,"SELECT * FROM News WHERE id='" . $_GET["id"] . "'");
$count = mysqli_num_rows($result);
if($count==0) {
$message = "Invalid ID";
header( 'Location: id_not_found.html' );
} else {
$message = "Correct ID!";
$m = mysqli_fetch_assoc($result);
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<title>NEWS</title>
</head>
<body>
<h1>NOTICIA</h1>
<div style="background-color:#c6c6c6;color:black;padding:20px;">
<h2><?php echo $m["title"]; ?></h2>
<p><?php echo $m["body"]; ?></p>
<p><?php echo $m["datetime"]; ?></p>
</div>
</body>
</html>
In my opinion, It seems that this code only has a Blind SQL vulnerability because It only prints the names of the columns of News Table, so If user insert some query, the results won't be printed.
However, when I do this injection:
http://localhost:8080/web_base_de_datos_asig/check_newsid.php?id=-1' UNION SELECT NULL,NULL,user(),NULL-- kyGC
The current user is printed because the query is returned the next array:
Array ( [0] => [id] => [1] => [title] => [2] => root@localhost [body] => root@localhost [3] => [datetime] => )
So, How can I program only a Blind SQL Injection? I really don't know how to do that.
UPDATE I write a dirty solution. However, It works (but I would like to get another solution more interesting). The fact is that, when data is returned, I do another query to the database asking for every parameter. If it exists, the data can be printed because It only contains true information (not, for example, db username).
$result2 = mysqli_query($conn,"SELECT * FROM News WHERE title='" . $m["title"] . "' and body='" . $m["body"] . "' and datetime='" . $m["datetime"] . "'");
$count2 = mysqli_num_rows($result2);