【sql】加密所有的存储程式

因项目管理规定,所有的存储程式(SP)都需要加密。

如何批量加密所有的SP呢?

在网上找到了参考的代码,然后发现除SP外连同View,Trigger,Function等也可以一并处理!
参考资料: https://download.csdn.net/download/xiaojie449/12171480

如下是在原作基础上略作补充的方法:

sql 复制代码
create procedure sp_EncryptObject 
(
    @Object sysname='All'
)
as
/*
    当@Object=All的时候,对所有的函数,存储过程,视图和触发器进行加密
    调用方法:
    1. Execute sp_EncryptObject 'All'
    2. Execute sp_EncryptObject 'ObjectName'
*/
begin
    set nocount on
    
    if @Object <>'All'
    begin
        if not exists(select 1 from sys.objects a where a.object_id=object_id(@Object) And a.type in('P','V','TR','FN','IF','TF'))
        begin
            --SQL Server 2008
            --raiserror 50001 N'无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。'

            --SQL Server 2012
            --throw 50001, N'无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。',1  
            --SQL Server 2016
            raiserror ('无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。',16,1)

            return
        end
        
        if exists(select 1 from sys.sql_modules a where a.object_id=object_id(@Object) and a.definition is null)
        begin
            --SQL Server 2008
            --raiserror 50001 N'对象已经加密!'

            --SQL Server 2012
            --throw 50001, N'对象已经加密!',1  

            --SQL Server 2016
            raiserror ('无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。',16,1)

            return
        end
    end
    
    declare @sql nvarchar(max),@C1 nchar(1),@C2 nchar(1),@type nvarchar(50),@Replace nvarchar(50)
    set @C1=nchar(13)
    set @C2=nchar(10)
    
    
    declare cur_Object 
        cursor for 
            select object_name(a.object_id) As ObjectName,a.definition 
                from sys.sql_modules a  
                    inner join sys.objects b on b.object_id=a.object_id
                        and b.is_ms_shipped=0
                        and not exists(select 1 
                                            from sys.extended_properties x
                                            where x.major_id=b.object_id
                                                and x.minor_id=0
                                                and x.class=1
                                                and x.name='microsoft_database_tools_support'
                                        )
                where b.type in('P','V','TR','FN','IF','TF')
                    and (b.name=@Object or @Object='All')
                    --and ( b.name like'%abc_%' or b.name like'%_20240820%'  )
                    and b.name <>'sp_EncryptObject'
                    and a.definition is not null                    
                order by Case 
                            when b.type ='V' then 1 
                            when b.type ='TR' then 2
                            when b.type in('FN','IF','TF') then 3 
                            else 4 end,b.create_date,b.object_id
                
    open cur_Object
    fetch next from cur_Object into @Object,@sql
    while @@fetch_status=0
    begin
        Begin Try  
            if objectproperty(object_id(@Object),'ExecIsAfterTrigger')=0 set @Replace='As' ; else set @Replace='For ';
                
            if (patindex('%'+@C1+@C2+@Replace+@C1+@C2+'%',@sql)>0)
            begin
                set @sql=Replace(@sql,@C1+@C2+@Replace+@C1+@C2,@C1+@C2+'With Encryption'+@C1+@C2+@Replace+@C1+@C2)
            end
            else if(patindex('%'+@C1+@Replace+@C1+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C1+@Replace+@C1,@C1+'With Encryption'+@C1+@Replace+@C1)
            end
            else if(patindex('%'+@C2+@Replace+@C2+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C2+@Replace+@C2,@C2+'With Encryption'+@C2+@Replace+@C2)
            end
            else if(patindex('%'+@C2+@Replace+@C1+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C2+@Replace+@C1,@C1+'With Encryption'+@C2+@Replace+@C1)
            end
            else if(patindex('%'+@C1+@C2+@Replace+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C1+@C2+@Replace,@C1+@C2+'With Encryption'+@C1+@C2+@Replace)
            end
            else if(patindex('%'+@C1+@Replace+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C1+@Replace,@C1+'With Encryption'+@C1+@Replace)
            end
            else if(patindex('%'+@C2+@Replace+'%',@sql)>0)
            begin 
                set @sql=Replace(@sql,@C2+@Replace,@C2+'With Encryption'+@C2+@Replace)
            end
                    
            set @type =
                case 
                    when object_id(@Object,'P')>0 then 'Proc'
                    when object_id(@Object,'V')>0 then 'View'
                    when object_id(@Object,'TR')>0  then 'Trigger'
                    when object_id(@Object,'FN')>0 or object_id(@Object,'IF')>0 or object_id(@Object,'TF')>0 then 'Function'
                end
            set @sql=Replace(@sql,'Create '+@type,'Alter '+@type)
            
            Begin Transaction
            exec(@sql)            
            print N'已完成加密对象('+@type+'):'+@Object            
            Commit Transaction
            
        End Try
        Begin Catch
            Declare @Error nvarchar(2047)
            Set @Error='Object: '+@Object+@C1+@C2+'Error: '+Error_message()

            Rollback Transaction          
            print @Error
            print @sql   
        End Catch
                    
        fetch next from cur_Object into @Object,@sql
    end

    close cur_Object
    deallocate cur_Object        
end

Go
exec sp_ms_marksystemobject 'sp_EncryptObject' --标识为系统对象
go

2022.08.22补充:
亲测存在的bug

  1. create proc 转换成 alter proc 时会检查中间的空格,所以不一定能加密成功
    2.数据库中存在_back命名的程式会被识别成备份还原,所以跑完会覆盖原有程式(此处风险极大)
相关推荐
一叶飘零_sweeeet几秒前
MongoDB 基础与应用
数据库·mongodb
猿小喵16 分钟前
DBA之路,始于足下
数据库·dba
tyler_download25 分钟前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
小春学渗透28 分钟前
Day107:代码审计-PHP模型开发篇&MVC层&RCE执行&文件对比法&1day分析&0day验证
开发语言·安全·web安全·php·mvc
weixin_449310841 小时前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
floret*1 小时前
HiveSQL面试题
hive·sql
落樱坠入星野1 小时前
拿下阿里云之后如何在本地运行镜像进行分析
经验分享·安全·网络安全·阿里云·云计算
iSee8571 小时前
ArcGIS地理空间平台 manager 任意文件读取漏洞复现
安全
Cachel wood2 小时前
Github配置ssh key原理及操作步骤
运维·开发语言·数据库·windows·postgresql·ssh·github
standxy2 小时前
如何将钉钉新收款单数据高效集成到MySQL
数据库·mysql·钉钉