Home » Migrate the sa password to another instance without knowing it

Migrate the sa password to another instance without knowing it

by Vlad Drumea
0 comments

This is a brief post showing how to migrate the sa password to another SQL Server instance without knowing it.

Backstory

I recently had to tear down an instance and rebuild it in order to downgrade the edition.
My usual process for this is:

The issue

Export-DbaLogin does a great job of exporting all logins, but it doesn’t offer a way to migrate the sa password to another instance.
In this case, I didn’t have the sa password and was required to ensure that the rebuilt instance is a 1:1 copy of the original one (edition excluded).

This also meant that the existing password used for sa had to be transferred to the rebuilt instance.

The solution

The main part of the solution comes from my post about cracking SQL Server login hashes offline.

Specifically, this query:


Migrating the sa password as a hash

To make things easier for similar future situations, I’ve adapted the above query into the following:


This T-SQL generates the ALTER LOGIN command that sets sa’s password to the current hash.

The gotcha here is that, in order to set a SQL Server’s login password using the hash, you need to set CHECK_POLICY to OFF.
This makes sense since there’s no way for SQL Server to know if the password matches the policy without actually having a clear text password to asses.

To account for this, the query also checks what the original status of CHECK_POLICY is, and if it’s ON, it includes the command to set it back to ON afterwards.

And this is how the output looks like:


The resulting output can be copy pasted and executed on an instance where you’d want to copy or transfer sa’s password.

Note that I’ve redacted the password hash in the example above so that it fits nicely into the screenshot.

Conclusion

Making up for other tools’ lack of an option to transfer sa’s password to another install of SQL Server with some hacky T-SQL.

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.