Manual SQL Injection

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

Manual SQL Injection

Entry

If you have never tried SQL injection before, remove the check box before IE menu> Tools> Internet Options> advanced => show friendly HTTP Error messages. Otherwise, no matter what error is returned by the server, IE Only displays as an HTTP 500 server error and cannot receive more prompts.

Section 1. Principles of SQL Injection

Here we start with www.xxxxx.com.

On the home page of the website, named "IE cannot open new window of a variety of solutions" link, address: http://www.xxxxx.com/showdetail.asp? Id = 49. we add a single quotation mark (') after this address. The server will return the following 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:

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

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

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 information about programs and servers and 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 of all, not necessarily the IIS of each server returns a specific error message to the client. If statements such as cint (parameter) are added to the program, SQL injection will not succeed, 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.

Second, some programmers who have a little knowledge about SQL Injection think that it is safe to filter out single quotes. This is not a minority case. If you use single quotes for testing, the injection points cannot be tested.

So what test method is more accurate? The answer is as follows:
Http://www.xxxxx.com/showdetail.asp? Id = 49
Http://www.xxxxx.com/showdetail.asp? Id = 49; and 1 = 1
Http://www.xxxxx.com/showdetail.asp? Id = 49; and 1 = 2

This is the classic 1 = 1, 1 = 2 test method. How can this problem be determined? Let's look at the results returned from the above three URLs. The injection performance is as follows:

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

Normally displayed, the content is basically the same as 1

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 (the program adds on error resume next)
If it is not possible to inject, it is easier to judge. 1 is displayed normally, and 2 and 3 generally have program-defined error prompts, or errors during type conversion.

Of course, this is only the judgment method used when the input parameters are numeric. in actual application, there will be between numeric and search parameters, I will analyze the SQL Injection general steps in the intermediate section.

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. Generally, Access and SQLServer are the most commonly used databases in ASP. 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 server IIS prompts that it is not closed and SQLServer returns an error message, you can get the error information directly by using the following method:

Http://www.xxxxx.com/showdetail.asp? Id = 49; and user> 0
This statement is very simple, but contains the essence of the SQL Server injection method. I also found this efficient method in an unintentional test. Let me take a look at its meaning: first, the preceding statement is normal, with emphasis on and user> 0. We know that user is a built-in variable of SQLServer, the value is the username of the current connection and the type is nvarchar. Compare the nvarchar value with the int value 0. The system will first try to convert the nvarchar value to the int type. Of course, the conversion process will definitely fail. The SQLServer error prompt is: A syntax error occurs when converting the nvarchar value "abc" to an int column. The value of abc is the value of the variable user. In this way, the user name of the database is obtained without any effort. 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. With the sa permission, you can almost certainly get the Administrator of the host. The above method can be used to easily test whether to log on with sa. Note that, if it is a log on with sa, an error occurs when "dbo" is converted 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 both have their own system tables, such as tables that store all objects in the database. Access is in the system table [msysobjects, however, when reading the table in the Web environment, the system prompts "no permission". SQLServer is in the table [sysobjects] and can be read normally in the Web environment.

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

Http://www.xxxxx.com/showdetail.asp? Id = 49; and (select count (*) from sysobjects)> 0 ytest
Http://www.xxxxx.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.xxxxxx.com/showdetail.asp? Id = 49 is roughly the same. However, because the second website cannot find the table msysobjects, an error is prompted. Even if the program is fault tolerant, the page is completely different from the original page.

If the database uses Access, the situation is different. The page of the first website is completely different from the original page. The second website is determined by whether the database allows reading the system table, generally, this is not allowed, so it is completely different from the original website. In most cases, the database type used by the system can be known through the first web site. The second web site is used only for verification when the IIS error prompt is enabled.

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.