SQL Server执行系统命令


SQL Server Transact http://wiki.huorong.cn/docs/sotd/sotd-1cge7hff93agv
SQL 工具概述https://learn.microsoft.com/zh-cn/sql/tools/overview-sql-tools?view=sql-server-ver16

简介

SQL Server执行系统命令需要xp_cmdshell、Ole Automation Procedures(sp_oacreate和sp_oamethod )和clr enabled其中一个功能。
开启这些功能需要高级配置(show advanced options),而开启需要serveradmin或sysadmin其中一个账户的权限。执行命令需要sysadmin权限。
执行命令和释放文件会通过查看程序集 、存储过程、作业等添加相关支持或自动执行作业。

开启功能

execute('sp_configure "show advanced options",1')      -- 开启高级配置
execute('reconfigure')                                 -- 保存设置

Exec sp_configure 'clr enabled', 1;                    -- 开启CLR 执行系统命令
execute('sp_configure "xp_cmdshell", 1')               -- 开启xp_cmdshell执行系统命令
EXEC sp_configure 'Ole Automation Procedures', 1;      -- 开启sp_oacreate执行系统命令

execute('sp_configure "show advanced options",0')      -- 开启高级配置
execute('reconfigure')                                 -- 保存设置
execute('sp_configure')                                -- 查看配置

查看权限

--查看当前账户权限
select is_srvrolemember('sysadmin')     --查看当前账户是否有sysadmin权限
EXEC sp_helpsrvrolemember 'sysadmin';   -- 查看角色下的用户
--查看服务器角色有哪些账户
select is_srvrolemember('serveradmin')  --查看当前账户是否有serveradmin权限
EXEC sp_helpsrvrolemember 'serveradmin';-- 查看角色下的用户

关闭功能

execute('sp_configure "show advanced options",1')      -- 开启高级配置
execute('reconfigure')                                 -- 保存设置

Exec sp_configure 'clr enabled', 0;                    -- 关闭CLR 执行系统命令
execute('sp_configure "xp_cmdshell", 0')               -- 关闭xp_cmdshell执行系统命令
EXEC sp_configure 'Ole Automation Procedures', 0;      -- 关闭sp_oacreate执行系统命令

execute('sp_configure "show advanced options",0')      -- 关闭高级配置
execute('reconfigure')                                 -- 保存设置
execute('sp_configure')                                -- 查看配置

查看是否开启

-- 查看 xp_cmdshell、clr enabled、Ole Automation Procedures状态
SELECT name,value,value_in_use,description FROM sys.configurations  Where (name = 'xp_cmdshell'or name = 'clr enabled'or name = 'Ole Automation Procedures')

查看程序集 、存储过程、作业

SELECT name,is_trustworthy_on FROM sys.databases       -- 查看安装程序集文件需要权限
-- ALTER DATABASE master SET TRUSTWORTHY ON;           -- 开启安装程序集文件需要权限
SELECT * FROM sys.assemblies WHERE is_user_defined = 1 -- 程序集名字(用户创建的)
SELECT * FROM sys.assembly_files                       -- 程序集文件
--DROP ASSEMBLY Helloworld ;                           -- 删除指定程序集
SELECT * FROM sys.all_objects WHERE type_desc = 'CLR_STORED_PROCEDURE' AND is_ms_shipped = 0 -- 非SQL Server内部存储过程
-- DROP PROCEDURE Helloworld ;                         -- 删除指定存储过程
EXEC dbo.sp_help_job                                   -- 查看全部作业
-- EXEC sp_delete_job @job_name = N'NightlyBackups' ;  -- 查看指定作业
-- sys.all_objects
-- https://learn.microsoft.com/zh-cn/sql/relational-databases/system-catalog-views/object-catalog-views-transact-sql?view=sql-server-ver16

sqlps

sql server的一个powershell工具,很多时候是自带的。
https://learn.microsoft.com/zh-cn/sql/powershell/download-sql-server-ps-module?view=sql-server-ver16

cd  SQL\计算机名字
ls DEFAULT\Databases\master\Assemblies
ls DEFAULT\Databases\master\StoredProcedures
ls DEFAULT\JobServer\Jobs
#rm DEFAULT\Databases\master\Assemblies\* # 删除全部程序集
#rm DEFAULT\Databases\master\StoredProcedures\* # 删除全部存储过程
#rm DEFAULT\JobServer\Jobs\my_job_name

参考

以上很多语句可以在微软搜索看官方帮助。
https://learn.microsoft.com/zh-cn/sql/tools/overview-sql-tools?view=sql-server-ver16
其他参考
https://blog.csdn.net/Ruishine/article/details/113883888
https://blog.csdn.net/weixin_46684578/article/details/118436385#CLR_313
https://github.com/mindspoof/MSSQL-Fileless-Rootkit-WarSQLKit/tree/master/WarSQLKit


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注