Index arrow Internet Business arrow Security

How bad guys hack into websites using SQL Injection PDF Print E-mail
Written by Matija Vidmar   
SQL Injection is one of the most common security vulnerabilities on the web. Here I'll try to explain in detail this kind of vulnerabilities with examples of bugs in PHP and possible solutions.

If you are not so confident with programming languages and web technologies you may be wondering what SQL stay for. Well, it's an acronym for Structured Query Language (pronounced "sequel"). It's "de facto" the standard language to access and manipulate data in databases.

Nowadays most websites rely on a database (usually MySQL) to store and access data.

Our example will be a common login form. Internet surfers see those login forms every day, you put your username and password in and then the server checks the credentials you supplied. Ok, that's simple, but what happens exactly on the server when he checks your credentials?

The client (or user) sends to the server two strings, the username and the password.

Usually the server will have a database with a table where the user's data are stored. This table has at least two columns, one to store the username and one for the password. When the server receives the username and password strings he will query the database to see if the supplied credentials are valid. He will use an SQL statement for that that may look like this:

SELECT * FROM users WHERE username='SUPPLIED_USER' AND password='SUPPLIED_PASS'

For those of you who are not familiar with the SQL language, in SQL the ' character is used as a delimiter for string variables. Here we use it to delimit the username and password strings supplied by the user.

In this example we see that the username and password supplied are inserted into the query between the ' and the entire query is then executed by the database engine. If the query returns any rows, then the supplied credentials are valid (that user exists in the database and has the password that was supplied).

Now, what happens if a user types a ' character into the username or password field? Well, by putting only a ' into the username field and living the password field blank, the query would become:

SELECT * FROM users WHERE username=''' AND password=''

This would trigger an error, since the database engine would consider the end of the string at the second ' and then it would trigger a parsing error at the third ' character. Let's now what would happen if we would send this input data:

Username: ' OR 'a'='a
Password: ' OR 'a'='a

The query would become
SELECT * FROM users WHERE username='' OR 'a'='a' AND password='' OR 'a'='a'

Since a is always equal to a, this query will return all the rows from the table users and the server will "think" we supplied him with valid credentials and let as in - the SQL injection was successful :).

Now we are going to see some more advanced techniques.. My example will be based on a PHP and MySQL platform. In my MySQL database I created the following table:

CREATE TABLE users (
username VARCHAR(128),
password VARCHAR(128),
email VARCHAR(128))

There's a single row in that table with data:

username: testuser
password: testing
email: This email address is being protected from spam bots, you need Javascript enabled to view it

To check the credentials I made the following query in the PHP code:

$query="select username, password from users where username='".$user."' and password='".$pass."'";

The server is also configured to print out errors triggered by MySQL (this is useful for debugging, but should be avoided on a production server).

So, last time I showed you how SQL injection basically works. Now I'll show you how can we make more complex queries and how to use the MySQL error messages to get more information about the database structure.

Lets get started! So, if we put just an ' character in the username field we get an error message like
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' and password=''' at line 1

That's because the query became

