How to Scan Azure Blob Files for Threats in C# .NET Core

Cloudmersive
4 min readFeb 22, 2024

Scanning files for threats directly in cloud storage has a lot of advantages. Chief among those, when we scan files directly in storage, we don’t have to move them around and risk executing malicious content in a sensitive area.

Using ready-to-run C# .NET Core code provided below, we can take advantage of a free API that directly scans individual files residing in Azure blob instances for a wide range of threats.

Not only can we use this API to check for virus & malware signatures, but we can check for threatening content types (executables, scripts, macros etc.) and classic signs of obfuscation (password protection, invalid files, etc.).

The virus and malware portion of our scan will be automatic, but the content threat detection portion of our scan can be heavily customized. We can set custom threat rules in our request against a wide range of threatening content types, and we can even set a custom comma-separated whitelist in our request to categorically flag all files that don’t conform to certain specific file format standards (e.g., ‘.pdf,.docx,.xlsx’).

To set up our API call, we’ll need to get a few important details from our Azure blob account. We’ll need the following information:

  1. Connection String (we can get this from the Access Keys tab of the Storage Account blade in the Azure Portal)
  2. Container Name
  3. Blob Path (such as ‘hello.pdf’ or ‘/folder/subfolder/hello.pdf’; if this contains Unicode characters, we’ll need to base64 encode the blob path and prepend with ‘base64’)

On top of that, we’ll need a free Cloudmersive API key to authorize our API call (this will allow up to 800 file scans per month with no commitments).

When we have all that information ready, we get our API call started by first installing the .NET Core SDK. We’ll just need to run this command in the Package Manager console to install via NuGet:

Install-Package Cloudmersive.APIClient.NETCore.VirusScan -Version 2.0.4

After that, we can copy the below code into our file to call the function. We can set custom threat rules to “true” or “false” depending on our preferences, and we can supply a comma-separated whitelist in the restrictFileTypes parameter if we wish (this list should NOT have any spaces between files and commas):

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.VirusScan.Api;
using Cloudmersive.APIClient.NETCore.VirusScan.Client;
using Cloudmersive.APIClient.NETCore.VirusScan.Model;

namespace Example
{
public class ScanCloudStorageScanAzureBlobAdvancedExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ScanCloudStorageApi();
var connectionString = connectionString_example; // string | Connection string for the Azure Blob Storage Account; you can get this connection string from the Access Keys tab of the Storage Account blade in the Azure Portal.
var containerName = containerName_example; // string | Name of the Blob container within the Azure Blob Storage account
var blobPath = blobPath_example; // string | Path to the blob within the container, such as 'hello.pdf' or '/folder/subfolder/world.pdf'. If the blob path contains Unicode characters, you must base64 encode the blob path and prepend it with 'base64:', such as: 'base64:6ZWV6ZWV6ZWV6ZWV6ZWV6ZWV'.
var allowExecutables = true; // bool? | Set to false to block executable files (program code) from being allowed in the input file. Default is false (recommended). (optional)
var allowInvalidFiles = true; // bool? | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document. Default is false (recommended). (optional)
var allowScripts = true; // bool? | Set to false to block script files, such as a PHP files, Python scripts, and other malicious content or security threats that can be embedded in the file. Set to true to allow these file types. Default is false (recommended). (optional)
var allowPasswordProtectedFiles = true; // bool? | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords. Set to true to allow these file types. Default is false (recommended). (optional)
var allowMacros = true; // bool? | Set to false to block macros and other threats embedded in document files, such as Word, Excel and PowerPoint embedded Macros, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
var allowXmlExternalEntities = true; // bool? | Set to false to block XML External Entities and other threats embedded in XML files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
var restrictFileTypes = restrictFileTypes_example; // string | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files. All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false. Set restrictFileTypes parameter to null or empty string to disable; default is disabled. (optional)

try
{
// Advanced Scan an Azure Blob for viruses
CloudStorageAdvancedVirusScanResult result = apiInstance.ScanCloudStorageScanAzureBlobAdvanced(connectionString, containerName, blobPath, allowExecutables, allowInvalidFiles, allowScripts, allowPasswordProtectedFiles, allowMacros, allowXmlExternalEntities, restrictFileTypes);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ScanCloudStorageApi.ScanCloudStorageScanAzureBlobAdvanced: " + e.Message );
}
}
}
}

Now we can easily build a custom application to scan our most sensitive Azure blob instances for malware and other hidden threats.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.