Sunday, October 8, 2017

Performing an Azure SQL Security Audit with AzSDK

I have really been enjoying being an MVP.  One of the great perks about this is access to mailing lists where all the MVPs can discuss relevant topics to the different Microsoft areas in scope.  One email thread got me to look at the Azure AzSDK project on github.  This project seems to have a bunch of Microsoft contributors, and is focused on building scripts to both report and remediate on security baselines in Azure.  The goal of this post is to show how to apply this project against your Azure SQL resources.

The first thing to cover here is that the AzSDK is focused on the Azure side of any given resource.  That is to say, it will investigate (via the APIs) how your resource is configured in Azure against recommended best practices.  In the case of SQL, for example, it will not "log in" to the server to do any checks at the sql level.

Step 1 in the process is obviously to install the module.  You can find detailed instructions in their posted Installation Guide.  What I like about the guide is it's focus on not having to use elevated permissions in powershell to get the project up and going. One key note here is to ensure you have the correct version of the AzureRM modules installed.

After installation, you can simply run the built-in set of command-lets against your Azure resources.  As always, I recommend reading the code before running it to get an idea of what it is doing.  There is a lot of bootstrapping code in the modules, which eventually targets JSON files that have all the rules to apply defined.

Here is an example of one of the SQL rules:

   {
      "ControlID": "Azure_SQLDatabase_Audit_Enable_Logging_and_Monitoring_DB",
      "Description": "Enable SQL Database audit with selected event types and retention period of minimum $($this.ControlSettings.SqlServer.AuditRetentionPeriod_Min) days",
      "Id": "SQLDatabase140",
      "ControlSeverity": "Medium",
      "Automated": "Yes",
      "MethodName": "CheckSqlDatabaseAuditing",
      "Rationale": "Auditing enables log collection of important system events pertinent to security. Regular monitoring of audit logs can help to detect suspicious and malicious activities early enough.",
      "Recommendation": "Run command  Set-AzureRmSqlDatabaseAuditingPolicy -ResourceGroupName '{ResourceGroupName}' -ServerName '{ServerName}' -DatabaseName '{DatabaseName}' -StorageAccountName '{StorageAccountName}' -EventType 'All' -AuditType 'Blob' -RetentionInDays $($this.ControlSettings.SqlServer.AuditRetentionPeriod_Min). Refer: https://docs.microsoft.com/en-us/powershell/module/azurerm.sql/set-azurermsqldatabaseauditingpolicy",
      "Tags": [
         "SDL",
         "TCP",
         "Automated",
         "Audit",
         "SOX",
         "SqlDatabase"
      ],
      "Enabled": true,
      "FixControl": {
         "FixMethodName": "EnableDatabaseAuditingPolicy",
         "FixControlImpact": "Low",
         "Parameters": {
            "StorageAccountName": ""
         }
      }
   },

I like that it is extensible, and allows you to potentially add your own rules if required.

Running the scan is pretty easy.  You can follow the instructions here and simply target the resource group you would like to evaluate.  Here is the output from the command on one of my resources in production.



Oh my, that is a lot of failures.  You can find detailed reports in a CSV that is stored on disk (see the last line of the screenshot above).  Upon review, it seems like all of my failures were due to auditing not being turned on at the database level.  Of course, auditing is turned on at the server level, and, as per the docs, this covers all databases.  Seems like this test could use some improvement.

In any event, I really like where this project is going and will be following it closely. I can see adding this type of automated check in a build/release pipeline to ensure at least the baselines are covered.  There is a LOT more that this project can do, so be sure to check it out if you are interested.


Sunday, September 17, 2017

A .NET update broke my Azure Backup

I am currently working at a client where we are making use of Azure Backup to keep one of our critical servers backed up.  As this is production, we have been fairly diligent about keeping our servers up to date.  It turns out that a .NET patch broke the Azure backup process.  This post talks a bit about this.

During a routine check of my provisioned Azure services, I noticed that my critical server was not being backed up.  Here is a sample error report that I got.



Umm... what?  I took a look at the troubleshooting steps and followed all the steps. 

1) My server is protected by NSGs, but we do not limit outbound communication, so that was fine. 

2) The agent seemed to be communicating (the portal was getting the correct information)

3)  I attempted rebooting ( no luck) and reinstalling the guest agent (also no luck)

The only thing I could tell was that it stopped (a) the last time we patched and (b) it errors out on the "take snapshot" step.

I ended up contacting support and the problem ended up being that a .NET update had happened, and a couple of registry keys has been blown away.  These registry keys have to do with the type of encryption used for TLS (by the .NET framework).  For reference, here they are:



[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v2.0.50727]
"SchUseStrongCrypto"=dword:00000001

 Adding these allowed me to get past the "take snapshot" step, and I started to have successful backups.  Hope that helps!

Monday, September 11, 2017

Azure Backup and Resource Group Locks

One important feature from a security perspective is the concept of Resource Group Locks.  I generally recommend that locks be placed at the resource group level for all production resources.  While you can place locks at the resource level, it can aid in manageability if the locks are placed at the resource group level.

It turns out that locking at the resource group level affects the way Azure backup functions with the VMs in that resource group, and, in some cases, can cause the backup to fail.

When you execute a backup, here is what the shows up in the activity log for the target resource group (where the VM lives)


As you can see, there is a delete operation that occurs at the end of the backup process.  If you have a resource group lock enabled, you'll see the following error message:

From my experience, this does not prevent the backup from showing up in the vault, but does prevent the backup job from completing successfully.  According to Azure support, this situation could prevent the VM from backing up altogether.

The solution going forward is to not use resource locks at the group level, but rather manage them via arm templates on the resource level itself.