Home
%3CLINGO-SUB%20id%3D%22lingo-sub-810733%22%20slang%3D%22en-US%22%3ETrack%20unauthorized%20backups%20on%20Azure%20SQL%20Database%20Managed%20Instance%20using%20Auditing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-810733%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20SQL%20Database%20Managed%20Instance%20provides%20T-SQL%20surface%20area%20functionalities%20that%20are%20very%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eclose%20to%20the%20SQL%20Server%20functional%20surface%20area.%20One%20of%20the%20SQL%20Server%20feature%20that%20is%20available%20in%20Managed%20Instance%20is%20taking%20the%20COPY_ONLY%20backups%20of%20the%20databases%20to%20an%20Azure%20Blob%20Storage%20container.%20This%20feature%20can%20be%20used%20for%20a%20manual%20transfer%20of%20databases%20(if%20you%20don%E2%80%99t%20use%20cross%20instance%20point-in-time-restore)%2C%20or%20to%20keep%20the%20backups%20longer%20than%2035%20days.%3C%2FP%3E%0A%3CP%3EThe%20rich%20SQL%20surface%20area%20that%20supports%20COPY_ONLY%20backup%20might%20also%20introduce%20a%20risk%20that%20someone%20might%20take%20an%20unauthorized%20copy%20of%20the%20database%20by%20creating%20a%20backup%20to%20Azure%20Blob%20Storage.%20Only%20high%20privileged%20users%20can%20take%20the%20backups%20of%20the%20databases%2C%20so%20make%20sure%20that%20you%20have%20given%20the%20proper%20permissions%20to%20users.%20However%2C%20in%20some%20cases%20you%20must%20be%20sure%20that%20even%20the%20privileged%20users%20cannot%20do%20malicious%20actions.%20This%20is%20the%20same%20risk%20that%20you%20have%20on%20your%20on-premises%20SQL%20Server%20where%20someone%20can%20execute%20a%20T-SQL%20BACKUP%20statement%20to%20some%20folder%20and%20this%20way%20copy%20the%20database%20content.%3C%2FP%3E%0A%3CP%3EOne%20mechanism%20to%20ensure%20that%20nobody%20can%20take%20the%20COPY_ONLY%20backup%20of%20your%20database%20is%20to%20use%20Transparent%20Data%20Encryption%20that%20automatically%20encrypts%20all%20backups.%20In%20that%20case%20you%20would%20need%20to%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsql-database%2Ftransparent-data-encryption-azure-sql%23customer-managed-transparent-data-encryption---bring-your-own-key%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECustomer-managed%20(BYOK)%20TDE%3C%2FA%3E%26nbsp%3Bwhere%20you%20will%20keep%20your%20encryption%20key%20in%20Azure%20Key-Vault.%20User-initiated%20COPY_ONLY%20backups%20are%20(currently)%20not%20allowed%20if%20you%20are%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsql-database%2Fsql-database-managed-instance-transact-sql-information%23tde-encrypted-databases-with-a-service-managed-key-dont-support-user-initiated-backups%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EService-managed%20TDE%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EIf%20you%20do%20not%20use%20TDE%20on%20the%20database%20or%20there%20is%20a%20risk%20that%20someone%20can%20remove%20TDE%20from%20database%20and%20then%20take%20a%20backup%2C%20you%20can%20use%20the%20Auditing%20functionality%20of%20SQL%20Database%20Managed%20Instance%20provides%20to%20track%20who%20performed%20a%20backup%20and%20when.%20Using%20Auditing%2C%20you%20can%20always%20track%20if%20an%20unauthorized%20action%20is%20happening.%3C%2FP%3E%0A%3CP%3EIn%20the%20following%20sections%20you%20will%20find%20the%20scripts%20that%20can%20executed%20on%20the%20Managed%20Instance%20to%20setup%20Auditing%20that%20will%20track%20any%20backup%20action%20performed%20on%20the%20Managed%20Instance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1817286405%22%20id%3D%22toc-hId-1817286405%22%3ESetup%3C%2FH2%3E%0A%3CP%3EThe%20first%20step%20you%20need%20to%20do%20to%20set%20up%20auditing%20is%20to%20create%20a%20credential%20with%20the%20name%20of%20the%20Azure%20Blob%20Storage%20where%20audit%20log%20records%20will%20be%20stored%2C%20including%20the%20SAS%20key%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EDROP%20CREDENTIAL%20%5Bhttps%3A%2F%2F.blob.core.windows.net%2Fmycontainer%5D%0A%20%0ACREATE%20CREDENTIAL%20%5Bhttps%3A%2F%2F.blob.core.windows.net%2Fmycontainer%5D%0AWITH%20IDENTITY%3D'SHARED%20ACCESS%20SIGNATURE'%2C%0ASECRET%20%3D%20'PUT%20SECRET%20HERE'%20%0A%20%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20credential%20will%20enable%20Managed%20Instance%20to%20write%20the%20audit%20log%20records%20into%20the%20file%20on%20the%20Azure%20Blob%20Storage%20container.%20For%20more%20information%20on%20how%20to%20create%20SQL%20Server%20Audits%2C%20see%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-managed-instance-auditing%23set-up-auditing-for-your-server-to-azure-storage%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-managed-instance-auditing%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EOnce%20you%20have%20prepared%20your%20storage%20account%2C%20you%20can%20create%20the%20Audit%20to%20the%20Azure%20Blob%20Storage%20container%20and%20the%20Audit%20specification%20that%20will%20capture%20Backup%20and%20Restore%20events%20and%20Changes%20to%20any%20audit.%20The%20following%20objects%20are%20required%20to%20setup%20the%20auditing%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20a%20Server%20Audit%20that%20will%20write%20audit%20logs%20in%20the%20Azure%20Blob%20Storage%20container.%3C%2FLI%3E%0A%3CLI%3ECreate%20an%20Audit%20Specification%20that%20will%20capture%20the%20BACKUP_RESTORE_GROUP%20and%20the%20AUDIT_CHANGE_GROUP.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20the%20following%20example%20you%20can%20find%20the%20script%20that%20setup%20these%20objects%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E--%20Create%20server%20audit%0ACREATE%20SERVER%20AUDIT%20%5BBackupRestoreAudit%5D%0ATO%20URL%20(PATH%20%3D%20'%3CA%20href%3D%22https%3A%2F%2FMYSTORAGE.blob.core.windows.net%2Fmycontainer%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2FMYSTORAGE.blob.core.windows.net%2Fmycontainer%3C%2FA%3E'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20RETENTION_DAYS%20%3D%2030)%3B%0A%20%0A--%20Define%20events%20to%20audit%0ACREATE%20SERVER%20AUDIT%20SPECIFICATION%20BackupRestoreAuditSpec%0AFOR%20SERVER%20AUDIT%20%5BBackupRestoreAudit%5D%0AADD%20(BACKUP_RESTORE_GROUP)%2C%0AADD%20(AUDIT_CHANGE_GROUP)%0AWITH%20(STATE%3DON)%3B%0A%20%0A%20%0AALTER%20SERVER%20AUDIT%20%5BBackupRestoreAudit%5D%0AWITH%20(STATE%3DON)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20Audit%20will%20add%20a%20log%20record%20whenever%20someone%20backup%20the%20database%20up%20to%20any%20blob%20storage%20account.%20It%20will%20also%20add%20a%20log%20record%20if%20someone%20with%20high%20DB%20Admin%20rights%20disables%20the%20audit.%3C%2FP%3E%0A%3CP%3EYou%20can%20find%20the%20file%20path%20where%20the%20audit%20events%20are%20logged%20using%20this%20query%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eselect%20name%2C%20audit_file_path%0A%20%20%20%20%20%20%20from%20sys.dm_server_audit_status%0A%20%20%20%20%20%20%20where%20name%20%3D%20'BackupRestoreAudit'%20and%20audit_file_path%20is%20not%20null%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUsing%20the%20audit%20file%20path%20returned%20by%20this%20query%20(something%20like%20'%3CA%20href%3D%22https%3A%2F%2FMYSTORAGE.blob.core.windows.net%2Fmycontainer%2F*****.xel%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2FMYSTORAGE.blob.core.windows.net%2Fmycontainer%2F*****.xel%3C%2FA%3E')%20you%20can%20read%20the%20audit%20log%20events%20using%20the%20following%20query%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ESELECT%20event_time%2C%20succeeded%2C%20statement%2C%20server_instance_name%2C%0A%20%20%20%20%20%20%20server_principal_name%2C%20client_ip%2C%20application_name%2C%20duration_milliseconds%0AFROM%20sys.fn_get_audit_file%0A('%3CA%20href%3D%22https%3A%2F%2FMYSTORAGE.blob.core.windows.net%2Fmycontainer%2F*****.xel%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2FMYSTORAGE.blob.core.windows.net%2Fmycontainer%2F*****.xel%3C%2FA%3E'%2C%20default%2C%20default)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20query%20will%20return%20all%20audit%20records%20written%20in%20this%20audit%20log%20file.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--734870556%22%20id%3D%22toc-hId--734870556%22%3ETesting%20the%20Audit%3C%2FH2%3E%0A%3CP%3ELet%E2%80%99s%20imagine%20that%20someone%20is%20trying%20to%20take%20an%20unauthorized%20backup%20of%20the%20database%20%3CSTRONG%3EWideWorldImportersStandardRefresh%3C%2FSTRONG%3Eusing%20the%20T-SQL%20code%20shown%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EBACKUP%20DATABASE%20WideWorldImportersStandardRefresh%0ATO%20URL%20%3D%20N'%3CA%20href%3D%22https%3A%2F%2Fmibackup.blob.core.windows.net%2Fmycontainer%2FWWI.bak%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmibackup.blob.core.windows.net%2Fmycontainer%2FWWI.bak%3C%2FA%3E'%0AWITH%20COPY_ONLY%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20read%20the%20events%20from%20the%20audit%20log%2C%20you%20will%20see%20the%20following%20entries%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F127472iEEF19DB12454819F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22backup-database-audit.PNG%22%20title%3D%22backup-database-audit.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EManaged%20Instance%20added%20an%20audit%20log%20entry%20in%20the%20file%20on%20the%20Azure%20Blob%20Storage%20with%20the%20event%20time%2C%20T-SQL%20statement%20that%20is%20executed%2C%20login%20name%20of%20the%20user%20who%20executed%20the%20statement%20and%20client%20IP%20address.%20This%20way%20you%20can%20track%20unauthorized%20backups%20of%20the%20databases.%3C%2FP%3E%0A%3CDIV%3EMake%20sure%20to%20only%20grant%20the%20necessary%20permissions%20for%20your%20user.%20In%20the%20most%20of%20cases%2C%20the%20users%20who%20can%20perform%20backups%20and%20have%20BACKUP%20DATABASE%20permission%20should%20not%20have%20ALTER%20ANY%20SERVER%20AUDIT%20permission.%20Otherwise%2C%20they%20would%20be%20able%20to%20disable%20auditing%20before%20taking%20a%20backup%20to%20prevent%20Managed%20Instance%20to%20write%20the%20audit%20records%20in%20the%20log%2C%20and%20then%20enable%20it%20again%20once%20the%20BACKUP%20is%20completed.%3C%2FDIV%3E%0A%3CP%3EIf%20you%20want%20to%20ensure%20that%20a%20high-privileged%20user%20who%20has%20BACKUP%20DATABASE%2B%20ALTER%20ANY%20SERVER%20AUDIT%20or%20CONTROL%20SERVER%20permission%20cannot%20do%20this%2C%20the%20Audit%20Specification%20must%20also%20include%20AUDIT_CHANGE_GROUP.%20In%20this%20case%20you%20will%20be%20able%20to%20see%20who%20disabled%20the%20specification%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F127473i3E877D129D542B4A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22disable-audit.PNG%22%20title%3D%22disable-audit.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3EAll%20audit%20logs%20are%20stored%20on%20Azure%20Blob%20Storage%20account%20that%20should%20not%20be%20available%20to%20any%20Database%20Administrator%20regardless%20of%20the%20privileges.%20Any%20other%20database%20user%20would%20be%20able%20to%20download%20the%20audit%20log%20files%20from%20Azure%20Blob%20Storage%20container%2C%20or%20query%20them%20directly%20via%20some%20other%20Managed%20Instance%20or%20even%20SQL%20Server%20instance.%20This%20way%2C%20you%20are%20able%20to%20monitor%20who%20is%20accessing%20your%20database%20and%20to%20track%20who%20made%20the%20potentially%20malicious%20actions.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1007939779%22%20id%3D%22toc-hId-1007939779%22%3ECleanup%3C%2FH2%3E%0A%3CP%3EIf%20you%20created%20an%20Audit%20only%20for%20the%20testing%20purposes%2C%20you%20could%20use%20the%20following%20script%20to%20remove%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EALTER%20SERVER%20AUDIT%20SPECIFICATION%20BackupRestoreAuditSpec%0AWITH%20(STATE%3DOFF)%3B%0ADROP%20SERVER%20AUDIT%20SPECIFICATION%20BackupRestoreAuditSpec%0A%20%0AALTER%20SERVER%20AUDIT%20%5BBackupRestoreAudit%5D%0AWITH%20(STATE%3DOFF)%3B%0ADROP%20SERVER%20AUDIT%20%5BBackupRestoreAudit%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-810733%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20SQL%20Database%20Managed%20Instance%20enables%20you%20to%20audit%20the%20backup%20actions%20and%20ensure%20that%20you%20track%20all%20backup%20actions%20that%20are%20done%20on%20our%20database.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-810733%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EManaged%20Instance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESecurity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Azure SQL Database Managed Instance provides T-SQL surface area functionalities that are very  close to the SQL Server functional surface area. One of the SQL Server feature that is available in Managed Instance is taking the COPY_ONLY backups of the databases to an Azure Blob Storage container. This feature can be used for a manual transfer of databases (if you don’t use cross instance point-in-time-restore), or to keep the backups longer than 35 days.

