How to Hide Stored Procedure’s Code? – WITH ENCRYPTION – Interview Question of the Week #088

Question: How can I hide a stored procedure definition from an ordinary metadata lookup?

A music box can be wound while its working cylinder remains obscured behind frosted glass

Short answer: Create the procedure WITH ENCRYPTION. This is the quick answer I hear in interviews. There is an important word hiding inside that answer: SQL Server obfuscates the definition. It does not turn the procedure into a security boundary.

Let us compare two tiny procedures. Run this in a scratch database where these names are free:

CREATE PROCEDURE dbo.RegularSP
AS
    SELECT 5 AS FIVE;
GO
CREATE PROCEDURE dbo.EncryptedSP
WITH ENCRYPTION
AS
    SELECT 5 AS FIVE;
GO
EXEC sys.sp_helptext N'dbo.RegularSP';
GO
EXEC sys.sp_helptext N'dbo.EncryptedSP';
GO

The first result returns definition text. The second says the object text is encrypted. These original SSMS captures show the same contrast with the original sample names:

Original SSMS result: sp_helptext returns the regular procedure definition

Original SSMS message: the encrypted procedure text cannot be displayed by sp_helptext

For another check, sys.sql_modules.definition is NULL for the encrypted procedure. For a nonencrypted object, NULL metadata can also mean you lack permission to view the definition, so check access before diagnosing encryption. Callers with execute permission can still run the encrypted routine:

SELECT OBJECT_NAME(object_id) AS ProcedureName, definition
FROM sys.sql_modules
WHERE object_id IN (OBJECT_ID(N'dbo.RegularSP'), OBJECT_ID(N'dbo.EncryptedSP'));
EXEC dbo.EncryptedSP;

Keep the original creation script somewhere controlled before using this option. You cannot rely on normal metadata to recover it later. Also, do not claim the text is impossible to retrieve: privileged access to database files or server internals can expose it. Use permissions to protect access, and use WITH ENCRYPTION only when hiding the ordinary definition view is the goal.

The interview answer is WITH ENCRYPTION. The production answer includes what that option does, and what it cannot promise.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Server, SQL Server Encryption, SQL Stored Procedure
Previous Post
How to Find Recent Executed Queries in SQL Server? – Interview Question of the Week #087
Next Post
How to Find Missing SQL Server Configuration Manager? – Interview Question of the Week #089

Related Posts

9 Comments. Leave new

  • Aaron Olson (@aarondolson)
    September 11, 2016 5:35 pm

    Everything we do these days is with a “security first” mindset, did not even think about the use-case for encrypting a stored procedure.

    Reply
  • Just do not use this to hide confidential information. It can be decrypted in two seconds using the code that you can google.

    Reply
  • There are many tools to decrypt stored procecure (even free, “decrypt all objects in db”…).

    Cons is: Missing info (procedure name, statements) in permormance monitoring views/tools.

    TL;DR: Don’t do it.

    Reply
  • Very true Aaron!

    Reply
  • How to update.

    Reply
  • hi,

    I know with “with encryption” option we are only obfuscate our procedures and function. then there are tools like dbforge that can decrypt them.

    is there a way to encrypt stored procedure and functions with keys and password to block decryption (or make it really hard to decrypt).

    Thanks,
    Sami

    Reply
  • Hi Sami,

    Did you get a solution for
    “is there a way to encrypt stored procedure and functions with keys and password to block decryption (or make it really hard to decrypt)”
    If yes please guide i too have a similar requirement.

    Reply
  • Hi Sami,

    Did you find any solution, i am also having similar requirement.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.