How to Block Invalid Image File Threats using C#
Identifying non-malware content threats is a challenge when we design security architecture for our web applications. If we aren’t careful, threat actors can sometimes use specially formatted images to exploit vulnerabilities in the processing technologies we use to buffer and render image files. These exploits can lead to disastrous outcomes like Denial-of-Service (DOS) attacks and even arbitrary code execution.
Thankfully, there’s a way we can simultaneously scan files for malware threats AND identify discrepancies in file formatting.
Using the below code, we can take advantage of a free API that offers 360-degree content protection as a low-code solution for C#/.NET applications. The underlying service offers a cutting-edge malware detection sandbox (including predictive zero-day threat detection policies and malware signature-based scanning) coupled with content validation capabilities for 100+ unique image formats. We can set custom parameters to block invalid image files (i.e., those that do not rigorously conform to their format standards).
Let’s start by installing the SDK. We can run this command in the Package Manager console to install via NuGet:
Install-Package Cloudmersive.APIClient.NET.VirusScan -Version 3.0.4
Next, we can copy the below code examples into our file to call the function. We can set allowInvalidFiles to “false” to flag invalid images, and we can also provide a comma-separated list of file extensions (e.g., .png,.jpg,.pdf) in the restrictFileTypes parameter to avoid
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.VirusScan.Api;
using Cloudmersive.APIClient.NET.VirusScan.Client;
using Cloudmersive.APIClient.NET.VirusScan.Model;
namespace Example
{
public class ScanFileAdvancedExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new ScanApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
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 allowInsecureDeserialization = true; // bool? | Set to false to block Insecure Deserialization and other threats embedded in JSON and other object serialization files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
var allowHtml = true; // bool? | Set to false to block HTML input in the top level file; HTML can contain XSS, scripts, local file accesses and other threats. Set to true to allow these file types. Default is false (recommended) [for API keys created prior to the release of this feature default is true for backward compatability]. (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 a file for viruses
VirusScanAdvancedResult result = apiInstance.ScanFileAdvanced(inputFile, allowExecutables, allowInvalidFiles, allowScripts, allowPasswordProtectedFiles, allowMacros, allowXmlExternalEntities, allowInsecureDeserialization, allowHtml, restrictFileTypes);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ScanApi.ScanFileAdvanced: " + e.Message );
}
}
}
}
Now all we’ll need is a free-tier API key to authorize our API call. These allow a limit of 800 API calls per month with no additional commitments.
That’s all there is to it — now we can improve our content security workflow with minimal code at zero cost.