Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 538 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL injection

#1
[Image: sqli1-big.png]

Hello all,

I hope you know how to do a SQL injection and have used it .In this tutorial we are going to see how it is working. What’s going on under the hood. How web application handle our input and process the SQL quarry. Let’s see.

Imagine that there is a web application like this.
Front-End Web Application

Hear we see how web application takes input from the user and send that data to PHP script through a GET request.
[Image: 12.png]

Back-end PHP Script

Now there should be a back-end script that handle this submitted data and make a SQL quarry.

After SQL quarry return some data from database PHP script processes that data and give user output.

Assume that following is the PHP code .

[Image: 13.png]

I hope you can understand what is going hear.

For this example we need a table that used by SQL quarry to take data from.

[Image: 14.png]

So all OK and fine.
SQL Quarry

Now let’s see how this is happening.

Imagine that I enter 3 as the input. So our ID will be equal to 3. What about our SQL quarry?


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



So it will give us the output.

Name for ID: Clara
Age for ID : 18

Nice!. It’s look like web application is working fine.

Do you remember in our previous tutorial we used a single quote to break a SQL quarry?

what if I enter 3′ as the input?

It give me an error.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


What the fuck goes hear?


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



You can clearly see that there is a syntax error near ID=’3”. Previously I explained why this happening.
Injecting SQL Quarry

Now we have to see how we can fix this error and extract data from the database. What if I enter flowing payload?.

3′ –+

Now our quarry is.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


But actually we only care the code before –+ Because it’s a comment character in SQL and everything after that will be ignored. So now this cod is valid and it’ll give us the output as expected.

This is the time to fetch data from database.But how?

Can we use another SELECT command with this one?

Yes indeed. we can use two SELECT commands with UNION operator like this.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


But one thing. Both of SELECT quarries must use slimier number of columns to fetch data. It’s no matter that they use different columns , only amount of used columns should be same.

You may say that it’s simple there are 5 columns in table. No buddy in real life you can’t see the table and you can’t figure out how many columns are there. :sad:

We can you a trick for finding the number of columns used by first SELECT command.

Think about our table and following commands.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


first command says select all columns from the users table and the order of results should be accordion to Name column. Second command also says that but results should be order with 3rd column(Age).

So even we don’t know column names we can order by 1,2,3 etc OK.

Now I enter this as the ID .


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


Let’s see our quarry.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



This gives us output as expected because there are more columns than 1 . So it’s possible to order result accordion to the first column.

Next we try order by 2. This also will be fine. Web application will work normally until we say order by 5.

What if we enter order by 6 ?

Surely it’ll give an error because there are no 6 columns. So by using this method we can find how many columns are using the first SELECT command. Now we can use another SELECT command


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



What going on hear is SQL quarry think that there is another table like following and quarry try to fetch data from both of them. After those data will be print to screen by PHP code.

Now if we can print data from second table we can find which columns are using by web application to show data.(In this example we know that PHP script get Name and Age columns). But unfortunately we still we can see only flowing output. :sad:

Name for ID: Clara
Age for ID : 18

why that? Because while our SQL quarry see there are two tables it first try to fetch data from first table. So if we want to get data from second one we must set a null value to first SELECT command . Our final payload .


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.




Since -3 is not in ID column data from our second table will be printed out.

Name for ID: 2
Age for ID : 3

Finlay it’s success. We can see column 2 and 3 is used by web app.

Now we can use these 2 channels for get any data from database. :smile:
Extracting Basic information

First of all let’s give a try to find out database name.




Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.




we can see the database name in the screen.

Name for ID: database_name
Age for ID : 3


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



Like this you can use

database() , user() , version() etc to fetch some basic data.

In next tutorials I'll explain more deep concepts in SQLi like blind sql injections etc.
Reply

#2
thank you for sharing.
Reply

#3
Quote:(04-18-2019, 06:51 PM)darkninja1980 Wrote:

[To see links please register here]

thank you for sharing. :smile:

You are welcome mate.
Reply

#4
Quote:(04-18-2019, 06:54 PM)thil4n Wrote:

[To see links please register here]

Quote: (04-18-2019, 06:51 PM)darkninja1980 Wrote:

[To see links please register here]

thank you for sharing. :smile:

You are welcome mate.

add more tutorials please. Also I will pm you requests as well.
Reply

#5
It's good to read you've elaborated on Union-Based SQLi.

Bookmarked, and shall read the lot tonight.
Reply

#6
Quote:(04-19-2019, 04:29 AM)mothered Wrote:

[To see links please register here]

It's good to read you've elaborated on Union-Based SQLi.

Bookmarked, and shall read the lot tonight.

Glad to hear it.
Reply

#7
Very nice man! Just have one question, when you do
SELECT * FROM users WHERE id= '3' UNION SELECT 1,2,3,4,5 --+ ' LIMIT 0,1

the UNION SELECT numbers are the columns, right? but with the SELEC * you are already getting all the columns?

Can you detail a little bit more
Reply

#8
Quote:(04-19-2019, 06:41 AM)Pinkz0rd Wrote:

[To see links please register here]

Very nice man! Just have one question, when you do
SELECT * FROM users WHERE id= '3' UNION SELECT 1,2,3,4,5 --+ ' LIMIT 0,1

the UNION SELECT numbers are the columns, right? but with the SELEC * you are already getting all the columns?

Can you detail a little bit more :tongue:

Think in this way. what if we use a quarry like following?

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


(We didn't specify any table to fetch data.) The result will be 1,2,3,4,5.
Now using UNION we combine those two queries to produce one result. Got it?
Reply

#9
Quote:(04-19-2019, 08:41 AM)thil4n Wrote:

[To see links please register here]

Quote: (04-19-2019, 06:41 AM)Pinkz0rd Wrote:

[To see links please register here]

Very nice man! Just have one question, when you do
SELECT * FROM users WHERE id= '3' UNION SELECT 1,2,3,4,5 --+ ' LIMIT 0,1

the UNION SELECT numbers are the columns, right? but with the SELEC * you are already getting all the columns?

Can you detail a little bit more :tongue:

Think in this way. what if we use a quarry like following?

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


(We didn't specify any table to fetch data.) The result will be 1,2,3,4,5.
Now using UNION we combine those two queries to produce one result. Got it?

Ah got it now! Thanks mate! So you do that 1,2,3,4,5 to check which columns the query is taking in?

Im getting on App Vulns, and SQLI looks so easy, but it is so much complex. Next thing I'm trying to learn is stacked queries. Do you know how those work? A tutorial like this one would be awesome! keep the good work!
Reply

#10
Quote:(04-19-2019, 11:48 AM)Pinkz0rd Wrote:

[To see links please register here]

So you do that 1,2,3,4,5 to check which columns the query is taking in?

Correct.

Given you're working with the URL, the vulnerable columns will be returned In the webpage you're currently working on.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through