The rich SQL surface area that supports COPY_ONLY backup might also introduce a risk that someone might take an unauthorized copy of the database by creating a backup to Azure Blob Storage. Only high privileged users can take the backups of the databases, so make sure that you have given the proper permissions to users. However, in some cases you must be sure that even the privileged users cannot do malicious actions. This is the same risk that you have on your on-premises SQL Server where someone can execute a T-SQL BACKUP statement to some folder and this way copy the database content.

One mechanism to ensure that nobody can take the COPY_ONLY backup of your database is to use Transparent Data Encryption that automatically encrypts all backups. In that case you would need to use Customer-managed (BYOK) TDE where you will keep your encryption key in Azure Key-Vault. User-initiated COPY_ONLY backups are (currently) not allowed if you are using Service-managed TDE.

If you do not use TDE on the database or there is a risk that someone can remove TDE from database and then take a backup, you can use the Auditing functionality of SQL Database Managed Instance provides to track who performed a backup and when. Using Auditing, you can always track if an unauthorized action is happening.

In the following sections you will find the scripts that can executed on the Managed Instance to setup Auditing that will track any backup action performed on the Managed Instance.

 

Setup

The first step you need to do to set up auditing is to create a credential with the name of the Azure Blob Storage where audit log records will be stored, including the SAS key:

 

 