select username, password from users where username=''' and password=''
What happens now if we try to put into the username field a string like ' or user='abc ?
The query becomes

select username, password from users where username='' or user='abc ' and password=''

And this give us the error message
Unknown column 'user' in 'where clause'

That's fine! Using these error messages we can guess the columns in the table. We can try to put in the username field ' or email=' and since we get no error message, we know that the email column exists in that table. If we know the email address of a user, we can now just try with ' or email=' This email address is being protected from spam bots, you need Javascript enabled to view it in both the username and password fields and our query becomes

select username, password from users where username='' or email=' This email address is being protected from spam bots, you need Javascript enabled to view it ' and password='' or email=' This email address is being protected from spam bots, you need Javascript enabled to view it '

which is a valid query and if that email address exists in the table we will successfully login!

You can also use the error messages to guess the table name. Since in SQL you can use the table.column notation, you can try to put in the username field ' or user.test=' and you will see an error message like
Unknown table 'user' in where clause

Fine! Let's try with ' or users.test=' and we have
Unknown column 'users.test' in 'where clause'

so logically there's a table named users :).

Basically, if the server is configured to give out the error messages, you can use them to enumerate the database structure and then you may be able to use these informations in an attack.

Matija Vidmar is an experienced programmer. He's also interested in computer security, networking and system administration and internet marketing. He owns a tech blog at http://calibro.candyham.com
 


144.jpg

128.jpg

129.jpg

18.jpg

Index arrow Internet Business arrow Security

Results 71 - 80 of 176


Body Worn Cameras Support Covert Operations Author : Shawn Davis
If you are a private investigator or a police officer, you know that body worn cameras are as much of a natural part of your investigative procedures as your ID. And because you are involved in this important, highly sensitive business, you often need to make video and audio recordings without the suspect knowing what you are doing. There is a great pocket DVR that is smaller than a package of cigarettes, and can actually be placed into an empty ...

Internet Security: The Secure Free Option Author : Simon Ecclestone
This high speed revolution has increased the swiftness of accessibility for unscrupulous hackers to upload private information quickly and easily, through your home internet connection. With the threat of viruses, identity fraud, phishing, spyware and keyloggers, never has it been more important to protect the valuable contents of your home computer. There are many types of internet security software out there, which offer varying ranges of...

How bad guys hack into websites using SQL Injection Author : Matija Vidmar
SQL Injection is one of the most common security vulnerabilities on the web. Here I'll try to explain in detail this kind of vulnerabilities with examples of bugs in PHP and possible solutions. If you are not so confident with programming languages and web technologies you may be wondering what SQL stay for. Well, it's an acronym for Structured Query Language (pronounced "sequel"). It's "de facto" the standard language to access and manipulate...

Top 5 Reasons to Choose An Internet Filtering Appliance Over Software Author : Jennifer
The need for organizations to monitor and control Internet usage in the workplace should be an accepted fact of doing business in a cyber-connected world. Statistics indicating that 30 to 40 percent of Internet use in the workplace is unrelated to work issues should come as no surprise. Neither should the report that 90 percent of employee computers harbor as many as 30 spyware programs. In fact, studies indicate that companies may be incurring a...

Diginus Ltd Secures ISO27001 Author : Jonathan Gay BA (hons) CISA MBCS
Diginus Ltd is proud to announce that we have successfully achieved ISO/IEC 27001 (formally BS7799) certification, the internationally recognised standard for information security. This success comes just ten months after achieving certification to ISO9001:2000 for our quality management system. Both certifications followed a rigorous inspection process by Lloyds Register Quality Assurance (LRQA). As a leading supplier of e-identity solutions ...

Identity Theft – Who is phishing for your information? Author : nan
There’s a new type of internet piracy called phishing (pronounced fishing). Internet thieves are fishing for your personal information. They’re looking for ways to trick you into giving out your Social Security Number, credit card number and other personal information that they can use to their advantage. You could become a victim of identity theft that could take years to clear your financial history and personal reputation. But understanding ho...

Identity Theft – Who is ‘phishing’ for your information? Author : nan
There’s a new type of internet piracy called ‘phishing’ (pronounced ‘fishing’). Internet thieves are ‘fishing’ for your personal information. They’re looking for ways to trick you into giving out your Social Security Number, credit card number and other personal information that they can use to their advantage. You could become a victim of identity theft that could take years to clear your financial history and personal reputation. But understand...

Worm_Grew.A Threat, Hype, or Dud? Author : Darren Miller
The Worm_Grew.A Virus Got Allot Of Attention ---------------------------- The 3rd has come and gone. What has been the real effect or impact of the “WORM_GREW.A (Also know as Nyxem, BlackMal, MyWife, and so on)” virus? For weeks there has been tons of warnings, in the news, print, e-zines, you name the forum and it has been a discussion topic. Current information indicates that the WORM_GREW.A virus has not caused mass destruction as promised o...

Security Management - Software and hardware to keep your investment secure. Author : Ventura
Security management is essential to any company. There are many different aspects of security management including Computer Network security, Entry Identification and Logging system and Fire Detection and Prevention. Companies are increasingly using programmed magnetic or chipped Identity Cards for employees that allow access only to specific parts of the site. The system records every entry and exit made, meaning that access to sensitive are...

Protecting your self against online credit card fraud Author : Steven Stoeterau
Today more and more people are looking to the internet to do their shopping. With online stores popping up all over the internet the urge to spend money on the World Wide Web has never been stronger. The unfortunate thing is that the urge for scam artists to take your money has never been stronger. So how do you protect your self from these thieves? If you follow these simple steps I promise you’ll enjoy shopping on the internet more having taken...

  
Top
 
 
 
© 2008 mmakers.org
Joomla! is Free Software released under the GNU/GPL License.