Email Login| Link Exchange | Cyber News | Phishing Attack | SQL Injection | SEO | DOS Attack | Hacking Tools | |Hacking Tricks | Penetration Testing | Trojans & Keyloggers |Hacking Videos | General Discussion | Website Hacking | Session Hijacking | Social Engineering | Anonymous Surfing | Recover Passwords | Bypass Firewall | Hacking Books | Network Sniffers | Password Cracking | Enumerating & Fingerprinting | Movies & Songs

Share This Post With Your Friends

Sunday, November 15, 2009

What is SQL Injection?

SQL Injection is an attack methodology that targets the data residing in a database through the firewall that shields it. It attempts to modify the parameters of a Web-based application in order to alter the SQL statements that are parsed to retrieve data from the database.
This is perhaps the simplest definition of SQL injection. Naturally, the first step in this direction should be to uncover web applications that are vulnerable to the attack. The attack takes advantage of poor code and website administration.

Concept
In SQL injection, user controlled data is placed into a SQL query without being validated for correct format or embedded escape strings. It has been known to affect majority of applications which use a database backend and do not filter variable types. It has been estimated that at least 50% of the large e-commerce sites and about 75% of the medium to small sites are vulnerable to this attack. The dominant cause is the improper validation in CFML, ASP, JSP, and PHP codes.


OLE DB Errors

The user filled fields are enclosed by single quotation marks ('). So a simple test of the form would be to try using (') as the username.


