Recover access to a SQL Server instance

Microsoft's relational database management system.
Forum rules
READ: Techie Rambles rules
User avatar
Kailey
Administrator
Administrator
Posts: 13
Joined: Sun Aug 27, 2023 3:25 am
Name: Kailey Snay

Recover access to a SQL Server instance

Post by Kailey »

Problem

Starting with SQL Server 2008, the local Administrators group is no longer added by default during SQL Server setup. This means that it is possible, especially if you don't use mixed authentication (or have forgotten the sa password), that you can be completely locked out of your own SQL Server instance. I've seen cases where an employee has moved on, but their Windows account, being the only one with Administrator privileges for SQL Server, had been completely obliterated from the system. Of course, that person was the only one who knew the sa password as well, and being a local admin or even a domain admin might not help you.

The typical workaround I have seen employed is to restart SQL Server in single user mode. However, this approach requires at least some downtime; in some systems, this would be unacceptable. And depending on what needs to be managed on the server, it might not be feasible to wait for a scheduled maintenance window.

A more extreme workaround is to shut down SQL Server, copy all the MDF/LDF files, install a new instance of SQL Server, and attach all the user databases. In addition to downtime, the problem here is that you must also re-create all the logins, linked servers, jobs, and other elements outside of the database in order to have a fully functioning system again. Plus, unless you uninstall the old instance first, the new instance will have a new instance name, and therefore all client applications will need to be updated.

Solution

Thanks to Mark Russinovich of Sysinternals fame, there is a very painless way to solve this problem without any downtime: PsExec. While it wasn't one of its primary design goals, PsExec allows you to run programs as the NT AUTHORITY\SYSTEM account, which - unlike "regular" Administrator accounts - has inherent access to SQL Server.

The process to get back up and running is quite simple. Log in to the server where SQL Server is running, as an account with local Administrator privileges. Download and extract PsExec.exe. Start an elevated command prompt (Shift + Right-click, "Run as Administrator"). Run the following command, adjusting for your actual path to Management Studio, which may be different:

Code: Select all

PsExec -s -i "C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe"
This command tells PsExec to run SSMS interactively (-i) and as the system account (-s).

You will get an error message if you are not an Administrator. You will need to agree to the license terms in order to proceed. When Management Studio launches, it will prompt you to connect to a server. You will notice that the authentication mode is Windows Authentication, and the username is hard coded as NT AUTHORITY\SYSTEM:

sql1.png
sql1.png (11.08 KiB) Viewed 23520 times

Once you connect, in Object Explorer, you will see that you are connected to the server as NT AUTHORITY\SYSTEM:

sql2.png
sql2.png (13.8 KiB) Viewed 23520 times

Now, you can go into Security > Logins and add your account as a sysadmin, add other admin accounts, update the sa password, and do anything else you need to do to make your instance manageable.

Caveats
  • You must be a local Administrator to masquerade as NT AUTHORITY\SYSTEM.
  • You may need to disable UAC.
  • You may also be able to do this remotely, but since I work primarily in virtual machines, I did not test this.
  • If you change the sa password, and you've used the sa account for external programs (which you shouldn't do), you will need to update those programs.
  • Leave the command prompt running in the background until you're done with all your changes - if you inadvertently Ctrl+C from within the command prompt, SSMS will vanish.
Conclusion

It is quite common to get locked out of a SQL Server instance, and having been there, I know it is quite frustrating. PsExec can get you into your systems without a lot of the headache that can be caused by more brute force methods. You should download PsExec and keep it handy; it can prevent you from going to extreme measures and having it already available can help in cases where the SQL Server machine you're trying to recover does not have ready access to the web.