Classic SQL Injection tutorial

Source: Internet
Author: User
Tags sql injection tutorial sql server injection

With the development of B/S application development, more and more programmers are writing applications using this mode. However, the entry point of this industry

The threshold is not high, and the programmer's level and experience are also uneven. A considerable number of programmers did not input data to users when writing code.

Security risks exist in applications. You can submit a piece of database query code based on the results returned by the program,

Obtain the data he wants to know. This is the so-called SQL Injection, that is, SQL Injection.

SQL injection is accessed from the normal WWW port, and the surface looks similar to the general Web page access, so the current municipal anti-DDOS service

Fire walls do not alert SQL injection. If the Administrator does not check IIS logs, it may not be detected for a long time.

However, the SQL injection method is quite flexible, and many unexpected situations may occur during the injection process. Can I analyze it based on the actual situation,

Constructing clever SQL statements to successfully obtain the desired data is the fundamental difference between the experts and cainiao.

According to national conditions, ASP + Access or SQLServer accounts for more than 70% of Chinese websites, PHP + MySQ accounts for L20 %, and other websites are less than 10%. In this article

We will explain the methods and skills of ASP injection from entry-level, advanced to advanced. The PHP injection article is written by another NB Alliance friend zwell.

It is intended to be useful to security workers and programmers. Do not skip this article because some people know about ASP Injection

There are still some mistakes in the basic judgment method. Are you ready? Lets Go...

Entry

If you have never tried SQL injection before, set IE menu> Tools> Internet Options> advanced> show friendly HTTP

Remove the check box before the error message. Otherwise, no matter what error is returned by the server, IE Only displays as an HTTP 500 server error and cannot obtain

Multiple prompts.

Section 1. Principles of SQL Injection

We start with a website www.19cn.com (note: the website owner has obtained the consent before this article is published, and most of the data is real data ).

On the homepage of the website, there is a link named "IE cannot open a new window for multiple solutions". The address is:

Http://www.19cn.com/showdetail.asp? Id = 49. we add a single quotation mark (') next to this address. The server will return the following error.

Error message:

Microsoft JET Database Engine error 80040e14

The syntax error of the string is found in the query expression ID = 49.

/Showdetail. asp, row 8

We can see the following points from the error prompt:

1. The website uses an Access database and connects to the database through the JET engine, instead of using ODBC.

2. The program does not determine whether the data submitted by the client meets the program requirements.

3. The table queried by this SQL statement has a field named ID.

 

From the above example, we can know that the principle of SQL injection is to submit special code from the client to collect the program and Server

Information to obtain the information you think.

Section 2: Determine whether SQL injection can be performed

After reading the first section, some people will think: I also often test whether it can be injected. Isn't it very easy?

In fact, this is not the best method. Why?

First, not necessarily the IIS of each server returns a specific error message to the client. If a cint (parameter) or other statement is added to the program,

SQL Injection fails, but the server also reports an error. The specific prompt is that an error occurs on the server when processing the URL. Contact the system administrator

Contact the administrator.

Second, some programmers who have a little knowledge about SQL Injection think that it is safe to filter out single quotes. This situation is not a minority, such

If you use single quotes for testing, the injection points are not tested.

So what test method is more accurate? The answer is as follows:

① Asp? Http://www.19cn.com/showdetail.asp? Id = 49

② Http://www.19cn.com/showdetail.asp? Id = 49; and 1 = 1

3 http://www.19cn.com/showdetail.asp? Id = 49; and 1 = 2

This is the classic 1 = 1, 1 = 2 test method. How can this problem be determined? You can see the results returned from the above three urls:

Injection performance:

① Normal display (this is inevitable, or the program is wrong)

② Normally displayed, the content is basically the same as ①

③ Prompt BOF or EOF (when the program does not make any judgment), or prompt that the record cannot be found (when rs. eof is determined), or the display content is blank

On error resume next is added in sequence)

If it cannot be injected, it is easier to judge. ① It is displayed normally. ② and ③ There are generally Program-defined error prompts or type conversion prompts.

Error.

Of course, this is only the method used to determine when the input parameters are numeric. in actual application, there will be numeric and search parameters.

In the intermediate section "General SQL Injection steps" for further analysis.

Section 3. Database types and injection methods

