Avatar of tschary
tschary
 asked on

SQL Server 'SA' account successful logon Alert

Hi Experts,

I want to capture 'SA' account successful logon alone , i need to send an email alert to security team.When anyone logged in using 'SA' account. We have SQL Server audit feature to achieve this . it captures all successful logins in security log. But i need only 'SA' user account successful audit information. Please provide your tips.

Thanks in Advance,

Sreenivasa
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
tschary

8/22/2022 - Mon
Vitor Montalvão

Sreenivasa, in SQL Server you have Logon Triggers that fires every time a logon occurs. If you want to send an email only a 'sa' Login fires the trigger then you can do something like:
CREATE TRIGGER trg_login
ON ALL SERVER 
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN()= 'sa' 
    EXEC msdb.dbo.sp_send_dbmail @recipients = 'SecurityTeam@company.com', @body = 'ALERT: sa login detected.', @subject = 'SA Login' ;
END;

Open in new window

tschary

ASKER
Hi Vitor,
It is working fine. I have modified the below email send line to inserting data into a temp table for testing purpose. It is working fine. But for single 'sa' login (using SQL Server Authentication mode) it inserting three records in the table instead one single record. Please suggest me on this.

 --EXEC msdb.dbo.sp_send_dbmail @recipients = 'SecurityTeam@company.com', @body = 'ALERT: sa login detected.', @subject = 'SA Login' ;
    INSERT INTO [tempdb].[dbo].[TestSA]([Value])VALUES ('tsc')
Vitor Montalvão

Strange.
How are you logging in?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
tschary

ASKER
Hi vitor,
I have tried with email profile account as well, it is sending 3-5 mails when i login through SA account. I am logging via SQL Server authentication mode . User name: sa, passowrd: xxxxxxx.Please find my code and review.

CREATE TRIGGER [testSA]
ON ALL SERVER
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN()= 'sa'
   
   EXEC msdb.dbo.sp_send_dbmail @profile_name='testProfile',
@recipients='xyz@abc.com', @subject='Test SA message',
@body='SA log on occured in the server '
     
END;
Vitor Montalvão

Sorry, I wasn't clear. What I meant is if you're logging in through an application or just SSMS?

I made some tests and looks like the logon trigger is fired many time when it SSMS. You just need to open some folders under the instance name and you get the trigger fired.
tschary

ASKER
Hi Vitor,

Yes. I am trying through SSMS and not from an application. I can see trigger under server objects folder.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tschary

ASKER
Thanks for the solution