Secure and disable the SQL Server SA Account

By:   |   Updated: 2010-05-14   |   Comments (3)   |   Related: > Security


Problem

Ideally your SQL instance would be configured to only allow for Windows Authentication. There may be times when mixed mode authentication is necessary at which point you will should configure a method to rotate the ‘sa' password on a regular basis. You want the new password to be random and secure from others. Not only do you not want anyone else to know the password, you don't even want to know it yourself.

Solution

SQL Server has an undocumented system stored procedure named sp_SetAutoSAPasswordAndDisable. This procedure will do exactly as the name suggests: it will reset the password and then disable the 'sa' login.

The procedure takes no parameters, so the syntax for usage is as follows:

EXEC sp_SetAutoSAPasswordAndDisable
GO

After completion you should see the standard message:

Command(s) completed successfully.

The actual code is as follows:

ALTER procedure [sys].[sp_SetAutoSAPasswordAndDisable]
as
    -- can execute only as SysAdmin
 if (not (is_srvrolemember('sysadmin') = 1))  -- Make sure that it is the SA executing this.
 begin
  raiserror(15247,-1,-1)
  return(1)
 end
    -- Begin a transaction
    BEGIN TRANSACTION
    
 -- Disable Password Policy on the SA Login
 ALTER LOGIN sa WITH CHECK_POLICY = OFF
 IF @@ERROR <> 0
 BEGIN
  ROLLBACK TRANSACTION
  RETURN (1)
 END
 -- Create a New Guid as the random password
 declare @randompwd UNIQUEIDENTIFIER
 declare @stmt nvarchar(4000)
 SET @randompwd = newid()
 SELECT @stmt = 'ALTER LOGIN sa WITH PASSWORD = ' + quotename(@randompwd, '''')
 EXEC(@stmt)
 IF @@ERROR <> 0
 BEGIN
  ROLLBACK TRANSACTION
  RETURN (1)
 END
 -- Now set the policy back
 ALTER LOGIN sa WITH CHECK_POLICY = ON
 IF @@ERROR <> 0
 BEGIN
  ROLLBACK TRANSACTION
  RETURN (1)
 END 
 -- Now set the policy back
 ALTER LOGIN sa DISABLE
 IF @@ERROR <> 0
 BEGIN
  ROLLBACK TRANSACTION
  RETURN (1)
 END 
 -- Commit the transaction
 COMMIT TRANSACTION

When you execute this stored procedure the password for the ‘sa' login will be reset to a random GUID, and then be disabled. Auditors love this aspect because not only is the password secure, but so is the account itself.

If you need to roll your own solution to rotate the password for the 'sa' login, then the sp_SetAutoSAPasswordAndDisable stored procedure may be exactly what you are looking for.

Next Steps
  • Execute the stored procedure against an instance where you want to have the password for the 'sa' login set to a random GUID and then disabled.
  • Read more SQL Server security tips


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Thomas LaRock Thomas LaRock is a Head Geek at SolarWinds and a Microsoft Certified Master, Microsoft Data Platform MVP, VMware vExpert, and a former Microsoft Certified Trainer with over 20 years’ experience.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2010-05-14

Comments For This Article




Tuesday, July 10, 2012 - 2:57:21 AM - VAhid Back To Top (18412)

Hello

I have a database server that users are connected through to it but i dont know a user is that drop database on server

i write a trigger and log history but again user can delete my database on server and trigger cant prevent that it

 

 

CREATE

 

Trigger [LogDB] onall

server

For

 

 

DROP_database,ALTER_database,ALTER_TABLE,DROP_TABLE,CREATE_DATABASE,Drop_Trigger

 

as

 

set

 

nocount

on

PRINT

 

 

 

'You must contact a DBA before dropping or altering tables!'

 

 

rollback

 

Declare

 

@CommandText

nvarchar(2000),

@ComputerName

nvarchar(100),

@ApplicationName

nvarchar(100),

@LogiName

nvarchar(100),

@LogDate

DateTime

SELECT

 

@CommandText =EVENTDATA().value

 

(

 

'(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)'

)

 

 

select

 

@ComputerName

=HostName,

@ApplicationName

=Program_name,

@LogiName

=suser_sname(),

@LogDate

=GetDate()

from

 

sys.sysprocesseswhere spid=

@@Spid

 

 

Insert

 

LogError.dbo.LogEvents

 

(

 

CommandText

,

ComputerName

,

ApplicationName

,

LogiName

,

LogDate

 

)

 

values

 

(

 

@CommandText

,

@ComputerName

,

@ApplicationName

,

@LogiName

,

@LogDate

 

)

 


Tuesday, June 26, 2012 - 3:48:45 PM - Tim Lehner Back To Top (18204)

@JimJ, I believe the idea is that you would create a different sysadmin account when installing SQL Server or at the earliest possible time, and use that new account (or other less-privileged accounts as appropriate) to perform admin duties.  This allows you to safely disable sa and essentially defeat attacks against it.


Saturday, May 22, 2010 - 10:28:36 AM - JimJ Back To Top (5436)

I am not sure why you would want to create an sa password that can't be accessed.  I find that I need SQL Server authentication when I am accessing a database (e.g. DSN) and I have not been joined to a network. In that case, the IP address\Instance Name and SQL Server authentication is what I have to use for the short interval to load the application and test appropriately.  What other alternative should I use?















get free sql tips
agree to terms