Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix impersonation failure for sysadmin users in SQLModules.cs #25

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions SQLRecon/SQLRecon/commands/SqlModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,13 @@ public static void impersonate()
// Extract all user names
List<string> logins = Print.ExtractColumnValues(_query, "name");

Dictionary<string, string> impersonationLogins = new Dictionary<string, string>();
Dictionary<string, string> impersonationLogins = new();

// Next check to see if the user is a sysadmin
if (Roles.CheckRoleMembership(Var.Connect, "sysadmin"))
{
// If the user is a sysadmin, they can impersonate any login
Print.Status("Current user is a sysadmin and can impersonate any account.", true);
if (logins.Any())
{
foreach (string login in logins)
Expand All @@ -568,6 +570,7 @@ public static void impersonate()
}
else
{
// If not a sysadmin, enumerate all users and check which ones can be impersonated
if (logins.Any())
{
foreach (string login in logins)
Expand All @@ -577,6 +580,9 @@ public static void impersonate()
if (canImpersonate)
{
impersonationLogins.Add(login, "True");
} else
{
impersonationLogins.Add(login, "False");
}
}
}
Expand Down Expand Up @@ -1070,17 +1076,17 @@ private static bool _determineContext(string context)
case "standard":
return true;
case "impersonation":

// Check to see if the supplied user can be impersonated.
if (Roles.CheckImpersonation(Var.Connect, Var.Impersonate))
{
return true;
}
else
{
// Go no further
Print.Error($"'{Var.Impersonate}' can not be impersonated on {Var.SqlServer}.", true);
return false;
}

// Go no further
Print.Error($"'{Var.Impersonate}' can not be impersonated on {Var.SqlServer}.", true);
return false;

case "linked" or "chained":
// Obtain a list linked SQL servers
string sqlOutput = Sql.CustomQuery(Var.Connect, Query.GetLinkedSqlServers);
Expand Down
2 changes: 1 addition & 1 deletion SQLRecon/SQLRecon/modules/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal static void LinkedOrChain(string linkedSqlServer, string[] linkedSqlSer
{ "ActiveSessions", Query.GetActiveSessions }
};

// Check to see if the user is a sysadmin. Consider linked SQL srver chains.
// Check to see if the user is a sysadmin. Consider linked SQL server chains.
bool sysadmin = (linkedSqlServerChain == null)
? Roles.CheckLinkedRoleMembership(Var.Connect, "sysadmin", linkedSqlServer)
: Roles.CheckLinkedRoleMembership(Var.Connect, "sysadmin", null, linkedSqlServerChain);
Expand Down
11 changes: 9 additions & 2 deletions SQLRecon/SQLRecon/modules/Roles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,17 @@ internal static bool CheckRoleMembership(SqlConnection con, string role, string
/// <returns></returns>
internal static bool CheckImpersonation(SqlConnection con, string user)
{

// If the user is a sysadmin, return true, as they can impersonate any user.
if (Roles.CheckRoleMembership(Var.Connect, "sysadmin"))
{
return true;
}

user = user.Replace("'", "''");

// Check if a specific user can be impersonated
return Sql.Query(con,string.Format(Query.CheckImpersonation, user)).Equals("1");
return Sql.Query(con, string.Format(Query.CheckImpersonation, user)).Equals("1");
}

/// <summary>
Expand Down