Login Guessing & Insertion


  • The attacker can try to login without a password. Typical usernames would be 1=1 or any text within single quotes.

  • The most common problem seen on Microsoft MS - SQL boxes is the default sa password.

  • The attacker can try to guess the username of an account by querying for similar user names (ex: ad%' is used to query for "admin").

  • The attacker can insert data by appending commands or writing queries.
In the preceding example we have seen how web application vulnerability could be detected using a single quote. We have also seen how improper input validation can result in an attacker accessing the database. Here, we will examine how an attacker can guess his way into the site.


Attack Methods

How does an attacker go about uncovering the susceptible web application? This discovery phase includes activities such as looking at web pages for anything resembling an ID number, category, or name. The attacker may sift through all forms of variables as well as cookies. Many a times session cookies are stored in a database and these cookies are passed into SQL queries with little or no format checks. He may try placing various strings into form fields and in query variables. However, typically, someone looking for SQL vulnerability will start off with single and double quotes and then try with parenthesis and the rest of the punctuation characters. The response expected is any response signifying an error.
Attack Methods
From database fingerprinting, if the attacker has determined that the database backend is SQL server, he will try his luck with the default admin login credentials - namely sa and a blank password.

Alternatively he can issue a query so that his query would retrieve a valid username. For instance, to retrieve the administrative account, he can query for users.userName like 'ad%' --
Now if the attacker does not want to login and just wants to 'harvest' the site, he may try to view extra information which is not otherwise available. He can choose to transform the url such as the ones shown below to retrieve information.

Here, the "sCategory" is the variable name, and "Tools" is the value assigned to the variable. The attacker changes this valid url into:
http://www.example.com/shopping/productdetail.asp?SKU=MS01&sCategory=Kits
If the code underlying the page has a segment similar to the one shown below:
sub_cat = request ("sCategory")
sqlstr="SELECT * FROM product WHERE Category='" & sub_cat &'""
Set rs=conn.execute (sqlstr)

                                      
Now, the value "Kits" taken in by the variable "sCategory" is attributed to sub_cat and hence the SQL statement becomes:

SELECT * FROM product WHERE Category='Kits'
Therefore the output will be a result set containing rows that match the WHERE condition. If the attacker appends the following to the valid url,


The SQL statement becomes SELECT * FROM product WHERE Category='Tools' or 1=1 --'
This leads the query to select everything from the product table irrespective of whether Category equals "Tools' or not. The double dash " --" instructs the SQL Server to ignore the rest of the query. This is done to eliminate the last hanging single quote ('). Sometimes, it is possible to replace double dash with single hash "#".
If the database backend in question is not an SQL Server, it will not recognize the double dash. The attacker can then try appending ' or 'a'='a, which should return the same result.

Depending on the actual SQL query, the various possibilities available to the attacker are:
'or 1=1--"or 1=1--or1=1--' or 'a'='a" or "a"="a') or ('a'='a
To use the database for his malevolent intent, the attacker needs to figure out more than just what database is running at the backend. He will have to determine the database structure and tables. Revisiting our product table, we see that the attacker can insert commands such as:

insert into Category value (warez)

                                      
Suppose the attacker wants to add a description of the files he wants to upload, he will need to determine the structure of the table. He might be able to do just that, if error messages are returned from the application according to the default behavior of ASP and decipher any value that can be read by the account the ASP application is using to connect to the SQL Server.

The insertion methods will vary according to the database at the backend. For instance, MS SQL is considered to be the easiest system for SQL Insertion. Oracle has no native command execution capability. In Sybase, the Command exec is disabled by default. However, it is similar to MS SQL - though without as many stored procedures. MySQL is very limited in scope. SubSelects are a possibility with newer versions. It is typically restricted to one SQL command per query.

Shutting Down SQL Server


  • One of SQL Server's most powerful commands is SHUTDOWN WITH NOWAIT, which causes it to shutdown, immediately stopping the Windows service.
    Username: ' ; shutdown with nowait; -- Password [Anything]
    
                                              

  • This can happen if the script runs the following query:
    select userName from users whereuserName=';     
    shutdown withnowait;-' and user_Pass=' '

Threat
The default installation of SQL Server has the system account (sa) which is accorded all the privileges of the administrator. An attacker who happens to stumble across this account while harvesting websites can take advantage of this and gain access to all commands, delete, rename, and add databases, tables, triggers, and more. One of the attacks he can carry out when he is done with the site is to issue a denial of service by shutting down the SQL Server.

Attack Methods
A powerful command recognized by SQL Server is SHUTDOWN WITH NOWAIT. This causes the server to shutdown, immediately stopping the Windows service. After this command has been issued, the service must be manually restarted by the administrator. Let us take a look at an example. At an input form such as login, which is susceptible to SQL injection, the attacker issues the following command.
Username: '; shutdown with nowait; --Password: [Anything]

                                      
This would make our login.asp script run the following query:
select userName from users where userName=";shutdown with nowait; 
--'and userPass="

                                      
The '--' character sequence is the 'single line comment' sequence in Transact -SQL, and the ';' character denotes the end of one query and the beginning of another. If he has used the default sa account, or has acquired the required privileges, SQL server will shut down, and will require a restart in order to function again.


Extended Stored Porcedures


  • There are several extended stored procedures that can cause permanent damage to a system.

  • We can execute an extended stored procedure using our login form with an injected command as the username as follows:

  • Username: ' ; exec master..xp_xxx; --

  • Password: [Anything]

  • Username: ' ; exec master..xp_cmdshell ' iisreset' ; --

  • Password: [Anything]

Note
A stored procedure is a collection of SQL statements that can be called as though they were a single function. A SQL stored procedure is similar to a batch file - both are text files consisting of commands, and can be run by invoking the name of the procedure or batch file. An extended stored procedure (XP) takes the notion of a stored procedure one step further. Where stored procedures consist of text files, XPs are written in high-languages like C and compiled into .DLLs. Stored procedures primarily consists of SQL commands, while XPs can provide entirely new functions via their code.

Attack Methods
An attacker can take advantage of extended stored procedure by entering a suitable command. This is possible if there is no proper input validation. xp_cmdshell is a built-in extended stored procedure that allows the execution of arbitrary command lines. For example: exec master..xp_cmdshell 'dir' will obtain a directory listing of the current working directory of the SQL Server process. In our example, the attacker may try entering the following input into a search form can be used for the attack.

' exec master..xp_cmdshell 'product handy cam/DELETE' --
When the query string is parsed and sent to SQL Server, the server will process the following code:
SELECT * FROM PTable WHERE input text =" exec master..xp_cmdshell ' producthandycam/DELETE' --'
The advantage of this attack method is that the DLL file only needs to be present on a machine accessible by the SQL Server. Here, the first single quote entered by the user closes the string and SQL Server executes the next SQL statements in the batch including a command to delete a product to the product table in the database.


SQL Server Talks!


This command uses the 'speech.voicetext' object, causing the SQL Server to speak:
admin'; declare @o int, @ret       int exec sp_oacreate       
'speech.voicetext', @o,       'register', NULL,'foo',       
'bar' exec sp_oasetproperty       @o, 'speed',150 exec       
sp_oamethod @o, 'speak',       NULL, 'all your sequel       
servers are belong to us',       528 waitfor delay '00:00:05'--
It is possible for an attacker to leverage built-in extended stored procedures which are provided for the creation of ActiveX Automation scripts in SQL server. These scripts are typically written in VBScript or JavaScript, and they create automation objects and interact with them. They are functionally similar to ASP scripts. Similarly an automation script written in Transact-SQL can accomplish what an ASP script or a WSH script will do.
Of the possible attack methodologies, this is an interesting one documented by Chris Anley in his oft quoted paper 'Advanced SQL Injection techniques'. This is one example from his paper that illustrates this aspect.
declare @o int, @ret intexec sp_oacreate 'speech.voicetext', 
@o outexec sp_oamethod @o, 'register', NULL, 'foo', 
'bar'exec sp_oasetproperty @o, 'speed', 150exec sp_oamethod @o, 
'speak', NULL, 'all your sequel servers belong to us', 
528waitfor delay '00:00:05'
This uses the 'speech.voicetext' object, causing the SQL Server to speak.
Newer Posts Older Posts Home