DROP CREDENTIAL [https://.blob.core.windows.net/mycontainer]
 
CREATE CREDENTIAL [https://.blob.core.windows.net/mycontainer]
WITH IDENTITY='SHARED ACCESS SIGNATURE',
SECRET = 'PUT SECRET HERE' 
 

 

 

This credential will enable Managed Instance to write the audit log records into the file on the Azure Blob Storage container. For more information on how to create SQL Server Audits, see here: https://docs.microsoft.com/en-us/azure/sql-database/sql-database-managed-instance-auditing.

Once you have prepared your storage account, you can create the Audit to the Azure Blob Storage container and the Audit specification that will capture Backup and Restore events and Changes to any audit. The following objects are required to setup the auditing:

  • Create a Server Audit that will write audit logs in the Azure Blob Storage container.
  • Create an Audit Specification that will capture the BACKUP_RESTORE_GROUP and the AUDIT_CHANGE_GROUP.

In the following example you can find the script that setup these objects:

 

 

-- Create server audit
CREATE SERVER AUDIT [BackupRestoreAudit]
TO URL (PATH = 'https://MYSTORAGE.blob.core.windows.net/mycontainer',
              RETENTION_DAYS = 30);
 
-- Define events to audit
CREATE SERVER AUDIT SPECIFICATION BackupRestoreAuditSpec
FOR SERVER AUDIT [BackupRestoreAudit]
ADD (BACKUP_RESTORE_GROUP),
ADD (AUDIT_CHANGE_GROUP)
WITH (STATE=ON);
 
 
ALTER SERVER AUDIT [BackupRestoreAudit]
WITH (STATE=ON);

 

 

This Audit will add a log record whenever someone backup the database up to any blob storage account. It will also add a log record if someone with high DB Admin rights disables the audit.

You can find the file path where the audit events are logged using this query:

 

 

select name, audit_file_path
       from sys.dm_server_audit_status
       where name = 'BackupRestoreAudit' and audit_file_path is not null

 

 

Using the audit file path returned by this query (something like 'https://MYSTORAGE.blob.core.windows.net/mycontainer/*****.xel') you can read the audit log events using the following query:

 

 

SELECT event_time, succeeded, statement, server_instance_name,
       server_principal_name, client_ip, application_name, duration_milliseconds
FROM sys.fn_get_audit_file
('https://MYSTORAGE.blob.core.windows.net/mycontainer/*****.xel', default, default);

 

 

This query will return all audit records written in this audit log file.

Testing the Audit

Let’s imagine that someone is trying to take an unauthorized backup of the database WideWorldImportersStandardRefresh using the T-SQL code shown below:

 

 

BACKUP DATABASE WideWorldImportersStandardRefresh
TO URL = N'https://mibackup.blob.core.windows.net/mycontainer/WWI.bak'
WITH COPY_ONLY

 

 

If you read the events from the audit log, you will see the following entries:

backup-database-audit.PNG

Managed Instance added an audit log entry in the file on the Azure Blob Storage with the event time, T-SQL statement that is executed, login name of the user who executed the statement and client IP address. This way you can track unauthorized backups of the databases.

Make sure to only grant the necessary permissions for your user. In the most of cases, the users who can perform backups and have BACKUP DATABASE permission should not have ALTER ANY SERVER AUDIT permission. Otherwise, they would be able to disable auditing before taking a backup to prevent Managed Instance to write the audit records in the log, and then enable it again once the BACKUP is completed.

If you want to ensure that a high-privileged user who has BACKUP DATABASE+ ALTER ANY SERVER AUDIT or CONTROL SERVER permission cannot do this, the Audit Specification must also include AUDIT_CHANGE_GROUP. In this case you will be able to see who disabled the specification:

disable-audit.PNG

All audit logs are stored on Azure Blob Storage account that should not be available to any Database Administrator regardless of the privileges. Any other database user would be able to download the audit log files from Azure Blob Storage container, or query them directly via some other Managed Instance or even SQL Server instance. This way, you are able to monitor who is accessing your database and to track who made the potentially malicious actions.

Cleanup

If you created an Audit only for the testing purposes, you could use the following script to remove it:

 

 

ALTER SERVER AUDIT SPECIFICATION BackupRestoreAuditSpec
WITH (STATE=OFF);
DROP SERVER AUDIT SPECIFICATION BackupRestoreAuditSpec
 
ALTER SERVER AUDIT [BackupRestoreAudit]
WITH (STATE=OFF);
DROP SERVER AUDIT [BackupRestoreAudit]