Different database functions and injection methods are different. Therefore, before injection, we need to determine the database type. Average

The most commonly used databases of ASP are Access and SQLServer, and more than 99% of websites on the Internet are among them.

How can a program tell you what database it uses? Let's take a look:

SQLServer has some system variables. If the IIS server prompts that it is not closed and SQLServer returns an error message, you can directly

To obtain error information, follow these steps:

Http://www.19cn.com/showdetail.asp? Id = 49; and user> 0

This statement is simple, but contains the essence of the SQL Server injection method. I also found this effect in an unintentional test.

The method of guessing with a high rate. Let me see what it means: first, the preceding statement is normal, with emphasis on and user> 0. We know that,

User is a built-in variable of SQLServer. Its value is the username of the current connection and its type is nvarchar. Take a nvarchar value and

If the number of int values is 0, the system first tries to convert the nvarchar value to the int type. Of course, there will be errors in the conversion process. SQLServer errors

The prompt is: a syntax error occurs when converting nvarchar value "abc" into a column whose data type is int. Haha, abc is the value of the variable user.

In this way, the user name of the database is obtained without waste. In the future, we will see many statements using this method.

By the way, as we all know, the SQLServer user sa is a role equivalent to the Adminstrators permission and has obtained the sa permission, almost certainly

You can get the Administrator of the host. The above method can easily test whether to log on with sa. Note that:

Log on to the sa and the system prompts that an error occurred while converting "dbo" to an int column, instead of "sa ".

If IIS on the server does not allow an error message to be returned, how can we determine the database type? We can start with the difference between Access and SQLServer

, Access and SQLServer have their own system tables, such as tables that store all objects in the database, Access is in the system table

[Msysobjects], but when you read the table in the Web environment, the system prompts "no permission". SQLServer is in the table [sysobjects] and in the Web Environment

Can be read normally.

Use the following statement to confirm that the injection can be performed:

Http://www.19cn.com/showdetail.asp? Id = 49; and (select count (*) from sysobjects)> 0

Http://www.19cn.com/showdetail.asp? Id = 49; and (select count (*) from msysobjects)> 0

If the database is SQLServer, then the first web site page with the original page http://www.19cn.com/showdetail.asp? Id = 49 Yes

The second web site, because the table msysobjects cannot be found, will prompt an error. Even if the program has a fault tolerance, the page is similar to the original

The page is completely different.

If the database uses Access, the situation is different. The page of the first website is different from the original one.

Whether or not the database settings allow reading the system table is generally not allowed, so it is completely different from the original website. In most cases

The first web site shows the database type used by the system. The second web site is used only for verification when the IIS error prompt is enabled.

Level 1

In this article, we learned how to judge SQL injection, but it is far from enough to obtain the website's confidential content. Next, I

We will continue to learn how to get the desired content from the database. First, let's take a look at the general steps of SQL injection:


Section 1: General steps of SQL Injection

First, judge the environment, find the injection point, and determine the database type. This is already discussed in the Getting Started article.

Secondly, according to the injection parameter type, the original appearance of the SQL statement is reconstructed in mind. There are three types of parameters:

(A) ID = 49 These injection parameters are numeric. The SQL statement is roughly as follows:
Select * from table name where field = 49
The injected parameter is ID = 49 And [query condition], that is, the generated statement:
Select * from table name where field = 49 And [query condition]


(B) Class = the injection parameters of the series are simplified. The SQL statements are roughly as follows:
Select * from table name where field = 'series'
The injected parameters are Class = series and [query conditions] and ''= ', that is, the generated statement:
Select * from table name where field = 'series' and [query conditions] and ''=''

(C) If parameters are not filtered during search, such as keyword = keyword, the original appearance of the SQL statement is roughly as follows:
Select * from table name where field like '% keyword %'
The injected parameter is keyword = 'and [query condition] and' % 25' = ', which is the generated statement:
Select * from table name where field like '%' and [query condition] and '%' = '%'

Then, replace the query condition with an SQL statement to guess the table name, for example:

ID = 49 And (Select Count (*) from Admin)> = 0

If the page is the same as that of ID = 49, the additional conditions are true, that is, the table Admin exists, and vice versa (Please remember this method ). For example

This loop until the table name is guessed.

After the table name is guessed, replace Count (*)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.