Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[TUT]Make your own SQLI Lab and and practice [TUT]

Hi friends i am CodeNinja a.k.a. Aakash Choudhary. Today i will help you to make your own sqli lab where you can practice sql injection in your localhost.

Requirement :-

1. XAMPP/LAMPP
2. Basic knowledge of PHP & MySQL

NOTE: Save all this files in you localhost folder.

Step 1:- Here is my code of sql --->
PHP Code

<?php 
-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- 
Host: localhost
-- Generation Time: Jun 12, 2015 at 05:22 PM
-- Server version: 5.6.21
-- PHP Version: 5.6.3

SET SQL_MODE 
= "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- 
Database: `webtest`
--

-- ------

--
-- 
Table structure for table `students`
--

CREATE TABLE IF NOT EXISTS `students` (
`
id` int(6) NOT NULL,
  `
firstname` varchar(30) NOT NULL,
  `
lastname` varchar(30) NOT NULL,
  `
email` varchar(50) DEFAULT NULL,
  `
password` varchar(20) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

--
-- 
Dumping data for table `students`
--

INSERT INTO `students` (`id`, `firstname`, `lastname`, `email`, `password`) VALUES
(1, 'Mak', 'Man', '[email protected]', 'pass1'),
(
2, 'Code', 'Ninja', '[email protected]', 'pass2'),
(
3, 'Aakash', 'Choudhary', '[email protected]', 'pass3'),
(
4, 'Anil', 'Kid', '[email protected]', 'pass4'),
(
5, 'Saqu', 'Brother', '[email protected]', 'pass5'),
(
6, 'Zen', 'Javanicus', '[email protected]', 'pass6');

--
-- 
Indexes for dumped tables
--

--
-- 
Indexes for table `students`
--
ALTER TABLE `students`
 
ADD PRIMARY KEY (`id`);

--
-- 
AUTO_INCREMENT for dumped tables
--

--
-- 
AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `id` int(6) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 

Save this file as "webtest.sql"

If you want to create database,tables,columns and insert data yourself with code then here is some codes but always remind that it is only for to increase your knowledge in MySQL 👍
------
Some commands for Basic of mysql with example :-

1. Create Database :-
Code
CREATE DATABASE webtest;
This will create database.

2. Create Tables :-

Code
CREATE TABLE `students` (
`id` int(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`firstname` varchar(30) NOT NULL,
`lastname` varchar(30) NOT NULL,
  `email` varchar(50) DEFAULT NULL,
  `password` varchar(20) NOT NULL
)

Here i create "student" table and 5 columns -----> "id","firstname","lastname","email","password"

& int(6) mean id is integer value with 6 length
& varchar(30) mean we take name with 30 length
& AUTO_INCREMENT mean it will increment id value automatic like 1,2,3 etc etc
& PRIMARY KEY mean id is our primary key (main key)

3.Insert our data in those columns :-

Code
INSERT INTO `students` (`id`, `firstname`, `lastname`, `email`, `password`) VALUES
(1, 'Mak', 'Man', '[email protected]', 'pass1'),
(2, 'Code', 'Ninja', '[email protected]', 'pass2'),
(3, 'Aakash', 'Choudhary', '[email protected]', 'pass3'),
(4, 'Anil', 'Kid', '[email protected]', 'pass4'),
(5, 'Saqu', 'Brother', '[email protected]', 'pass5'),
(6, 'Zen', 'Javanicus', '[email protected]', 'pass6');
So we insert our data in student table with those values.

So this is all which we can do manually but i have given you my sql file to save your time. 😝
------
Step 2 :- Make a php file and save following codes in that

PHP Code

<?php 
include("connection.php); // This is  you connection file
if(isset(
$_GET["id"])) { 
                    
$id = $_GET["id"];
                    
$query = $conn->query("SELECT * FROM students WHERE id=$id"); // Here student is our database name
                      if(!
$query) {  // If this query is not running then you will see error
                      echo "
Error: " . mysqli_error($conn);  
                      } else {
                      
$data = mysqli_fetch_assoc($query);
                      echo "
<h3 style='text-align:center; -webkit-background-clip: text, border;
    background-clip: text, border;
  -webkit-text-stroke: 5px rgba(101, 86, 65, 0.2);;font-size:40px;color:#436b95;'
> " .$data["firstname"]."<br><br>". $data["lastname"] . "</h3>";  
/*Here we are fetching data from query like firstname and lastname.It is your wish what you want to fetch data whether it is email,password or something else.*/
                      }
                } else {
                    echo "
<p>Please input the ID as parameter with numeric value like -> ?id=1</p>";
                }
?>

Save this php file as "integer.php"

Step 3 :- Make another php file and save following codes in php file

PHP Code

<?php 

$servername 
= "localhost";
$username = "root";
$password = "You Password Here"; //Set your password here if have
$database = "webtest"; // This is our database name.

//create connection
$conn = new mysqli($servername,$username,$password,$database);

//check connection
if ($conn->connect_error){
    die(
"Connection failed: " . $conn->connect_error);
}

?>

Save this file as "connection.php" .

Now browse your localhost in your browser ---> http://localhost/lab/integer.php?id=1

and you will see page.

Now if you want practice string based sql injection then just change following line :-

PHP Code

<?php 
$query 
= $conn->query("SELECT * FROM students WHERE id=$id"); 

with this
PHP Code

<?php 
$query 
= $conn->query(" SELECT * FROM students WHERE id= '$id' "); 

Notice :- id = ' $id ' <===== I use quoted to make it string based.

So when you type :- id=1' order by 100--+ then you will see error by putting ' so it is string based.

Now if you want to practice with double quatation ( " ) instead of single ( ' ) then here is the code :-

PHP Code

<?php 
$id 
= '"' . $_GET["id"] . '"';
 
$query = $conn->query(" SELECT * FROM students WHERE id= ($id) "); 


Here if you type :- id=1") order by 100--+ then you will see error otherwise not if you use id=1') order by 100--+

So this is small tutorial for you so enjoy this.
Also if you have any question, problem and if i have done any mistake then tell me.Next time i will tell about putting WAF when i learn it completely. Thanks 🤝 🎉
Hope you like it.
A very nice tutorial. However, I don't want people taking this as an excuse to avoid learning PHP / SQL.
N9c tut CodeNinja.Thanks for sharing ur knowledge :)
setting dis lab in my arch <3 .thnx buddy.
when i open localhost/integer.php?id=1 i get only white page .what is my problem?
Nice tut, it's a nice easy way to practice for all new people.
(Jun 21, 2015 01:04 PM)•^v^–[ bLacKn3x ]–^v^• Wrote:  when i open localhost/integer.php?id=1 i get only white page .what is my problem?

PM me for get my skype. Then i will help you live
Nice tutorial, keep the good work upcoming.
okay now i got it .you forgot in your code include("connection.php) .

Thanks for that it helps me a lot for understanding this more
this is good tutorial for who wanna be injector i like it nice work dude
Thank you very much for this. I got idea to make vulnerable web application now. :D
Posting so that many beginners learn from my tutorial
Thanks
good job......................................................................
Wow thank you for this tutorial. Once i learn php and mysql i will use this tutorial. Bookmarked it.
My mentor told me to learn php and mysql and then build vulnerable application so i searched in forum and got this awesome tutorial. Don't know why he is no active ?
Thanks for this! Trying to learn PHP and SQL but this definitely helps me feel like I can actually use my skills, in the interim.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by ExperTTJul 3, 2018 08:34 PM 1 147 Jul 3, 2018 08:34 PM
Last Post: mohammad rahel
by InjectorBoyApr 16, 2018 08:07 AM 8 3,065 Apr 16, 2018 08:07 AM
Last Post: PurineGAWs
by 7h3_Ph3n0mAug 27, 2016 01:12 AM 12 793 Aug 27, 2016 01:12 AM
Last Post: 7h3_Ph3n0m